util: Utility functions to print to a string buffer without overflowing.
authorJosé Fonseca <jrfonseca@tungstengraphics.com>
Sat, 9 Aug 2008 10:51:29 +0000 (11:51 +0100)
committerJosé Fonseca <jrfonseca@tungstengraphics.com>
Sat, 9 Aug 2008 13:10:25 +0000 (14:10 +0100)
src/gallium/auxiliary/util/u_string.h

index 73c88d87b4c643c79d815ea674813b6a5b3f2cc3..abc3232b492de4e0d643fbf219000e34211e00c7 100644 (file)
@@ -176,6 +176,43 @@ util_memmove(void *dest, const void *src, size_t n)
 #endif
 
 
+/**
+ * Printable string buffer
+ */
+struct util_strbuf
+{
+   char *str;
+   char *ptr;
+   size_t left;
+};
+
+
+static INLINE void
+util_strbuf_init(struct util_strbuf *sbuf, char *str, size_t size) 
+{
+   sbuf->str = str;
+   sbuf->str[0] = 0;
+   sbuf->ptr = sbuf->str;
+   sbuf->left = size;
+}
+
+
+static INLINE void
+util_strbuf_printf(struct util_strbuf *sbuf, const char *format, ...)
+{
+   if(sbuf->left > 1) {
+      size_t written;
+      va_list ap;
+      va_start(ap, format);
+      written = util_vsnprintf(sbuf->ptr, sbuf->left, format, ap);
+      va_end(ap);
+      sbuf->ptr += written;
+      sbuf->left -= written;
+   }
+}
+
+
+
 #ifdef __cplusplus
 }
 #endif