Code

http plugin: http_value_list_to_string: Clean-up.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 21 Aug 2009 09:23:30 +0000 (11:23 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 21 Aug 2009 09:23:30 +0000 (11:23 +0200)
src/http.c

index 8add24b3366661d524479aa583558dff36500756..d5cc2e249f91cb778a7fc134d411f72a5ca9741e 100644 (file)
@@ -103,56 +103,52 @@ static int http_init(void) /* {{{ */
         return (0);
 } /* }}} */
 
-static int http_value_list_to_string (char *buffer, int buffer_len, /* {{{ */
+static int http_value_list_to_string (char *buffer, /* {{{ */
+                size_t buffer_size,
                 const data_set_t *ds, const value_list_t *vl)
 {
-        int offset = 0;
+        size_t offset = 0;
         int status;
         int i;
 
         assert (0 == strcmp (ds->type, vl->type));
 
-        memset (buffer, '\0', buffer_len);
+        memset (buffer, 0, buffer_size);
 
-        for (i = 0; i < ds->ds_num; i++)
-        {
-                if ((ds->ds[i].type != DS_TYPE_COUNTER)
-                                && (ds->ds[i].type != DS_TYPE_GAUGE)
-                                && (ds->ds[i].type != DS_TYPE_DERIVE)
-                                && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
-                        return (-1);
+#define BUFFER_ADD(...) do { \
+        status = ssnprintf (buffer + offset, buffer_size - offset, \
+                        __VA_ARGS__); \
+        if (status < 1) \
+                return (-1); \
+        else if (((size_t) status) >= (buffer_size - offset)) \
+                return (-1); \
+        else \
+                offset += ((size_t) status); \
+} while (0)
 
-               status = -1;
-                if (ds->ds[i].type == DS_TYPE_GAUGE)
-                {
-                        status = ssnprintf (buffer + offset, buffer_len - offset,
-                                        ":%lf", vl->values[i].gauge);
-                }
-                else if ((ds->ds[i].type == DS_TYPE_COUNTER)
-                               || (ds->ds[i].type == DS_TYPE_ABSOLUTE))
-                {
-                        status = ssnprintf (buffer + offset,
-                                        buffer_len - offset,
-                                        ":%"PRIu64,
-                                       (ds->ds[i].type == DS_TYPE_COUNTER)
-                                        ? vl->values[i].counter
-                                        : vl->values[i].absolute);
-                }
-                else if (ds->ds[i].type == DS_TYPE_DERIVE)
-                {
-                        status = ssnprintf (buffer + offset,
-                                        buffer_len - offset,
-                                        ":%"PRIi64,
-                                        vl->values[i].derive);
-                }
+        BUFFER_ADD ("%lu", (unsigned long) vl->time);
 
-                if ((status < 1) || (status >= (buffer_len - offset)))
-                       return (-1);
+        for (i = 0; i < ds->ds_num; i++)
+{
+        if (ds->ds[i].type == DS_TYPE_GAUGE)
+                BUFFER_ADD (":%f", vl->values[i].gauge);
+        else if (ds->ds[i].type == DS_TYPE_COUNTER)
+                BUFFER_ADD (":%llu", vl->values[i].counter);
+        else if (ds->ds[i].type == DS_TYPE_DERIVE)
+                BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
+        else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
+                BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
+        else
+        {
+                ERROR ("http plugin: Unknown data source type: %i",
+                                ds->ds[i].type);
+                return (-1);
+        }
+} /* for ds->ds_num */
 
-                offset += status;
-        } /* for ds->ds_num */
+#undef BUFFER_ADD
 
-        return (0);
+return (0);
 } /* }}} int http_value_list_to_string */
 
 static int http_config (const char *key, const char *value) /* {{{ */