diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-04-15 20:58:53 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-04-15 20:58:53 +0930 |
commit | bbf1f5d35e8312fb7373a997664309adf9527af4 (patch) | |
tree | 39dbd1d56cd730e07a27854adda504b8a120ce2f /strbuf.h | |
parent | a336401403ed55ca1956c627a5413e456b1f87e8 (diff) | |
download | lua-cjson-bbf1f5d35e8312fb7373a997664309adf9527af4.tar.gz lua-cjson-bbf1f5d35e8312fb7373a997664309adf9527af4.tar.bz2 lua-cjson-bbf1f5d35e8312fb7373a997664309adf9527af4.zip |
Initial commit
Split Lua JSON from parent project to create standalone module.
Remove unnecesssary files from new repo.
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 | */ | ||