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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/* Some string handling -- header
* Copyright (C) 2009 Chris Hall (GMCH), Highwayman
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2, or (at your
* option) any later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef _ZEBRA_QSTRING_H
#define _ZEBRA_QSTRING_H
#include "zebra.h"
#include <stddef.h>
#include <stdint.h>
#ifndef Inline
#define Inline static inline
#endif
/* GCC have printf type attribute check. */
#ifdef __GNUC__
#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
#else
#define PRINTF_ATTRIBUTE(a,b)
#endif /* __GNUC__ */
/*==============================================================================
* These "qstrings" address the ...
*
*
*
*/
typedef struct qstring qstring_t ;
typedef struct qstring* qstring ;
struct qstring
{
void* body ;
size_t size ;
size_t len ;
size_t cp ;
} ;
/*------------------------------------------------------------------------------
* Access functions for body of qstring -- to take care of casting pointers
*
* NB: if the body has not yet been allocated, these functions will return
* NULL or NULL + the offset.
*/
Inline char* /* pointer to body of qstring */
qs_chars(qstring qs)
{
return (char*)qs->body ;
} ;
Inline unsigned char* /* pointer to body of qstring */
qs_bytes(qstring qs)
{
return (unsigned char*)qs->body ;
} ;
Inline char* /* pointer to given offset in qstring */
qs_chars_at(qstring qs, size_t off)
{
return qs_chars(qs) + off ;
} ;
Inline unsigned char* /* pointer to given offset in qstring */
qs_bytes_at(qstring qs, size_t off)
{
return qs_bytes(qs) + off ;
} ;
Inline char* /* pointer to 'cp' offset in qstring */
qs_cp_char(qstring qs)
{
return qs_chars_at(qs, qs->cp) ;
} ;
Inline unsigned char* /* pointer to 'cp' offset in qstring */
qs_cp_byte(qstring qs)
{
return qs_bytes_at(qs, qs->cp) ;
} ;
Inline char* /* pointer to 'len' offset in qstring */
qs_ep_char(qstring qs)
{
return qs_chars_at(qs, qs->len) ;
} ;
Inline unsigned char* /* pointer to 'len' offset in qstring */
qs_ep_byte(qstring qs)
{
return qs_bytes_at(qs, qs->len) ;
} ;
/*==============================================================================
* Functions
*/
extern qstring
qs_init_new(qstring qs, size_t len) ;
extern size_t
qs_alloc(qstring qs, size_t len) ;
extern void
qs_free_body(qstring qs) ;
extern qstring
qs_reset(qstring qs, int free_structure) ;
#define qs_reset_keep(qs) qs_reset(qs, 0)
#define qs_reset_free(qs) qs_reset(qs, 1)
extern int
qs_printf(qstring qs, const char* format, ...) PRINTF_ATTRIBUTE(2, 3) ;
extern int
qs_vprintf(qstring qs, const char *format, va_list args) ;
extern size_t
qs_set(qstring qs, const char* s) ;
extern size_t
qs_set_n(qstring qs, const char* s, size_t len) ;
Inline size_t
qs_need(qstring qs, size_t len) ;
Inline size_t
qs_set_len(qstring qs, size_t len) ;
Inline void
qs_set_empty(qstring qs) ;
Inline size_t
qs_len(qstring qs) ;
Inline size_t
qs_size(qstring qs) ;
Inline void*
qs_term(qstring qs) ;
Inline size_t
qs_insert(qstring qs, const void* src, size_t n) ;
Inline void
qs_replace(qstring qs, const void* src, size_t n) ;
Inline size_t
qs_delete(qstring qs, size_t n) ;
/*==============================================================================
* The Inline functions.
*/
/*------------------------------------------------------------------------------
* Need space for a string of 'len' characters (plus possible '\0').
*
* Returns: size of the qstring body
* (which includes the extra space allowed for '\0')
*
* NB: asking for 0 bytes will cause a body to be allocated, ready for any
* '\0' !
*
* NB: has no effect on 'cp' or 'len'.
*/
Inline size_t
qs_need(qstring qs, size_t len)
{
if (len < qs->size)
{
assert(qs->body != NULL) ;
return qs->size ;
}
else
return qs_alloc(qs, len) ;
} ;
/*------------------------------------------------------------------------------
* Set 'len' -- allocate or extend body as required.
*
* Returns: size of the qstring body
* (which includes the extra space allowed for '\0')
*
* NB: asking for 0 bytes will cause a body to be allocated, ready for any
* '\0' !
*
* NB: has no effect on 'cp' -- even if 'cp' > 'len'.
*/
Inline size_t
qs_set_len(qstring qs, size_t len)
{
qs->len = len ;
return qs_need(qs, len) ;
} ;
/*------------------------------------------------------------------------------
* Reset contents of qstring.
*
* Sets 'cp' = 'len' = 0. Sets first byte of body (if any) to NULL.
*/
Inline void
qs_set_empty(qstring qs)
{
qs->len = 0 ;
qs->cp = 0 ;
if (qs->body != NULL)
*((char*)qs->body) = '\0' ;
} ;
/*------------------------------------------------------------------------------
* Get length of qstring -- by doing strlen() -- and record it in qs->len.
*
* Returns: the string length
*
* NB: if no body has been allocated, length = 0
*/
Inline size_t
qs_len(qstring qs)
{
return qs->len = (qs->body != NULL) ? strlen(qs_chars(qs)) : 0 ;
} ;
/*------------------------------------------------------------------------------
* Get size of qstring body.
*
* NB: if no body has been allocated, size = 0
*/
Inline size_t
qs_size(qstring qs)
{
return qs->size ;
} ;
/*------------------------------------------------------------------------------
* Set '\0' at qs->len -- allocate or extend body as required.
*
* Returns address of body.
*/
Inline void*
qs_term(qstring qs)
{
size_t len ;
if ((len = qs->len) >= qs->size)
qs_alloc(qs, len) ;
*qs_chars_at(qs, len) = '\0' ;
return qs->body ;
} ;
/*------------------------------------------------------------------------------
* Insert 'n' bytes at 'cp' -- moves anything cp..len up.
*
* Increases 'len'. but does not affect 'cp'.
*
* Returns: number of bytes beyond 'cp' that were moved before insert.
*
* NB: if 'cp' > 'len', then sets 'len' = 'cp' first -- which will introduce
* one or more undefined bytes.
*
* NB: the string is NOT re-terminated.
*/
Inline size_t
qs_insert(qstring qs, const void* src, size_t n)
{
size_t after ;
char* p ;
if (qs->len < qs->cp)
qs->len = qs->cp ;
after = qs->len - qs->cp ;
qs_set_len(qs, qs->len + n) ; /* set len and ensure have space */
p = qs_cp_char(qs) ;
if (after > 0)
memmove (p + n, p, after) ;
if (n > 0)
memmove(p, src, n) ;
return after ;
} ;
/*------------------------------------------------------------------------------
* Replace 'n' bytes at 'cp' -- extending if required.
*
* May increase 'len'. but does not affect 'cp'.
*
* NB: if 'cp' > 'len', then sets 'len' = 'cp' first -- which will introduce
* one or more undefined bytes.
*
* NB: the string is NOT re-terminated.
*/
Inline void
qs_replace(qstring qs, const void* src, size_t n)
{
if (qs->len < qs->cp + n)
qs_set_len(qs, qs->cp + n) ; /* set len and ensure have space */
if (n > 0)
memmove(qs_cp_char(qs), src, n) ;
} ;
/*------------------------------------------------------------------------------
* Remove 'n' bytes at 'cp' -- extending if required.
*
* May change 'len'. but does not affect 'cp'.
*
* Returns: number of bytes beyond 'cp' that were moved before insert.
*
* NB: if 'cp' > 'len', then sets 'len' = 'cp' first -- which will introduce
* one or more undefined bytes.
*
* NB: the string is NOT re-terminated.
*/
Inline size_t
qs_delete(qstring qs, size_t n)
{
size_t after ;
char* p ;
/* If deleting up to or beyond len, then simply set len == cp */
if ((qs->cp + n) >= qs->len)
{
qs_set_len(qs, qs->cp) ; /* set len, looks after cp > len */
return 0 ; /* nothing after */
}
/* There is at least one byte after cp (so body must exist) */
after = qs->len - (qs->cp + n) ;
if (n > 0)
{
p = qs_cp_char(qs) ;
memmove (p, p + n, after) ;
qs->len -= n ;
} ;
return after ;
} ;
#endif /* _ZEBRA_QSTRING_H */
|