aboutsummaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c64
1 files changed, 43 insertions, 21 deletions
diff --git a/strbuf.c b/strbuf.c
index f823884..9f0d212 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -18,10 +18,14 @@ static void die(const char *format, ...)
18 18
19void strbuf_init(strbuf_t *s) 19void strbuf_init(strbuf_t *s)
20{ 20{
21 s->data = NULL; 21 s->buf = NULL;
22 s->size = 0; 22 s->size = 0;
23 s->length = 0; 23 s->length = 0;
24 s->increment = STRBUF_DEFAULT_INCREMENT; 24 s->increment = STRBUF_DEFAULT_INCREMENT;
25 s->dynamic = 0;
26
27 strbuf_resize(s, 0);
28 strbuf_ensure_null(s);
25} 29}
26 30
27strbuf_t *strbuf_new() 31strbuf_t *strbuf_new()
@@ -34,6 +38,9 @@ strbuf_t *strbuf_new()
34 38
35 strbuf_init(s); 39 strbuf_init(s);
36 40
41 /* Dynamic strbuf allocation / deallocation */
42 s->dynamic = 1;
43
37 return s; 44 return s;
38} 45}
39 46
@@ -47,22 +54,26 @@ void strbuf_set_increment(strbuf_t *s, int increment)
47 54
48void strbuf_free(strbuf_t *s) 55void strbuf_free(strbuf_t *s)
49{ 56{
50 if (s->data) 57 if (s->buf)
51 free(s->data); 58 free(s->buf);
52 free(s); 59 if (s->dynamic)
60 free(s);
53} 61}
54 62
55char *strbuf_to_char(strbuf_t *s, int *len) 63char *strbuf_free_to_string(strbuf_t *s, int *len)
56{ 64{
57 char *data; 65 char *buf;
58 66
59 data = s->data; 67 strbuf_ensure_null(s);
68
69 buf = s->buf;
60 if (len) 70 if (len)
61 *len = s->length; 71 *len = s->length;
62 72
63 free(s); 73 if (s->dynamic)
74 free(s);
64 75
65 return data; 76 return buf;
66} 77}
67 78
68/* Ensure strbuf can handle a string length bytes long (ignoring NULL 79/* Ensure strbuf can handle a string length bytes long (ignoring NULL
@@ -71,28 +82,41 @@ void strbuf_resize(strbuf_t *s, int len)
71{ 82{
72 int newsize; 83 int newsize;
73 84
74 /* Esnure there is room for optional NULL termination */ 85 /* Ensure there is room for optional NULL termination */
75 newsize = len + 1; 86 newsize = len + 1;
76 /* Round up to the next increment */ 87 /* Round up to the next increment */
77 newsize = ((newsize + s->increment - 1) / s->increment) * s->increment; 88 newsize = ((newsize + s->increment - 1) / s->increment) * s->increment;
78 s->size = newsize; 89 s->size = newsize;
79 s->data = realloc(s->data, s->size); 90 s->buf = realloc(s->buf, s->size);
80 if (!s->data) 91 if (!s->buf)
81 die("Out of memory"); 92 die("Out of memory");
82} 93}
83 94
84void strbuf_append_mem(strbuf_t *s, const char *c, int len) 95void strbuf_append_mem(strbuf_t *s, const char *c, int len)
85{ 96{
86 if (len > strbuf_emptylen(s)) 97 if (len > strbuf_empty_length(s))
87 strbuf_resize(s, s->length + len); 98 strbuf_resize(s, s->length + len);
88 99
89 memcpy(s->data + s->length, c, len); 100 memcpy(s->buf + s->length, c, len);
90 s->length += len; 101 s->length += len;
91} 102}
92 103
93void strbuf_ensure_null(strbuf_t *s) 104void strbuf_append_string(strbuf_t *s, const char *str)
94{ 105{
95 s->data[s->length] = 0; 106 int space, i;
107
108 space = strbuf_empty_length(s);
109
110 for (i = 0; str[i]; i++) {
111 if (space < 1) {
112 strbuf_resize(s, s->length + s->increment);
113 space = strbuf_empty_length(s);
114 }
115
116 s->buf[s->length] = str[i];
117 s->length++;
118 space--;
119 }
96} 120}
97 121
98void strbuf_append_fmt(strbuf_t *s, const char *fmt, ...) 122void strbuf_append_fmt(strbuf_t *s, const char *fmt, ...)
@@ -108,11 +132,9 @@ void strbuf_append_fmt(strbuf_t *s, const char *fmt, ...)
108 /* Append the new formatted string */ 132 /* Append the new formatted string */
109 /* fmt_len is the length of the string required, excluding the 133 /* fmt_len is the length of the string required, excluding the
110 * trailing NULL */ 134 * trailing NULL */
111 empty_len = strbuf_emptylen(s); 135 empty_len = strbuf_empty_length(s);
112 /* Add 1 since there is also space for the terminating NULL. 136 /* Add 1 since there is also space to store the terminating NULL. */
113 * If the string hasn't been allocated then empty_len == -1, 137 fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg);
114 * and vsprintf() won't store anything on the first pass */
115 fmt_len = vsnprintf(s->data + s->length, empty_len + 1, fmt, arg);
116 va_end(arg); 138 va_end(arg);
117 139
118 if (fmt_len <= empty_len) 140 if (fmt_len <= empty_len)