Code

data: Let sdb_data_format output to a character array.
authorSebastian Harl <sh@tokkee.org>
Thu, 20 Feb 2014 21:37:01 +0000 (22:37 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 20 Feb 2014 21:37:01 +0000 (22:37 +0100)
This is more universally usable.

src/core/data.c
src/core/store.c
src/include/core/data.h
t/core/data_test.c

index 4c89b43dd179363aa7644a8a35dda5206739d10d..a0c2be11014d7de81b6aa1e12f9e31a39871f595 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <inttypes.h>
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -114,21 +115,22 @@ sdb_data_strlen(sdb_data_t *datum)
 } /* sdb_data_strlen */
 
 int
-sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf)
+sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen)
 {
        if ((! datum) || (! buf))
                return -1;
 
        switch (datum->type) {
                case SDB_TYPE_INTEGER:
-                       sdb_strbuf_append(buf, "%"PRIi64, datum->data.integer);
+                       snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
                        break;
                case SDB_TYPE_DECIMAL:
-                       sdb_strbuf_append(buf, "%a", datum->data.decimal);
+                       snprintf(buf, buflen, "%a", datum->data.decimal);
                        break;
                case SDB_TYPE_STRING:
                        if (! datum->data.string) {
-                               sdb_strbuf_append(buf, "\"NULL\"");
+                               strncpy(buf, "\"NULL\"", buflen);
+                               buf[buflen - 1] = '\0';
                                return 0;
                        }
                        {
@@ -147,7 +149,7 @@ sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf)
                                        ++pos;
                                }
                                tmp[pos] = '\0';
-                               sdb_strbuf_append(buf, "\"%s\"", tmp);
+                               snprintf(buf, buflen, "\"%s\"", tmp);
                        }
                        break;
                case SDB_TYPE_DATETIME:
@@ -157,7 +159,7 @@ sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf)
                                                        datum->data.datetime))
                                        return -1;
                                tmp[sizeof(tmp) - 1] = '\0';
-                               sdb_strbuf_append(buf, "\"%s\"", tmp);
+                               snprintf(buf, buflen, "\"%s\"", tmp);
                        }
                        break;
                case SDB_TYPE_BINARY:
@@ -183,12 +185,13 @@ sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf)
                                        ++pos;
                                }
                                tmp[pos] = '\0';
-                               sdb_strbuf_append(buf, "\"%s\"", tmp);
+                               snprintf(buf, buflen, "\"%s\"", tmp);
                        }
                        break;
                default:
                        return -1;
        }
+       buf[buflen - 1] = '\0';
        return 0;
 } /* sdb_data_format */
 
index d3448c20b08a678785d5decca96d2078f353cb8b..dc27eb48cd042c34b7bf9e101fd39dd8348ec85a 100644 (file)
@@ -369,9 +369,10 @@ store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
 
                sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
                if (type == SDB_ATTRIBUTE) {
-                       sdb_strbuf_append(buf, "\"value\": ");
-                       sdb_data_format(&SDB_ATTR(sobj)->value, buf);
-                       sdb_strbuf_append(buf, ", \"last_update\": \"%s\"}", time_str);
+                       char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
+                       sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp));
+                       sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\"}",
+                                       tmp, time_str);
                }
                else
                        sdb_strbuf_append(buf, "\"last_update\": \"%s\"}", time_str);
index 54a2ed2d0d703bec5e227dfaafa53ad136753781..01d323b122d6ca25babc161ade270f4b05c60ced 100644 (file)
@@ -29,7 +29,6 @@
 #define SDB_CORE_DATA_H 1
 
 #include "core/time.h"
-#include "utils/strbuf.h"
 
 #include <inttypes.h>
 #include <stddef.h>
@@ -112,15 +111,17 @@ sdb_data_strlen(sdb_data_t *datum);
 
 /*
  * sdb_data_format:
- * Append the specified datum to the specified string buffer using a default
- * format.
+ * Output the specified datum to the specified string using a default format.
+ * If the buffer size is less than the return value of sdb_data_strlen, the
+ * datum may be truncated. The buffer will always be nul-terminated after
+ * calling this function.
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
-sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf);
+sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen);
 
 #ifdef __cplusplus
 } /* extern "C" */
index d6bd01459bc83019a9fee93f51c399933a24a919..420ee851bd38587f3168981393df54b154c0d7a8 100644 (file)
@@ -109,9 +109,6 @@ END_TEST
 
 START_TEST(test_format)
 {
-       sdb_strbuf_t *buf;
-       size_t i;
-
        struct {
                sdb_data_t datum;
                const char *expected;
@@ -153,27 +150,21 @@ START_TEST(test_format)
                },
        };
 
-       buf = sdb_strbuf_create(1024);
-       fail_unless(buf != NULL,
-               "INTERNAL ERROR: Failed to allocate string buffer");
+       size_t i;
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
-               const char *string;
+               sdb_data_t *datum = &golden_data[i].datum;
+               char buf[sdb_data_strlen(datum) + 1];
                int check;
 
-               check = sdb_data_format(&golden_data[i].datum, buf);
+               check = sdb_data_format(datum, buf, sizeof(buf));
                fail_unless(! check,
                                "sdb_data_format(type=%s) = %d; expected: 0",
-                               SDB_TYPE_TO_STRING(golden_data[i].datum.type), check);
-               string = sdb_strbuf_string(buf);
-               fail_unless(! strcmp(string, golden_data[i].expected),
+                               SDB_TYPE_TO_STRING(datum->type), check);
+               fail_unless(! strcmp(buf, golden_data[i].expected),
                                "sdb_data_format(type=%s) used wrong format: %s; expected: %s",
-                               SDB_TYPE_TO_STRING(golden_data[i].datum.type),
-                               string, golden_data[i].expected);
-               sdb_strbuf_clear(buf);
+                               SDB_TYPE_TO_STRING(datum->type), buf, golden_data[i].expected);
        }
-
-       sdb_strbuf_destroy(buf);
 }
 END_TEST