Code

data: Let sdb_data_format() return the number of bytes written.
authorSebastian Harl <sh@tokkee.org>
Thu, 20 Feb 2014 21:48:43 +0000 (22:48 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 20 Feb 2014 21:48:43 +0000 (22:48 +0100)
src/core/data.c
src/include/core/data.h
t/core/data_test.c

index a0c2be11014d7de81b6aa1e12f9e31a39871f595..bf1f55ec8a1f053c2fce945e3ba161b1ea16eeaa 100644 (file)
@@ -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 : */
index 01d323b122d6ca25babc161ade270f4b05c60ced..230c1c9f887cd434b7c34bd74edf72ec12b88a5d 100644 (file)
@@ -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
index 8af042a8a79a1cfb53deccd7c6a55874d327ec15..4d2efedd6b5b40ead3abf98c3edfda9cf32319c1 100644 (file)
@@ -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",