X-Git-Url: https://git.danieliu.xyz/?p=smdp.git;a=blobdiff_plain;f=src%2Fcstring.c;h=15707868d442e417beaae4552ff7d4563cc09b63;hp=558a78662bc29aa2a68859a58f1d0b01e20ec171;hb=4b8ad1b3b464836eca2e876dd418a8e8d87bd721;hpb=09d6bd1a8a33fac75a999f0822ec10cb77fbc072 diff --git a/src/cstring.c b/src/cstring.c index 558a786..1570786 100644 --- a/src/cstring.c +++ b/src/cstring.c @@ -19,7 +19,7 @@ * */ -#include // strlen, memmove +#include // wcslen, wcscat, wmemmove #include // fprintf #include // malloc, realloc @@ -42,41 +42,41 @@ cstring_t *cstring_init() { 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)); +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->text = realloc(self->text, self->alloc)) == NULL) { fprintf(stderr, "%s\n", "cstring_expand() failed to reallocate memory."); exit(EXIT_FAILURE); } } self->text[self->size] = x; - self->text[self->size+1] = '\0'; - self->size = strlen(self->text); + self->text[self->size+1] = L'\0'; + self->size = wcslen(self->text); } -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)); +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->text = realloc(self->text, self->alloc)) == NULL) { - fprintf(stderr, "%s\n", "cstring_expand() failed to reallocate memory."); + fprintf(stderr, "%s\n", "cstring_expand_arr() failed to reallocate memory."); exit(EXIT_FAILURE); } } - self->text = strcat(self->text, x); - self->size = strlen(self->text); - self->text[self->size+1] = '\0'; + self->text = wcscat(self->text, x); + self->size = wcslen(self->text); + self->text[self->size+1] = L'\0'; } void cstring_strip(cstring_t *self, int pos, int len) { if(pos + len >= self->size) { if(pos <= self->size) { - self->text[pos] = '\0'; + self->text[pos] = L'\0'; self->size = pos; } return; } - memmove(&self->text[pos], &self->text[pos+len], self->size - pos - len+1); + wmemmove(&self->text[pos], &self->text[pos+len], self->size - pos - len+1); self->size -= len; }