sorted some includes + added version number
[smdp.git] / cstring.c
1 #include <string.h> // strlen
2 #include <stdlib.h> // malloc, realloc
3
4 #include "include/cstring.h"
5
6 cstring_t *cstring_init() {
7     cstring_t *x = malloc(sizeof(cstring_t));
8     x->text = (void*)0;
9     x->size = x->alloc = 0;
10     x->expand = cstring_expand;
11     x->expand_arr = cstring_expand_arr;
12     x->reset = cstring_reset;
13     x->delete = cstring_delete;
14     return x;
15 }
16
17 void cstring_expand(cstring_t *self, char x) {
18     if(self->size + sizeof(x) + sizeof(char) > self->alloc) {
19         self->alloc += (REALLOC_ADD * sizeof(char));
20         self->text = realloc(self->text, self->alloc);
21     }
22     self->text[self->size] = x;
23     self->text[self->size+1] = '\0';
24     self->size = strlen(self->text);
25 }
26
27 void cstring_expand_arr(cstring_t *self, char *x) {
28     if(self->size + strlen(x) + sizeof(char) > self->alloc) {
29         self->alloc += (REALLOC_ADD * sizeof(char));
30         self->text = realloc(self->text, self->alloc);
31     }
32     self->text = strcat(self->text, x);
33     self->size = strlen(self->text);
34 }
35
36 void cstring_reset(cstring_t *self) {
37     free(self->text);
38     self->text = (void*)0;
39     self->size = self->alloc = 0;
40 }
41
42 void cstring_delete(cstring_t *self) {
43     free(self->text);
44     free(self);
45 }
46