From: Sebastian Harl Date: Thu, 20 Feb 2014 21:48:43 +0000 (+0100) Subject: data: Let sdb_data_format() return the number of bytes written. X-Git-Tag: sysdb-0.1.0~200 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=38f2afdc026507956e28d00ab02f4fd235d8a846;p=sysdb.git data: Let sdb_data_format() return the number of bytes written. --- diff --git a/src/core/data.c b/src/core/data.c index a0c2be1..bf1f55e 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -117,23 +117,22 @@ sdb_data_strlen(sdb_data_t *datum) 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; @@ -149,7 +148,7 @@ sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen) ++pos; } tmp[pos] = '\0'; - snprintf(buf, buflen, "\"%s\"", tmp); + ret = snprintf(buf, buflen, "\"%s\"", tmp); } break; case SDB_TYPE_DATETIME: @@ -159,7 +158,7 @@ sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen) 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: @@ -185,14 +184,12 @@ sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen) ++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 : */ diff --git a/src/include/core/data.h b/src/include/core/data.h index 01d323b..230c1c9 100644 --- a/src/include/core/data.h +++ b/src/include/core/data.h @@ -117,7 +117,9 @@ sdb_data_strlen(sdb_data_t *datum); * 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 8af042a..4d2efed 100644 --- a/t/core/data_test.c +++ b/t/core/data_test.c @@ -160,8 +160,8 @@ START_TEST(test_format) 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",