From 39dd3c251db7a70b9ec013781ca74fe6fc241792 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Wed, 5 Oct 2011 09:28:17 +0200 Subject: Use parse_{time,duration}() where appropriate Make use of these new helpers at various places. Note that this patch implies a few behavioural changes: * Short forms such as "23:" and ":45" are allowed when entering times. * Durations always need to be prefixed with a plus sign ("+"), with the nice side effect that you can now use "+3:30" to declare an appointment that lasts three hours and thirty minutes (that's much more convenient than "+210"). Signed-off-by: Lukas Fleischer --- src/apoint.c | 50 +++++++++++++++++++++++++------------------------- src/day.c | 32 ++++++++++++++------------------ 2 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/apoint.c b/src/apoint.c index 9700c1b..174d1a9 100644 --- a/src/apoint.c +++ b/src/apoint.c @@ -155,26 +155,26 @@ apoint_add (void) { #define LTIME 6 char *mesg_1 = - _("Enter start time ([hh:mm] or [h:mm]), " - "leave blank for an all-day event : "); + _("Enter start time ([hh:mm]), leave blank for an all-day event : "); char *mesg_2 = - _("Enter end time ([hh:mm] or [h:mm]) or duration (in minutes) : "); + _("Enter end time ([hh:mm]) or duration ([+hh:mm]) : "); char *mesg_3 = _("Enter description :"); char *format_message_1 = - _("You entered an invalid start time, should be [h:mm] or [hh:mm]"); + _("You entered an invalid start time, should be [hh:mm]"); char *format_message_2 = - _("You entered an invalid end time, should be [h:mm] or [hh:mm] or [mm]"); + _("You entered an invalid end time, should be [hh:mm], [+hh:mm] or [+mm]"); char *enter_str = _("Press [Enter] to continue"); int Id = 1; char item_time[LTIME] = ""; char item_mesg[BUFSIZ] = ""; - long apoint_duration = 0, apoint_start; + long apoint_start; unsigned heures, minutes; + unsigned apoint_duration; unsigned end_h, end_m; int is_appointment = 1; /* Get the starting time */ - do + for (;;) { status_mesg (mesg_1, ""); if (getstring (win[STA].p, item_time, LTIME, 0, 1) != GETSTRING_ESC) @@ -184,18 +184,18 @@ apoint_add (void) is_appointment = 0; break; } - else if (check_time (item_time) != 1) + + if (parse_time (item_time, &heures, &minutes) == 1) + break; + else { status_mesg (format_message_1, enter_str); (void)wgetch (win[STA].p); } - else - (void)sscanf (item_time, "%u:%u", &heures, &minutes); } else return; } - while (check_time (item_time) != 1); /* * Check if an event or appointment is entered, @@ -205,23 +205,16 @@ apoint_add (void) if (is_appointment) { /* Get the appointment duration */ item_time[0] = '\0'; - do + for (;;) { status_mesg (mesg_2, ""); - if (getstring (win[STA].p, item_time, LTIME, 0, 1) != GETSTRING_VALID) - return; //nothing entered, cancel adding of event - else if (check_time (item_time) == 0) + if (getstring (win[STA].p, item_time, LTIME, 0, 1) != GETSTRING_ESC) { - status_mesg (format_message_2, enter_str); - (void)wgetch (win[STA].p); - } - else - { - if (check_time (item_time) == 2) - apoint_duration = atoi (item_time); - else if (check_time (item_time) == 1) + if (*item_time == '+' && parse_duration (item_time + 1, + &apoint_duration) == 1) + break; + else if (parse_time (item_time, &end_h, &end_m) == 1) { - (void)sscanf (item_time, "%u:%u", &end_h, &end_m); if (end_h < heures || ((end_h == heures) && (end_m < minutes))) { apoint_duration = MININSEC - minutes + end_m @@ -232,10 +225,17 @@ apoint_add (void) apoint_duration = MININSEC - minutes + end_m + (end_h - (heures + 1)) * MININSEC; } + break; + } + else + { + status_mesg (format_message_2, enter_str); + (void)wgetch (win[STA].p); } } + else + return; } - while (check_time (item_time) == 0); } else /* Insert the event Id */ Id = 1; diff --git a/src/day.c b/src/day.c index 496eeda..894d730 100644 --- a/src/day.c +++ b/src/day.c @@ -567,26 +567,28 @@ day_chk_busy_slices (struct date day, int slicesno, int *slices) } /* Request the user to enter a new time. */ -static char * -day_edit_time (long time) +static int +day_edit_time (int time, unsigned *new_hour, unsigned *new_minute) { - char *timestr; - char *msg_time = _("Enter the new time ([hh:mm] or [h:mm]) : "); + char *timestr = date_sec2date_str (time, "%H:%M"); + char *msg_time = _("Enter the new time ([hh:mm]) : "); char *enter_str = _("Press [Enter] to continue"); - char *fmt_msg = _("You entered an invalid time, should be [h:mm] or [hh:mm]"); + char *fmt_msg = _("You entered an invalid time, should be [hh:mm]"); - while (1) + for (;;) { status_mesg (msg_time, ""); - timestr = date_sec2date_str (time, "%H:%M"); updatestring (win[STA].p, ×tr, 0, 1); - if (check_time (timestr) != 1 || strlen (timestr) == 0) + if (parse_time (timestr, new_hour, new_minute) == 1) + { + mem_free (timestr); + return 1; + } + else { status_mesg (fmt_msg, enter_str); (void)wgetch (win[STA].p); } - else - return (timestr); } } @@ -596,15 +598,12 @@ update_start_time (long *start, long *dur) long newtime; unsigned hr, mn; int valid_date; - char *timestr; char *msg_wrong_time = _("Invalid time: start time must be before end time!"); char *msg_enter = _("Press [Enter] to continue"); do { - timestr = day_edit_time (*start); - (void)sscanf (timestr, "%u:%u", &hr, &mn); - mem_free (timestr); + day_edit_time (*start, &hr, &mn); newtime = update_time_in_date (*start, hr, mn); if (newtime < *start + *dur) { @@ -627,11 +626,8 @@ update_duration (long *start, long *dur) { long newtime; unsigned hr, mn; - char *timestr; - timestr = day_edit_time (*start + *dur); - (void)sscanf (timestr, "%u:%u", &hr, &mn); - mem_free (timestr); + day_edit_time (*start + *dur, &hr, &mn); newtime = update_time_in_date (*start, hr, mn); *dur = (newtime > *start) ? newtime - *start : DAYINSEC + newtime - *start; } -- cgit v1.2.3-54-g00ecf