# HG changeset patch # User lucabe # Date 1184826962 0 # Node ID 2cd0add8ac0ce1d85dcc5a5a8706046e5db6749b # Parent 99c56fd6e3f4989b98cb8cc03e746ef5ecfdf10f Implement av_strlcatf(): a strlcat which adds a printf style formatted string diff -r 99c56fd6e3f4 -r 2cd0add8ac0c avstring.h --- a/avstring.h Wed Jul 18 12:22:07 2007 +0000 +++ b/avstring.h Thu Jul 19 06:36:02 2007 +0000 @@ -73,4 +73,18 @@ */ size_t av_strlcat(char *dst, const char *src, size_t size); +/** + * Append output to a string, according to a format. Never write out of + * the destination buffer, and and always put a terminating 0 within + * the buffer. + * @param dst destination buffer (string to which the output is + * appended) + * @param size total size of the destination buffer + * @param fmt printf-compatible format string, specifying how the + * following parameters are used + * @return the length of the string that would have been generated + * if enough space had been available + */ +size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...); + #endif /* AVUTIL_STRING_H */ diff -r 99c56fd6e3f4 -r 2cd0add8ac0c string.c --- a/string.c Wed Jul 18 12:22:07 2007 +0000 +++ b/string.c Thu Jul 19 06:36:02 2007 +0000 @@ -19,6 +19,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include +#include #include #include #include "avstring.h" @@ -62,3 +64,15 @@ return len + strlen(src); return len + av_strlcpy(dst + len, src, size - len); } + +size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) +{ + int len = strlen(dst); + va_list vl; + + va_start(vl, fmt); + len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl); + va_end(vl); + + return len; +}