xref: /linux/tools/perf/util/strlist.h (revision 8ff9daf3c16769817d0eaf16154d9e9198ec1bda)
18b40f521SJohn Kacur #ifndef __PERF_STRLIST_H
28b40f521SJohn Kacur #define __PERF_STRLIST_H
325903407SArnaldo Carvalho de Melo 
443cbcd8aSArnaldo Carvalho de Melo #include <linux/rbtree.h>
525903407SArnaldo Carvalho de Melo #include <stdbool.h>
625903407SArnaldo Carvalho de Melo 
7ee8dd3caSDavid Ahern #include "rblist.h"
8ee8dd3caSDavid Ahern 
925903407SArnaldo Carvalho de Melo struct str_node {
1025903407SArnaldo Carvalho de Melo 	struct rb_node rb_node;
1125903407SArnaldo Carvalho de Melo 	const char     *s;
1225903407SArnaldo Carvalho de Melo };
1325903407SArnaldo Carvalho de Melo 
1425903407SArnaldo Carvalho de Melo struct strlist {
15ee8dd3caSDavid Ahern 	struct rblist rblist;
1625903407SArnaldo Carvalho de Melo 	bool	       dupstr;
1725903407SArnaldo Carvalho de Melo };
1825903407SArnaldo Carvalho de Melo 
194a77e218SArnaldo Carvalho de Melo struct strlist_config {
204a77e218SArnaldo Carvalho de Melo 	bool dont_dupstr;
21*8ff9daf3SArnaldo Carvalho de Melo 	const char *dirname;
224a77e218SArnaldo Carvalho de Melo };
234a77e218SArnaldo Carvalho de Melo 
244a77e218SArnaldo Carvalho de Melo struct strlist *strlist__new(const char *slist, const struct strlist_config *config);
25d8639f06SArnaldo Carvalho de Melo void strlist__delete(struct strlist *slist);
2625903407SArnaldo Carvalho de Melo 
27d8639f06SArnaldo Carvalho de Melo void strlist__remove(struct strlist *slist, struct str_node *sn);
28d8639f06SArnaldo Carvalho de Melo int strlist__load(struct strlist *slist, const char *filename);
29d8639f06SArnaldo Carvalho de Melo int strlist__add(struct strlist *slist, const char *str);
3025903407SArnaldo Carvalho de Melo 
31d8639f06SArnaldo Carvalho de Melo struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx);
32d8639f06SArnaldo Carvalho de Melo struct str_node *strlist__find(struct strlist *slist, const char *entry);
333e340590SMasami Hiramatsu 
34d8639f06SArnaldo Carvalho de Melo static inline bool strlist__has_entry(struct strlist *slist, const char *entry)
353e340590SMasami Hiramatsu {
36d8639f06SArnaldo Carvalho de Melo 	return strlist__find(slist, entry) != NULL;
373e340590SMasami Hiramatsu }
3825903407SArnaldo Carvalho de Melo 
39d8639f06SArnaldo Carvalho de Melo static inline bool strlist__empty(const struct strlist *slist)
4025903407SArnaldo Carvalho de Melo {
41d8639f06SArnaldo Carvalho de Melo 	return rblist__empty(&slist->rblist);
4227d0fd41SArnaldo Carvalho de Melo }
4327d0fd41SArnaldo Carvalho de Melo 
44d8639f06SArnaldo Carvalho de Melo static inline unsigned int strlist__nr_entries(const struct strlist *slist)
4527d0fd41SArnaldo Carvalho de Melo {
46d8639f06SArnaldo Carvalho de Melo 	return rblist__nr_entries(&slist->rblist);
4725903407SArnaldo Carvalho de Melo }
4825903407SArnaldo Carvalho de Melo 
49abf5ef72SMasami Hiramatsu /* For strlist iteration */
50d8639f06SArnaldo Carvalho de Melo static inline struct str_node *strlist__first(struct strlist *slist)
51abf5ef72SMasami Hiramatsu {
52d8639f06SArnaldo Carvalho de Melo 	struct rb_node *rn = rb_first(&slist->rblist.entries);
53abf5ef72SMasami Hiramatsu 	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
54abf5ef72SMasami Hiramatsu }
55abf5ef72SMasami Hiramatsu static inline struct str_node *strlist__next(struct str_node *sn)
56abf5ef72SMasami Hiramatsu {
57abf5ef72SMasami Hiramatsu 	struct rb_node *rn;
58abf5ef72SMasami Hiramatsu 	if (!sn)
59abf5ef72SMasami Hiramatsu 		return NULL;
60abf5ef72SMasami Hiramatsu 	rn = rb_next(&sn->rb_node);
61abf5ef72SMasami Hiramatsu 	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
62abf5ef72SMasami Hiramatsu }
63abf5ef72SMasami Hiramatsu 
64abf5ef72SMasami Hiramatsu /**
65abf5ef72SMasami Hiramatsu  * strlist_for_each      - iterate over a strlist
66abf5ef72SMasami Hiramatsu  * @pos:	the &struct str_node to use as a loop cursor.
67d8639f06SArnaldo Carvalho de Melo  * @slist:	the &struct strlist for loop.
68abf5ef72SMasami Hiramatsu  */
69d8639f06SArnaldo Carvalho de Melo #define strlist__for_each(pos, slist)	\
70d8639f06SArnaldo Carvalho de Melo 	for (pos = strlist__first(slist); pos; pos = strlist__next(pos))
71abf5ef72SMasami Hiramatsu 
72abf5ef72SMasami Hiramatsu /**
73abf5ef72SMasami Hiramatsu  * strlist_for_each_safe - iterate over a strlist safe against removal of
74abf5ef72SMasami Hiramatsu  *                         str_node
75abf5ef72SMasami Hiramatsu  * @pos:	the &struct str_node to use as a loop cursor.
76abf5ef72SMasami Hiramatsu  * @n:		another &struct str_node to use as temporary storage.
77d8639f06SArnaldo Carvalho de Melo  * @slist:	the &struct strlist for loop.
78abf5ef72SMasami Hiramatsu  */
79d8639f06SArnaldo Carvalho de Melo #define strlist__for_each_safe(pos, n, slist)	\
80d8639f06SArnaldo Carvalho de Melo 	for (pos = strlist__first(slist), n = strlist__next(pos); pos;\
81abf5ef72SMasami Hiramatsu 	     pos = n, n = strlist__next(n))
82abf5ef72SMasami Hiramatsu 
83*8ff9daf3SArnaldo Carvalho de Melo int strlist__parse_list(struct strlist *slist, const char *s, const char *subst_dir);
848b40f521SJohn Kacur #endif /* __PERF_STRLIST_H */
85