summaryrefslogtreecommitdiff
path: root/app/dynstring/dynstring.c
blob: eb7b2c5f6063f5e0822f31302234f2aae1f76499 (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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/** \file dynstring.c
* Library for dynamic string functions
*/

#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdarg.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "dynstring.h"

/**
* Get the current length of the string
*
* \param s IN the dynamic string
* \return    the length of the string in bytes
*/

size_t DynStringSize(DynString *s)
{
    if (isnas(s)) {
        return 0;
    }

    return s->size;
}

/* An initialized empty struct string */
#define STRINIT(s) (DynStringMalloc(s, 16))

/**
* Allocate memory for a string of the desired length. To optimize memory usage
* a minimum length of 16 bytes is allocated. The allocated string has to be freed
* using the DynStringFree() function.
*
* \param s IN pointer to DynString structure 
* \param size IN number of bytes to allocate
* \return pointer to the DynString
*/

DynString *DynStringMalloc(DynString *s, size_t size)
{
    if (size < 16) {
        size = 16;
    }

    s->s = malloc(size);
    s->size = 0;
    s->b_size = (size_t)(size | STR_FREEABLE );
    return (s);
}

/**
* Try to compact string memory by reallocating a buffer large enough for the current
* string content.
*
* \param s IN the dynamic string
*/

void DynStringRealloc(DynString *s)
{
    char *buf;

    /* Not a string? */
    if (isnas(s)) {
        return;
    }

    /* Can't realloc? */
    if (!(s->b_size & STR_FREEABLE)) {
        return;
    }

    /* Don't invoke undefined behaviour with realloc(x, 0) */
    if (!s->size) {
        free(s->s);
        s->s = malloc(16);
		s->b_size = (size_t)(16 | STR_FREEABLE);
    } else {
        /* Try to compact */
        buf = realloc(s->s, s->size);

        if (buf) {
            s->s = buf;
        }
		s->b_size = (size_t)(s->size | STR_FREEABLE);
    }
}

/**
* Clear the dynamic string. Current content is deleted. The buffer is shrinked to the 
* minimum size. 
*
* \param s IN the dynamic string
*/

void DynStringClear(DynString *s)
{
	/* Not a string? */
	if (isnas(s))
	{
		return;
	}

	/* Can't realloc? */
	if (!(s->b_size & STR_FREEABLE))
	{
		return;
	}
	s->size = 0;

	DynStringRealloc(s);
}
/**
* Resize the string for a minimum number of bytes. In order to optimize memory usage the
* actually allocated block of memory can be larger than the requested size.
* In case of an error, the string is set to NaS
*
* \param s IN  OUT the string
* \param size IN the requested new size
*/

void DynStringResize(DynString *s, size_t size)
{
    char *buf;
    size_t bsize;

    /* Are we not a string? */
    if (isnas(s)) {
        return;
    }

    /* Not resizable */
    if (!(s->b_size & STR_FREEABLE)) {
        DynString s2;

        /* Don't do anything if we want to shrink */
        if (size <= s->size) {
            return;
        }

        /* Need to alloc a new string */
        DynStringMalloc(&s2, size);
        /* Copy into new string */
        memcpy(s2.s, s->s, s->size);
        /* Point to new string */
        s->s = s2.s;
        s->b_size = s2.b_size;
        return;
    }

    /* Too big */
    if (size & STR_FREEABLE) {
        DynString nas = NaS;
        free(s->s);
        *s = nas;
        return;
    }

    bsize = (size_t)(s->b_size - STR_FREEABLE);

    /* Keep at least 16 bytes */
    if (size < 16) {
        size = 16;
    }

    /* Nothing to do? */
    if ((4 * size > 3 * bsize) && (size <= bsize)) {
        return;
    }

    /* Try to double size instead of using a small increment */
    if ((size > bsize) && (size < bsize * 2)) {
        size = bsize * 2;
    }

    /* Keep at least 16 bytes */
    if (size < 16) {
        size = 16;
    }

    buf = realloc(s->s, size);

    if (!buf) {
        DynString nas = NaS;
        /* Failed, go to NaS state */
        free(s->s);
        *s = nas;
    } else {
        s->s = buf;
        s->b_size = (size_t)(size | STR_FREEABLE);
    }
}

/**
* Free the previously allocated string.
*
* \param s IN OUT the dynamic string
*/

void DynStringFree(DynString *s)
{
    DynString nas = NaS;

    if (s->b_size & STR_FREEABLE) {
        free(s->s);
    }

    *s = nas;
}

/**
* Create a newly allocated copy of the passed dynamic string.
*
* \param s IN the dynamic string
* \return the newly allocated dynamic string
*/

/* Create a new string as a copy of an old one */
DynString *DynStringDupStr(DynString *s2, DynString *s)
{
    DynString nas = NaS;

    /* Not a string? */
    if (isnas(s)) {
        return NULL;
    }

    DynStringMalloc(s2, s->size);
    s2->size = s->size;
    memcpy(s2->s, s->s, s->size);
    return s2;
}

/**
* Copy the memory from the source string into the dest string.
*
* \param dest IN the destination dynamic string
* \param src IN the source dynamic string
*/

void DynStringCpyStr(DynString *dest, DynString *src)
{
    /* Are we no a string */
    if (isnas(src)) {
        return;
    }

    DynStringResize(dest, src->size);

    if (isnas(dest)) {
        return;
    }

    dest->size = src->size;
    memcpy(dest->s, src->s, src->size);
}

/**
* Return the content of the dynamic string as a \0 terminated C string. This memory may not be freed by the
* caller.
*
* \param s IN the dynamic string
* \return the C string
*/

char *DynStringToCStr(DynString *s)
{
    size_t bsize;

    /* Are we not a string? */
    if (isnas(s)) {
        return NULL;
    }

    /* Get real buffer size */
    bsize = s->b_size & ~STR_FREEABLE;

    if (s->size == bsize) {
        /* Increase buffer size */
        DynStringResize(s, bsize + 1);

        /* Are we no longer a string? */
        if (isnas(s)) {
            return NULL;
        }
    }

    /* Tack a zero on the end */
    s->s[s->size] = 0;
    /* Don't update the size */
    /* Can use this buffer as long as you don't append anything else */
    return s->s;
}

/**
* Concatenate a number of bytes from the source string to the dynamic string.
*
* \param s IN the dynamic string
* \param len IN the number of bytes to append to the dynamic string
* \param str IN the source string
*/

void DynStringNCatCStr(DynString *s, size_t len, const char *str)
{
    size_t bsize;

    /* Are we not a string? */
    if (isnas(s)) {
        return;
    }

    /* Nothing to do? */
    if (!str || !len) {
        return;
    }

    /* Get real buffer size */
    bsize = s->b_size & ~STR_FREEABLE;

    if (s->size + len >= bsize) {
        DynStringResize(s, s->size + len);

        /* Are we no longer a string? */
        if (isnas(s)) {
            return;
        }
    }

    memcpy(&s->s[s->size], str, len);
    s->size += len;
}

/**
* Concatenate a zero-terminated source string to the dynamic string.
*
* \param s IN the dynamic string
* \param str IN the source string
*/

void DynStringCatCStr(DynString *s, const char *str)
{
    if (str) {
        DynStringNCatCStr(s, strlen(str), str);
    }
}

/**
* Concatenate a dynamic string to another dynamic string.
*
* \param s IN the destination dynamic string
* \param s2 IN the source dynamic string
*/

void DynStringCatStr(DynString *s, const DynString *s2)
{
    DynStringNCatCStr(s, s2->size, s2->s);
}

/**
* Concatenate a variable number zero terminated strings to the dynamic string. The
* list of source strings has to be terminated by a NULL pointer.
*
* \param s IN the dynamic string
* \param ... IN variable number of C strings
*/

void DynStringCatCStrs(DynString *s, ...)
{
    const char *str;
    va_list v;

    /* Are we not a string? */
    if (isnas(s)) {
        return;
    }

    va_start(v, s);

    for (str = va_arg(v, const char *); str; str = va_arg(v, const char *)) {
        DynStringNCatCStr(s, strlen(str), str);
    }

    va_end(v);
}

/**
* Concatenate a variable number of dynamic string to another dynamic string.
* The list of source strings has to be terminated by a NULL pointer.
*
* \param s IN the destination dynamic string
* \param s2 IN the source dynamic strings
*/

void DynStringCatStrs(DynString *s1, ...)
{
    const DynString *s2;
    va_list v;

    /* Are we not a string? */
    if (isnas(s1)) {
        return;
    }

    va_start(v, s1);

    for (s2 = va_arg(v, const DynString *); s2; s2 = va_arg(v, const DynString *)) {
        DynStringNCatCStr(s1, s2->size, s2->s);
    }

    va_end(v);
}

/**
* Return a formatted dynamic string. Formatting is performed similar to the printf style
* of functions.
*
* \param s IN the dynamic string
* \param fmt IN format specification
* \param ... IN values
*/

void DynStringPrintf(DynString *s, const char *fmt, ...)
{
    va_list v;
    size_t len;
    DynString nas = NaS;

    /* Are we not a string? */
    if (isnas(s)) {
        STRINIT(s);
    }

    /* Nothing to do? */
    if (!fmt) {
        return;
    }

    va_start(v, fmt);
    len = vsnprintf(NULL, 0, fmt, v) + 1;
    va_end(v);
    DynStringResize(s, len);

    /* Are we no longer a string? */
    if (isnas(s)) {
        return;
    }

    va_start(v, fmt);
    vsnprintf(s->s, len, fmt, v);
    va_end(v);
    s->size = len - 1;
}