From: Sebastian Harl Date: Wed, 8 Oct 2014 10:24:51 +0000 (+0200) Subject: data: Added sdb_data_parse_op(). X-Git-Tag: sysdb-0.6.0~130 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=613d33154547150d227282dd1fe7ea382255e67c;p=sysdb.git data: Added sdb_data_parse_op(). This function parses the string representation of an operator. --- diff --git a/src/core/data.c b/src/core/data.c index 2a8c439..4e52133 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -399,6 +399,24 @@ sdb_data_isnull(const sdb_data_t *datum) 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) diff --git a/src/include/core/data.h b/src/include/core/data.h index 87607f8..f1c0324 100644 --- a/src/include/core/data.h +++ b/src/include/core/data.h @@ -153,18 +153,24 @@ enum { }; #define SDB_DATA_OP_TO_STRING(op) \ - (((op) == SDB_DATA_ADD) \ - ? "+" \ - : ((op) == SDB_DATA_SUB) \ - ? "-" \ - : ((op) == SDB_DATA_MUL) \ - ? "*" \ - : ((op) == SDB_DATA_DIV) \ - ? "/" \ - : ((op) == SDB_DATA_MOD) \ - ? "%" \ - : ((op) == SDB_DATA_CONCAT) \ - ? "||" : "UNKNOWN") + (((op) == SDB_DATA_ADD) ? "+" \ + : ((op) == SDB_DATA_SUB) ? "-" \ + : ((op) == SDB_DATA_MUL) ? "*" \ + : ((op) == SDB_DATA_DIV) ? "/" \ + : ((op) == SDB_DATA_MOD) ? "%" \ + : ((op) == SDB_DATA_CONCAT) ? "||" : "UNKNOWN") + +/* + * sdb_data_parse_op: + * Parse the string representation of an operator supported by + * sdb_data_expr_eval. + * + * Returns: + * - the ID of the operator + * - a negative value in case the operator does not exist + */ +int +sdb_data_parse_op(const char *op); /* * sdb_data_expr_eval: diff --git a/t/unit/core/data_test.c b/t/unit/core/data_test.c index 742466e..a1b4caa 100644 --- a/t/unit/core/data_test.c +++ b/t/unit/core/data_test.c @@ -617,6 +617,43 @@ START_TEST(test_strcmp) } END_TEST +START_TEST(test_parse_op) +{ + struct { + const char *op; + int id; + } golden_data[] = { + { "+", SDB_DATA_ADD }, + { "-", SDB_DATA_SUB }, + { "*", SDB_DATA_MUL }, + { "/", SDB_DATA_DIV }, + { "%", SDB_DATA_MOD }, + { "||", SDB_DATA_CONCAT }, + { "&&", -1 }, + }; + + size_t i; + + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { + const char *op; + int id; + + id = sdb_data_parse_op(golden_data[i].op); + fail_unless(id == golden_data[i].id, + "sdb_data_parse_op(%s) = %d; expected: %d", + golden_data[i].op, id, golden_data[i].id); + + if (id <= 0) + continue; + + op = SDB_DATA_OP_TO_STRING(id); + fail_unless(!strcmp(op, golden_data[i].op), + "SDB_DATA_OP_TO_STRING(%d) = '%s'; expected: '%s'", + id, op, golden_data[i].op); + } +} +END_TEST + START_TEST(test_expr_eval) { struct { @@ -1013,6 +1050,7 @@ core_data_suite(void) tcase_add_test(tc, test_data); tcase_add_test(tc, test_cmp); tcase_add_test(tc, test_strcmp); + tcase_add_test(tc, test_parse_op); tcase_add_test(tc, test_expr_eval); tcase_add_test(tc, test_format); tcase_add_test(tc, test_parse);