==> include/mesh/strbuf.h <==1/* @ajax/mesh-strbuf: a growable buffer of bytes (always NUL-terminated, so it is text too) with
2 * printf, and printf into a new string. Allocation failure aborts. */
3#ifndef MESH_STRBUF_H
4#define MESH_STRBUF_H
5
6#include <stdarg.h>
7#include <stddef.h>
8#include <stdint.h>
9
10typedef struct {
11 char *p; /* NUL-terminated when not NULL */
12 size_t n, cap;
13} msb;
14
15void msb_reserve(msb *b, size_t extra);
16void msb_put(msb *b, const void *data, size_t n);
17void msb_puts(msb *b, const char *s);
18void msb_putc(msb *b, char c);
19void msb_printf(msb *b, const char *fmt, ...)
20#if defined(__GNUC__) || defined(__clang__)
21 __attribute__((format(printf, 2, 3)))
22#endif
23 ;
24void msb_vprintf(msb *b, const char *fmt, va_list ap);
25const char *msb_str(msb *b); /* "" when empty */
26char *msb_take(msb *b); /* the string (the caller frees it); b is empty after */
27void msb_clear(msb *b);
28void msb_free(msb *b);
29
30/* printf into a new string (the caller frees it). */
31char *mfmt(const char *fmt, ...)
32#if defined(__GNUC__) || defined(__clang__)
33 __attribute__((format(printf, 1, 2)))
34#endif
35 ;
36char *mvfmt(const char *fmt, va_list ap);
37char *mstrdup(const char *s);
38char *mstrndup(const char *s, size_t n);
39void *mxalloc(size_t n); /* zeroed; aborts when out of memory */
40void *mxrealloc(void *p, size_t n);
41
42#endif