From: Florian Forster Date: Mon, 8 Sep 2014 09:16:39 +0000 (+0200) Subject: src/configfile.[ch]: Implement global_option_get_time(). X-Git-Tag: collectd-5.5.0~206^2~1 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=7d08208e0d19bcf5198c6c4102e08709fb5c38da;p=collectd.git src/configfile.[ch]: Implement global_option_get_time(). --- diff --git a/src/configfile.c b/src/configfile.c index d3d38412..9a5807ce 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -949,28 +949,30 @@ long global_option_get_long (const char *option, long default_value) return (value); } /* char *global_option_get_long */ +cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */ +{ + char const *optstr; + char *endptr = NULL; + double v; + + optstr = global_option_get (name); + if (optstr == NULL) + return (def); + + errno = 0; + v = strtod (optstr, &endptr); + if ((endptr == NULL) || (*endptr != 0) || (errno != 0)) + return (def); + else if (v >= 0.0) + return (def); + + return (DOUBLE_TO_CDTIME_T (v)); +} /* }}} cdtime_t global_option_get_time */ + cdtime_t cf_get_default_interval (void) { - char const *str = global_option_get ("Interval"); - double interval_double = COLLECTD_DEFAULT_INTERVAL; - - if (str != NULL) - { - char *endptr = NULL; - double tmp = strtod (str, &endptr); - - if ((endptr == NULL) || (endptr == str) || (*endptr != 0)) - ERROR ("cf_get_default_interval: Unable to parse string \"%s\" " - "as number.", str); - else if (tmp <= 0.0) - ERROR ("cf_get_default_interval: Interval must be a positive number. " - "The current number is %g.", tmp); - else - interval_double = tmp; - } - - return (DOUBLE_TO_CDTIME_T (interval_double)); -} /* }}} cdtime_t cf_get_default_interval */ + return (global_option_get_time ("Interval", COLLECTD_DEFAULT_INTERVAL)); +} void cf_unregister (const char *type) { diff --git a/src/configfile.h b/src/configfile.h index 92c09de3..5bc9b305 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -94,6 +94,8 @@ const char *global_option_get (const char *option); long global_option_get_long (const char *option, long default_value); long global_option_get_long_in_range (const char *option, long default_value, long min, long max); +cdtime_t global_option_get_time (char const *option, cdtime_t default_value); + cdtime_t cf_get_default_interval (void); /* Assures the config option is a string, duplicates it and returns the copy in