ec028f322fa6e40b4e7426ff07f4e9ffdfcae5a0
[smdp.git] / include / cstring.h
1 #if !defined( CSTRING_H )
2 #define CSTRING_H
3
4 /*
5  * An implementation of expandable c strings in heap memory.
6  * Copyright (C) 2014 Michael Goehler
7  *
8  * This file is part of mpd.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  *
23  *
24  * struct: cstring_t which defines a expandable c string type in heap memory
25  *
26  * function: cstring_init to intialize struct of type cstring_t
27  * function: cstring_t->expand to add one character to the struct
28  * function: cstring_t->expand_arr to add a string to the struct
29  * function: cstring_t->reset to clear and reuse the struct
30  * function: cstring_t->delete to free the allocated memory
31  *
32  * Example:
33  *      cstring_t *p = cstring_init();
34  *      (p->expand)(p, 'X');
35  *      (p->delete)(p);
36  *
37  */
38
39 // The amount of memory allocated from heap when string expansion hits the
40 // allocated memory limit
41 #define REALLOC_ADD 10
42
43 typedef struct _cstring_t {
44     char *text;
45     size_t size;
46     size_t alloc;
47     void (*expand)(struct _cstring_t *self, char x);
48     void (*expand_arr)(struct _cstring_t *self, char *x);
49     void (*reset)(struct _cstring_t *self);
50     void (*delete)(struct _cstring_t *self);
51 } cstring_t;
52
53 cstring_t *cstring_init();
54 void cstring_expand(cstring_t *self, char x);
55 void cstring_expand_arr(cstring_t *self, char *x);
56 void cstring_reset(cstring_t *self);
57 void cstring_delete(cstring_t *self);
58
59 #endif // !defined( CSTRING_H )