diff options
Diffstat (limited to 'common/common.c')
-rw-r--r-- | common/common.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/common/common.c b/common/common.c index 6084176610..afab994d72 100644 --- a/common/common.c +++ b/common/common.c @@ -109,16 +109,23 @@ bool mp_rect_intersection(struct mp_rect *rc, const struct mp_rect *rc2) // This works like snprintf(), except that it starts writing the first output // character to str[strlen(str)]. This returns the number of characters the -// string would have assuming a large enough buffer, will make sure str is -// null-terminated, and will never write to str[size] or past. -int mp_snprintf_append(char *str, size_t size, const char *format, ...) +// string would have *appended* assuming a large enough buffer, will make sure +// str is null-terminated, and will never write to str[size] or past. +// Example: +// int example(char *buf, size_t buf_size, double num, char *str) { +// int n = 0; +// n += mp_snprintf_cat(buf, size, "%f", num); +// n += mp_snprintf_cat(buf, size, "%s", str); +// return n; } +// Note how this can be chained with functions similar in style. +int mp_snprintf_cat(char *str, size_t size, const char *format, ...) { size_t len = strnlen(str, size); assert(!size || len < size); // str with no 0-termination is not allowed int r; va_list ap; va_start(ap, format); - r = len + vsnprintf(str + len, size - len, format, ap); + r = vsnprintf(str + len, size - len, format, ap); va_end(ap); return r; } |