]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
add ast_build_string library function (from bug #3644)
authorKevin P. Fleming <kpfleming@digium.com>
Sun, 15 May 2005 22:33:02 +0000 (22:33 +0000)
committerKevin P. Fleming <kpfleming@digium.com>
Sun, 15 May 2005 22:33:02 +0000 (22:33 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5684 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/utils.h
utils.c

index 5668b316e499188c82bece0a41e46a533512ba85..5a772b12fac19cb80c9700ce5b921cab66c3ad99 100755 (executable)
@@ -185,6 +185,7 @@ extern char *ast_strcasestr(const char *, const char *);
   \param dst The destination buffer.
   \param src The source string
   \param size The size of the destination buffer
+  \return Nothing.
 
   This is similar to \a strncpy, with two important differences:
     - the destination buffer will \b always be null-terminated
@@ -193,8 +194,20 @@ extern char *ast_strcasestr(const char *, const char *);
   not leave the destination buffer unterminated. There is no need to pass an artificially
   reduced buffer size to this function (unlike \a strncpy), and the buffer does not need
   to be initialized to zeroes prior to calling this function.
-  No return value.
 */
 void ast_copy_string(char *dst, const char *src, size_t size);
 
+/*!
+  \brief Build a string in a buffer, designed to be called repeatedly
+  
+  This is a wrapper for snprintf, that properly handles the buffer pointer
+  and buffer space available.
+
+  \return 0 on success, non-zero on failure.
+  \param buffer current position in buffer to place string into (will be updated on return)
+  \param space remaining space in buffer (will be updated on return)
+  \param fmt printf-style format string
+*/
+int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
+
 #endif /* _ASTERISK_UTILS_H */
diff --git a/utils.c b/utils.c
index 5264ecc0538e9d2f993b3a0da84402539d29fe1d..594ef6e7b73f00158867078a07910a9d2ad61c27 100755 (executable)
--- a/utils.c
+++ b/utils.c
@@ -17,6 +17,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -433,6 +434,28 @@ void ast_copy_string(char *dst, const char *src, size_t size)
        *dst = '\0';
 }
 
+int ast_build_string(char **buffer, size_t *space, const char *fmt, ...)
+{
+       va_list ap;
+       int result;
+
+       if (!buffer || !*buffer || !space || !*space)
+               return -1;
+
+       va_start(ap, fmt);
+       result = vsnprintf(*buffer, *space, fmt, ap);
+       va_end(ap);
+
+       if (result < 0)
+               return -1;
+       else if (result > *space)
+               result = *space;
+
+       *buffer += result;
+       *space -= result;
+       return 0;
+}
+
 /* Case-insensitive substring matching */
 #ifndef LINUX
 static char *upper(const char *orig, char *buf, int bufsize)