Code

core/data: Added sdb_data_format() function.
authorSebastian Harl <sh@tokkee.org>
Thu, 30 Jan 2014 17:53:29 +0000 (18:53 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 30 Jan 2014 17:53:29 +0000 (18:53 +0100)
This function creates a string representation of a datum using a default
format.

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

index c71e3630e4f76dcf8d61fbb82370f818590b8b21..1bb5400028223c89eca2f9a10e7ee3e81cc5cb03 100644 (file)
@@ -27,6 +27,8 @@
 
 #include "core/data.h"
 
+#include <inttypes.h>
+
 #include <stdlib.h>
 #include <string.h>
 
@@ -83,5 +85,49 @@ sdb_data_free_datum(sdb_data_t *datum)
        }
 } /* sdb_data_free_datum */
 
+int
+sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf)
+{
+       if ((! datum) || (! buf))
+               return -1;
+
+       switch (datum->type) {
+               case SDB_TYPE_INTEGER:
+                       sdb_strbuf_append(buf, "%"PRIi64, datum->data.integer);
+                       break;
+               case SDB_TYPE_DECIMAL:
+                       sdb_strbuf_append(buf, "%a", datum->data.decimal);
+                       break;
+               case SDB_TYPE_STRING:
+                       /* TODO: escape special characters */
+                       sdb_strbuf_append(buf, "\"%s\"", datum->data.string);
+                       break;
+               case SDB_TYPE_DATETIME:
+                       {
+                               char tmp[64];
+                               if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
+                                                       datum->data.datetime))
+                                       return -1;
+                               tmp[sizeof(tmp) - 1] = '\0';
+                               sdb_strbuf_append(buf, "%s", tmp);
+                       }
+                       break;
+               case SDB_TYPE_BINARY:
+                       {
+                               size_t i;
+                               /* TODO: improve this! */
+                               sdb_strbuf_append(buf, "%s", "\"");
+                               for (i = 0; i < datum->data.binary.length; ++i)
+                                       sdb_strbuf_append(buf, "\\%x",
+                                                       (int)datum->data.binary.datum[i]);
+                               sdb_strbuf_append(buf, "%s", "\"");
+                       }
+                       break;
+               default:
+                       return -1;
+       }
+       return 0;
+} /* sdb_data_format */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
index 53b68193dae4765eb432565992c5d8d8cd208fe6..58e92cde59583dd7b114b163304223ecdb2f5d9c 100644 (file)
@@ -29,6 +29,7 @@
 #define SDB_CORE_DATA_H 1
 
 #include "core/time.h"
+#include "utils/strbuf.h"
 
 #include <inttypes.h>
 #include <stddef.h>
@@ -87,6 +88,18 @@ sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src);
 void
 sdb_data_free_datum(sdb_data_t *datum);
 
+/*
+ * sdb_data_format:
+ * Append the specified datum to the specified string buffer using a default
+ * format.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
index 7efa5549fed5e8a780cc8fe4080d19ca7630f526..fb3b1145bac8001458b95b598a348c92dc15ca20 100644 (file)
@@ -107,6 +107,81 @@ START_TEST(test_data)
 }
 END_TEST
 
+START_TEST(test_format)
+{
+       sdb_data_t datum;
+       sdb_strbuf_t *buf;
+       const char *string;
+       const char *expected;
+
+       int check;
+
+       buf = sdb_strbuf_create(1024);
+       fail_unless(buf != NULL,
+                       "INTERNAL ERROR: Failed to allocate string buffer");
+
+       datum.type = SDB_TYPE_INTEGER;
+       datum.data.integer = 4711;
+       check = sdb_data_format(&datum, buf);
+       fail_unless(! check,
+                       "sdb_data_format(INTEGER) = %d; expected: 0", check);
+       string = sdb_strbuf_string(buf);
+       expected = "4711";
+       fail_unless(! strcmp(string, expected),
+                       "sdb_data_format() used wrong format: %s; expected: %s",
+                       string, expected);
+
+       datum.type = SDB_TYPE_DECIMAL;
+       datum.data.decimal = 65536.0;
+       sdb_strbuf_clear(buf);
+       check = sdb_data_format(&datum, buf);
+       fail_unless(! check,
+                       "sdb_data_format(DECIMAL) = %d; expected: 0", check);
+       string = sdb_strbuf_string(buf);
+       expected = "0x1p+16";
+       fail_unless(! strcmp(string, expected),
+                       "sdb_data_format() used wrong format: %s; expected: %s",
+                       string, expected);
+
+       datum.type = SDB_TYPE_STRING;
+       datum.data.string = "this is a test";
+       sdb_strbuf_clear(buf);
+       check = sdb_data_format(&datum, buf);
+       fail_unless(! check,
+                       "sdb_data_format(STRING) = %d; expected: 0", check);
+       string = sdb_strbuf_string(buf);
+       expected = "\"this is a test\"";
+       fail_unless(! strcmp(string, expected),
+                       "sdb_data_format() used wrong format: %s; expected: %s",
+                       string, expected);
+
+       datum.type = SDB_TYPE_DATETIME;
+       datum.data.datetime = 471147114711471100;
+       sdb_strbuf_clear(buf);
+       check = sdb_data_format(&datum, buf);
+       fail_unless(! check,
+                       "sdb_data_format(DATETIME) = %d; expected: 0", check);
+       string = sdb_strbuf_string(buf);
+       expected = "1984-12-06 02:11:54 +0000";
+       fail_unless(! strcmp(string, expected),
+                       "sdb_data_format() used wrong format: %s; expected: %s",
+                       string, expected);
+
+       datum.type = SDB_TYPE_BINARY;
+       datum.data.binary.datum = (unsigned char *)"binary\0crap\x42";
+       datum.data.binary.length = 12;
+       sdb_strbuf_clear(buf);
+       check = sdb_data_format(&datum, buf);
+       fail_unless(! check,
+                       "sdb_data_format(BINARY) = %d; expected: 0", check);
+       string = sdb_strbuf_string(buf);
+       expected = "\"\\62\\69\\6e\\61\\72\\79\\0\\63\\72\\61\\70\\42\"";
+       fail_unless(! strcmp(string, expected),
+                       "sdb_data_format() used wrong format: %s; expected: %s",
+                       string, expected);
+}
+END_TEST
+
 Suite *
 core_data_suite(void)
 {
@@ -115,6 +190,7 @@ core_data_suite(void)
 
        tc = tcase_create("core");
        tcase_add_test(tc, test_data);
+       tcase_add_test(tc, test_format);
        suite_add_tcase(s, tc);
 
        return s;