From ada4556833ce249a0d4c3c2a5a91e0a6a01b837f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 21 Jan 2019 11:37:43 +0100 Subject: Rename all instances of string to strbuf. --- main.c | 5 +---- parse.c | 22 +++++++++++----------- strbuf.c | 34 ++++++++++++++++++---------------- strbuf.h | 40 +++++++++++++++++++--------------------- vcal.c | 20 ++++++++++---------- vcal.h | 22 +++++++++++----------- 6 files changed, 70 insertions(+), 73 deletions(-) diff --git a/main.c b/main.c index 0cf0439a..278aac26 100644 --- a/main.c +++ b/main.c @@ -17,9 +17,7 @@ int main (int argc, char* argv[argc]) { puts("Please give vdir as first argument"); exit (1); } - // vcalendar cal; - // init_vcalendar(&cal); - // CONSTRUCT(vcalendar, &cal); + SNEW(vcalendar, cal); char* dname = argv[1]; @@ -28,7 +26,6 @@ int main (int argc, char* argv[argc]) { int fcount = 0; while ((d = readdir(dir)) != NULL) { - /* Check that it's a regular file */ if (d->d_type != DT_REG) continue; diff --git a/parse.c b/parse.c index f71e264d..76eb2300 100644 --- a/parse.c +++ b/parse.c @@ -4,7 +4,7 @@ /* - * TODO currently not all pointers inside strings are reset correctly, + * TODO currently not all pointers inside strbufs are reset correctly, * leading to old garbage data being read way to much. * * A better ERR macro would solve most problems, @@ -16,7 +16,7 @@ int parse_file(FILE* f, vcalendar* cal) { int segments = 1; - string str; + strbuf str; strbuf_init_1 (&str, segments * SEGSIZE); part_context p_ctx = p_key; @@ -57,8 +57,8 @@ int parse_file(FILE* f, vcalendar* cal) { // TODO segments is always incremented here, meaning // that segment grows larger for every multi line // encountered. - if (realloc_string(&str, ++segments * SEGSIZE) != 0) { /* TODO signal error */ - ERR("Failed to realloc string", line); + if (strbuf_realloc(&str, ++segments * SEGSIZE) != 0) { /* TODO signal error */ + ERR("Failed to realloc strbuf", line); exit (1); } continue; @@ -75,10 +75,10 @@ int parse_file(FILE* f, vcalendar* cal) { if (str.ptr + 1 > vallen) { vallen = str.ptr + 1; // TODO this fails - realloc_string(&cline.val, vallen); + strbuf_realloc(&cline.val, vallen); } */ - copy_strbuf(&cline.val, &str); + strbuf_copy(&cline.val, &str); strbuf_cap(&cline.val); ++line; @@ -102,10 +102,10 @@ int parse_file(FILE* f, vcalendar* cal) { if (str.ptr + 1 > keylen) { keylen = str.ptr + 1; // TODO this might break everything - realloc_string(&key, keylen); + strbuf_realloc(&key, keylen); } */ - copy_strbuf(&cline.key, &str); + strbuf_copy(&cline.key, &str); strbuf_cap(&cline.key); strbuf_soft_reset(&str); p_ctx = p_value; @@ -126,12 +126,12 @@ int parse_file(FILE* f, vcalendar* cal) { */ if (str.ptr + 1 > vallen) { vallen = str.ptr + 1; - realloc_string(&cline.val, vallen); + strbuf_realloc(&cline.val, vallen); } - copy_strbuf(&cline.val, &str); + strbuf_copy(&cline.val, &str); *strbuf_cur(&cline.val) = 0; } - free_string(&str); + strbuf_free(&str); content_line_free(&cline); return 0; diff --git a/strbuf.c b/strbuf.c index f6aad92a..932b31e0 100644 --- a/strbuf.c +++ b/strbuf.c @@ -2,10 +2,12 @@ #include +#ifdef SAFE_STR #include #define ERR(s) fprintf(stderr, "\x1B[0;31mERR\x1b[m (strbuf %3i): %s\n", __LINE__, s) +#endif -int strbuf_init_0(string* str) { +int strbuf_init_0(strbuf* str) { str->mem = NULL; str->alloc = 0; str->len = 0; @@ -13,7 +15,7 @@ int strbuf_init_0(string* str) { return 0; } -int strbuf_init_1(string* str, size_t len) { +int strbuf_init_1(strbuf* str, size_t len) { str->mem = malloc(len); str->alloc = len; str->ptr = 0; @@ -21,7 +23,7 @@ int strbuf_init_1(string* str, size_t len) { return 0; } -int realloc_string(string* str, size_t len) { +int strbuf_realloc(strbuf* str, size_t len) { #ifdef SAFE_STR if (str->mem == NULL || str->alloc == 0) { ERR("String memory not initialized"); @@ -33,7 +35,7 @@ int realloc_string(string* str, size_t len) { return 0; } -int free_string(string* str) { +int strbuf_free(strbuf* str) { #ifdef SAFE_STR if (str->alloc == 0 || str->mem == NULL) { ERR("String not allocated"); @@ -45,18 +47,18 @@ int free_string(string* str) { return 0; } -int strbuf_append(string* s, char c) { +int strbuf_append(strbuf* s, char c) { s->mem[s->len] = c; s->ptr = ++s->len; return 0; } -int strbuf_cap(string* s) { +int strbuf_cap(strbuf* s) { return strbuf_append(s, 0); } -int copy_strbuf(string* dest, string* src) { +int strbuf_copy(strbuf* dest, strbuf* src) { #ifdef SAFE_STR if (dest->alloc < src->len) { ERR("Not enough memmory allocated"); @@ -68,15 +70,15 @@ int copy_strbuf(string* dest, string* src) { return 0; } -int strbuf_cmp(string* a, string* b) { +int strbuf_cmp(strbuf* a, strbuf* b) { return strcmp(a->mem, b->mem); } -int strbuf_c(string* a, char* b) { +int strbuf_c(strbuf* a, char* b) { return strcmp(a->mem, b) == 0; } -char* charat(string* s, int idx) { +char* charat(strbuf* s, int idx) { #ifdef SAFE_STR if (idx > s->len) { ERR("Index out of bounds"); @@ -86,17 +88,17 @@ char* charat(string* s, int idx) { return &s->mem[idx]; } -char* strbuf_cur(string* s) { +char* strbuf_cur(strbuf* s) { return &s->mem[s->ptr]; } -int strbuf_reset(string* s) +int strbuf_reset(strbuf* s) { s->ptr = 0; return 0; } -int strbuf_init_copy(string* dest, string* src) { +int strbuf_init_copy(strbuf* dest, strbuf* src) { #ifdef SAFE_STR if (dest->alloc != 0) { ERR("Dest already allocated"); @@ -105,16 +107,16 @@ int strbuf_init_copy(string* dest, string* src) { #endif strbuf_init_1(dest, src->len + 1); - copy_strbuf(dest, src); + strbuf_copy(dest, src); return 0; } -char* strbuf_end(string* s) { +char* strbuf_end(strbuf* s) { return &s->mem[s->len]; } -int strbuf_soft_reset(string* s) { +int strbuf_soft_reset(strbuf* s) { s->ptr = s->len = 0; return 0; } diff --git a/strbuf.h b/strbuf.h index 1a49ca5d..af673693 100644 --- a/strbuf.h +++ b/strbuf.h @@ -2,41 +2,39 @@ #define STRBUF_H #include +#include "macro.h" typedef struct { char* mem; size_t ptr; size_t alloc; size_t len; -} string; +} strbuf; /* - * TODO rename everything to be on the form - * strbuf_.* - * * TODO Check memmory allocation for last +1 byte for null. */ /* - * Init string to size of 0 + * Init strbuf to size of 0 * Doesnt't call malloc. */ -int strbuf_init_0(string* str); +int CONSTRUCTOR_DECL(strbuf); /* * Constructor */ -int strbuf_init_1(string* str, size_t len); +int CONSTRUCTOR_DECL(strbuf, size_t len); /* * Like realloc, but for strbuf */ -int realloc_string(string* str, size_t len); +int strbuf_realloc(strbuf* str, size_t len); /* * Free's contents of str, but keeps str. */ -int free_string(string* str); +int strbuf_free(strbuf* str); /* * Copy contents from src to dest. @@ -44,37 +42,37 @@ int free_string(string* str); * * also see: strbuf_alloc_copy */ -int copy_strbuf(string* dest, string* src); -int strbuf_cmp(string* a, string* b); -int strbuf_c(string* a, char* b); +int strbuf_copy(strbuf* dest, strbuf* src); +int strbuf_cmp(strbuf* a, strbuf* b); +int strbuf_c(strbuf* a, char* b); /* - * Append char to end of string, determined by s->len. + * Append char to end of strbuf, determined by s->len. */ -int strbuf_append(string* s, char c); +int strbuf_append(strbuf* s, char c); /* * Calls strbuf_append with NULL. */ -int strbuf_cap(string* s); -int strbuf_reset(string* s); -char* charat(string* s, int idx); -char* strbuf_cur(string* s); +int strbuf_cap(strbuf* s); +int strbuf_reset(strbuf* s); +char* charat(strbuf* s, int idx); +char* strbuf_cur(strbuf* s); /* * Sets the length and seek ptr to 0, but doesn't touch the memmory. */ -int strbuf_soft_reset(string* s); +int strbuf_soft_reset(strbuf* s); /* * Returns the character after the last, so where null hopefully is. */ -char* strbuf_end(string* s); +char* strbuf_end(strbuf* s); /* * Copies contents from src to dest, also allocating dest in the * process. dest should not be initialized before this call. */ -int strbuf_init_copy(string* dest, string* src); +int strbuf_init_copy(strbuf* dest, strbuf* src); #endif /* STRBUF_H */ diff --git a/vcal.c b/vcal.c index 6c318a19..1d1e521a 100644 --- a/vcal.c +++ b/vcal.c @@ -44,8 +44,8 @@ int content_line_copy (content_line* dest, content_line* src) { } int content_line_free (content_line* c) { - free_string(&c->key); - free_string(&c->val); + strbuf_free(&c->key); + strbuf_free(&c->val); // TODO remaining fields @@ -54,10 +54,10 @@ int content_line_free (content_line* c) { /* TODO reimplement this */ int copy_vevent(vevent* dest, vevent* src) { - // copy_strbuf(&dest->dtstart , &src->dtstart); - // copy_strbuf(&dest->dtend , &src->dtend); - // copy_strbuf(&dest->summary , &src->summary); - // copy_strbuf(&dest->description , &src->description); + // strbuf_copy(&dest->dtstart , &src->dtstart); + // strbuf_copy(&dest->dtend , &src->dtend); + // strbuf_copy(&dest->summary , &src->summary); + // strbuf_copy(&dest->description , &src->description); return 0; } @@ -72,10 +72,10 @@ int vevent_init_copy(vevent* dest, vevent* src) { /* TODO reimplement this */ int free_vevent (vevent* ev) { - // free_string(&ev->dtstart); - // free_string(&ev->dtend); - // free_string(&ev->summary); - // free_string(&ev->description); + // strbuf_free(&ev->dtstart); + // strbuf_free(&ev->dtend); + // strbuf_free(&ev->summary); + // strbuf_free(&ev->description); return 0; } diff --git a/vcal.h b/vcal.h index 58d347c1..285d2d0b 100644 --- a/vcal.h +++ b/vcal.h @@ -6,17 +6,17 @@ #include "strbuf.h" typedef struct { - string key; - string value; - string* vals; + strbuf key; + strbuf value; + strbuf* vals; int val_count; } parameter; typedef struct { - string key; - string val; + strbuf key; + strbuf val; - string* aux_values; + strbuf* aux_values; int value_count; parameter* params; @@ -29,10 +29,10 @@ typedef struct { struct s_vevent { /* - string dtstart; - string dtend; - string summary; - string description; + strbuf dtstart; + strbuf dtend; + strbuf summary; + strbuf description; */ TABLE(content_line) clines; }; @@ -60,7 +60,7 @@ int add_content_line (vevent* ev, content_line* c); int copy_vevent(vevent* dest, vevent* src); /* - * Copies src -> dest, initializing all the strings along the way. + * Copies src -> dest, initializing all the strbufs along the way. * Requires dest to be initialized. */ int vevent_init_copy(vevent* dest, vevent* src); -- cgit v1.2.3