summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: d4808fb)
raw | patch | inline | side by side (parent: d4808fb)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Feb 2014 21:37:01 +0000 (22:37 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Feb 2014 21:37:01 +0000 (22:37 +0100) |
This is more universally usable.
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 4c89b43dd179363aa7644a8a35dda5206739d10d..a0c2be11014d7de81b6aa1e12f9e31a39871f595 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
#include <inttypes.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
} /* 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;
}
{
++pos;
}
tmp[pos] = '\0';
- sdb_strbuf_append(buf, "\"%s\"", tmp);
+ snprintf(buf, buflen, "\"%s\"", tmp);
}
break;
case SDB_TYPE_DATETIME:
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:
++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 d3448c20b08a678785d5decca96d2078f353cb8b..dc27eb48cd042c34b7bf9e101fd39dd8348ec85a 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) {
- 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);
index 54a2ed2d0d703bec5e227dfaafa53ad136753781..01d323b122d6ca25babc161ade270f4b05c60ced 100644 (file)
--- a/src/include/core/data.h
+++ b/src/include/core/data.h
#define SDB_CORE_DATA_H 1
#include "core/time.h"
-#include "utils/strbuf.h"
#include <inttypes.h>
#include <stddef.h>
/*
* 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 d6bd01459bc83019a9fee93f51c399933a24a919..420ee851bd38587f3168981393df54b154c0d7a8 100644 (file)
--- a/t/core/data_test.c
+++ b/t/core/data_test.c
START_TEST(test_format)
{
- sdb_strbuf_t *buf;
- size_t i;
-
struct {
sdb_data_t datum;
const char *expected;
},
};
- 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