strlist.h 2.3 KB
Newer Older
1 2
#ifndef __PERF_STRLIST_H
#define __PERF_STRLIST_H
3

4
#include <linux/rbtree.h>
5 6
#include <stdbool.h>

7 8
#include "rblist.h"

9 10 11 12 13 14
struct str_node {
	struct rb_node rb_node;
	const char     *s;
};

struct strlist {
15
	struct rblist rblist;
16
	bool	       dupstr;
17 18
};

19 20 21 22 23
struct strlist_config {
	bool dont_dupstr;
};

struct strlist *strlist__new(const char *slist, const struct strlist_config *config);
24
void strlist__delete(struct strlist *slist);
25

26 27 28
void strlist__remove(struct strlist *slist, struct str_node *sn);
int strlist__load(struct strlist *slist, const char *filename);
int strlist__add(struct strlist *slist, const char *str);
29

30 31
struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx);
struct str_node *strlist__find(struct strlist *slist, const char *entry);
32

33
static inline bool strlist__has_entry(struct strlist *slist, const char *entry)
34
{
35
	return strlist__find(slist, entry) != NULL;
36
}
37

38
static inline bool strlist__empty(const struct strlist *slist)
39
{
40
	return rblist__empty(&slist->rblist);
41 42
}

43
static inline unsigned int strlist__nr_entries(const struct strlist *slist)
44
{
45
	return rblist__nr_entries(&slist->rblist);
46 47
}

48
/* For strlist iteration */
49
static inline struct str_node *strlist__first(struct strlist *slist)
50
{
51
	struct rb_node *rn = rb_first(&slist->rblist.entries);
52 53 54 55 56 57 58 59 60 61 62 63 64 65
	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
}
static inline struct str_node *strlist__next(struct str_node *sn)
{
	struct rb_node *rn;
	if (!sn)
		return NULL;
	rn = rb_next(&sn->rb_node);
	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
}

/**
 * strlist_for_each      - iterate over a strlist
 * @pos:	the &struct str_node to use as a loop cursor.
66
 * @slist:	the &struct strlist for loop.
67
 */
68 69
#define strlist__for_each(pos, slist)	\
	for (pos = strlist__first(slist); pos; pos = strlist__next(pos))
70 71 72 73 74 75

/**
 * strlist_for_each_safe - iterate over a strlist safe against removal of
 *                         str_node
 * @pos:	the &struct str_node to use as a loop cursor.
 * @n:		another &struct str_node to use as temporary storage.
76
 * @slist:	the &struct strlist for loop.
77
 */
78 79
#define strlist__for_each_safe(pos, n, slist)	\
	for (pos = strlist__first(slist), n = strlist__next(pos); pos;\
80 81
	     pos = n, n = strlist__next(n))

82
int strlist__parse_list(struct strlist *slist, const char *s);
83
#endif /* __PERF_STRLIST_H */