diff options
| author | Mark Pulford <mark@kyne.com.au> | 2011-04-15 23:34:12 +0930 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2011-04-15 23:34:12 +0930 |
| commit | a99fc753a590d7dd1cf19d083af504cb3ec9a8d4 (patch) | |
| tree | 3dbb5b86808ae0040d9826dc85f9d24500529638 /strbuf.h | |
| parent | bbf1f5d35e8312fb7373a997664309adf9527af4 (diff) | |
| download | lua-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().
Diffstat (limited to '')
| -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: |
