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_test - 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.
58 * if (strset_test(&set, "hello"))
59 * printf("hello is in the set\n");
61 char *strset_test(const struct strset *set, const char *member);
64 * strset_set - place a member in the string set.
66 * @member: the string to place in the set.
68 * This returns false if we run out of memory (errno = ENOMEM), or
69 * (more normally) if that string already appears in the set (EEXIST).
71 * Note that the pointer is placed in the set, the string is not copied. If
72 * you want a copy in the set, use strdup().
75 * if (!strset_set(&set, "goodbye"))
76 * printf("goodbye was already in the set\n");
78 bool strset_set(struct strset *set, const char *member);
81 * strset_clear - remove a member from the string set.
83 * @member: the string to remove from the set.
85 * This returns the string which was passed to strset_set(), or NULL.
86 * This means that if you allocated a string (eg. using strdup()), you can
90 * if (!strset_clear(&set, "goodbye"))
91 * printf("goodbye was not in the set?\n");
93 char *strset_clear(struct strset *set, const char *member);
96 * strset_destroy - remove every member from the set.
99 * The set will be empty after this.
102 * strset_destroy(&set);
104 void strset_destroy(struct strset *set);
107 * strset_iterate - ordered iteration over a set
109 * @handle: the function to call.
110 * @arg: the argument for the function (types should match).
112 * You should not alter the set within the @handle function! If it returns
113 * true, the iteration will stop.
116 * static bool dump_some(const char *member, int *num)
118 * // Only dump out num nodes.
121 * printf("%s\n", member);
125 * static void dump_set(const struct strset *set)
128 * strset_iterate(set, dump_some, &max);
130 * printf("... (truncated to 100 entries)\n");
133 #define strset_iterate(set, handle, arg) \
134 strset_iterate_((set), typesafe_cb_preargs(bool, void *, \
138 void strset_iterate_(const struct strset *set,
139 bool (*handle)(const char *, void *), void *data);
143 * strset_prefix - return a subset matching a prefix
145 * @prefix: the prefix.
147 * This returns a pointer into @set, so don't alter @set while using
148 * the return value. You can use strset_iterate(), strset_test() or
149 * strset_empty() on the returned pointer.
152 * static void dump_prefix(const struct strset *set, const char *prefix)
155 * printf("Nodes with prefix %s:\n", prefix);
156 * strset_iterate(strset_prefix(set, prefix), dump_some, &max);
158 * printf("... (truncated to 100 entries)\n");
161 const struct strset *strset_prefix(const struct strset *set,
164 #endif /* CCAN_STRSET_H */