From: Florian Forster Date: Wed, 10 Sep 2014 14:15:40 +0000 (+0200) Subject: src/common.[ch]: Implement strtogauge(). X-Git-Tag: collectd-5.5.0~200 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=9901983046c6a4e2793c060b3c10b4c7d467f813;p=collectd.git src/common.[ch]: Implement strtogauge(). --- diff --git a/src/common.c b/src/common.c index 8691d3eb..5386739f 100644 --- a/src/common.c +++ b/src/common.c @@ -1543,6 +1543,26 @@ int strtoderive (const char *string, derive_t *ret_value) /* {{{ */ return (0); } /* }}} int strtoderive */ +int strtogauge (const char *string, gauge_t *ret_value) /* {{{ */ +{ + gauge_t tmp; + char *endptr = NULL; + + if ((string == NULL) || (ret_value == NULL)) + return (EINVAL); + + errno = 0; + endptr = NULL; + tmp = (gauge_t) strtod (string, &endptr); + if (errno != 0) + return (errno); + else if ((endptr == NULL) || (*endptr != 0)) + return (EINVAL); + + *ret_value = tmp; + return (0); +} /* }}} int strtogauge */ + int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str) /* {{{ */ { char **array; diff --git a/src/common.h b/src/common.h index b2e4c676..434ed019 100644 --- a/src/common.h +++ b/src/common.h @@ -347,6 +347,10 @@ int service_name_to_port_number (const char *service_name); * failure. If failure is returned, ret_value is not touched. */ int strtoderive (const char *string, derive_t *ret_value); +/** Parse a string to a gauge_t value. Returns zero on success or non-zero on + * failure. If failure is returned, ret_value is not touched. */ +int strtogauge (const char *string, gauge_t *ret_value); + int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str); void strarray_free (char **array, size_t array_len);