Code

data: Let sdb_data_format() different quoting styles.
authorSebastian Harl <sh@tokkee.org>
Thu, 20 Feb 2014 22:11:47 +0000 (23:11 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 20 Feb 2014 22:11:47 +0000 (23:11 +0100)
src/core/data.c
src/core/store.c
src/include/core/data.h
t/core/data_test.c

index bf1f55ec8a1f053c2fce945e3ba161b1ea16eeaa..b990f397013ece515a90cad2ee634039a9863d37 100644 (file)
@@ -115,10 +115,14 @@ sdb_data_strlen(sdb_data_t *datum)
 } /* sdb_data_strlen */
 
 int
-sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen)
+sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen, int quoted)
 {
+       char tmp[sdb_data_strlen(datum) + 1];
+       char *data = NULL;
        int ret = -1;
 
+       size_t i, pos;
+
        if ((! datum) || (! buf))
                return -1;
 
@@ -131,11 +135,8 @@ sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen)
                        break;
                case SDB_TYPE_STRING:
                        if (! datum->data.string)
-                               ret = snprintf(buf, buflen, "\"NULL\"");
+                               data = "NULL";
                        else {
-                               char tmp[2 * strlen(datum->data.string) + 1];
-                               size_t i, pos;
-
                                pos = 0;
                                for (i = 0; i < strlen(datum->data.string); ++i) {
                                        char byte = datum->data.string[i];
@@ -148,46 +149,47 @@ sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen)
                                        ++pos;
                                }
                                tmp[pos] = '\0';
-                               ret = snprintf(buf, buflen, "\"%s\"", tmp);
+                               data = tmp;
                        }
                        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';
-                               ret = snprintf(buf, buflen, "\"%s\"", tmp);
-                       }
+                       if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
+                                               datum->data.datetime))
+                               return -1;
+                       tmp[sizeof(tmp) - 1] = '\0';
+                       data = tmp;
                        break;
                case SDB_TYPE_BINARY:
-                       {
-                               char tmp[4 * datum->data.binary.length + 1];
-                               size_t i, pos;
-
-                               pos = 0;
-                               for (i = 0; i < datum->data.binary.length; ++i) {
-                                       int byte = (int)datum->data.binary.datum[i];
-                                       char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
-                                               '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
-
-                                       tmp[pos] = '\\';
-                                       tmp[pos + 1] = 'x';
-                                       pos += 2;
-
-                                       if (byte > 0xf) {
-                                               tmp[pos] = hex[byte >> 4];
-                                               ++pos;
-                                       }
-                                       tmp[pos] = hex[byte & 0xf];
+                       pos = 0;
+                       for (i = 0; i < datum->data.binary.length; ++i) {
+                               int byte = (int)datum->data.binary.datum[i];
+                               char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
+                                       '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
+                               tmp[pos] = '\\';
+                               tmp[pos + 1] = 'x';
+                               pos += 2;
+
+                               if (byte > 0xf) {
+                                       tmp[pos] = hex[byte >> 4];
                                        ++pos;
                                }
-                               tmp[pos] = '\0';
-                               ret = snprintf(buf, buflen, "\"%s\"", tmp);
+                               tmp[pos] = hex[byte & 0xf];
+                               ++pos;
                        }
+                       tmp[pos] = '\0';
+                       data = tmp;
                        break;
        }
+
+       if (data) {
+               if (quoted == SDB_UNQUOTED)
+                       ret = snprintf(buf, buflen, "%s", data);
+               else if (quoted == SDB_SINGLE_QUOTED)
+                       ret = snprintf(buf, buflen, "'%s'", data);
+               else
+                       ret = snprintf(buf, buflen, "\"%s\"", data);
+       }
        buf[buflen - 1] = '\0';
        return ret;
 } /* sdb_data_format */
index dc27eb48cd042c34b7bf9e101fd39dd8348ec85a..652b24b33c738b2dba3ff5a91c08f0c9d56eb645 100644 (file)
@@ -370,7 +370,8 @@ 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) {
                        char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
-                       sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp));
+                       sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
+                                       SDB_DOUBLE_QUOTED);
                        sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\"}",
                                        tmp, time_str);
                }
index 230c1c9f887cd434b7c34bd74edf72ec12b88a5d..0b4b2e7419312e10d68f9725d7db061dea9d0d14 100644 (file)
@@ -93,7 +93,7 @@ sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src);
 /*
  * sdb_data_free_datum:
  * Free any dynamic memory referenced by the specified datum. Does not free
- * the memory allocated for the sdb_data_t object itself. This function must
+ * the memory allocated by the sdb_data_t object itself. This function must
  * not be used if any static or stack memory is referenced from the data
  * object.
  */
@@ -109,12 +109,19 @@ sdb_data_free_datum(sdb_data_t *datum);
 size_t
 sdb_data_strlen(sdb_data_t *datum);
 
+enum {
+       SDB_UNQUOTED = 0,
+       SDB_SINGLE_QUOTED,
+       SDB_DOUBLE_QUOTED,
+};
+
 /*
  * sdb_data_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.
+ * The value of 'quoted' determines whether and how non-integer and
+ * non-decimal values are quoted. 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:
  *  - the number of characters written to the buffer (excluding the terminated
@@ -123,7 +130,7 @@ sdb_data_strlen(sdb_data_t *datum);
  *  - a negative value else
  */
 int
-sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen);
+sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen, int quoted);
 
 #ifdef __cplusplus
 } /* extern "C" */
index f6f9b4826c9037d993c598f832eb894d8d7b0ed1..261d5fd5a08ef9c60eb15eaf175897aeb27ed7fa 100644 (file)
@@ -159,7 +159,8 @@ START_TEST(test_format)
 
                memset(buf, (int)'A', sizeof(buf));
 
-               check = sdb_data_format(datum, buf, sizeof(buf) - 1);
+               check = sdb_data_format(datum, buf, sizeof(buf) - 1,
+                               SDB_DOUBLE_QUOTED);
                fail_unless(check > 0,
                                "sdb_data_format(type=%s) = %d; expected: >0",
                                SDB_TYPE_TO_STRING(datum->type), check);