It seems redundant: strmap_get() and strmap_del() can only return NULL
when the string is not a member. However, it became clear in writing
ccan/tsort that it's much more convenient for callers if we set errno
in this case too, so they can pass it up.
- /* 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);
+ if (streq(member, n->u.s))
+ return n->v;
+ }
+ errno = ENOENT;
u8 direction = 0; /* prevent bogus gcc warning. */
/* Empty map? */
u8 direction = 0; /* prevent bogus gcc warning. */
/* Empty map? */
+ if (!map->u.n) {
+ errno = ENOENT;
/* Find closest, but keep track of parent. */
n = map;
/* Find closest, but keep track of parent. */
n = map;
- if (!streq(member, n->u.s))
+ if (!streq(member, n->u.s)) {
+ errno = ENOENT;
ret = n->u.s;
if (valuep)
ret = n->u.s;
if (valuep)
* @map: the typed strmap to search.
* @member: the string to search for.
*
* @map: the typed strmap to search.
* @member: the string to search for.
*
- * Returns the value, or NULL if it isn't in the map.
+ * Returns the value, or NULL if it isn't in the map (and sets errno = ENOENT).
*
* Example:
* int *val = strmap_get(&map, "hello");
*
* Example:
* int *val = strmap_get(&map, "hello");
* @member: the string to remove from the map.
* @valuep: the value (if non-NULL)
*
* @member: the string to remove from the map.
* @valuep: the value (if non-NULL)
*
- * This returns the string which was passed to strmap_map(), or NULL.
+ * This returns the string which was passed to strmap_map(), or NULL if
+ * it was not in the map (and sets errno = ENOENT).
+ *
* This means that if you allocated a string (eg. using strdup()), you
* can free it here. Similarly, the value is returned in @valuep if
* @valuep is not NULL.
* This means that if you allocated a string (eg. using strdup()), you
* can free it here. Similarly, the value is returned in @valuep if
* @valuep is not NULL.
char *v;
/* This is how many tests you plan to run */
char *v;
/* This is how many tests you plan to run */
strmap_init(&map);
ok1(!strmap_get(&map, str));
strmap_init(&map);
ok1(!strmap_get(&map, str));
ok1(!strmap_get(&map, none));
ok1(!strmap_get(&map, none));
ok1(!strmap_del(&map, str, NULL));
ok1(!strmap_del(&map, str, NULL));
ok1(!strmap_del(&map, none, NULL));
ok1(!strmap_del(&map, none, NULL));
ok1(strmap_add(&map, str, val));
ok1(strmap_get(&map, str) == val);
/* We compare the string, not the pointer. */
ok1(strmap_get(&map, dup) == val);
ok1(!strmap_get(&map, none));
ok1(strmap_add(&map, str, val));
ok1(strmap_get(&map, str) == val);
/* We compare the string, not the pointer. */
ok1(strmap_get(&map, dup) == val);
ok1(!strmap_get(&map, none));
/* Add a duplicate should fail. */
ok1(!strmap_add(&map, dup, val));
/* Add a duplicate should fail. */
ok1(!strmap_add(&map, dup, val));
ok1(strmap_del(&map, dup, &v) == str);
ok1(v == val);
ok1(!strmap_get(&map, str));
ok1(strmap_del(&map, dup, &v) == str);
ok1(v == val);
ok1(!strmap_get(&map, str));
ok1(!strmap_get(&map, none));
ok1(!strmap_get(&map, none));
/* Try insert and delete of empty string. */
ok1(strmap_add(&map, none, none));
ok1(strmap_get(&map, none) == none);
ok1(!strmap_get(&map, str));
/* Try insert and delete of empty string. */
ok1(strmap_add(&map, none, none));
ok1(strmap_get(&map, none) == none);
ok1(!strmap_get(&map, str));
/* Delete should return original string. */
ok1(strmap_del(&map, "", &v) == none);
ok1(v == none);
ok1(!strmap_get(&map, str));
/* Delete should return original string. */
ok1(strmap_del(&map, "", &v) == none);
ok1(v == none);
ok1(!strmap_get(&map, str));
ok1(!strmap_get(&map, none));
ok1(!strmap_get(&map, none));
/* Both at once... */
ok1(strmap_add(&map, none, none));
/* Both at once... */
ok1(strmap_add(&map, none, none));