diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-01-17 18:49:57 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-01-17 18:49:57 +0100 |
commit | 068c19291e78ac62b2cdd02a0c7753eb8254e2c2 (patch) | |
tree | d83bfd61ac862ad7b32695861a6cdbed05712dc5 | |
parent | Further work. (diff) | |
download | calp-068c19291e78ac62b2cdd02a0c7753eb8254e2c2.tar.gz calp-068c19291e78ac62b2cdd02a0c7753eb8254e2c2.tar.xz |
Works reasonably well for single calendar files.
Diffstat (limited to '')
-rw-r--r-- | parse.c | 20 | ||||
-rw-r--r-- | strbuf.c | 7 | ||||
-rw-r--r-- | strbuf.h | 11 | ||||
-rw-r--r-- | vcal.c | 23 | ||||
-rw-r--r-- | vcal.h | 16 |
5 files changed, 59 insertions, 18 deletions
@@ -27,6 +27,8 @@ fprintf(stderr, "ERR %i: %s (cal %i)\n", __LINE__, (x), (line)); \ } while(0) +#define LINE(nr, key, value) fprintf(stderr, "%i: [%s] := [%s]\n", nr, key, value); + typedef enum { p_key, p_value } part_context; @@ -79,7 +81,7 @@ int parse_file(char* fname, vcalendar* cal) { // TODO segments is always incremented here, meaning // that segment grows larger for every multi line // encountered. -#if 0 +#if 1 if (realloc_string(&str, ++segments * SEGSIZE) != 0) { /* TODO signal error */ ERR("Failed to realloc string", line); exit (1); @@ -97,12 +99,12 @@ int parse_file(char* fname, vcalendar* cal) { realloc_string(&val, vallen); } copy_strbuf(&val, &str); - *strbuf_cur(&val) = 0; + strbuf_cap(&val); ++line; /* We just got a value */ - /* TODO for some reason both key and val is empty here */ + LINE(line, key.mem, val.mem); handle_kv(cal, &ev, &key, &val, line, &s_ctx); strbuf_soft_reset(&str); p_ctx = p_key; @@ -127,7 +129,7 @@ int parse_file(char* fname, vcalendar* cal) { strbuf_append(&str, c); } if (errno != 0) { - ERR("Error parsing", -1); + ERR("Error parsing", errno); } else { /* * Close last pair if the file is lacking trailing whitespace. @@ -141,15 +143,12 @@ int parse_file(char* fname, vcalendar* cal) { copy_strbuf(&val, &str); *strbuf_cur(&val) = 0; } - // TODO this segfaults - /* free_vevent(&ev); free_string(&str); free_string(&key); free_string(&val); - */ - // fclose(f); + fclose(f); return 0; } @@ -159,13 +158,14 @@ int main (int argc, char* argv[argc]) { puts("Please give a ics file as first argument"); exit (1); } + printf("\nParsing %s\n", argv[1]); vcalendar cal; init_vcalendar(&cal); parse_file(argv[1], &cal); - printf("Parsed calendar file containing [%lu] events\n\n", cal.n_events); + printf("\nParsed calendar file containing [%lu] events\n", cal.n_events); for (size_t i = 0; i < cal.n_events; i++) { - printf("%lu: %s\n", i, cal.events[i].summary.mem); + printf("%2lu. %s\n", i + 1, cal.events[i].summary.mem); } free_vcalendar(&cal); @@ -40,6 +40,11 @@ int strbuf_append(string* s, char c) { return 0; } +int strbuf_cap(string* s) { + return strbuf_append(s, 0); +} + + int copy_strbuf(string* dest, string* src) { #ifdef SAFE_STR if (dest->alloc < src->len) { @@ -88,7 +93,7 @@ int strbuf_init_copy(string* dest, string* src) { } #endif - init_string(dest, src->alloc); + init_string(dest, src->len + 1); copy_strbuf(dest, src); return 0; @@ -41,7 +41,16 @@ int free_string(string* str); int copy_strbuf(string* dest, string* src); int strbuf_cmp(string* a, string* b); int strbuf_c(string* a, char* b); -int strbuf_append(string* a, char c); + +/* + * Append char to end of string, determined by s->len. + */ +int strbuf_append(string* 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); @@ -10,6 +10,14 @@ int copy_vevent(vevent* dest, vevent* src) { return 0; } +int vevent_init_copy(vevent* dest, vevent* src) { + strbuf_init_copy(&dest->dtstart , &src->dtstart); + strbuf_init_copy(&dest->dtend , &src->dtend); + strbuf_init_copy(&dest->summary , &src->summary); + strbuf_init_copy(&dest->description , &src->description); + return 0; +} + int free_vevent (vevent* ev) { free_string(&ev->dtstart); free_string(&ev->dtend); @@ -19,11 +27,15 @@ int free_vevent (vevent* ev) { } int push_event(vcalendar* cal, vevent* ev) { - if (cal->n_events + 1> cal->alloc) { + + /* Make sure that cal->events is large enough */ + if (cal->n_events + 1 > cal->alloc) { cal->alloc <<= 1; + cal->events = realloc(cal->events, sizeof(*cal->events) * cal->alloc); } - cal->events = realloc(cal->events, cal->alloc); - copy_vevent(&cal->events[cal->n_events], ev); + + vevent_init_copy(&cal->events[cal->n_events], ev); + cal->n_events++; return 0; } @@ -37,9 +49,8 @@ int init_vcalendar(vcalendar* cal) { int free_vcalendar (vcalendar* cal) { for (size_t i = 0; i < cal->n_events; i++) { - vevent* v = & cal->events[i]; - free_vevent(v); - free(v); + free_vevent(& cal->events[i]); } + free (cal->events); return 0; } @@ -5,6 +5,11 @@ #include "strbuf.h" + +/* + * It's intentionall that there is no vevent_init. That since + * the length of the strings isn't known. + */ typedef struct { string dtstart; string dtend; @@ -12,7 +17,18 @@ typedef struct { string description; } vevent; +/* + * Deep copy from src -> dest + * Requires dest to be initialized beforehand + * TODO possibly remove this. + */ int copy_vevent(vevent* dest, vevent* src); + +/* + * Copies src -> dest, initializing all the strings along the way. + * Requires dest to be initialized. + */ +int vevent_init_copy(vevent* dest, vevent* src); int free_vevent(vevent* ev); typedef struct { |