]> git.ozlabs.org Git - ccan/blobdiff - ccan/strmap/strmap.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / strmap / strmap.c
index 2b89fe0da66cdf8185b4096681175601a2a01fb0..16a30e036ad44c3492a9e05074747b7b15a6bff6 100644 (file)
@@ -17,9 +17,8 @@ struct node {
 };
 
 /* Closest member to this in a non-empty map. */
-static struct strmap *closest(struct strmap *n, const char *member)
+static struct strmap *closest(struct strmap *n, const char *member, size_t len)
 {
-       size_t len = strlen(member);
        const u8 *bytes = (const u8 *)member;
 
        /* Anything with NULL value is a node. */
@@ -35,19 +34,26 @@ static struct strmap *closest(struct strmap *n, const char *member)
        return n;
 }
 
-void *strmap_get_(const struct strmap *map, const char *member)
+void *strmap_getn_(const struct strmap *map,
+                  const char *member, size_t memberlen)
 {
        struct strmap *n;
 
-       /* Empty map? */
-       if (!map->u.n)
-               return NULL;
-       n = closest((struct strmap *)map, member);
-       if (streq(member, n->u.s))
-               return n->v;
+       /* Not empty map? */
+       if (map->u.n) {
+               n = closest((struct strmap *)map, member, memberlen);
+               if (!strncmp(member, n->u.s, memberlen) && !n->u.s[memberlen])
+                       return n->v;
+       }
+       errno = ENOENT;
        return NULL;
 }
 
+void *strmap_get_(const struct strmap *map, const char *member)
+{
+       return strmap_getn_(map, member, strlen(member));
+}
+
 bool strmap_add_(struct strmap *map, const char *member, const void *value)
 {
        size_t len = strlen(member);
@@ -67,7 +73,7 @@ bool strmap_add_(struct strmap *map, const char *member, const void *value)
        }
 
        /* Find closest existing member. */
-       n = closest(map, member);
+       n = closest(map, member, len);
 
        /* Find where they differ. */
        for (byte_num = 0; n->u.s[byte_num] == member[byte_num]; byte_num++) {
@@ -129,8 +135,10 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep)
        u8 direction = 0; /* prevent bogus gcc warning. */
 
        /* Empty map? */
-       if (!map->u.n)
+       if (!map->u.n) {
+               errno = ENOENT;
                return NULL;
+       }
 
        /* Find closest, but keep track of parent. */
        n = map;
@@ -148,8 +156,10 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep)
        }
 
        /* Did we find it? */
-       if (!streq(member, n->u.s))
+       if (!streq(member, n->u.s)) {
+               errno = ENOENT;
                return NULL;
+       }
 
        ret = n->u.s;
        if (valuep)
@@ -169,17 +179,19 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep)
 }
 
 static bool iterate(struct strmap n,
-                   bool (*handle)(const char *, void *, void *), void *data)
+                   bool (*handle)(const char *, void *, void *),
+                   const void *data)
 {
        if (n.v)
-               return handle(n.u.s, n.v, data);
+               return handle(n.u.s, n.v, (void *)data);
 
        return iterate(n.u.n->child[0], handle, data)
-               || iterate(n.u.n->child[1], handle, data);
+               && iterate(n.u.n->child[1], handle, data);
 }
 
 void strmap_iterate_(const struct strmap *map,
-                    bool (*handle)(const char *, void *, void *), void *data)
+                    bool (*handle)(const char *, void *, void *),
+                    const void *data)
 {
        /* Empty map? */
        if (!map->u.n)