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; |
Namhyung Kim | dd8232b | 2016-01-09 19:16:27 +0900 | [diff] [blame] | 16 | bool dupstr; |
| 17 | bool file_only; |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 18 | }; |
| 19 | |
Namhyung Kim | dd8232b | 2016-01-09 19:16:27 +0900 | [diff] [blame] | 20 | /* |
| 21 | * @file_only: When dirname is present, only consider entries as filenames, |
| 22 | * that should not be added to the list if dirname/entry is not |
| 23 | * found |
| 24 | */ |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 25 | struct strlist_config { |
| 26 | bool dont_dupstr; |
Namhyung Kim | dd8232b | 2016-01-09 19:16:27 +0900 | [diff] [blame] | 27 | bool file_only; |
Arnaldo Carvalho de Melo | 8ff9daf | 2015-07-17 12:07:25 -0300 | [diff] [blame] | 28 | const char *dirname; |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | struct strlist *strlist__new(const char *slist, const struct strlist_config *config); |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 32 | void strlist__delete(struct strlist *slist); |
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 | void strlist__remove(struct strlist *slist, struct str_node *sn); |
| 35 | int strlist__load(struct strlist *slist, const char *filename); |
| 36 | int strlist__add(struct strlist *slist, const char *str); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 37 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 38 | struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx); |
| 39 | struct str_node *strlist__find(struct strlist *slist, const char *entry); |
Masami Hiramatsu | 3e34059 | 2009-12-15 10:31:49 -0500 | [diff] [blame] | 40 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 41 | static inline bool strlist__has_entry(struct strlist *slist, const char *entry) |
Masami Hiramatsu | 3e34059 | 2009-12-15 10:31:49 -0500 | [diff] [blame] | 42 | { |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 43 | return strlist__find(slist, entry) != NULL; |
Masami Hiramatsu | 3e34059 | 2009-12-15 10:31:49 -0500 | [diff] [blame] | 44 | } |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 45 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 46 | static inline bool strlist__empty(const struct strlist *slist) |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 47 | { |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 48 | return rblist__empty(&slist->rblist); |
Arnaldo Carvalho de Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 49 | } |
| 50 | |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 51 | 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] | 52 | { |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 53 | return rblist__nr_entries(&slist->rblist); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 54 | } |
| 55 | |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 56 | /* For strlist iteration */ |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 57 | static inline struct str_node *strlist__first(struct strlist *slist) |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 58 | { |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 59 | struct rb_node *rn = rb_first(&slist->rblist.entries); |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 60 | return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; |
| 61 | } |
| 62 | static inline struct str_node *strlist__next(struct str_node *sn) |
| 63 | { |
| 64 | struct rb_node *rn; |
| 65 | if (!sn) |
| 66 | return NULL; |
| 67 | rn = rb_next(&sn->rb_node); |
| 68 | return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * strlist_for_each - iterate over a strlist |
| 73 | * @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] | 74 | * @slist: the &struct strlist for loop. |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 75 | */ |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 76 | #define strlist__for_each_entry(pos, slist) \ |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 77 | for (pos = strlist__first(slist); pos; pos = strlist__next(pos)) |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * strlist_for_each_safe - iterate over a strlist safe against removal of |
| 81 | * str_node |
| 82 | * @pos: the &struct str_node to use as a loop cursor. |
| 83 | * @n: another &struct str_node to use as temporary storage. |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 84 | * @slist: the &struct strlist for loop. |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 85 | */ |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 86 | #define strlist__for_each_entry_safe(pos, n, slist) \ |
Arnaldo Carvalho de Melo | d8639f0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 87 | for (pos = strlist__first(slist), n = strlist__next(pos); pos;\ |
Masami Hiramatsu | abf5ef7 | 2009-12-15 10:31:56 -0500 | [diff] [blame] | 88 | pos = n, n = strlist__next(n)) |
John Kacur | 8b40f52 | 2009-09-24 18:02:18 +0200 | [diff] [blame] | 89 | #endif /* __PERF_STRLIST_H */ |