From 3e1974f94ef8796a5ac9e750d47ccb63c677a85b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Sat, 9 Aug 2008 11:51:29 +0100 Subject: [PATCH] util: Utility functions to print to a string buffer without overflowing. --- src/gallium/auxiliary/util/u_string.h | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/gallium/auxiliary/util/u_string.h b/src/gallium/auxiliary/util/u_string.h index 73c88d87b4c..abc3232b492 100644 --- a/src/gallium/auxiliary/util/u_string.h +++ b/src/gallium/auxiliary/util/u_string.h @@ -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 -- 2.30.2