X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fdata.c;h=f489367a42218e35a94bbf64afbff7d6255442fb;hb=b0e3b4920d34fcf9a70671bc1c424384bd388a2b;hp=dc2d0ae8afde4dbb741bc2525444fddad05ed0a8;hpb=31c79a0408f61f43310093e0e03681c7f5206483;p=sysdb.git diff --git a/src/core/data.c b/src/core/data.c index dc2d0ae..f489367 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -29,7 +29,14 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include "sysdb.h" + #include "core/data.h" +#include "utils/error.h" + +#include + +#include #include @@ -37,10 +44,193 @@ #include #include +#include + +/* + * private helper functions + */ + +/* Calculate the linear function 'd1 + n * d2'. */ +static int +data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res) +{ + if (d1->type != d2->type) + return -1; + + if (d1->type == SDB_TYPE_INTEGER) + res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer; + else if (d1->type == SDB_TYPE_DECIMAL) + res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal; + else if (d1->type == SDB_TYPE_DATETIME) + res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime; + else + return -1; + res->type = d1->type; + return 0; +} /* data_lin */ + +/* Multiply d1 with d2. */ +static int +data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res) +{ + if (d1->type == SDB_TYPE_INTEGER) { + if (d2->type == SDB_TYPE_INTEGER) + res->data.integer = d1->data.integer * d2->data.integer; + else if (d2->type == SDB_TYPE_DATETIME) { + res->data.datetime = (sdb_time_t)d1->data.integer + * d2->data.datetime; + res->type = SDB_TYPE_DATETIME; + return 0; + } + else + return -1; + } + else if (d1->type == SDB_TYPE_DECIMAL) { + if (d2->type == SDB_TYPE_DECIMAL) + res->data.decimal = d1->data.decimal * d2->data.decimal; + else if (d2->type == SDB_TYPE_DATETIME) { + res->data.datetime = (sdb_time_t)(d1->data.decimal + * (double)d2->data.datetime); + res->type = SDB_TYPE_DATETIME; + return 0; + } + else + return -1; + } + else if (d1->type == SDB_TYPE_DATETIME) { + if (d2->type == SDB_TYPE_DATETIME) + res->data.datetime = d1->data.datetime + * d2->data.datetime; + else if (d2->type == SDB_TYPE_INTEGER) + res->data.datetime = d1->data.datetime + * (sdb_time_t)d2->data.integer; + else if (d2->type == SDB_TYPE_DECIMAL) + res->data.datetime = (sdb_time_t)((double)d1->data.datetime + * d2->data.decimal); + else + return -1; + } + else + return -1; + + res->type = d1->type; + return 0; +} /* data_mul */ + +/* Device d1 by d2 and return the result and the remainder. */ +static int +data_div(const sdb_data_t *d1, const sdb_data_t *d2, + sdb_data_t *res, sdb_data_t *rem) +{ + if (d1->type == SDB_TYPE_INTEGER) { + if (d2->type != SDB_TYPE_INTEGER) + return -1; + if (res) + res->data.integer = d1->data.integer / d2->data.integer; + if (rem) + rem->data.integer = d1->data.integer % d2->data.integer; + } + else if (d1->type == SDB_TYPE_DECIMAL) { + if (d2->type != SDB_TYPE_DECIMAL) + return -1; + if (res) + res->data.decimal = d1->data.decimal / d2->data.decimal; + if (rem) + rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal); + } + else if (d1->type == SDB_TYPE_DATETIME) { + if (d2->type == SDB_TYPE_DECIMAL) { + if (res) + res->data.datetime = (sdb_time_t)((double)d1->data.datetime + / d2->data.decimal); + if (rem) { + double tmp = fmod((double)d1->data.datetime, d2->data.decimal); + rem->data.datetime = (sdb_time_t)tmp; + } + } + else { + sdb_time_t a, b; + if (d2->type == SDB_TYPE_DATETIME) { + a = d1->data.datetime; + b = d2->data.datetime; + } + else if (d2->type == SDB_TYPE_INTEGER) { + a = d1->data.datetime; + b = (sdb_time_t)d2->data.integer; + } + else + return -1; + if (res) + res->data.datetime = a / b; + if (rem) + rem->data.datetime = a % b; + } + } + else + return -1; + + if (res) + res->type = d1->type; + if (rem) + rem->type = d1->type; + return 0; +} /* data_div */ + +/* Concatenate d1 and d2. */ +static int +data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res) +{ + unsigned char *new; + unsigned char *s1, *s2; + size_t len1, len2; + + if (d1->type != d2->type) + return -1; + + if (d1->type == SDB_TYPE_STRING) { + s1 = (unsigned char *)d1->data.string; + s2 = (unsigned char *)d2->data.string; + len1 = s1 ? strlen((char *)s1) : 0; + len2 = s2 ? strlen((char *)s2) : 0; + } + else if (d1->type == SDB_TYPE_BINARY) { + s1 = d1->data.binary.datum; + s2 = d2->data.binary.datum; + len1 = d1->data.binary.length; + len2 = d2->data.binary.length; + } + else + return -1; + + assert(s1 && s2); + + new = malloc(len1 + len2 + 1); + if (! new) + return -1; + + if (len1) + memcpy(new, s1, len1); + if (len2) + memcpy(new + len1, s2, len2); + new[len1 + len2] = '\0'; + + res->type = d1->type; + if (res->type == SDB_TYPE_STRING) { + res->data.string = (char *)new; + } + else { + res->data.binary.datum = new; + res->data.binary.length = len1 + len2; + } + return 0; +} /* data_concat */ + /* * public API */ +const sdb_data_t SDB_DATA_NULL = SDB_DATA_INIT; + int sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src) { @@ -52,19 +242,40 @@ sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src) tmp = *src; switch (src->type) { case SDB_TYPE_STRING: - tmp.data.string = strdup(src->data.string); - if (! tmp.data.string) - return -1; + if (src->data.string) { + tmp.data.string = strdup(src->data.string); + if (! tmp.data.string) + return -1; + } break; case SDB_TYPE_BINARY: - tmp.data.binary.datum = malloc(src->data.binary.length); - if (! tmp.data.binary.datum) - return -1; - memcpy(tmp.data.binary.datum, src->data.binary.datum, - src->data.binary.length); + if (src->data.binary.datum) { + tmp.data.binary.datum = malloc(src->data.binary.length); + if (! tmp.data.binary.datum) + return -1; + memcpy(tmp.data.binary.datum, src->data.binary.datum, + src->data.binary.length); + } + break; + case SDB_TYPE_REGEX: + if (src->data.re.raw) { + tmp.data.re.raw = strdup(src->data.re.raw); + if (! tmp.data.re.raw) + return -1; + /* we need to recompile because the regex might point to + * dynamically allocated memory */ + if (regcomp(&tmp.data.re.regex, tmp.data.re.raw, + REG_EXTENDED | REG_ICASE | REG_NOSUB)) { + free(tmp.data.re.raw); + return -1; + } + } + else + memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex)); break; } + sdb_data_free_datum(dst); *dst = tmp; return 0; } /* sdb_data_copy */ @@ -87,11 +298,19 @@ sdb_data_free_datum(sdb_data_t *datum) datum->data.binary.datum = NULL; datum->data.binary.length = 0; break; + case SDB_TYPE_REGEX: + if (datum->data.re.raw) { + free(datum->data.re.raw); + regfree(&datum->data.re.regex); + } + datum->data.re.raw = NULL; + memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex)); + break; } } /* sdb_data_free_datum */ int -sdb_data_cmp(sdb_data_t *d1, sdb_data_t *d2) +sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2) { #define CMP_NULL(a, b) \ do { \ @@ -103,19 +322,18 @@ sdb_data_cmp(sdb_data_t *d1, sdb_data_t *d2) CMP_NULL(d1, d2); if (d1->type != d2->type) - return -1; + return SDB_CMP(d1->type, d2->type); -#define CMP(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0) switch (d1->type) { case SDB_TYPE_INTEGER: - return CMP(d1->data.integer, d2->data.integer); + return SDB_CMP(d1->data.integer, d2->data.integer); case SDB_TYPE_DECIMAL: - return CMP(d1->data.decimal, d2->data.decimal); + return SDB_CMP(d1->data.decimal, d2->data.decimal); case SDB_TYPE_STRING: CMP_NULL(d1->data.string, d2->data.string); return strcasecmp(d1->data.string, d2->data.string); case SDB_TYPE_DATETIME: - return CMP(d1->data.datetime, d2->data.datetime); + return SDB_CMP(d1->data.datetime, d2->data.datetime); case SDB_TYPE_BINARY: { int diff; @@ -139,13 +357,96 @@ sdb_data_cmp(sdb_data_t *d1, sdb_data_t *d2) return diff; } - default: - return -1; + case SDB_TYPE_REGEX: + CMP_NULL(d1->data.re.raw, d2->data.re.raw); + return strcmp(d1->data.re.raw, d2->data.re.raw); } -#undef CMP -#undef CMP_NULL + return -1; } /* sdb_data_cmp */ +int +sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2) +{ + char d1_str[sdb_data_strlen(d1) + 1]; + char d2_str[sdb_data_strlen(d2) + 1]; + + if (sdb_data_isnull(d1)) + d1 = NULL; + if (sdb_data_isnull(d2)) + d2 = NULL; + + CMP_NULL(d1, d2); + + if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0) + return SDB_CMP(sizeof(d1_str), sizeof(d2_str)); + if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0) + return SDB_CMP(sizeof(d1_str), sizeof(d2_str)); + + return strcasecmp(d1_str, d2_str); +#undef CMP_NULL +} /* sdb_data_strcmp */ + +_Bool +sdb_data_isnull(const sdb_data_t *datum) +{ + if (! datum) + return 1; + if (datum->type == SDB_TYPE_NULL) + return 1; + if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string)) + return 1; + if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum)) + return 1; + if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw)) + return 1; + return 0; +} /* sdb_data_isnull */ + +int +sdb_data_parse_op(const char *op) +{ + if (! strcmp(op, "+")) + return SDB_DATA_ADD; + else if (! strcmp(op, "-")) + return SDB_DATA_SUB; + else if (! strcmp(op, "*")) + return SDB_DATA_MUL; + else if (! strcmp(op, "/")) + return SDB_DATA_DIV; + else if (! strcmp(op, "%")) + return SDB_DATA_MOD; + else if (! strcmp(op, "||")) + return SDB_DATA_CONCAT; + return -1; +} /* sdb_data_parse_op */ + +int +sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2, + sdb_data_t *res) +{ + if ((! d1) || (! d2) || (! res)) + return -1; + if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) { + *res = SDB_DATA_NULL; + return 0; + } + switch (op) { + case SDB_DATA_CONCAT: + return data_concat(d1, d2, res); + case SDB_DATA_ADD: + return data_lin(d1, 1, d2, res); + case SDB_DATA_SUB: + return data_lin(d1, -1, d2, res); + case SDB_DATA_MUL: + return data_mul(d1, d2, res); + case SDB_DATA_DIV: + return data_div(d1, d2, res, NULL); + case SDB_DATA_MOD: + return data_div(d1, d2, NULL, res); + } + return -1; +} /* sdb_data_expr_eval */ + size_t sdb_data_strlen(const sdb_data_t *datum) { @@ -157,19 +458,26 @@ sdb_data_strlen(const sdb_data_t *datum) /* log(64) */ return 20; case SDB_TYPE_DECIMAL: - /* XXX: -0xN.NNNNNNp+NNN */ + /* XXX: -d.dddddde+dd or -ddddd.dddddd */ return 42; case SDB_TYPE_STRING: if (! datum->data.string) - return 6; /* "NULL" */ + return 8; /* "" */ /* in the worst case, each character needs to be escaped */ return 2 * strlen(datum->data.string) + 2; case SDB_TYPE_DATETIME: /* "YYYY-MM-DD HH:MM:SS +zzzz" */ return 27; case SDB_TYPE_BINARY: + if (! datum->data.binary.datum) + return 8; /* "" */ /* "\xNN" */ return 4 * datum->data.binary.length + 2; + case SDB_TYPE_REGEX: + if (! datum->data.re.raw) + return 8; /* "" */ + /* "/.../" */ + return strlen(datum->data.re.raw) + 4; } return 0; } /* sdb_data_strlen */ @@ -191,11 +499,11 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted) ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer); break; case SDB_TYPE_DECIMAL: - ret = snprintf(buf, buflen, "%a", datum->data.decimal); + ret = snprintf(buf, buflen, "%g", datum->data.decimal); break; case SDB_TYPE_STRING: if (! datum->data.string) - data = "NULL"; + data = ""; else { pos = 0; for (i = 0; i < strlen(datum->data.string); ++i) { @@ -237,8 +545,20 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted) tmp[pos] = hex[byte & 0xf]; ++pos; } - tmp[pos] = '\0'; - data = tmp; + if (datum->data.binary.datum) { + tmp[pos] = '\0'; + data = tmp; + } + else + data = ""; + break; + case SDB_TYPE_REGEX: + if (! datum->data.re.raw) + data = ""; + else { + snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw); + data = tmp; + } break; } @@ -254,5 +574,77 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted) return ret; } /* sdb_data_format */ +int +sdb_data_parse(char *str, int type, sdb_data_t *data) +{ + sdb_data_t tmp; + + char *endptr = NULL; + + errno = 0; + switch (type) { + case SDB_TYPE_INTEGER: + tmp.data.integer = strtoll(str, &endptr, 0); + break; + case SDB_TYPE_DECIMAL: + tmp.data.decimal = strtod(str, &endptr); + break; + case SDB_TYPE_STRING: + tmp.data.string = str; + break; + case SDB_TYPE_DATETIME: + { + double datetime = strtod(str, &endptr); + tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime); + } + break; + case SDB_TYPE_BINARY: + /* we don't support any binary information containing 0-bytes */ + tmp.data.binary.length = strlen(str); + tmp.data.binary.datum = (unsigned char *)str; + break; + case SDB_TYPE_REGEX: + tmp.data.re.raw = strdup(str); + if (! tmp.data.re.raw) + return -1; + if (regcomp(&tmp.data.re.regex, tmp.data.re.raw, + REG_EXTENDED | REG_ICASE | REG_NOSUB)) { + sdb_log(SDB_LOG_ERR, "core: Failed to compile regular " + "expression '%s'", tmp.data.re.raw); + free(tmp.data.re.raw); + return -1; + } + if (! data) { + tmp.type = SDB_TYPE_REGEX; + sdb_data_free_datum(&tmp); + } + break; + default: + errno = EINVAL; + return -1; + } + + if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL) + || (type == SDB_TYPE_DATETIME)) { + if (errno || (str == endptr)) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "core: Failed to parse string " + "'%s' as numeric value (type %i): %s", str, type, + sdb_strerror(errno, errbuf, sizeof(errbuf))); + return -1; + } + else if (endptr && (*endptr != '\0')) + sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after " + "number while parsing numeric value (type %i): %s.", + type, endptr); + } + + if (data) { + *data = tmp; + data->type = type; + } + return 0; +} /* sdb_data_parse */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */