aboutsummaryrefslogtreecommitdiff
path: root/strbuf.h
diff options
context:
space:
mode:
Diffstat (limited to 'strbuf.h')
-rw-r--r--strbuf.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/strbuf.h b/strbuf.h
new file mode 100644
index 0000000..fb07e6f
--- /dev/null
+++ b/strbuf.h
@@ -0,0 +1,48 @@
1#include <stdlib.h>
2#include <stdarg.h>
3
4typedef struct {
5 char *data;
6 int size; /* Bytes allocated */
7 int length; /* Current length of string, not including NULL */
8 int increment; /* Allocation Increments */
9} strbuf_t;
10
11#ifndef STRBUF_DEFAULT_INCREMENT
12#define STRBUF_DEFAULT_INCREMENT 8
13#endif
14
15extern void strbuf_init(strbuf_t *s);
16extern strbuf_t *strbuf_new();
17extern void strbuf_free(strbuf_t *s);
18extern char *strbuf_to_char(strbuf_t *s, int *len);
19
20extern void strbuf_set_increment(strbuf_t *s, int increment);
21extern void strbuf_resize(strbuf_t *s, int len);
22extern void strbuf_append_fmt(strbuf_t *s, const char *format, ...);
23extern void strbuf_append_mem(strbuf_t *s, const char *c, int len);
24extern void strbuf_ensure_null(strbuf_t *s);
25
26/* Return bytes remaining in the string buffer
27 * Ensure there is space for a NULL.
28 * Returns -1 if the string has not been allocated yet */
29static inline int strbuf_emptylen(strbuf_t *s)
30{
31 return s->size - s->length - 1;
32}
33
34static inline int strbuf_length(strbuf_t *s)
35{
36 return s->length;
37}
38
39static inline void strbuf_append_char(strbuf_t *s, const char c)
40{
41 if (strbuf_emptylen(s) < 1)
42 strbuf_resize(s, s->length + 1);
43
44 s->data[s->length++] = c;
45}
46
47/* vi:ai et sw=4 ts=4:
48 */