blob: dd9f922ec67c58845ff22b6d3fabe3eb888c21e6 [file] [log] [blame]
John Kacur8b40f522009-09-24 18:02:18 +02001#ifndef __PERF_STRLIST_H
2#define __PERF_STRLIST_H
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03003
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -03004#include <linux/rbtree.h>
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03005#include <stdbool.h>
6
David Ahernee8dd3c2012-07-30 22:31:33 -06007#include "rblist.h"
8
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03009struct str_node {
10 struct rb_node rb_node;
11 const char *s;
12};
13
14struct strlist {
David Ahernee8dd3c2012-07-30 22:31:33 -060015 struct rblist rblist;
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -030016 bool dupstr;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030017};
18
19struct strlist *strlist__new(bool dupstr, const char *slist);
20void strlist__delete(struct strlist *self);
21
22void strlist__remove(struct strlist *self, struct str_node *sn);
23int strlist__load(struct strlist *self, const char *filename);
24int strlist__add(struct strlist *self, const char *str);
25
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -030026struct str_node *strlist__entry(const struct strlist *self, unsigned int idx);
Masami Hiramatsu3e340592009-12-15 10:31:49 -050027struct str_node *strlist__find(struct strlist *self, const char *entry);
28
29static inline bool strlist__has_entry(struct strlist *self, const char *entry)
30{
31 return strlist__find(self, entry) != NULL;
32}
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030033
34static inline bool strlist__empty(const struct strlist *self)
35{
David Ahernee8dd3c2012-07-30 22:31:33 -060036 return rblist__empty(&self->rblist);
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -030037}
38
39static inline unsigned int strlist__nr_entries(const struct strlist *self)
40{
David Ahernee8dd3c2012-07-30 22:31:33 -060041 return rblist__nr_entries(&self->rblist);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030042}
43
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050044/* For strlist iteration */
45static inline struct str_node *strlist__first(struct strlist *self)
46{
David Ahernee8dd3c2012-07-30 22:31:33 -060047 struct rb_node *rn = rb_first(&self->rblist.entries);
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050048 return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
49}
50static inline struct str_node *strlist__next(struct str_node *sn)
51{
52 struct rb_node *rn;
53 if (!sn)
54 return NULL;
55 rn = rb_next(&sn->rb_node);
56 return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
57}
58
59/**
60 * strlist_for_each - iterate over a strlist
61 * @pos: the &struct str_node to use as a loop cursor.
62 * @self: the &struct strlist for loop.
63 */
64#define strlist__for_each(pos, self) \
65 for (pos = strlist__first(self); pos; pos = strlist__next(pos))
66
67/**
68 * strlist_for_each_safe - iterate over a strlist safe against removal of
69 * str_node
70 * @pos: the &struct str_node to use as a loop cursor.
71 * @n: another &struct str_node to use as temporary storage.
72 * @self: the &struct strlist for loop.
73 */
74#define strlist__for_each_safe(pos, n, self) \
75 for (pos = strlist__first(self), n = strlist__next(pos); pos;\
76 pos = n, n = strlist__next(n))
77
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030078int strlist__parse_list(struct strlist *self, const char *s);
John Kacur8b40f522009-09-24 18:02:18 +020079#endif /* __PERF_STRLIST_H */