mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-05-06 09:28:07 +02:00
util: Utility functions to print to a string buffer without overflowing.
This commit is contained in:
parent
b65259de6c
commit
3e1974f94e
1 changed files with 37 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue