scorpiox-bot-plugin-webhook
←wh_buf.hc
39 lines · 1 KBHEADBlameDownload
1/*
2 * wh_buf.h - a tiny growable string buffer with JSON escapes.
3 *
4 * Every response this plugin writes is assembled in one of these and emitted
5 * with a single fputs(). That is deliberate: a body built half in a buffer and
6 * half with printf() silently reorders itself (and a half-written JSON object
7 * is not JSON), which is a bug that only a parser catches.
8 *
9 * MIT License. Copyright (c) 2026 SCORPIOX INC.
10 */
11#ifndef SX_WH_BUF_H
12#define SX_WH_BUF_H
13
14#include <stddef.h>
15
16typedef struct {
17 char *p;
18 size_t n;
19 size_t cap;
20 int oom;
21} wh_buf;
22
23void wh_buf_init(wh_buf *b);
24void wh_buf_free(wh_buf *b);
25void wh_buf_add(wh_buf *b, const char *s, size_t n);
26void wh_buf_str(wh_buf *b, const char *s);
27void wh_buf_printf(wh_buf *b, const char *fmt, ...);
28const char *wh_buf_cstr(wh_buf *b); /* NUL-terminates; "" when empty */
29
30/* Append `s` as a JSON string literal (quoted, escaped, NUL-safe). */
31void wh_buf_json_str(wh_buf *b, const char *s);
32/* Append a JSON object key: ,"name": -- the caller writes the value and the
33 * leading comma is decided by `first`. */
34void wh_buf_key(wh_buf *b, const char *key, int *first);
35
36/* HTML-escape into a fresh buffer (caller frees with free()). */
37char *wh_html_escape(const char *s);
38
39#endif /* SX_WH_BUF_H */