]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
8b40f521 JK |
2 | #ifndef __PERF_STRLIST_H |
3 | #define __PERF_STRLIST_H | |
25903407 | 4 | |
43cbcd8a | 5 | #include <linux/rbtree.h> |
25903407 ACM |
6 | #include <stdbool.h> |
7 | ||
ee8dd3ca DA |
8 | #include "rblist.h" |
9 | ||
25903407 ACM |
10 | struct str_node { |
11 | struct rb_node rb_node; | |
12 | const char *s; | |
13 | }; | |
14 | ||
15 | struct strlist { | |
ee8dd3ca | 16 | struct rblist rblist; |
dd8232bc NK |
17 | bool dupstr; |
18 | bool file_only; | |
25903407 ACM |
19 | }; |
20 | ||
dd8232bc NK |
21 | /* |
22 | * @file_only: When dirname is present, only consider entries as filenames, | |
23 | * that should not be added to the list if dirname/entry is not | |
24 | * found | |
25 | */ | |
4a77e218 ACM |
26 | struct strlist_config { |
27 | bool dont_dupstr; | |
dd8232bc | 28 | bool file_only; |
8ff9daf3 | 29 | const char *dirname; |
4a77e218 ACM |
30 | }; |
31 | ||
32 | struct strlist *strlist__new(const char *slist, const struct strlist_config *config); | |
d8639f06 | 33 | void strlist__delete(struct strlist *slist); |
25903407 | 34 | |
d8639f06 ACM |
35 | void strlist__remove(struct strlist *slist, struct str_node *sn); |
36 | int strlist__load(struct strlist *slist, const char *filename); | |
37 | int strlist__add(struct strlist *slist, const char *str); | |
25903407 | 38 | |
d8639f06 ACM |
39 | struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx); |
40 | struct str_node *strlist__find(struct strlist *slist, const char *entry); | |
3e340590 | 41 | |
d8639f06 | 42 | static inline bool strlist__has_entry(struct strlist *slist, const char *entry) |
3e340590 | 43 | { |
d8639f06 | 44 | return strlist__find(slist, entry) != NULL; |
3e340590 | 45 | } |
25903407 | 46 | |
d8639f06 | 47 | static inline bool strlist__empty(const struct strlist *slist) |
25903407 | 48 | { |
d8639f06 | 49 | return rblist__empty(&slist->rblist); |
27d0fd41 ACM |
50 | } |
51 | ||
d8639f06 | 52 | static inline unsigned int strlist__nr_entries(const struct strlist *slist) |
27d0fd41 | 53 | { |
d8639f06 | 54 | return rblist__nr_entries(&slist->rblist); |
25903407 ACM |
55 | } |
56 | ||
abf5ef72 | 57 | /* For strlist iteration */ |
d8639f06 | 58 | static inline struct str_node *strlist__first(struct strlist *slist) |
abf5ef72 | 59 | { |
d8639f06 | 60 | struct rb_node *rn = rb_first(&slist->rblist.entries); |
abf5ef72 MH |
61 | return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; |
62 | } | |
63 | static inline struct str_node *strlist__next(struct str_node *sn) | |
64 | { | |
65 | struct rb_node *rn; | |
66 | if (!sn) | |
67 | return NULL; | |
68 | rn = rb_next(&sn->rb_node); | |
69 | return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; | |
70 | } | |
71 | ||
72 | /** | |
73 | * strlist_for_each - iterate over a strlist | |
74 | * @pos: the &struct str_node to use as a loop cursor. | |
d8639f06 | 75 | * @slist: the &struct strlist for loop. |
abf5ef72 | 76 | */ |
602a1f4d | 77 | #define strlist__for_each_entry(pos, slist) \ |
d8639f06 | 78 | for (pos = strlist__first(slist); pos; pos = strlist__next(pos)) |
abf5ef72 MH |
79 | |
80 | /** | |
81 | * strlist_for_each_safe - iterate over a strlist safe against removal of | |
82 | * str_node | |
83 | * @pos: the &struct str_node to use as a loop cursor. | |
84 | * @n: another &struct str_node to use as temporary storage. | |
d8639f06 | 85 | * @slist: the &struct strlist for loop. |
abf5ef72 | 86 | */ |
602a1f4d | 87 | #define strlist__for_each_entry_safe(pos, n, slist) \ |
d8639f06 | 88 | for (pos = strlist__first(slist), n = strlist__next(pos); pos;\ |
abf5ef72 | 89 | pos = n, n = strlist__next(n)) |
8b40f521 | 90 | #endif /* __PERF_STRLIST_H */ |