/* * wh_buf.h - a tiny growable string buffer with JSON escapes. * * Every response this plugin writes is assembled in one of these and emitted * with a single fputs(). That is deliberate: a body built half in a buffer and * half with printf() silently reorders itself (and a half-written JSON object * is not JSON), which is a bug that only a parser catches. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_WH_BUF_H #define SX_WH_BUF_H #include typedef struct { char *p; size_t n; size_t cap; int oom; } wh_buf; void wh_buf_init(wh_buf *b); void wh_buf_free(wh_buf *b); void wh_buf_add(wh_buf *b, const char *s, size_t n); void wh_buf_str(wh_buf *b, const char *s); void wh_buf_printf(wh_buf *b, const char *fmt, ...); const char *wh_buf_cstr(wh_buf *b); /* NUL-terminates; "" when empty */ /* Append `s` as a JSON string literal (quoted, escaped, NUL-safe). */ void wh_buf_json_str(wh_buf *b, const char *s); /* Append a JSON object key: ,"name": -- the caller writes the value and the * leading comma is decided by `first`. */ void wh_buf_key(wh_buf *b, const char *key, int *first); /* HTML-escape into a fresh buffer (caller frees with free()). */ char *wh_html_escape(const char *s); #endif /* SX_WH_BUF_H */