Moved parse_value() from the table plugin to the common module.
authorSebastian Harl <sh@tokkee.org>
Thu, 19 Feb 2009 08:14:22 +0000 (09:14 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 19 Feb 2009 08:50:12 +0000 (09:50 +0100)
Some other plugins will find that useful as well.

src/common.c
src/common.h
src/table.c
src/utils_cmd_putval.c

index 3ec4c6e..914857c 100644 (file)
@@ -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++;
index 8b401d6..2bdc894 100644 (file)
@@ -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
index ebd1c4c..d48a2ef 100644 (file)
@@ -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;
        }
 
index 5bd6ec7..639b8ab 100644 (file)
@@ -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. */