From: Sebastian Harl Date: Thu, 20 Feb 2014 21:37:01 +0000 (+0100) Subject: data: Let sdb_data_format output to a character array. X-Git-Tag: sysdb-0.1.0~202 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e990840022affb61add5682b7a60ba6dba89fbf2;p=sysdb.git data: Let sdb_data_format output to a character array. This is more universally usable. --- diff --git a/src/core/data.c b/src/core/data.c index 4c89b43..a0c2be1 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -29,6 +29,7 @@ #include +#include #include #include @@ -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 */ diff --git a/src/core/store.c b/src/core/store.c index d3448c2..dc27eb4 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -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); diff --git a/src/include/core/data.h b/src/include/core/data.h index 54a2ed2..01d323b 100644 --- a/src/include/core/data.h +++ b/src/include/core/data.h @@ -29,7 +29,6 @@ #define SDB_CORE_DATA_H 1 #include "core/time.h" -#include "utils/strbuf.h" #include #include @@ -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" */ diff --git a/t/core/data_test.c b/t/core/data_test.c index d6bd014..420ee85 100644 --- a/t/core/data_test.c +++ b/t/core/data_test.c @@ -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