From 41f73b0a45ba3e63ac33ebdc99f12481f5b7cdab Mon Sep 17 00:00:00 2001 From: katherine Date: Fri, 20 Dec 2019 09:28:54 -0700 Subject: implement printing --- src/calendar.c | 125 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 58 deletions(-) (limited to 'src/calendar.c') diff --git a/src/calendar.c b/src/calendar.c index 1240547..06d80d7 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -20,6 +20,7 @@ struct state_s { size_t line, col; size_t last_line, last_col; struct entry_interval_s cur_warn; + struct entry_interval_s cur_stay; bool cur_urgent; bool cur_local; FILE *f; @@ -30,6 +31,7 @@ enum cmd_type_e { CMD_TYPE_WARN, CMD_TYPE_URGENT, CMD_TYPE_LOCAL, + CMD_TYPE_STAY, }; struct cmd_s { @@ -364,6 +366,17 @@ static inline struct cmd_s sub_get_cmd_warn(struct state_s *s) return cmd; } +static inline struct cmd_s sub_get_cmd_stay(struct state_s *s) +{ + struct cmd_s cmd = { + .type = CMD_TYPE_STAY + }; + + cmd.iv = sub_get_interval(s); + + return cmd; +} + static bool sub_get_bool(struct state_s *s) { bool r = false; @@ -526,7 +539,19 @@ static inline struct cmd_s sub_get_cmd(struct state_s *s) ) { s->col += 5; sub_eat_spaces(s); - return sub_get_cmd_urgent(s); + return sub_get_cmd_local(s); + } + break; + + case 's': + if ( + getc(s->f) == 't' && + getc(s->f) == 'a' && + getc(s->f) == 'y' + ) { + s->col += 4; + sub_eat_spaces(s); + return sub_get_cmd_stay(s); } break; } @@ -545,7 +570,7 @@ static char* sub_get_msg(struct state_s *s) while ( (c = getc(s->f)) != '\n' && c != EOF) { s->col++; - if (count == width) { + if (count == width || count + 1 == width) { width += step_size; msg = realloc(msg, width * sizeof(*msg)); @@ -568,6 +593,7 @@ static char* sub_get_msg(struct state_s *s) count++; } + msg[count] = '\0'; s->col = 0; s->line++; @@ -577,10 +603,18 @@ static char* sub_get_msg(struct state_s *s) static inline struct entry_s sub_get_entry_on(struct state_s *s) { struct entry_s e = { - .type = ENTRY_TYPE_ON + .type = ENTRY_TYPE_ON, + .urgent = s->cur_urgent, + .local = s->cur_local, + .warn = s->cur_warn, + .stay = s->cur_stay, }; time_t rt; int c; + struct pos_s p = { + .line = s->last_line, + .col = s->last_col, + }; rt = sub_get_date(s); if (s->err_flag) @@ -593,19 +627,10 @@ static inline struct entry_s sub_get_entry_on(struct state_s *s) c = getc(s->f); if (c != ',') { - if (c == 'w' - && getc(s->f) == 'a' - && getc(s->f) == 'r' - && getc(s->f) == 'n' - ) { - s->col += 4; - e.warn = sub_get_interval(s); - if (s->err_flag) - return e; - } else { - ungetc(c, s->f); - return e; - } + sub_set_pos(s, p); + ERRP("invalid calendar entry"); + s->err_flag = true; + return e; } s->col++; @@ -622,8 +647,9 @@ static inline struct entry_s sub_get_entry_every(struct state_s *s) .urgent = s->cur_urgent, .local = s->cur_local, .warn = s->cur_warn, + .stay = s->cur_stay, }; - bool from_seen = false, to_seen = false, warn_seen = false; + bool from_seen = false, to_seen = false; char c; struct pos_s p = { .line = s->last_line, @@ -685,29 +711,6 @@ static inline struct entry_s sub_get_entry_every(struct state_s *s) break; } - case 'w': - if ( - getc(s->f) == 'a' && - getc(s->f) == 'r' && - getc(s->f) == 'n' - ) { - if (warn_seen) { - ERRP("invalid calendar entry"); - s->err_flag = true; - return e; - } - warn_seen = true; - - s->col += 4; - sub_eat_spaces(s); - e.warn = sub_get_interval(s); - - if (s->err_flag) - return e; - - break; - } - default: goto done; } @@ -792,22 +795,6 @@ static inline void sub_cal_push(struct state_s *s, struct calendar_s *cal, cal->count++; } -static void sub_cal_free(struct calendar_s *cal) -{ - size_t i; - - if (cal->entries == NULL) - return; - - for (i = 0; i < cal->count; i++) { - if (cal->entries[i].msg != NULL) - free(cal->entries[i].msg); - } - - free(cal->entries); - cal->entries = NULL; -} - struct calendar_s calendar_parse(FILE *f) { struct calendar_s cal = { @@ -821,6 +808,9 @@ struct calendar_s calendar_parse(FILE *f) .cur_warn = { .day = 1, }, + .cur_stay = { + .day = 1, + }, .cur_urgent = false, .cur_local = true, .f = f, @@ -843,7 +833,7 @@ struct calendar_s calendar_parse(FILE *f) cmd = sub_get_cmd(&s); if (s.err_flag) { - sub_cal_free(&cal); + calendar_wipe(&cal); cal.err_flag = true; return cal; } @@ -858,6 +848,9 @@ struct calendar_s calendar_parse(FILE *f) case CMD_TYPE_LOCAL: s.cur_local = cmd.b; break; + case CMD_TYPE_STAY: + s.cur_stay = cmd.iv; + break; } continue; @@ -867,7 +860,7 @@ struct calendar_s calendar_parse(FILE *f) e = sub_get_entry(&s); if (s.err_flag) { - sub_cal_free(&cal); + calendar_wipe(&cal); cal.err_flag = true; return cal; } @@ -876,3 +869,19 @@ struct calendar_s calendar_parse(FILE *f) return cal; } + +void calendar_wipe(struct calendar_s *cal) +{ + size_t i; + + if (cal->entries == NULL) + return; + + for (i = 0; i < cal->count; i++) { + if (cal->entries[i].msg != NULL) + free(cal->entries[i].msg); + } + + free(cal->entries); + cal->entries = NULL; +} -- cgit v1.2.3