From 43bdd12254082c58b04e0d3b3032569443e96e4f Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Wed, 27 Feb 2013 10:45:51 +0100 Subject: parse_{date,time}(): Split out date/time validation Split date/time validation into separate functions check_date() and check_time(). These will be used to validate date/time information when reading items from the appointments file. Signed-off-by: Lukas Fleischer --- src/calcurse.h | 2 ++ src/utils.c | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/calcurse.h b/src/calcurse.h index 64c8808..e8313b0 100644 --- a/src/calcurse.h +++ b/src/calcurse.h @@ -974,7 +974,9 @@ char *nowstr(void); void print_bool_option_incolor(WINDOW *, unsigned, int, int); const char *get_tempdir(void); char *new_tempfile(const char *, int); +int check_date(unsigned, unsigned, unsigned); int parse_date(const char *, enum datefmt, int *, int *, int *, struct date *); +int check_time(unsigned, unsigned); int parse_time(const char *, unsigned *, unsigned *); int parse_duration(const char *, unsigned *); void file_close(FILE *, const char *); diff --git a/src/utils.c b/src/utils.c index 11b7d60..03cf7d6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -633,6 +633,17 @@ char *new_tempfile(const char *prefix, int trailing_len) return mem_strdup(fullname + prefix_len); } +/* + * Check if a date is valid. + */ +int +check_date(unsigned year, unsigned month, unsigned day) +{ + return (year >= 1902 && year <= 2037 && month >= 1 && month <= 12 && + day >= 1 && day <= days[month - 1] + (month == 2 && + ISLEAP(year)) ? 1 : 0); +} + /* * Convert a string containing a date into three integers containing the year, * month and day. @@ -707,8 +718,7 @@ parse_date(const char *date_string, enum datefmt datefmt, int *year, } /* check if date is valid, take leap years into account */ - if (y < 1902 || y > 2037 || m < 1 || m > 12 || d < 1 || - d > days[m - 1] + (m == 2 && ISLEAP(y)) ? 1 : 0) + if (!check_date(y, m, d)) return 0; if (year) @@ -721,6 +731,16 @@ parse_date(const char *date_string, enum datefmt datefmt, int *year, return 1; } +/* + * Check if time is valid. + */ +int +check_time(unsigned hours, unsigned minutes) +{ + return (hours < DAYINHOURS && minutes < HOURINMIN); +} + + /* * Converts a time string into hours and minutes. Short forms like "23:" * (23:00) or ":45" (0:45) are allowed. @@ -749,7 +769,7 @@ int parse_time(const char *string, unsigned *hour, unsigned *minute) } } - if (n != 1 || in[0] >= DAYINHOURS || in[1] >= HOURINMIN) + if (n != 1 || !check_time(in[0], in[1])) return 0; *hour = in[0]; -- cgit v1.2.3-70-g09d2