Browse code

Implement av_strlcatf(): a strlcat which adds a printf style formatted string

Originally committed as revision 9753 to svn://svn.ffmpeg.org/ffmpeg/trunk

Luca Abeni authored on 2007/07/19 15:36:02
Showing 2 changed files
... ...
@@ -73,4 +73,18 @@ size_t av_strlcpy(char *dst, const char *src, size_t size);
73 73
  */
74 74
 size_t av_strlcat(char *dst, const char *src, size_t size);
75 75
 
76
+/**
77
+ * Append output to a string, according to a format. Never write out of
78
+ * the destination buffer, and and always put a terminating 0 within
79
+ * the buffer.
80
+ * @param dst destination buffer (string to which the output is
81
+ *  appended)
82
+ * @param size total size of the destination buffer
83
+ * @param fmt printf-compatible format string, specifying how the
84
+ *  following parameters are used
85
+ * @return the length of the string that would have been generated
86
+ *  if enough space had been available
87
+ */
88
+size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...);
89
+
76 90
 #endif /* AVUTIL_STRING_H */
... ...
@@ -19,6 +19,8 @@
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
  */
21 21
 
22
+#include <stdarg.h>
23
+#include <stdio.h>
22 24
 #include <string.h>
23 25
 #include <ctype.h>
24 26
 #include "avstring.h"
... ...
@@ -62,3 +64,15 @@ size_t av_strlcat(char *dst, const char *src, size_t size)
62 62
         return len + strlen(src);
63 63
     return len + av_strlcpy(dst + len, src, size - len);
64 64
 }
65
+
66
+size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
67
+{
68
+    int len = strlen(dst);
69
+    va_list vl;
70
+
71
+    va_start(vl, fmt);
72
+    len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
73
+    va_end(vl);
74
+
75
+    return len;
76
+}