From e5dee68dcf318b58a3339250ae63fa5f26cdf790 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Tue, 19 Jul 2011 14:51:39 +0200 Subject: Refactor out integer variable parsing Introduce conf_parse_unsigned() and conf_parse_int() similar to conf_parse_bool(). Signed-off-by: Lukas Fleischer --- src/custom.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/custom.c b/src/custom.c index 492d34e..94286b1 100644 --- a/src/custom.c +++ b/src/custom.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "calcurse.h" @@ -84,6 +85,28 @@ conf_parse_bool (unsigned *dest, char *val) return 1; } +static int +conf_parse_unsigned (unsigned *dest, char *val) +{ + if (is_all_digit (val)) + *dest = atoi (val); + else + return 0; + + return 1; +} + +static int +conf_parse_int (int *dest, char *val) +{ + if ((*val == '+' || *val == '-' || isdigit (*val)) && is_all_digit (val + 1)) + *dest = atoi (val); + else + return 0; + + return 1; +} + /* * Load user color theme from file. * Need to handle calcurse versions prior to 1.8, where colors where handled @@ -242,10 +265,7 @@ custom_set_conf (struct conf *conf, int background, enum conf_var var, char *val conf_parse_bool (&conf->auto_save, val); break; case CUSTOM_CONF_PERIODICSAVE: - if (atoi (val) < 0) - conf->periodic_save = 0; - else - conf->periodic_save = atoi (val); + conf_parse_unsigned (&conf->periodic_save, val); break; case CUSTOM_CONF_CONFIRMQUIT: conf_parse_bool (&conf->confirm_quit, val); @@ -288,7 +308,7 @@ custom_set_conf (struct conf *conf, int background, enum conf_var var, char *val (void)strncpy (nbar.timefmt, val, strlen (val) + 1); break; case CUSTOM_CONF_NOTIFYBARWARNING: - nbar.cntdwn = atoi (val); + conf_parse_int (&nbar.cntdwn, val); break; case CUSTOM_CONF_NOTIFYBARCOMMAND: (void)strncpy (nbar.cmd, val, strlen (val) + 1); @@ -298,7 +318,7 @@ custom_set_conf (struct conf *conf, int background, enum conf_var var, char *val (void)strncpy (conf->output_datefmt, val, strlen (val) + 1); break; case CUSTOM_CONF_INPUTDATEFMT: - conf->input_datefmt = atoi (val); + conf_parse_int (&conf->input_datefmt, val); if (conf->input_datefmt <= 0 || conf->input_datefmt >= DATE_FORMATS) conf->input_datefmt = 1; break; -- cgit v1.2.3-70-g09d2