[Mesa-dev] [PATCH v2 1/3] util: introduce the util_strnappend function

Silvestrs Timofejevs silvestrs.timofejevs at imgtec.com
Thu Dec 20 10:02:33 UTC 2018


This function is similar to strncat, but unlike strncat it allows to
concatenate the buffer with a formatted string. The alternative would
be to have an intermediate string that is formated first, and then
appended via strncat.

Signed-off-by: Silvestrs Timofejevs <silvestrs.timofejevs at imgtec.com>
---
 src/util/u_string.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/util/u_string.h b/src/util/u_string.h
index e408146..abf4598 100644
--- a/src/util/u_string.h
+++ b/src/util/u_string.h
@@ -202,7 +202,6 @@ util_strstr(const char *haystack, const char *needle)
    return NULL;
 }
 
-
 #define util_strcasecmp stricmp
 #define util_strdup _strdup
 
@@ -223,6 +222,21 @@ util_strstr(const char *haystack, const char *needle)
 
 #endif
 
+/* Append a formatted string to the buffer, up to the buffer size */
+static inline void
+util_strnappend(char *const buf, const int buf_size, const char *fmt, ...)
+{
+   int max_allowed;
+   va_list args;
+   size_t buf_len = strlen(buf);
+
+   max_allowed = buf_size - buf_len;
+   assert(max_allowed >= 0);
+
+   va_start(args, fmt);
+   (void) util_vsnprintf(&buf[buf_len], max_allowed, fmt, args);
+   va_end(args);
+}
 
 #ifdef __cplusplus
 }
-- 
2.7.4



More information about the mesa-dev mailing list