};
/**
- * STRMAP_MEMBERS - declare members for a type-specific strmap.
+ * STRMAP - declare a type-specific strmap
* @type: type for this map's values, or void * for any pointer.
*
* You use this to create your own typed strmap for a particular type.
* value!
*
* Example:
- * struct strmap_intp {
- * STRMAP_MEMBERS(int *);
- * };
+ * STRMAP(int *) int_strmap;
+ * strmap_init(&int_strmap);
*/
-#define STRMAP_MEMBERS(type) \
- struct strmap raw; \
- TCON(type canary)
-
+#define STRMAP(type) \
+ TCON_WRAP(struct strmap, type canary)
/**
* strmap_init - initialize a string map (empty)
* need this.
*
* Example:
- * struct strmap_intp map;
+ * STRMAP(int *) map;
*
* strmap_init(&map);
*/
-#define strmap_init(map) strmap_init_(&(map)->raw)
+#define strmap_init(map) strmap_init_(tcon_unwrap(map))
static inline void strmap_init_(struct strmap *map)
{
* if (!strmap_empty(&map))
* abort();
*/
-#define strmap_empty(map) strmap_empty_(&(map)->raw)
+#define strmap_empty(map) strmap_empty_(tcon_unwrap(map))
static inline bool strmap_empty_(const struct strmap *map)
{
/**
* strmap_get - get a value from a string map
* @map: the typed strmap to search.
- * @member: the string to search for.
+ * @member: the string to search for (nul terminated)
*
- * 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");
* printf("hello => %i\n", *val);
*/
#define strmap_get(map, member) \
- tcon_cast((map), canary, strmap_get_(&(map)->raw, (member)))
+ tcon_cast((map), canary, strmap_get_(tcon_unwrap(map), (member)))
void *strmap_get_(const struct strmap *map, const char *member);
+/**
+ * strmap_getn - get a value from a string map
+ * @map: the typed strmap to search.
+ * @member: the string to search for.
+ * @memberlen: the length of @member.
+ *
+ * Returns the value, or NULL if it isn't in the map (and sets errno = ENOENT).
+ *
+ * Example:
+ * val = strmap_getn(&map, "hello", 5);
+ * if (val)
+ * printf("hello => %i\n", *val);
+ */
+#define strmap_getn(map, member, n) \
+ tcon_cast((map), canary, strmap_getn_(tcon_unwrap(map), (member), (n)))
+void *strmap_getn_(const struct strmap *map, const char *member, size_t n);
+
/**
* strmap_add - place a member in the string map.
* @map: the typed strmap to add to.
* @member: the string to place in the map.
* @v: the (non-NULL) value.
*
- * This returns false if we run out of memory, or (more normally) if that
- * string already appears in the map.
+ * This returns false if we run out of memory (errno = ENOMEM), or
+ * (more normally) if that string already appears in the map (EEXIST).
*
* Note that the pointer is placed in the map, the string is not copied. If
* you want a copy in the map, use strdup(). Similarly for the value.
* if (!strmap_add(&map, "goodbye", val))
* printf("goodbye was already in the map\n");
*/
-#define strmap_add(map, member, value) \
- strmap_add_(&tcon_check((map), canary, (value))->raw, \
+#define strmap_add(map, member, value) \
+ strmap_add_(tcon_unwrap(tcon_check((map), canary, (value))), \
(member), (void *)(value))
bool strmap_add_(struct strmap *map, const char *member, const void *value);
* @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.
* printf("goodbye was not in the map?\n");
*/
#define strmap_del(map, member, valuep) \
- strmap_del_(&tcon_check_ptr((map), canary, valuep)->raw, \
+ strmap_del_(tcon_unwrap(tcon_check_ptr((map), canary, valuep)), \
(member), (void **)valuep)
char *strmap_del_(struct strmap *map, const char *member, void **valuep);
* Example:
* strmap_clear(&map);
*/
-#define strmap_clear(map) strmap_clear_(&(map)->raw)
+#define strmap_clear(map) strmap_clear_(tcon_unwrap(map))
void strmap_clear_(struct strmap *map);
* @handle's prototype should be:
* bool @handle(const char *member, type value, typeof(arg) arg)
*
- * If @handle returns true, the iteration will stop.
+ * If @handle returns false, the iteration will stop.
* You should not alter the map within the @handle function!
*
* Example:
- * struct strmap_intp {
- * STRMAP_MEMBERS(int *);
- * };
+ * typedef STRMAP(int *) strmap_intp;
* static bool dump_some(const char *member, int *value, int *num)
* {
* // Only dump out num nodes.
* if (*(num--) == 0)
- * return true;
+ * return false;
* printf("%s=>%i\n", member, *value);
- * return false;
+ * return true;
* }
*
- * static void dump_map(const struct strmap_intp *map)
+ * static void dump_map(const strmap_intp *map)
* {
* int max = 100;
* strmap_iterate(map, dump_some, &max);
* }
*/
#define strmap_iterate(map, handle, arg) \
- strmap_iterate_(&(map)->raw, \
+ strmap_iterate_(tcon_unwrap(map), \
typesafe_cb_cast(bool (*)(const char *, \
void *, void *), \
bool (*)(const char *, \
__typeof__(arg)), (handle)), \
(arg))
void strmap_iterate_(const struct strmap *map,
- bool (*handle)(const char *, void *, void *), void *data);
-
+ bool (*handle)(const char *, void *, void *),
+ const void *data);
/**
* strmap_prefix - return a submap matching a prefix
* strmap_empty() on the returned pointer.
*
* Example:
- * static void dump_prefix(const struct strmap_intp *map,
+ * static void dump_prefix(const strmap_intp *map,
* const char *prefix)
* {
* int max = 100;
*/
#if HAVE_TYPEOF
#define strmap_prefix(map, prefix) \
- ((const __typeof__(map))strmap_prefix_(&(map)->raw, (prefix)))
+ ((const __typeof__(map))strmap_prefix_(tcon_unwrap(map), (prefix)))
#else
#define strmap_prefix(map, prefix) \
- ((const void *)strmap_prefix_(&(map)->raw, (prefix)))
+ ((const void *)strmap_prefix_(tcon_unwrap(map), (prefix)))
#endif
const struct strmap *strmap_prefix_(const struct strmap *map,