Code

data: Added sdb_data_strlen.
[sysdb.git] / src / include / core / data.h
index a5438a2c8e76d4821b29e1b6ae40aa638aea0bb8..54a2ed2d0d703bec5e227dfaafa53ad136753781 100644 (file)
@@ -29,6 +29,7 @@
 #define SDB_CORE_DATA_H 1
 
 #include "core/time.h"
+#include "utils/strbuf.h"
 
 #include <inttypes.h>
 #include <stddef.h>
@@ -45,13 +46,22 @@ enum {
        SDB_TYPE_BINARY,
 };
 
+#define SDB_TYPE_TO_STRING(t) \
+       (((t) == SDB_TYPE_INTEGER) \
+               ? "INTEGER" \
+               : ((t) == SDB_TYPE_DECIMAL) \
+                       ? "DECIMAL" \
+                       : ((t) == SDB_TYPE_STRING) \
+                               ? "STRING" \
+                               : ((t) == SDB_TYPE_DATETIME) \
+                                       ? "DATETIME" \
+                                       : ((t) == SDB_TYPE_BINARY) \
+                                               ? "BINARY" \
+                                               : "UNKNOWN")
+
 /*
  * sdb_data_t:
  * A datum retrieved from an arbitrary data source.
- *
- * The string and binary objects are managed by whoever creates the data
- * object, thus, they must not be freed or modified. If you want to keep them,
- * make sure to make a copy.
  */
 typedef struct {
        int type;
@@ -91,6 +101,27 @@ sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src);
 void
 sdb_data_free_datum(sdb_data_t *datum);
 
+/*
+ * sdb_data_strlen:
+ * Returns a (worst-case) estimate for the number of bytes required to format
+ * the datum as a string. Does not take the terminating null byte into
+ * account.
+ */
+size_t
+sdb_data_strlen(sdb_data_t *datum);
+
+/*
+ * sdb_data_format:
+ * Append the specified datum to the specified string buffer using a default
+ * format.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif