-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcstring.h
135 lines (114 loc) · 3.18 KB
/
cstring.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* xml2json
*
* Copyright (c) 2018 Partha Susarla <[email protected]>
* cstring - String handling library based of git's strbuf implementation.
*/
#ifndef XML2JSON_CSTRING_H
#define XML2JSON_CSTRING_H
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
struct _cstring {
size_t len;
size_t alloc;
char *buf;
};
typedef struct _cstring cstring;
extern char cstring_base[];
#define CSTRING_INIT { .len = 0, .alloc = 0, .buf = cstring_base }
/* cstring_init():
* Initialise the cstring structure.
*
*/
void cstring_init(cstring *cstr, size_t len);
/* cstring_release():
* Release the cstring structure and memory.
*/
void cstring_release(cstring *cstr);
/* cstring_detach():
* The caller needs to free(), the string returned.
*/
char *cstring_detach(cstring *cstr, size_t *len);
/* cstring_attach():
* Attach a string to a cstring buffer. You should
* specify the string to attach, the length of string and
* the amount of allocated memory. The amount should be
* larger than the string length. This string must be
* malloc()ed, and after attaching shouldn't be free()d.
*/
void cstring_attach(cstring *cstr, void *buf, size_t len, size_t alloc);
/* cstring_grow():
* Increase the cstring buffer size allocated length by `len`.
*/
void cstring_grow(cstring *cstr, size_t len);
/* cstring_available() :
* Returns the available space in the cstring buffer.
*/
static inline size_t cstring_available(const cstring *cstr)
{
return cstr->alloc ? cstr->alloc - cstr->len - 1 : 0;
}
/* cstring_setlen()
* Set the length of the cstring buffer to the new value.
* This function does not allocate new memory.
*/
static inline void cstring_setlen(cstring *cstr, size_t len)
{
if (len > (cstr->alloc ? cstr->alloc -1 : 0)) {
fprintf(stderr, "Cannot set length beyond buffer size\n");
exit(EXIT_FAILURE);
}
cstr->len = len;
if (cstr->buf != cstring_base)
cstr->buf[len] = '\0';
else
assert(!cstring_base[0]);
}
/* cstring_addch():
* Add a single character to a cstring
*/
static inline void cstring_addch(cstring *cstr, int ch)
{
if (!cstring_available(cstr))
cstring_grow(cstr, 1);
cstr->buf[cstr->len++] = ch;
cstr->buf[cstr->len] = '\0';
}
/* cstring_add():
* Add data of a given length to the cstring buffer.
*
*/
void cstring_add(cstring *cstr, const void *data, size_t len);
/*
* cstring_addstr():
* Add a NULL terminated string to the cstring buffer
*/
static inline void cstring_addstr(cstring *cstr, const char *str)
{
cstring_add(cstr, (const void *)str, strlen(str));
}
/*
* cstring_dup():
* Copy the contents of on cstring buffer to another
*/
void cstring_dup(cstring *src, cstring *dest);
/* cstring_ltrim():
* trim leading spaces from a cstring
*/
void cstring_ltrim(cstring *cstr);
/* cstring_rtrim():
* trim trailing spaces from a cstring
*/
void cstring_rtrim(cstring *cstr);
/* cstring_trim():
* trim leading and trailing spaces from a cstring
*/
void cstring_trim(cstring *cstr);
#ifdef __cplusplus
}
#endif
#endif /* XML2JSON_CSTRING_H_ */