From: Sebastian Harl Date: Thu, 19 Feb 2009 08:14:22 +0000 (+0100) Subject: Moved parse_value() from the table plugin to the common module. X-Git-Tag: collectd-4.7.0~136 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=a3529866c3d98643d7857d2d0ad751c73f312c4e;p=collectd.git Moved parse_value() from the table plugin to the common module. Some other plugins will find that useful as well. --- diff --git a/src/common.c b/src/common.c index 3ec4c6e1..914857c8 100644 --- a/src/common.c +++ b/src/common.c @@ -790,6 +790,30 @@ int parse_identifier (char *str, char **ret_host, return (0); } /* int parse_identifier */ +int parse_value (char *value, value_t *ret_value, const data_source_t ds) +{ + 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 { + 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; +} /* int parse_value */ + int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds) { int i; @@ -816,12 +840,10 @@ int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds) } else { - if (strcmp ("U", ptr) == 0) + if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE)) vl->values[i].gauge = NAN; - else if (ds->ds[i].type == DS_TYPE_COUNTER) - vl->values[i].counter = atoll (ptr); - else if (ds->ds[i].type == DS_TYPE_GAUGE) - vl->values[i].gauge = atof (ptr); + else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i])) + return -1; } i++; diff --git a/src/common.h b/src/common.h index 8b401d68..2bdc8949 100644 --- a/src/common.h +++ b/src/common.h @@ -230,6 +230,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 (char *value, value_t *ret_value, const data_source_t ds); int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds); #if !HAVE_GETPWNAM_R diff --git a/src/table.c b/src/table.c index ebd1c4cf..d48a2ef6 100644 --- a/src/table.c +++ b/src/table.c @@ -365,30 +365,6 @@ static int tbl_finish (tbl_t *tbl) return 0; } /* tbl_finish */ -static int tbl_parse_value (char *value, value_t *ret_value, - data_source_t ds) -{ - 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 { - log_err ("tbl_parse_value: Invalid data source \"%s\" " - "(type = %i).", ds.name, ds.type); - return -1; - } - - if (value == endptr) { - log_err ("Failed to parse string as number: %s.", value); - return -1; - } - else if ((NULL != endptr) && ('\0' != *endptr)) - log_warn ("Ignoring trailing garbage after number: %s.", endptr); - return 0; -} /* tbl_parse_value */ - static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res, char **fields, size_t fields_num) { @@ -406,7 +382,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 != tbl_parse_value (value, &values[i], res->ds->ds[i])) + if (0 != parse_value (value, &values[i], res->ds->ds[i])) return -1; } diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c index 5bd6ec73..639b8ab1 100644 --- a/src/utils_cmd_putval.c +++ b/src/utils_cmd_putval.c @@ -33,7 +33,7 @@ return -1; \ } -static int parse_value (const data_set_t *ds, value_list_t *vl, +static int dispatch_values (const data_set_t *ds, value_list_t *vl, FILE *fh, char *buffer) { char *dummy; @@ -79,7 +79,7 @@ static int parse_value (const data_set_t *ds, value_list_t *vl, { char identifier[128]; FORMAT_VL (identifier, sizeof (identifier), vl, ds); - ERROR ("cmd putval: parse_value: " + ERROR ("cmd putval: dispatch_values: " "Number of values incorrect: " "Got %i, expected %i. Identifier is `%s'.", i, vl->values_len, identifier); @@ -91,7 +91,7 @@ static int parse_value (const data_set_t *ds, value_list_t *vl, plugin_dispatch_values (vl); return (0); -} /* int parse_value */ +} /* int dispatch_values */ static int set_option (value_list_t *vl, const char *key, const char *value) { @@ -252,7 +252,7 @@ int handle_putval (FILE *fh, char *buffer) } assert (string != NULL); - status = parse_value (ds, &vl, fh, string); + status = dispatch_values (ds, &vl, fh, string); if (status != 0) { /* An error has already been printed. */