1 /*
2 * An implementation of expandable c strings in heap memory.
3 * Copyright (C) 2018 Michael Goehler
4 *
5 * This file is part of mdp.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include <wchar.h> // wcslen, wcscat, wmemmove
23 #include <stdio.h> // fprintf
24 #include <stdlib.h> // malloc, realloc
25
26 #include "cstring.h"
27
cstring_init()28 cstring_t *cstring_init() {
29 cstring_t *x = NULL;
30 if((x = malloc(sizeof(cstring_t))) != NULL) {
31 x->value = NULL;
32 x->size = x->alloc = 0;
33 x->expand = cstring_expand;
34 x->expand_arr = cstring_expand_arr;
35 x->strip = cstring_strip;
36 x->reset = cstring_reset;
37 x->delete = cstring_delete;
38 } else {
39 fprintf(stderr, "%s\n", "cstring_init() failed to allocate memory.");
40 exit(EXIT_FAILURE);
41 }
42 return x;
43 }
44
cstring_expand(cstring_t * self,wchar_t x)45 void cstring_expand(cstring_t *self, wchar_t x) {
46 if((self->size + 2) * sizeof(wchar_t) > self->alloc) {
47 self->alloc += (REALLOC_ADD * sizeof(wchar_t));
48 if((self->value = realloc(self->value, self->alloc)) == NULL) {
49 fprintf(stderr, "%s\n", "cstring_expand() failed to reallocate memory.");
50 exit(EXIT_FAILURE);
51 }
52 }
53 self->value[self->size] = x;
54 self->value[self->size+1] = L'\0';
55 self->size = wcslen(self->value);
56 }
57
cstring_expand_arr(cstring_t * self,wchar_t * x)58 void cstring_expand_arr(cstring_t *self, wchar_t *x) {
59 if((self->size + wcslen(x) + 1) * sizeof(wchar_t) > self->alloc) {
60 self->alloc = ((self->size + wcslen(x) + 1) * sizeof(wchar_t));
61 if((self->value = realloc(self->value, self->alloc)) == NULL) {
62 fprintf(stderr, "%s\n", "cstring_expand_arr() failed to reallocate memory.");
63 exit(EXIT_FAILURE);
64 }
65 }
66 self->value = wcscat(self->value, x);
67 self->size = wcslen(self->value);
68 self->value[self->size+1] = L'\0';
69 }
70
cstring_strip(cstring_t * self,int pos,int len)71 void cstring_strip(cstring_t *self, int pos, int len) {
72 if(pos + len >= self->size) {
73 if(pos <= self->size) {
74 self->value[pos] = L'\0';
75 self->size = pos;
76 }
77 return;
78 }
79 wmemmove(&self->value[pos], &self->value[pos+len], self->size - pos - len+1);
80 self->size -= len;
81 }
82
cstring_reset(cstring_t * self)83 void cstring_reset(cstring_t *self) {
84 free(self->value);
85 self->value = NULL;
86 self->size = self->alloc = 0;
87 }
88
cstring_delete(cstring_t * self)89 void cstring_delete(cstring_t *self) {
90 free(self->value);
91 free(self);
92 }
93