blob: 19207e50fce50a0bf7d01f11bbc87b6b64ecba6a [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;
Namhyung Kimdd8232b2016-01-09 19:16:27 +090016 bool dupstr;
17 bool file_only;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030018};
19
Namhyung Kimdd8232b2016-01-09 19:16:27 +090020/*
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 Melo4a77e212015-07-20 12:13:34 -030025struct strlist_config {
26 bool dont_dupstr;
Namhyung Kimdd8232b2016-01-09 19:16:27 +090027 bool file_only;
Arnaldo Carvalho de Melo8ff9daf2015-07-17 12:07:25 -030028 const char *dirname;
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -030029};
30
31struct strlist *strlist__new(const char *slist, const struct strlist_config *config);
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030032void strlist__delete(struct strlist *slist);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030033
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030034void strlist__remove(struct strlist *slist, struct str_node *sn);
35int strlist__load(struct strlist *slist, const char *filename);
36int strlist__add(struct strlist *slist, const char *str);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030037
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030038struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx);
39struct str_node *strlist__find(struct strlist *slist, const char *entry);
Masami Hiramatsu3e340592009-12-15 10:31:49 -050040
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030041static inline bool strlist__has_entry(struct strlist *slist, const char *entry)
Masami Hiramatsu3e340592009-12-15 10:31:49 -050042{
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030043 return strlist__find(slist, entry) != NULL;
Masami Hiramatsu3e340592009-12-15 10:31:49 -050044}
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030045
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030046static inline bool strlist__empty(const struct strlist *slist)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030047{
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030048 return rblist__empty(&slist->rblist);
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -030049}
50
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030051static inline unsigned int strlist__nr_entries(const struct strlist *slist)
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -030052{
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030053 return rblist__nr_entries(&slist->rblist);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030054}
55
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050056/* For strlist iteration */
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030057static inline struct str_node *strlist__first(struct strlist *slist)
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050058{
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030059 struct rb_node *rn = rb_first(&slist->rblist.entries);
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050060 return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
61}
62static 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 Melod8639f02013-01-24 21:59:59 -030074 * @slist: the &struct strlist for loop.
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050075 */
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -030076#define strlist__for_each_entry(pos, slist) \
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030077 for (pos = strlist__first(slist); pos; pos = strlist__next(pos))
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050078
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 Melod8639f02013-01-24 21:59:59 -030084 * @slist: the &struct strlist for loop.
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050085 */
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -030086#define strlist__for_each_entry_safe(pos, n, slist) \
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030087 for (pos = strlist__first(slist), n = strlist__next(pos); pos;\
Masami Hiramatsuabf5ef72009-12-15 10:31:56 -050088 pos = n, n = strlist__next(n))
John Kacur8b40f522009-09-24 18:02:18 +020089#endif /* __PERF_STRLIST_H */