From: Florian Forster Date: Wed, 20 Jan 2010 17:08:49 +0000 (+0100) Subject: src/common.[ch]: Implement “strtoderive”. X-Git-Tag: collectd-4.10.0~67 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=3b8dcc64a73b25b67d4eb242e0e58ca5bc6dbb7c;p=collectd.git src/common.[ch]: Implement “strtoderive”. Simplifies error handling. --- diff --git a/src/common.c b/src/common.c index 3695a9b7..8c2aeaea 100644 --- a/src/common.c +++ b/src/common.c @@ -1142,3 +1142,21 @@ int service_name_to_port_number (const char *service_name) return (service_number); return (-1); } /* int service_name_to_port_number */ + +int strtoderive (const char *string, derive_t *ret_value) /* {{{ */ +{ + derive_t tmp; + char *endptr; + + if ((string == NULL) || (ret_value == NULL)) + return (EINVAL); + + errno = 0; + endptr = NULL; + tmp = (derive_t) strtoll (string, &endptr, /* base = */ 0); + if ((endptr == string) || (errno != 0)) + return (-1); + + *ret_value = tmp; + return (0); +} /* }}} int strtoderive */ diff --git a/src/common.h b/src/common.h index 2d5c7945..fc809f21 100644 --- a/src/common.h +++ b/src/common.h @@ -289,4 +289,6 @@ counter_t counter_diff (counter_t old_value, counter_t new_value); * (in the range [1-65535]). Returns less than zero on error. */ int service_name_to_port_number (const char *service_name); +int strtoderive (const char *string, derive_t *ret_value); + #endif /* COMMON_H */