Code

unixsock, data: Moved sdb_unixsock_parse_cell() to sdb_data_parse().
[sysdb.git] / src / include / core / data.h
index 58e92cde59583dd7b114b163304223ecdb2f5d9c..4d7fda31c11d590a865fc6aff0665292249f0014 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SysDB - src/include/core/data.h
- * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2012-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -29,7 +29,6 @@
 #define SDB_CORE_DATA_H 1
 
 #include "core/time.h"
-#include "utils/strbuf.h"
 
 #include <inttypes.h>
 #include <stddef.h>
@@ -46,6 +45,19 @@ 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.
@@ -69,7 +81,8 @@ typedef struct {
  * Copy the datum stored in 'src' to the memory location pointed to by 'dst'.
  * Any dynamic data (strings, binary data) is copied to newly allocated
  * memory. Use, for example, sdb_data_free_datum() to free any dynamic memory
- * stored in a datum.
+ * stored in a datum. On error, 'dst' is unchanged. Else, any dynamic memory
+ * in 'dst' will be freed.
  *
  * Returns:
  *  - 0 on success
@@ -81,24 +94,77 @@ sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src);
 /*
  * sdb_data_free_datum:
  * Free any dynamic memory referenced by the specified datum. Does not free
- * the memory allocated for the sdb_data_t object itself. This function must
+ * the memory allocated by the sdb_data_t object itself. This function must
  * not be used if any static or stack memory is referenced from the data
  * object.
  */
 void
 sdb_data_free_datum(sdb_data_t *datum);
 
+/*
+ * sdb_data_cmp:
+ * Compare two data points. A NULL datum is considered less than any non-NULL
+ * datum. On data-type mismatch, the function always returns a negative value.
+ *
+ * Returns:
+ *  - a value less than zero if d1 compares less than d2
+ *  - zero if d1 compares equal to d2
+ *  - a value greater than zero if d1 compares greater than d2
+ */
+int
+sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2);
+
+/*
+ * 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(const sdb_data_t *datum);
+
+enum {
+       SDB_UNQUOTED = 0,
+       SDB_SINGLE_QUOTED,
+       SDB_DOUBLE_QUOTED,
+};
+
 /*
  * 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.
+ * The value of 'quoted' determines whether and how non-integer and
+ * non-decimal values are quoted. 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:
+ *  - 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
+sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted);
+
+/*
+ * sdb_data_parse:
+ * Parse the specified string into a datum using the specified type. The
+ * string value is expected to be a raw value of the specified type. Integer
+ * and decimal numbers may be signed or unsigned octal (base 8, if the first
+ * character of the string is "0"), sedecimal (base 16, if the string includes
+ * the "0x" prefix), or decimal. Decimal numbers may also be "infinity" or
+ * "NaN" or may use a decimal exponent. Date-time values are expected to be
+ * specified as (floating point) number of seconds since the epoch. For string
+ * and binary data, the input string is passed to the datum. The function does
+ * not allocate new memory for that purpose. Use sdb_data_copy() if you want
+ * to do that.
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
-sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf);
+sdb_data_parse(char *str, int type, sdb_data_t *data);
 
 #ifdef __cplusplus
 } /* extern "C" */