blob: 3ba839007d2c975c49e4f3f286e9d64eff3d3f60 [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
7struct str_node {
8 struct rb_node rb_node;
9 const char *s;
10};
11
12struct strlist {
13 struct rb_root entries;
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -030014 unsigned int nr_entries;
15 bool dupstr;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030016};
17
18struct strlist *strlist__new(bool dupstr, const char *slist);
19void strlist__delete(struct strlist *self);
20
21void strlist__remove(struct strlist *self, struct str_node *sn);
22int strlist__load(struct strlist *self, const char *filename);
23int strlist__add(struct strlist *self, const char *str);
24
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -030025struct str_node *strlist__entry(const struct strlist *self, unsigned int idx);
Masami Hiramatsu3e340592009-12-15 10:31:49 -050026struct str_node *strlist__find(struct strlist *self, const char *entry);
27
28static inline bool strlist__has_entry(struct strlist *self, const char *entry)
29{
30 return strlist__find(self, entry) != NULL;
31}
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030032
33static inline bool strlist__empty(const struct strlist *self)
34{
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -030035 return self->nr_entries == 0;
36}
37
38static inline unsigned int strlist__nr_entries(const struct strlist *self)
39{
40 return self->nr_entries;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030041}
42
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050043/* For strlist iteration */
44static inline struct str_node *strlist__first(struct strlist *self)
45{
46 struct rb_node *rn = rb_first(&self->entries);
47 return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
48}
49static inline struct str_node *strlist__next(struct str_node *sn)
50{
51 struct rb_node *rn;
52 if (!sn)
53 return NULL;
54 rn = rb_next(&sn->rb_node);
55 return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
56}
57
58/**
59 * strlist_for_each - iterate over a strlist
60 * @pos: the &struct str_node to use as a loop cursor.
61 * @self: the &struct strlist for loop.
62 */
63#define strlist__for_each(pos, self) \
64 for (pos = strlist__first(self); pos; pos = strlist__next(pos))
65
66/**
67 * strlist_for_each_safe - iterate over a strlist safe against removal of
68 * str_node
69 * @pos: the &struct str_node to use as a loop cursor.
70 * @n: another &struct str_node to use as temporary storage.
71 * @self: the &struct strlist for loop.
72 */
73#define strlist__for_each_safe(pos, n, self) \
74 for (pos = strlist__first(self), n = strlist__next(pos); pos;\
75 pos = n, n = strlist__next(n))
76
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030077int strlist__parse_list(struct strlist *self, const char *s);
John Kacur8b40f522009-09-24 18:02:18 +020078#endif /* __PERF_STRLIST_H */