From 5ff5059ff7e207b61032ed65f7a5288f1b296a9a Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 1 Jul 2009 14:50:24 +0200 Subject: [PATCH] src/common.c: Rewrite `parse_value'. --- src/common.c | 61 ++++++++++++++++++++++++++++++---------------------- src/common.h | 2 +- src/powerdns.c | 2 +- src/table.c | 2 +- src/utils_db_query.c | 4 ++-- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/common.c b/src/common.c index 8de871bd..d284fb87 100644 --- a/src/common.c +++ b/src/common.c @@ -837,32 +837,41 @@ int parse_identifier (char *str, char **ret_host, return (0); } /* int parse_identifier */ -int parse_value (const char *value, value_t *ret_value, const data_source_t ds) +int parse_value (const char *value, value_t *ret_value, int ds_type) { - char *endptr = NULL; - - if (DS_TYPE_COUNTER == ds.type) - ret_value->counter = (counter_t)strtoll (value, &endptr, 0); - else if (DS_TYPE_GAUGE == ds.type) - ret_value->gauge = (gauge_t)strtod (value, &endptr); - else if (DS_TYPE_DERIVE == ds.type) - ret_value->counter = (derive_t)strtoll (value, &endptr, 0); - else if (DS_TYPE_ABSOLUTE == ds.type) - ret_value->counter = (absolute_t)strtoll (value, &endptr, 0); - else { - ERROR ("parse_value: Invalid data source \"%s\" " - "(type = %i).", ds.name, ds.type); - return -1; - } - - if (value == endptr) { - ERROR ("parse_value: Failed to parse string as number: %s.", value); - return -1; - } - else if ((NULL != endptr) && ('\0' != *endptr)) - WARNING ("parse_value: Ignoring trailing garbage after number: %s.", - endptr); - return 0; + char *endptr = NULL; + + switch (ds_type) + { + case DS_TYPE_COUNTER: + ret_value->counter = (counter_t) strtoull (value, &endptr, 0); + break; + + case DS_TYPE_GAUGE: + ret_value->gauge = (gauge_t) strtod (value, &endptr); + break; + + case DS_TYPE_DERIVE: + ret_value->counter = (derive_t) strtoll (value, &endptr, 0); + break; + + case DS_TYPE_ABSOLUTE: + ret_value->counter = (absolute_t) strtoull (value, &endptr, 0); + break; + + default: + ERROR ("parse_value: Invalid data source type: %i.", ds_type); + return -1; + } + + if (value == endptr) { + ERROR ("parse_value: Failed to parse string as number: %s.", value); + return -1; + } + else if ((NULL != endptr) && ('\0' != *endptr)) + WARNING ("parse_value: Ignoring trailing garbage after number: %s.", + endptr); + return 0; } /* int parse_value */ int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds) @@ -893,7 +902,7 @@ int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds) { if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE)) vl->values[i].gauge = NAN; - else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i])) + else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i].type)) return -1; } diff --git a/src/common.h b/src/common.h index d0cc4e82..e424f5dd 100644 --- a/src/common.h +++ b/src/common.h @@ -260,7 +260,7 @@ int format_name (char *ret, int ret_len, int parse_identifier (char *str, char **ret_host, char **ret_plugin, char **ret_plugin_instance, char **ret_type, char **ret_type_instance); -int parse_value (const char *value, value_t *ret_value, const data_source_t ds); +int parse_value (const char *value, value_t *ret_value, int ds_type); int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds); #if !HAVE_GETPWNAM_R diff --git a/src/powerdns.c b/src/powerdns.c index beb49fbb..87997027 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -289,7 +289,7 @@ static void submit (const char *plugin_instance, /* {{{ */ return; } - if (0 != parse_value (value, &values[0], ds->ds[0])) + if (0 != parse_value (value, &values[0], ds->ds[0].type)) { ERROR ("powerdns plugin: Cannot convert `%s' " "to a number.", value); diff --git a/src/table.c b/src/table.c index 2911bf02..9641c759 100644 --- a/src/table.c +++ b/src/table.c @@ -383,7 +383,7 @@ static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res, assert (res->values[i] < fields_num); value = fields[res->values[i]]; - if (0 != parse_value (value, &values[i], res->ds->ds[i])) + if (0 != parse_value (value, &values[i], res->ds->ds[i].type)) return -1; } diff --git a/src/utils_db_query.c b/src/utils_db_query.c index 5531b25f..9a211b4d 100644 --- a/src/utils_db_query.c +++ b/src/utils_db_query.c @@ -207,7 +207,7 @@ static int udb_legacy_result_handle_result (udb_result_t *r, /* {{{ */ vl.values_len = 1; value_str = column_values[r->legacy_position]; - if (0 != parse_value (value_str, &vl.values[0], r->ds->ds[0])) + if (0 != parse_value (value_str, &vl.values[0], r->ds->ds[0].type)) { ERROR ("db query utils: udb_legacy_result_handle_result: " "Parsing `%s' as %s failed.", value_str, @@ -364,7 +364,7 @@ static int udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */ { char *value_str = r->values_buffer[i]; - if (0 != parse_value (value_str, &vl.values[i], r->ds->ds[i])) + if (0 != parse_value (value_str, &vl.values[i], r->ds->ds[i].type)) { ERROR ("db query utils: udb_result_submit: Parsing `%s' as %s failed.", value_str, -- 2.11.0