blob: fb07e6f5b74138684dd54b405ae2244c84b9d123 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include <stdlib.h>
#include <stdarg.h>
typedef struct {
char *data;
int size; /* Bytes allocated */
int length; /* Current length of string, not including NULL */
int increment; /* Allocation Increments */
} strbuf_t;
#ifndef STRBUF_DEFAULT_INCREMENT
#define STRBUF_DEFAULT_INCREMENT 8
#endif
extern void strbuf_init(strbuf_t *s);
extern strbuf_t *strbuf_new();
extern void strbuf_free(strbuf_t *s);
extern char *strbuf_to_char(strbuf_t *s, int *len);
extern void strbuf_set_increment(strbuf_t *s, int increment);
extern void strbuf_resize(strbuf_t *s, int len);
extern void strbuf_append_fmt(strbuf_t *s, const char *format, ...);
extern void strbuf_append_mem(strbuf_t *s, const char *c, int len);
extern void strbuf_ensure_null(strbuf_t *s);
/* Return bytes remaining in the string buffer
* Ensure there is space for a NULL.
* Returns -1 if the string has not been allocated yet */
static inline int strbuf_emptylen(strbuf_t *s)
{
return s->size - s->length - 1;
}
static inline int strbuf_length(strbuf_t *s)
{
return s->length;
}
static inline void strbuf_append_char(strbuf_t *s, const char c)
{
if (strbuf_emptylen(s) < 1)
strbuf_resize(s, s->length + 1);
s->data[s->length++] = c;
}
/* vi:ai et sw=4 ts=4:
*/
|