summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c2f349f)
raw | patch | inline | side by side (parent: c2f349f)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Feb 2014 22:11:47 +0000 (23:11 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Feb 2014 22:11:47 +0000 (23:11 +0100) |
src/core/data.c | patch | blob | history | |
src/core/store.c | patch | blob | history | |
src/include/core/data.h | patch | blob | history | |
t/core/data_test.c | patch | blob | history |
diff --git a/src/core/data.c b/src/core/data.c
index bf1f55ec8a1f053c2fce945e3ba161b1ea16eeaa..b990f397013ece515a90cad2ee634039a9863d37 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
} /* 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;
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];
++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 dc27eb48cd042c34b7bf9e101fd39dd8348ec85a..652b24b33c738b2dba3ff5a91c08f0c9d56eb645 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
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)
--- a/src/include/core/data.h
+++ b/src/include/core/data.h
/*
* 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.
*/
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
* - 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 f6f9b4826c9037d993c598f832eb894d8d7b0ed1..261d5fd5a08ef9c60eb15eaf175897aeb27ed7fa 100644 (file)
--- a/t/core/data_test.c
+++ b/t/core/data_test.c
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);