diff options
author | José Fonseca <[email protected]> | 2008-08-09 11:51:29 +0100 |
---|---|---|
committer | José Fonseca <[email protected]> | 2008-08-09 14:10:25 +0100 |
commit | 3e1974f94ef8796a5ac9e750d47ccb63c677a85b (patch) | |
tree | 761440eb9a1d208c0139e9fab5f8d0fd6e32ff57 /src/gallium/auxiliary/util | |
parent | b65259de6c0a2e77550bbef6b291c6d09dfb5867 (diff) |
util: Utility functions to print to a string buffer without overflowing.
Diffstat (limited to 'src/gallium/auxiliary/util')
-rw-r--r-- | src/gallium/auxiliary/util/u_string.h | 37 |
1 files changed, 37 insertions, 0 deletions
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 |