diff --git a/src/common/strbuf.c b/src/common/strbuf.c new file mode 100644 index 0000000..c7d0835 --- /dev/null +++ b/src/common/strbuf.c @@ -0,0 +1,112 @@ +/* common/strbuf.c + * + * Copyright (C) 2010 Henrik Hautakoski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include "strbuf.h" + +#define CHNK_SIZE 128 + +static void buf_expand(strbuf_t *s, size_t l) { + + if (l <= s->alloc_size) + return; + + do + s->alloc_size += CHNK_SIZE; + while(l > s->alloc_size); + + s->buf = realloc(s->buf, s->alloc_size); + assert(s->buf != NULL); +} + +static inline void swap(char *a, char *b) { + + char tmp = *a; + *a = *b; + *b = tmp; +} + +void strbuf_init(strbuf_t *s) { + + s->buf = NULL; + s->len = 0; + s->alloc_size = 0; +} + +void strbuf_append(strbuf_t *s, char *str, size_t len) { + + if (str == NULL) + return; + + buf_expand(s, s->len + len + 1); + + memcpy(s->buf + s->len, str, len); + s->len += len; + s->buf[s->len] = '\0'; +} + +void strbuf_trim(strbuf_t *s) { + + strbuf_rtrim(s); + strbuf_ltrim(s); +} + +void strbuf_rtrim(strbuf_t *s) { + + for(; s->len > 0 && isspace(s->buf[s->len-1]); s->len--); + s->buf[s->len] = '\0'; +} + +void strbuf_ltrim(strbuf_t *s) { + + size_t i, of = 0; + + for(; of < s->len && isspace(s->buf[of]); of++); + + if (of < 1) + return; + + s->len -= of; + for(i=0; i <= s->len; i++) + s->buf[i] = s->buf[i + of]; +} + +void strbuf_rev(strbuf_t *s) { + + int i; + + for(i=0; i < s->len / 2; i++) + swap(s->buf + i, s->buf + (s->len-1)-i); +} + +char* strbuf_release(strbuf_t *s) { + + char *ret; + + if (s->len + 1 != s->alloc_size) { + ret = realloc(s->buf, s->len + 1); + assert(ret != NULL); + } else { + ret = s->buf; + } + + strbuf_init(s); + + return ret; +} + +void strbuf_free(strbuf_t *s) { + + if (s->alloc_size > 0) + free(s->buf); + s->buf = NULL; +} diff --git a/src/common/strbuf.h b/src/common/strbuf.h new file mode 100644 index 0000000..8e06484 --- /dev/null +++ b/src/common/strbuf.h @@ -0,0 +1,40 @@ +/* common/strbuf.h + * + * Copyright (C) 2010 Henrik Hautakoski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + +#ifndef __COMMON_STRBUF_H +#define __COMMON_STRBUF_H + +#include + +#define STRBUF_INIT { 0, 0, NULL } + +typedef struct { + size_t alloc_size; + size_t len; + char *buf; +} strbuf_t; + +void strbuf_init(strbuf_t *s); + +void strbuf_append(strbuf_t *s, char *str, size_t len); + +void strbuf_trim(strbuf_t *s); + +void strbuf_rtrim(strbuf_t *s); + +void strbuf_ltrim(strbuf_t *s); + +void strbuf_rev(strbuf_t *s); + +char* strbuf_release(strbuf_t *s); + +void strbuf_free(strbuf_t *s); + +#endif /* __COMMON_STRBUF_H */ diff --git a/test/Makefile b/test/Makefile index 028d038..061d292 100644 --- a/test/Makefile +++ b/test/Makefile @@ -10,6 +10,12 @@ all : raw_inotify : $(CC) -linotifytools raw_inotify.c -o raw_inotify + +strbuf : + $(CC) $(CFLAGS) ../src/common/strbuf.c t_strbuf.c -o test_strbuf + +config : + $(CC) ../src/common/config.c t_config.c -o test_config path : $(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/path.c t_path.c -o test_path diff --git a/test/t_strbuf.c b/test/t_strbuf.c new file mode 100644 index 0000000..badc40f --- /dev/null +++ b/test/t_strbuf.c @@ -0,0 +1,49 @@ + +#include +#include +#include "../src/common/strbuf.h" + +void print_strbuf(strbuf_t *s) { + + printf("block: %i, len: %i |%s|\n", s->alloc_size, s->len, s->buf); +} + +int main() { + + strbuf_t b = STRBUF_INIT; + char *str; + + strbuf_append(&b, " ", 4); + strbuf_append(&b, "abcdef", 6); + + print_strbuf(&b); + + strbuf_append(&b, "012345678901234567890123456789", 30); + strbuf_append(&b, " ", 6); + + print_strbuf(&b); + + strbuf_rtrim(&b); + + print_strbuf(&b); + + strbuf_ltrim(&b); + + print_strbuf(&b); + + strbuf_trim(&b); + + print_strbuf(&b); + + strbuf_rev(&b); + + print_strbuf(&b); + + str = strbuf_release(&b); + + printf("released |%s|\n", str); + + free(str); + + return 0; +}