diff options
author | Lukas Fleischer <calcurse@cryptocrack.de> | 2011-07-19 14:51:39 +0200 |
---|---|---|
committer | Lukas Fleischer <calcurse@cryptocrack.de> | 2011-07-21 17:56:25 +0200 |
commit | e5dee68dcf318b58a3339250ae63fa5f26cdf790 (patch) | |
tree | a6ca83a3c03c0baf87cf61b32050fc8945f7c540 /src/custom.c | |
parent | 7e4f995692eb71fb8825cbbb2f45aad630b3cd80 (diff) | |
download | calcurse-e5dee68dcf318b58a3339250ae63fa5f26cdf790.zip |
Refactor out integer variable parsing
Introduce conf_parse_unsigned() and conf_parse_int() similar to
conf_parse_bool().
Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src/custom.c')
-rw-r--r-- | src/custom.c | 32 |
1 files changed, 26 insertions, 6 deletions
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 <string.h> #include <stdlib.h> #include <math.h> +#include <ctype.h> #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; |