diff options
Diffstat (limited to 'strbuf.h')
-rw-r--r-- | strbuf.h | 48 |
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 | |||
4 | typedef 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 | |||
15 | extern void strbuf_init(strbuf_t *s); | ||
16 | extern strbuf_t *strbuf_new(); | ||
17 | extern void strbuf_free(strbuf_t *s); | ||
18 | extern char *strbuf_to_char(strbuf_t *s, int *len); | ||
19 | |||
20 | extern void strbuf_set_increment(strbuf_t *s, int increment); | ||
21 | extern void strbuf_resize(strbuf_t *s, int len); | ||
22 | extern void strbuf_append_fmt(strbuf_t *s, const char *format, ...); | ||
23 | extern void strbuf_append_mem(strbuf_t *s, const char *c, int len); | ||
24 | extern 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 */ | ||
29 | static inline int strbuf_emptylen(strbuf_t *s) | ||
30 | { | ||
31 | return s->size - s->length - 1; | ||
32 | } | ||
33 | |||
34 | static inline int strbuf_length(strbuf_t *s) | ||
35 | { | ||
36 | return s->length; | ||
37 | } | ||
38 | |||
39 | static 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 | */ | ||