From: Michael Göhler Date: Sun, 10 Aug 2014 18:46:18 +0000 (+0200) Subject: cstring header + test X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=71843db5886f60d3959d966a5e062a516c24798e;p=smdp.git cstring header + test --- 71843db5886f60d3959d966a5e062a516c24798e diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d318563 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +# set some compiler flags +CFLAGS=-lncurses + +# find *.c in the current directory +SOURCES=$(wildcard *.c) +TESTS=$(wildcard test/*.c) + +# define the output objects by replacing .c with .o +TARGETS=$(SOURCES:.c=) +TEST_TARGETS=$(TESTS:.c=) + +# this will make all objects +all: $(TEST_TARGETS) $(TARGETS) +test: $(TEST_TARGETS) + +# each objects will be build by a *.c file +#%.o: %.c +%: %.c + cc $(CFLAGS) -o $@ $^ + +# this will delete all objects +# if not all objects are there, they will be compiled first +clean: $(TARGETS) $(TEST_TARGETS) + rm -f $^ + diff --git a/include/cstring.h b/include/cstring.h new file mode 100644 index 0000000..b87bc8d --- /dev/null +++ b/include/cstring.h @@ -0,0 +1,75 @@ +#if !defined( CSTRING_H ) +#define CSTRING_H + +/* + * A implementation of expandable c strings in heap memory. + * + * struct: cstring_t which defines a expandable c string type in heap memory + * + * function: cstring_init to intialize struct of type cstring_t + * function: cstring_t->expand to add one character to the string + * function: cstring_t->delete to free the allocated memory + * + * Example: + * cstring_t *p = cstring_init(); + * (p->expand)(p, 'X'); + * (p->delete)(p); + * + */ + +#include // strlen +#include // malloc, realloc + +// The amount of memory allocated from heap when string expansion hits the +// allocated memory limit +#define REALLOC_ADD 10 + +typedef struct _cstring_t { + char *text; + size_t size; + size_t alloc; + void (*expand)(struct _cstring_t *self, char x); + void (*expand_arr)(struct _cstring_t *self, char *x); + 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_delete(cstring_t *self); + +cstring_t *cstring_init() { + cstring_t *x = malloc(sizeof(cstring_t)); + x->text = (void*)0; + x->size = x->alloc = 0; + x->expand = cstring_expand; + x->expand_arr = cstring_expand_arr; + x->delete = cstring_delete; + 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); + } + self->text[self->size] = x; + self->text[self->size+1] = '\0'; + self->size = strlen(self->text); +} + +void cstring_expand_arr(cstring_t *self, char *x) { + if(self->size + strlen(x) + sizeof(char) > self->alloc) { + self->alloc += (REALLOC_ADD * sizeof(char)); + self->text = realloc(self->text, self->alloc); + } + self->text = strcat(self->text, x); + self->size = strlen(self->text); +} + + +void cstring_delete(cstring_t *self) { + free(self->text); + free(self); +} + +#endif // !defined( CSTRING_H ) diff --git a/test/cstring.c b/test/cstring.c new file mode 100644 index 0000000..a1d2b2c --- /dev/null +++ b/test/cstring.c @@ -0,0 +1,43 @@ +#include + +#include "../include/cstring.h" + +int main(int argc, char *argv[]) { + + // testing with char + cstring_t *p = cstring_init(); + 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->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->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->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->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->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(); + printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + + char x[2] = {'X', '\0'}; + + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + (p->expand_arr)(p, x); printf("text: %s, size: %i, alloc: %i\n", p->text, p->size, p->alloc); + + (p->delete)(p); +} +