From 7f29e5abd3fad68ce9e3cfc11bd216a164fdcf43 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 1 Apr 2014 22:09:26 +0200 Subject: [PATCH] store: Let sdb_store_matcher_matches return true if it matches. Rather than using 0 for matches and -1 else. --- src/core/store_lookup.c | 56 +++++++++++++++++----------------- src/include/core/store.h | 4 +-- t/core/store_lookup_test.c | 62 +++++++++++++++++++------------------- 3 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 35c53e5..b0b7bf8 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -126,7 +126,7 @@ lookup_iter(sdb_store_base_t *obj, void *user_data) { lookup_iter_data_t *d = user_data; - if (! sdb_store_matcher_matches(d->m, obj)) + if (sdb_store_matcher_matches(d->m, obj)) return d->cb(obj, d->user_data); return 0; } /* lookup_iter */ @@ -148,17 +148,17 @@ match_name(name_matcher_t *m, const char *name) assert(m); if ((! m->name) && (! m->name_re)) - return 0; + return 1; if (! name) name = ""; if (m->name && strcasecmp(m->name, name)) - return -1; + return 0; if (m->name_re && regexec(m->name_re, name, /* matches */ 0, NULL, /* flags = */ 0)) - return -1; - return 0; + return 0; + return 1; } /* match_name */ /* match attribute specific values; @@ -169,14 +169,14 @@ match_attr(attr_matcher_t *m, sdb_store_base_t *obj) assert(m && obj); if (obj->type != SDB_ATTRIBUTE) - return -1; + return 0; { sdb_attribute_t *attr = SDB_ATTR(obj); char buf[sdb_data_strlen(&attr->value) + 1]; if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0) - return -1; + return 0; return match_name(&m->value, buf); } } /* match_attr */ @@ -191,23 +191,23 @@ match_service(service_matcher_t *m, sdb_store_base_t *obj) assert(m && obj); if (obj->type != SDB_SERVICE) - return -1; + return 0; if (! m->attr) - return 0; + return 1; iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes); while (sdb_llist_iter_has_next(iter)) { sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter)); /* if any of the attributes matches we found a matching service */ - if (! match_obj(M(m->attr), attr)) { + if (match_obj(M(m->attr), attr)) { sdb_llist_iter_destroy(iter); - return 0; + return 1; } } sdb_llist_iter_destroy(iter); - return -1; + return 0; } /* match_service */ /* match host specific values; @@ -221,44 +221,44 @@ match_host(host_matcher_t *m, sdb_store_base_t *obj) assert(m && obj); if (obj->type != SDB_HOST) - return -1; + return 0; if (m->service) { iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children); - status = -1; + status = 0; } else { iter = NULL; - status = 0; + status = 1; } while (sdb_llist_iter_has_next(iter)) { sdb_store_base_t *service = STORE_BASE(sdb_llist_iter_get_next(iter)); /* found a matching service */ - if (! match_obj(M(m->service), service)) { - status = 0; + if (match_obj(M(m->service), service)) { + status = 1; break; } } sdb_llist_iter_destroy(iter); - if (status) + if (! status) return status; else if (! m->attr) - return 0; + return 1; iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes); while (sdb_llist_iter_has_next(iter)) { sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter)); /* if any attribute matches, we found a matching host */ - if (! match_obj(M(m->attr), attr)) { + if (match_obj(M(m->attr), attr)) { sdb_llist_iter_destroy(iter); - return 0; + return 1; } } sdb_llist_iter_destroy(iter); - return -1; + return 0; } /* match_host */ /* generic matchers */ @@ -292,9 +292,9 @@ match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj) status = sdb_store_matcher_matches(OP_M(m)->left, obj); /* lazy evaluation */ - if (status && (m->type == MATCHER_AND)) + if ((! status) && (m->type == MATCHER_AND)) return status; - else if ((! status) && (m->type == MATCHER_OR)) + else if (status && (m->type == MATCHER_OR)) return status; return sdb_store_matcher_matches(OP_M(m)->right, obj); @@ -308,7 +308,7 @@ match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj) assert(m && obj); status = match_name(&OBJ_M(m)->name, obj->super.name); - if (status) + if (! status) return status; switch (m->type) { @@ -322,7 +322,7 @@ match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj) return match_host(HOST_M(m), obj); break; } - return -1; + return 0; } /* match_obj */ /* @@ -555,10 +555,10 @@ sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj) { /* "NULL" always matches */ if ((! m) || (! obj)) - return 0; + return 1; if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers))) - return -1; + return 0; return matchers[m->type](m, obj); } /* sdb_store_matcher_matches */ diff --git a/src/include/core/store.h b/src/include/core/store.h index 25bd91a..e09483f 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -188,8 +188,8 @@ sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right); * Check whether the specified matcher matches the specified store object. * * Returns: - * - 0 if the object matches - * - a negative value else + * - 1 if the object matches + * - 0 else */ int sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj); diff --git a/t/core/store_lookup_test.c b/t/core/store_lookup_test.c index 465bb08..f95598e 100644 --- a/t/core/store_lookup_test.c +++ b/t/core/store_lookup_test.c @@ -101,107 +101,107 @@ START_TEST(test_store_match) { /* host */ NULL, NULL, /* svc */ NULL, NULL, - /* attr */ NULL, NULL, NULL, NULL, 0 + /* attr */ NULL, NULL, NULL, NULL, 1 }, { /* host */ "a", NULL, /* svc */ NULL, NULL, - /* attr */ NULL, NULL, NULL, NULL, 0 + /* attr */ NULL, NULL, NULL, NULL, 1 }, { /* host */ "b", NULL, /* svc */ NULL, NULL, - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ NULL, "^a$", /* svc */ NULL, NULL, - /* attr */ NULL, NULL, NULL, NULL, 0 + /* attr */ NULL, NULL, NULL, NULL, 1 }, { /* host */ NULL, "^b$", /* svc */ NULL, NULL, - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ "a", "^a$", /* svc */ NULL, NULL, - /* attr */ NULL, NULL, NULL, NULL, 0 + /* attr */ NULL, NULL, NULL, NULL, 1 }, { /* host */ "a", "^b$", /* svc */ NULL, NULL, - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ "b", "^a$", /* svc */ NULL, NULL, - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ "a", "^a$", /* svc */ "s1", NULL, - /* attr */ NULL, NULL, NULL, NULL, 0 + /* attr */ NULL, NULL, NULL, NULL, 1 }, { /* host */ "a", "^a$", /* svc */ NULL, "^s1$", - /* attr */ NULL, NULL, NULL, NULL, 0 + /* attr */ NULL, NULL, NULL, NULL, 1 }, { /* host */ "a", "^a$", /* svc */ "s1", "^s1$", - /* attr */ NULL, NULL, NULL, NULL, 0 + /* attr */ NULL, NULL, NULL, NULL, 1 }, { /* host */ "a", "^a$", /* svc */ "x1", NULL, - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ "a", "^a$", /* svc */ NULL, "x", - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ "a", "^a$", /* svc */ "x1", "x", - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ "a", "^a$", /* svc */ "s1", "x", - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ "a", "^a$", /* svc */ "x1", "s", - /* attr */ NULL, NULL, NULL, NULL, -1 + /* attr */ NULL, NULL, NULL, NULL, 0 }, { /* host */ "a", "^a$", /* svc */ "s1", "^s1$", - /* attr */ "k1", NULL, NULL, NULL, 0 + /* attr */ "k1", NULL, NULL, NULL, 1 }, { /* host */ "a", "^a$", /* svc */ "s1", "^s1$", - /* attr */ NULL, "^k", NULL, NULL, 0 + /* attr */ NULL, "^k", NULL, NULL, 1 }, { /* host */ "a", "^a$", /* svc */ "s1", "^s1$", - /* attr */ NULL, NULL, "v1", NULL, 0 + /* attr */ NULL, NULL, "v1", NULL, 1 }, { /* host */ "a", "^a$", /* svc */ "s1", "^s1$", - /* attr */ NULL, NULL, NULL, "^v1$", 0 + /* attr */ NULL, NULL, NULL, "^v1$", 1 }, { /* host */ "a", "^a$", /* svc */ "s1", "^s1$", - /* attr */ "k1", "1", "v1", "1", 0 + /* attr */ "k1", "1", "v1", "1", 1 }, }; @@ -262,14 +262,14 @@ START_TEST(test_store_match_op) sdb_store_matcher_t *right; int expected; } golden_data[] = { - { "OR", always, always, 0 }, - { "OR", always, never, 0 }, - { "OR", never, always, 0 }, - { "OR", never, never, -1 }, - { "AND", always, always, 0 }, - { "AND", always, never, -1 }, - { "AND", never, always, -1 }, - { "AND", never, never, -1 }, + { "OR", always, always, 1 }, + { "OR", always, never, 1 }, + { "OR", never, always, 1 }, + { "OR", never, never, 0 }, + { "AND", always, always, 1 }, + { "AND", always, never, 0 }, + { "AND", never, always, 0 }, + { "AND", never, never, 0 }, }; int status; @@ -278,10 +278,10 @@ START_TEST(test_store_match_op) obj = sdb_store_get_host("a"); status = sdb_store_matcher_matches(always, obj); - fail_unless(status == 0, + fail_unless(status == 1, "INTERNAL ERROR: 'always' did not match host"); status = sdb_store_matcher_matches(never, obj); - fail_unless(status < 0, + fail_unless(status == 0, "INTERNAL ERROR: 'never' matches host"); for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { -- 2.30.2