diff options
Diffstat (limited to 'strbuf.h')
-rw-r--r-- | strbuf.h | 56 |
1 files changed, 43 insertions, 13 deletions
@@ -1,32 +1,49 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <stdarg.h> | 2 | #include <stdarg.h> |
3 | 3 | ||
4 | /* Size: Total bytes allocated to *buf | ||
5 | * Length: String length, excluding optional NULL terminator. | ||
6 | * Increment: Allocation increments when resizing the string buffer. | ||
7 | * Dynamic: True if created via strbuf_new() | ||
8 | */ | ||
9 | |||
4 | typedef struct { | 10 | typedef struct { |
5 | char *data; | 11 | char *buf; |
6 | int size; /* Bytes allocated */ | 12 | int size; |
7 | int length; /* Current length of string, not including NULL */ | 13 | int length; |
8 | int increment; /* Allocation Increments */ | 14 | int increment; |
15 | int dynamic; | ||
9 | } strbuf_t; | 16 | } strbuf_t; |
10 | 17 | ||
11 | #ifndef STRBUF_DEFAULT_INCREMENT | 18 | #ifndef STRBUF_DEFAULT_INCREMENT |
12 | #define STRBUF_DEFAULT_INCREMENT 8 | 19 | #define STRBUF_DEFAULT_INCREMENT 8 |
13 | #endif | 20 | #endif |
14 | 21 | ||
15 | extern void strbuf_init(strbuf_t *s); | 22 | /* Initialise */ |
16 | extern strbuf_t *strbuf_new(); | 23 | extern strbuf_t *strbuf_new(); |
24 | extern void strbuf_init(strbuf_t *s); | ||
25 | extern void strbuf_set_increment(strbuf_t *s, int increment); | ||
26 | |||
27 | /* Release */ | ||
17 | extern void strbuf_free(strbuf_t *s); | 28 | extern void strbuf_free(strbuf_t *s); |
18 | extern char *strbuf_to_char(strbuf_t *s, int *len); | 29 | extern char *strbuf_free_to_string(strbuf_t *s, int *len); |
19 | 30 | ||
20 | extern void strbuf_set_increment(strbuf_t *s, int increment); | 31 | /* Management */ |
21 | extern void strbuf_resize(strbuf_t *s, int len); | 32 | extern void strbuf_resize(strbuf_t *s, int len); |
33 | static int strbuf_empty_length(strbuf_t *s); | ||
34 | static int strbuf_length(strbuf_t *s); | ||
35 | static char *strbuf_string(strbuf_t *s, int *len); | ||
36 | |||
37 | /* Update */ | ||
22 | extern void strbuf_append_fmt(strbuf_t *s, const char *format, ...); | 38 | 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); | 39 | extern void strbuf_append_mem(strbuf_t *s, const char *c, int len); |
24 | extern void strbuf_ensure_null(strbuf_t *s); | 40 | extern void strbuf_append_string(strbuf_t *s, const char *str); |
41 | static void strbuf_append_char(strbuf_t *s, const char c); | ||
42 | static void strbuf_ensure_null(strbuf_t *s); | ||
25 | 43 | ||
26 | /* Return bytes remaining in the string buffer | 44 | /* Return bytes remaining in the string buffer |
27 | * Ensure there is space for a NULL. | 45 | * Ensure there is space for a NULL terminator. */ |
28 | * Returns -1 if the string has not been allocated yet */ | 46 | static inline int strbuf_empty_length(strbuf_t *s) |
29 | static inline int strbuf_emptylen(strbuf_t *s) | ||
30 | { | 47 | { |
31 | return s->size - s->length - 1; | 48 | return s->size - s->length - 1; |
32 | } | 49 | } |
@@ -38,10 +55,23 @@ static inline int strbuf_length(strbuf_t *s) | |||
38 | 55 | ||
39 | static inline void strbuf_append_char(strbuf_t *s, const char c) | 56 | static inline void strbuf_append_char(strbuf_t *s, const char c) |
40 | { | 57 | { |
41 | if (strbuf_emptylen(s) < 1) | 58 | if (strbuf_empty_length(s) < 1) |
42 | strbuf_resize(s, s->length + 1); | 59 | strbuf_resize(s, s->length + 1); |
43 | 60 | ||
44 | s->data[s->length++] = c; | 61 | s->buf[s->length++] = c; |
62 | } | ||
63 | |||
64 | static inline void strbuf_ensure_null(strbuf_t *s) | ||
65 | { | ||
66 | s->buf[s->length] = 0; | ||
67 | } | ||
68 | |||
69 | static inline char *strbuf_string(strbuf_t *s, int *len) | ||
70 | { | ||
71 | if (len) | ||
72 | *len = s->length; | ||
73 | |||
74 | return s->buf; | ||
45 | } | 75 | } |
46 | 76 | ||
47 | /* vi:ai et sw=4 ts=4: | 77 | /* vi:ai et sw=4 ts=4: |