summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 91f2b91)
raw | patch | inline | side by side (parent: 91f2b91)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Feb 2014 21:48:43 +0000 (22:48 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Feb 2014 21:48:43 +0000 (22:48 +0100) |
src/core/data.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 a0c2be11014d7de81b6aa1e12f9e31a39871f595..bf1f55ec8a1f053c2fce945e3ba161b1ea16eeaa 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
int
sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen)
{
+ int ret = -1;
+
if ((! datum) || (! buf))
return -1;
switch (datum->type) {
case SDB_TYPE_INTEGER:
- snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
+ ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
break;
case SDB_TYPE_DECIMAL:
- snprintf(buf, buflen, "%a", datum->data.decimal);
+ ret = snprintf(buf, buflen, "%a", datum->data.decimal);
break;
case SDB_TYPE_STRING:
- if (! datum->data.string) {
- strncpy(buf, "\"NULL\"", buflen);
- buf[buflen - 1] = '\0';
- return 0;
- }
- {
+ if (! datum->data.string)
+ ret = snprintf(buf, buflen, "\"NULL\"");
+ else {
char tmp[2 * strlen(datum->data.string) + 1];
size_t i, pos;
++pos;
}
tmp[pos] = '\0';
- snprintf(buf, buflen, "\"%s\"", tmp);
+ ret = snprintf(buf, buflen, "\"%s\"", tmp);
}
break;
case SDB_TYPE_DATETIME:
datum->data.datetime))
return -1;
tmp[sizeof(tmp) - 1] = '\0';
- snprintf(buf, buflen, "\"%s\"", tmp);
+ ret = snprintf(buf, buflen, "\"%s\"", tmp);
}
break;
case SDB_TYPE_BINARY:
++pos;
}
tmp[pos] = '\0';
- snprintf(buf, buflen, "\"%s\"", tmp);
+ ret = snprintf(buf, buflen, "\"%s\"", tmp);
}
break;
- default:
- return -1;
}
buf[buflen - 1] = '\0';
- return 0;
+ return ret;
} /* sdb_data_format */
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
index 01d323b122d6ca25babc161ade270f4b05c60ced..230c1c9f887cd434b7c34bd74edf72ec12b88a5d 100644 (file)
--- a/src/include/core/data.h
+++ b/src/include/core/data.h
* calling this function.
*
* Returns:
- * - 0 on success
+ * - the number of characters written to the buffer (excluding the terminated
+ * null byte) or the number of characters which would have been written in
+ * case the output was truncated
* - a negative value else
*/
int
diff --git a/t/core/data_test.c b/t/core/data_test.c
index 8af042a8a79a1cfb53deccd7c6a55874d327ec15..4d2efedd6b5b40ead3abf98c3edfda9cf32319c1 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);
- fail_unless(! check,
- "sdb_data_format(type=%s) = %d; expected: 0",
+ fail_unless(check > 0,
+ "sdb_data_format(type=%s) = %d; expected: >0",
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",