Date: Wed, 26 Oct 2016 06:10:39 +0800 (PHT)
   From: Paul Goyette <p...@whooppee.com>

   There's only 9 snprintf() calls.  I could simply provide a macro:

   #define ADD_TEXT(dest, end, format, ...)                     \
           {                                                    \
                   int len, max = (end) - (dest);                       \

                   len = snprintf((dest), max, (format), __VA_ARGS__); \
                   if (len > max)                                       \
                           return;                                      \
                   (dest) += len;                                       \
           }


   Then all of the snprintf() calls become simply

           ADD_TEXT(cp, ep, <format>, ...);

   (Of course, after last use I'd add a #undef ADD_TEXT to clean up...)

Maybe we should have a standard function to do this:

        if ((error = snprintf_inplace(&p, &n, fmt, x, y, z)) != 0)
                goto out;

would be equivalent to

        nfmt = snprintf(p, n, fmt, x, y, z);
        if (nfmt > n) {
                n = nfmt;
                error = ETRUNC;
                goto out;
        }
        p += nfmt;
        n -= nfmt;

or something like that, with the appropriate choice of update so that
it is easy either to report an error or to realloc a buffer and retry.
(Of course, then we need to decide whether p is const-qualified or
not, signed or unsigned or unqualified char, &c.  Bleh.)

Reply via email to