4 #include <ccan/typesafe_cb/typesafe_cb.h>
9 * struct strset - representation of a string set
11 * It's exposed here to allow you to embed it and so we can inline the
22 * strset_init - initialize a string set (empty)
24 * For completeness; if you've arranged for it to be NULL already you don't
32 static inline void strset_init(struct strset *set)
38 * strset_empty - is this string set empty?
42 * if (!strset_empty(&set))
45 static inline bool strset_empty(const struct strset *set)
47 return set->u.n == NULL;
51 * strset_get - is this a member of this string set?
53 * @member: the string to search for.
55 * Returns the member, or NULL if it isn't in the set (and sets errno
59 * if (strset_get(&set, "hello"))
60 * printf("hello is in the set\n");
62 char *strset_get(const struct strset *set, const char *member);
65 * strset_add - place a member in the string set.
67 * @member: the string to place in the set.
69 * This returns false if we run out of memory (errno = ENOMEM), or
70 * (more normally) if that string already appears in the set (EEXIST).
72 * Note that the pointer is placed in the set, the string is not copied. If
73 * you want a copy in the set, use strdup().
76 * if (!strset_add(&set, "goodbye"))
77 * printf("goodbye was already in the set\n");
79 bool strset_add(struct strset *set, const char *member);
82 * strset_del - remove a member from the string set.
84 * @member: the string to remove from the set.
86 * This returns the string which was passed to strset_add(), or NULL if
87 * the string was not in the map (in which case it sets errno = ENOENT).
89 * This means that if you allocated a string (eg. using strdup()), you can
93 * if (!strset_del(&set, "goodbye"))
94 * printf("goodbye was not in the set?\n");
96 char *strset_del(struct strset *set, const char *member);
99 * strset_clear - remove every member from the set.
102 * The set will be empty after this.
105 * strset_clear(&set);
107 void strset_clear(struct strset *set);
110 * strset_iterate - ordered iteration over a set
112 * @handle: the function to call.
113 * @arg: the argument for the function (types should match).
115 * You should not alter the set within the @handle function! If it returns
116 * false, the iteration will stop.
119 * static bool dump_some(const char *member, int *num)
121 * // Only dump out num nodes.
124 * printf("%s\n", member);
128 * static void dump_set(const struct strset *set)
131 * strset_iterate(set, dump_some, &max);
133 * printf("... (truncated to 100 entries)\n");
136 #define strset_iterate(set, handle, arg) \
137 strset_iterate_((set), typesafe_cb_preargs(bool, void *, \
141 void strset_iterate_(const struct strset *set,
142 bool (*handle)(const char *, void *), const void *data);
146 * strset_prefix - return a subset matching a prefix
148 * @prefix: the prefix.
150 * This returns a pointer into @set, so don't alter @set while using
151 * the return value. You can use strset_iterate(), strset_test() or
152 * strset_empty() on the returned pointer.
155 * static void dump_prefix(const struct strset *set, const char *prefix)
158 * printf("Nodes with prefix %s:\n", prefix);
159 * strset_iterate(strset_prefix(set, prefix), dump_some, &max);
161 * printf("... (truncated to 100 entries)\n");
164 const struct strset *strset_prefix(const struct strset *set,
167 #endif /* CCAN_STRSET_H */