aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-04-15 23:34:12 +0930
committerMark Pulford <mark@kyne.com.au>2011-04-15 23:34:12 +0930
commita99fc753a590d7dd1cf19d083af504cb3ec9a8d4 (patch)
tree3dbb5b86808ae0040d9826dc85f9d24500529638
parentbbf1f5d35e8312fb7373a997664309adf9527af4 (diff)
downloadlua-cjson-a99fc753a590d7dd1cf19d083af504cb3ec9a8d4.tar.gz
lua-cjson-a99fc753a590d7dd1cf19d083af504cb3ec9a8d4.tar.bz2
lua-cjson-a99fc753a590d7dd1cf19d083af504cb3ec9a8d4.zip
Add functions for new strbuf API and tidy
- Support strbuf_free() when strbuf is not dynamically created. - Ensure strbuf_free_to_string() returns a null terminated string. - Tidy API function naming. - Add strbuf_append_string(), strbuf_string(). - Allocate initial buffer in strbuf_init().
-rw-r--r--strbuf.c64
-rw-r--r--strbuf.h56
2 files changed, 86 insertions, 34 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)
diff --git a/strbuf.h b/strbuf.h
index fb07e6f..96dd97b 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -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
4typedef struct { 10typedef 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
15extern void strbuf_init(strbuf_t *s); 22/* Initialise */
16extern strbuf_t *strbuf_new(); 23extern strbuf_t *strbuf_new();
24extern void strbuf_init(strbuf_t *s);
25extern void strbuf_set_increment(strbuf_t *s, int increment);
26
27/* Release */
17extern void strbuf_free(strbuf_t *s); 28extern void strbuf_free(strbuf_t *s);
18extern char *strbuf_to_char(strbuf_t *s, int *len); 29extern char *strbuf_free_to_string(strbuf_t *s, int *len);
19 30
20extern void strbuf_set_increment(strbuf_t *s, int increment); 31/* Management */
21extern void strbuf_resize(strbuf_t *s, int len); 32extern void strbuf_resize(strbuf_t *s, int len);
33static int strbuf_empty_length(strbuf_t *s);
34static int strbuf_length(strbuf_t *s);
35static char *strbuf_string(strbuf_t *s, int *len);
36
37/* Update */
22extern void strbuf_append_fmt(strbuf_t *s, const char *format, ...); 38extern void strbuf_append_fmt(strbuf_t *s, const char *format, ...);
23extern void strbuf_append_mem(strbuf_t *s, const char *c, int len); 39extern void strbuf_append_mem(strbuf_t *s, const char *c, int len);
24extern void strbuf_ensure_null(strbuf_t *s); 40extern void strbuf_append_string(strbuf_t *s, const char *str);
41static void strbuf_append_char(strbuf_t *s, const char c);
42static 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 */ 46static inline int strbuf_empty_length(strbuf_t *s)
29static 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
39static inline void strbuf_append_char(strbuf_t *s, const char c) 56static 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
64static inline void strbuf_ensure_null(strbuf_t *s)
65{
66 s->buf[s->length] = 0;
67}
68
69static 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: