From 1e7f363804cb9fbe4eb8193a5711fdfd00619776 Mon Sep 17 00:00:00 2001 From: Luca Abeni Date: Thu, 19 Jul 2007 06:36:02 +0000 Subject: Implement av_strlcatf(): a strlcat which adds a printf style formatted string Originally committed as revision 9753 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavutil/avstring.h | 14 ++++++++++++++ libavutil/string.c | 14 ++++++++++++++ 2 files changed, 28 insertions(+) (limited to 'libavutil') diff --git a/libavutil/avstring.h b/libavutil/avstring.h index bc24ea1..f25dd31 100644 --- a/libavutil/avstring.h +++ b/libavutil/avstring.h @@ -73,4 +73,18 @@ size_t av_strlcpy(char *dst, const char *src, size_t size); */ 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 --git a/libavutil/string.c b/libavutil/string.c index 2f4de8e..8ee2a6b 100644 --- a/libavutil/string.c +++ b/libavutil/string.c @@ -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 @@ size_t av_strlcat(char *dst, const char *src, size_t size) 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; +} -- cgit v1.1