gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
gwion_print.c
Go to the documentation of this file.
1#include "gwion_util.h"
2
3ANN2(1) static int fmtlen(const char *fmt, va_list args) {
4 va_list tmpa;
5 va_copy(tmpa, args);
6 const int size = vsnprintf(NULL, 0, fmt, tmpa);
7 va_end(tmpa);
8 return size;
9}
10
11ANN2(2)
12static int gw_vasprintf(MemPool mp, char **str, const char *fmt, va_list args) {
13 char * base = *str;
14 const size_t base_len = base ? strlen(base) : 0;
15 const int size = fmtlen(fmt, args);
16 if (size < 0) return -1;
17 char *ret = mp_malloc2(mp, base_len + size + 1);
18 if (base) strcpy(ret, base);
19 const int final_len = vsprintf(ret + base_len, fmt, args);
20 if (final_len < 0) {
21 mp_free2(mp, base_len + size + 1, ret);
22 return -1;
23 }
24 if (base) mp_free2(mp, strlen(base), base);
25 *str = ret;
26 return final_len;
27}
28
29ANN2(2) int gw_asprintf(MemPool mp, char **str, const char *fmt, ...) {
30 va_list args;
31 va_start(args, fmt);
32 const int ret = gw_vasprintf(mp, str, fmt, args);
33 va_end(args);
34 return ret;
35}
#define ANN2(...)
Definition defs.h:20
static int gw_vasprintf(MemPool mp, char **str, const char *fmt, va_list args)
Definition gwion_print.c:12
char ** str
Definition gwion_print.h:2
char const char * fmt
Definition gwion_print.h:2
meta header (use this to include the whole library)
#define mp_free2(p, sz, a)
Definition mpool.h:28
#define mp_malloc2(p, sz)
Definition mpool.h:30