src/common.[ch]: Implement "parse_identifier_vl".
[collectd.git] / src / common.c
index 08653dc..1c80e8f 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/common.c
- * Copyright (C) 2005-2009  Florian octo Forster
+ * Copyright (C) 2005-2010  Florian octo Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -16,7 +16,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
  * Authors:
- *   Florian octo Forster <octo at verplant.org>
+ *   Florian octo Forster <octo at collectd.org>
  *   Niki W. Waibel <niki.waibel@gmx.net>
  *   Sebastian Harl <sh at tokkee.org>
  *   Michał Mirosław <mirq-linux at rere.qmqm.pl>
@@ -833,7 +833,7 @@ int format_values (char *ret, size_t ret_len, /* {{{ */
                 offset += ((size_t) status); \
 } while (0)
 
-        BUFFER_ADD ("%lu", (unsigned long) vl->time);
+        BUFFER_ADD ("%.3f", CDTIME_T_TO_DOUBLE (vl->time));
 
         for (i = 0; i < ds->ds_num; i++)
         {
@@ -918,6 +918,36 @@ int parse_identifier (char *str, char **ret_host,
        return (0);
 } /* int parse_identifier */
 
+int parse_identifier_vl (const char *str, value_list_t *vl) /* {{{ */
+{
+       char str_copy[6 * DATA_MAX_NAME_LEN];
+       char *host = NULL;
+       char *plugin = NULL;
+       char *plugin_instance = NULL;
+       char *type = NULL;
+       char *type_instance = NULL;
+       int status;
+
+       if ((str == NULL) || (vl == NULL))
+               return (EINVAL);
+
+       sstrncpy (str_copy, str, sizeof (str_copy));
+
+       status = parse_identifier (str_copy, &host,
+                       &plugin, &plugin_instance,
+                       &type, &type_instance);
+       if (status != 0)
+               return (status);
+
+       sstrncpy (vl->host, host, sizeof (host));
+       sstrncpy (vl->plugin, plugin, sizeof (plugin));
+       sstrncpy (vl->plugin_instance, plugin_instance, sizeof (plugin_instance));
+       sstrncpy (vl->type, type, sizeof (type));
+       sstrncpy (vl->type_instance, type_instance, sizeof (type_instance));
+
+       return (0);
+} /* }}} int parse_identifier_vl */
+
 int parse_value (const char *value, value_t *ret_value, int ds_type)
 {
   char *endptr = NULL;
@@ -979,9 +1009,22 @@ int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
                if (i == -1)
                {
                        if (strcmp ("N", ptr) == 0)
-                               vl->time = time (NULL);
+                               vl->time = cdtime ();
                        else
-                               vl->time = (time_t) atoi (ptr);
+                       {
+                               char *endptr = NULL;
+                               double tmp;
+
+                               errno = 0;
+                               tmp = strtod (ptr, &endptr);
+                               if ((errno != 0)                    /* Overflow */
+                                               || (endptr == ptr)  /* Invalid string */
+                                               || (endptr == NULL) /* This should not happen */
+                                               || (*endptr != 0))  /* Trailing chars */
+                                       return (-1);
+
+                               vl->time = DOUBLE_TO_CDTIME_T (tmp);
+                       }
                }
                else
                {