From 9901983046c6a4e2793c060b3c10b4c7d467f813 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 10 Sep 2014 16:15:40 +0200 Subject: [PATCH] src/common.[ch]: Implement strtogauge(). --- src/common.c | 20 ++++++++++++++++++++ src/common.h | 4 ++++ 2 files changed, 24 insertions(+) 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); -- 2.11.0