1 /* multi-purpose string strbuf, will be initialized to be
2 * at least 1024 bytes long.
13 #define GET_STRING(cmd, param, param_str, ...) do { \
14 error = cmd(__VA_ARGS__, param, 0, NULL, &nusz); \
15 if (REPORT_ERROR("get " param_str " size")) break; \
17 REALLOC(strbuf, nusz, #param); \
20 error = cmd(__VA_ARGS__, param, bufsz, strbuf, NULL); \
21 REPORT_ERROR("get " param_str); \
24 /* Skip leading whitespace in a string */
25 static inline const char* skip_leading_ws(const char *str)
27 const char *ret = str;
28 while (isspace(*ret)) ++ret;
32 /* replace last 3 chars in strbuf with ... */
33 static const char ellip[] = "...";
35 static void trunc_strbuf(void)
37 memcpy(strbuf + bufsz - 4, ellip, 4);
40 /* copy a string to strbuf, at the given offset,
41 * returning the amount of bytes written (excluding the
44 static inline size_t bufcpy_len(size_t offset, const char *str, size_t len)
46 size_t maxlen = bufsz - offset - 1;
47 char *dst = strbuf + offset;
50 fprintf(stderr, "bufcpy overflow copying %s at offset %" PRIuS "/%" PRIuS " (%s)\n",
51 str, offset, bufsz, strbuf);
58 /* TODO enlarge strbuf instead, if maxlen > 0 */
60 memcpy(dst, str, len);
65 strbuf[offset] = '\0';
69 /* As above, auto-compute string length */
70 static inline size_t bufcpy(size_t offset, const char *str)
72 return bufcpy_len(offset, str, strlen(str));
76 /* Separators: we want to be able to prepend separators as needed to strbuf,
77 * which we do only if halfway through the buffer. The callers should first
78 * call a 'set_separator' and then use add_separator(&offset) to add it, where szval
79 * is an offset inside the buffer, which will be incremented as needed
85 void set_separator(const char* _sep)
91 /* Note that no overflow check is done: it is assumed that strbuf will have enough room */
92 void add_separator(size_t *offset)
95 *offset += bufcpy_len(*offset, sep, sepsz);