diff options
| -rw-r--r-- | map.c | 31 | ||||
| -rw-r--r-- | map.h | 7 | ||||
| -rw-r--r-- | test/core/map-test.c | 9 |
3 files changed, 15 insertions, 32 deletions
@@ -11,7 +11,6 @@ */ #include <stdlib.h> #include <string.h> -#include <errno.h> #include <inttypes.h> #include "map.h" @@ -63,14 +62,10 @@ void *map_get(const Map *map, const char *key) void *map_closest(const Map *map, const char *prefix) { - errno = 0; - void *v = map_get(map, prefix); - if (v) - return v; - const Map *m = map_prefix(map, prefix); - if (map_empty(m)) - errno = ENOENT; - return m->v; + void *result = map_get(map, prefix); + if (!result) + result = map_prefix(map, prefix)->v; + return result; } bool map_contains(const Map *map, const char *prefix) @@ -88,15 +83,11 @@ bool map_put(Map *map, const char *k, const void *value) uint8_t bit_num, new_dir; char *key; - if (!value) { - errno = EINVAL; + if (!value) return false; - } - if (!(key = strdup(k))) { - errno = ENOMEM; + if (!(key = strdup(k))) return false; - } /* Empty map? */ if (!map->u.n) { @@ -113,7 +104,6 @@ bool map_put(Map *map, const char *k, const void *value) if (key[byte_num] == '\0') { /* All identical! */ free(key); - errno = EEXIST; return false; } } @@ -130,7 +120,6 @@ bool map_put(Map *map, const char *k, const void *value) newn = malloc(sizeof(*newn)); if (!newn) { free(key); - errno = ENOMEM; return false; } newn->byte_num = byte_num; @@ -171,10 +160,8 @@ void *map_delete(Map *map, const char *key) uint8_t direction; /* Empty map? */ - if (!map->u.n) { - errno = ENOENT; + if (!map->u.n) return NULL; - } /* Find closest, but keep track of parent. */ n = map; @@ -193,10 +180,8 @@ void *map_delete(Map *map, const char *key) } /* Did we find it? */ - if (strcmp(key, n->u.s)) { - errno = ENOENT; + if (strcmp(key, n->u.s)) return NULL; - } free((char*)n->u.s); value = n->v; @@ -31,8 +31,7 @@ void *map_first(const Map *map, const char **key); * @param map The map to search within. * @param prefix The prefix to search for. * @return The corresponding value, if the given prefix is unique. - * Otherwise ``NULL``. If no such prefix exists, then ``errno`` - * is set to ``ENOENT``. + * Otherwise ``NULL``. */ void *map_closest(const Map *map, const char *prefix); /** @@ -47,8 +46,8 @@ bool map_contains(const Map *map, const char *prefix); * @param map The map to store the key-value pair in. * @param key The key to store. * @param value The value associated with the key. - * @return False if we run out of memory (``errno = ENOMEM``), or if the key - * already appears in the map (``errno = EEXIST``). + * @return False if we run out of memory, or if the key + * already appears in the map. */ bool map_put(Map *map, const char *key, const void *value); /** diff --git a/test/core/map-test.c b/test/core/map-test.c index 7a23d57..beb6ba2 100644 --- a/test/core/map-test.c +++ b/test/core/map-test.c @@ -2,7 +2,6 @@ #include <stdbool.h> #include <string.h> #include <stdio.h> -#include <errno.h> #include "tap.h" #include "map.h" @@ -52,16 +51,16 @@ int main(int argc, char *argv[]) { ok(!map_get(map, "404"), "Get non-existing key"); ok(!map_contains(map, "404"), "Contains non-existing key"); - ok(!map_closest(map, "404") && errno == ENOENT, "Closest non-existing key"); + ok(!map_closest(map, "404"), "Closest non-existing key"); - ok(!map_put(map, "a", NULL) && errno == EINVAL && map_empty(map) && !map_get(map, "a"), "Put NULL value"); + ok(!map_put(map, "a", NULL) && map_empty(map) && !map_get(map, "a"), "Put NULL value"); ok(map_put(map, "a", &values[0]) && !map_empty(map) && get(map, "a", &values[0]), "Put 1"); ok(map_first(map, &key) == &values[0] && strcmp(key, "a") == 0, "First on map with 1 value"); key = NULL; ok(map_first(map_prefix(map, "a"), &key) == &values[0] && strcmp(key, "a") == 0, "First on prefix map"); ok(map_contains(map, "a"), "Contains existing key"); ok(map_closest(map, "a") == &values[0], "Closest match existing key"); - ok(!map_put(map, "a", &values[1]) && errno == EEXIST && get(map, "a", &values[0]), "Put duplicate"); + ok(!map_put(map, "a", &values[1]) && get(map, "a", &values[0]), "Put duplicate"); ok(map_put(map, "cafebabe", &values[2]) && get(map, "cafebabe", &values[2]), "Put 2"); ok(map_put(map, "cafe", &values[1]) && get(map, "cafe", &values[1]), "Put 3"); key = NULL; @@ -77,7 +76,7 @@ int main(int argc, char *argv[]) { map_iterate(copy, once, &counter); ok(counter == 1, "Iterate stop condition"); - ok(!map_get(map, "ca") && !map_closest(map, "ca") && errno == 0, "Closest ambigious"); + ok(!map_get(map, "ca") && !map_closest(map, "ca"), "Closest ambigious"); int visited[] = { 0, 0, 0 }; |
