John Kacur | 8b40f52 | 2009-09-24 18:02:18 +0200 | [diff] [blame] | 1 | #ifndef __PERF_STRLIST_H |
| 2 | #define __PERF_STRLIST_H |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 3 | |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 4 | #include <linux/rbtree.h> |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 5 | #include <stdbool.h> |
| 6 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 7 | #include "rblist.h" |
| 8 | |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 9 | struct str_node { |
| 10 | struct rb_node rb_node; |
| 11 | const char *s; |
| 12 | }; |
| 13 | |
| 14 | struct strlist { |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 15 | struct rblist rblist; |
Arnaldo Carvalho de Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 16 | bool dupstr; |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | struct strlist *strlist__new(bool dupstr, const char *slist); |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 20 | void strlist__delete(struct strlist *slist); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 21 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 22 | void strlist__remove(struct strlist *slist, struct str_node *sn); |
| 23 | int strlist__load(struct strlist *slist, const char *filename); |
| 24 | int strlist__add(struct strlist *slist, const char *str); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 25 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 26 | struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx); |
| 27 | struct str_node *strlist__find(struct strlist *slist, const char *entry); |
Masami Hiramatsu | 3e34059 | 2009-12-15 10:31:49 -0500 | [diff] [blame] | 28 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 29 | static inline bool strlist__has_entry(struct strlist *slist, const char *entry) |
Masami Hiramatsu | 3e34059 | 2009-12-15 10:31:49 -0500 | [diff] [blame] | 30 | { |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 31 | return strlist__find(slist, entry) != NULL; |
Masami Hiramatsu | 3e34059 | 2009-12-15 10:31:49 -0500 | [diff] [blame] | 32 | } |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 33 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 34 | static inline bool strlist__empty(const struct strlist *slist) |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 35 | { |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 36 | return rblist__empty(&slist->rblist); |
Arnaldo Carvalho de Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 37 | } |
| 38 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 39 | static inline unsigned int strlist__nr_entries(const struct strlist *slist) |
Arnaldo Carvalho de Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 40 | { |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 41 | return rblist__nr_entries(&slist->rblist); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 42 | } |
| 43 | |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 44 | /* For strlist iteration */ |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 45 | static inline struct str_node *strlist__first(struct strlist *slist) |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 46 | { |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 47 | struct rb_node *rn = rb_first(&slist->rblist.entries); |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 48 | return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; |
| 49 | } |
| 50 | static 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. |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 62 | * @slist: the &struct strlist for loop. |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 63 | */ |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 64 | #define strlist__for_each(pos, slist) \ |
| 65 | for (pos = strlist__first(slist); pos; pos = strlist__next(pos)) |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 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. |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 72 | * @slist: the &struct strlist for loop. |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 73 | */ |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 74 | #define strlist__for_each_safe(pos, n, slist) \ |
| 75 | for (pos = strlist__first(slist), n = strlist__next(pos); pos;\ |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 76 | pos = n, n = strlist__next(n)) |
| 77 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame^] | 78 | int strlist__parse_list(struct strlist *slist, const char *s); |
John Kacur | 8b40f52 | 2009-09-24 18:02:18 +0200 | [diff] [blame] | 79 | #endif /* __PERF_STRLIST_H */ |