aboutsummaryrefslogtreecommitdiff
path: root/strbuf.h
diff options
context:
space:
mode:
Diffstat (limited to 'strbuf.h')
-rw-r--r--strbuf.h56
1 files changed, 43 insertions, 13 deletions
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: