From: Sebastian Harl Date: Thu, 20 Feb 2014 22:11:47 +0000 (+0100) Subject: data: Let sdb_data_format() different quoting styles. X-Git-Tag: sysdb-0.1.0~198 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=fd6d010be88fd86a3e3b02d9426e372b59fbb547;p=sysdb.git data: Let sdb_data_format() different quoting styles. --- diff --git a/src/core/data.c b/src/core/data.c index bf1f55e..b990f39 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -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 */ diff --git a/src/core/store.c b/src/core/store.c index dc27eb4..652b24b 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -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); } diff --git a/src/include/core/data.h b/src/include/core/data.h index 230c1c9..0b4b2e7 100644 --- a/src/include/core/data.h +++ b/src/include/core/data.h @@ -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" */ diff --git a/t/core/data_test.c b/t/core/data_test.c index f6f9b48..261d5fd 100644 --- a/t/core/data_test.c +++ b/t/core/data_test.c @@ -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);