From c17330578022cc147543488615023bfb49c46ebe Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michael=20G=C3=B6hler?= Date: Sat, 16 Aug 2014 17:31:00 +0200 Subject: [PATCH] reset function for cstrings --- include/cstring.h | 8 ++++++++ test/cstring.c | 5 +---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/cstring.h b/include/cstring.h index b87bc8d..175cc0b 100644 --- a/include/cstring.h +++ b/include/cstring.h @@ -30,11 +30,13 @@ typedef struct _cstring_t { size_t alloc; void (*expand)(struct _cstring_t *self, char x); void (*expand_arr)(struct _cstring_t *self, char *x); + void (*reset)(struct _cstring_t *self); void (*delete)(struct _cstring_t *self); } cstring_t; void cstring_expand(cstring_t *self, char x); void cstring_expand_arr(cstring_t *self, char *x); +void cstring_reset(cstring_t *self); void cstring_delete(cstring_t *self); cstring_t *cstring_init() { @@ -43,6 +45,7 @@ cstring_t *cstring_init() { x->size = x->alloc = 0; x->expand = cstring_expand; x->expand_arr = cstring_expand_arr; + x->reset = cstring_reset; x->delete = cstring_delete; return x; } @@ -66,6 +69,11 @@ void cstring_expand_arr(cstring_t *self, char *x) { self->size = strlen(self->text); } +void cstring_reset(cstring_t *self) { + free(self->text); + self->text = (void*)0; + self->size = self->alloc = 0; +} void cstring_delete(cstring_t *self) { free(self->text); diff --git a/test/cstring.c b/test/cstring.c index a1d2b2c..c9d50c2 100644 --- a/test/cstring.c +++ b/test/cstring.c @@ -19,10 +19,7 @@ int main(int argc, char *argv[]) { (p->expand)(p, 'X'); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); (p->expand)(p, 'X'); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); - (p->delete)(p); - - // testing with char array - p = cstring_init(); + (p->reset)(p); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); char x[2] = {'X', '\0'}; -- 2.20.1