X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=src%2Fcstring.c;h=cf5756ffd3699532b4d6ac7a64613e12fac2e56b;hb=354cf2e07ccd0d7900f96bffc279093997cbe203;hp=6d16d2bb8427edfe9767259f3f9c01ec20a1ca6a;hpb=43aa3385fb0e78b302a9191789d59115402035f8;p=smdp.git diff --git a/src/cstring.c b/src/cstring.c index 6d16d2b..cf5756f 100644 --- a/src/cstring.c +++ b/src/cstring.c @@ -1,6 +1,6 @@ /* * An implementation of expandable c strings in heap memory. - * Copyright (C) 2014 Michael Goehler + * Copyright (C) 2018 Michael Goehler * * This file is part of mdp. * @@ -19,49 +19,74 @@ * */ -#include // strlen +#include // wcslen, wcscat, wmemmove +#include // fprintf #include // malloc, realloc #include "cstring.h" cstring_t *cstring_init() { - cstring_t *x = malloc(sizeof(cstring_t)); - x->text = NULL; - x->size = x->alloc = 0; - x->expand = cstring_expand; - x->expand_arr = cstring_expand_arr; - x->reset = cstring_reset; - x->delete = cstring_delete; + cstring_t *x = NULL; + if((x = malloc(sizeof(cstring_t))) != NULL) { + x->value = NULL; + x->size = x->alloc = 0; + x->expand = cstring_expand; + x->expand_arr = cstring_expand_arr; + x->strip = cstring_strip; + x->reset = cstring_reset; + x->delete = cstring_delete; + } else { + fprintf(stderr, "%s\n", "cstring_init() failed to allocate memory."); + exit(EXIT_FAILURE); + } return x; } -void cstring_expand(cstring_t *self, char x) { - if(self->size + sizeof(x) + sizeof(char) > self->alloc) { - self->alloc += (REALLOC_ADD * sizeof(char)); - self->text = realloc(self->text, self->alloc); +void cstring_expand(cstring_t *self, wchar_t x) { + if((self->size + 2) * sizeof(wchar_t) > self->alloc) { + self->alloc += (REALLOC_ADD * sizeof(wchar_t)); + if((self->value = realloc(self->value, self->alloc)) == NULL) { + fprintf(stderr, "%s\n", "cstring_expand() failed to reallocate memory."); + exit(EXIT_FAILURE); + } + } + self->value[self->size] = x; + self->value[self->size+1] = L'\0'; + self->size = wcslen(self->value); +} + +void cstring_expand_arr(cstring_t *self, wchar_t *x) { + if((self->size + wcslen(x) + 1) * sizeof(wchar_t) > self->alloc) { + self->alloc = ((self->size + wcslen(x) + 1) * sizeof(wchar_t)); + if((self->value = realloc(self->value, self->alloc)) == NULL) { + fprintf(stderr, "%s\n", "cstring_expand_arr() failed to reallocate memory."); + exit(EXIT_FAILURE); + } } - self->text[self->size] = x; - self->text[self->size+1] = '\0'; - self->size = strlen(self->text); + self->value = wcscat(self->value, x); + self->size = wcslen(self->value); + self->value[self->size+1] = L'\0'; } -void cstring_expand_arr(cstring_t *self, char *x) { - if(self->size + strlen(x) + sizeof(char) > self->alloc) { - self->alloc = ((strlen(x) + self->size + 1) * sizeof(char)); - self->text = realloc(self->text, self->alloc); +void cstring_strip(cstring_t *self, int pos, int len) { + if(pos + len >= self->size) { + if(pos <= self->size) { + self->value[pos] = L'\0'; + self->size = pos; + } + return; } - self->text = strcat(self->text, x); - self->size = strlen(self->text); - self->text[self->size+1] = '\0'; + wmemmove(&self->value[pos], &self->value[pos+len], self->size - pos - len+1); + self->size -= len; } void cstring_reset(cstring_t *self) { - free(self->text); - self->text = NULL; + free(self->value); + self->value = NULL; self->size = self->alloc = 0; } void cstring_delete(cstring_t *self) { - free(self->text); + free(self->value); free(self); }