mirror of
https://github.com/labwc/labwc.git
synced 2025-11-04 13:30:07 -05:00
buf: add buf_add_fmt()
This commit is contained in:
parent
cd961b1ac1
commit
6564e1bc8d
3 changed files with 56 additions and 0 deletions
|
|
@ -1,7 +1,9 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "common/buf.h"
|
||||
#include "common/macros.h"
|
||||
|
|
@ -95,6 +97,38 @@ buf_expand(struct buf *s, int new_alloc)
|
|||
s->alloc = new_alloc;
|
||||
}
|
||||
|
||||
void
|
||||
buf_add_fmt(struct buf *s, const char *fmt, ...)
|
||||
{
|
||||
if (string_null_or_empty(fmt)) {
|
||||
return;
|
||||
}
|
||||
size_t size = 0;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
int n = vsnprintf(NULL, size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (n < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
size = (size_t)n + 1;
|
||||
buf_expand(s, s->len + size);
|
||||
|
||||
va_start(ap, fmt);
|
||||
n = vsnprintf(s->data + s->len, size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (n < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
s->len += n;
|
||||
s->data[s->len] = 0;
|
||||
}
|
||||
|
||||
void
|
||||
buf_add(struct buf *s, const char *data)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue