7 #define STRBUFFER_MIN_SIZE 16
8 #define STRBUFFER_FACTOR 2
10 int strbuffer_init(strbuffer_t *strbuff)
12 strbuff->size = STRBUFFER_MIN_SIZE;
15 strbuff->value = malloc(strbuff->size);
19 /* initialize to empty */
20 strbuff->value[0] = '\0';
24 void strbuffer_close(strbuffer_t *strbuff)
29 strbuff->value = NULL;
32 const char *strbuffer_value(strbuffer_t *strbuff)
34 return strbuff->value;
37 char *strbuffer_steal_value(strbuffer_t *strbuff)
39 char *result = strbuff->value;
40 strbuffer_init(strbuff);
44 int strbuffer_append(strbuffer_t *strbuff, const char *string)
46 return strbuffer_append_bytes(strbuff, string, strlen(string));
49 int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size)
51 if(strbuff->length + size >= strbuff->size)
53 strbuff->size = max(strbuff->size * STRBUFFER_FACTOR,
54 strbuff->length + size + 1);
56 strbuff->value = realloc(strbuff->value, strbuff->size);
61 memcpy(strbuff->value + strbuff->length, data, size);
62 strbuff->length += size;
63 strbuff->value[strbuff->length] = '\0';