x86/xen: resume timer irqs early
[linux/fpc-iii.git] / tools / perf / util / strlist.c
blobeabdce0a2daa0a0a2c5c882529617322ce6e8852
1 /*
2 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Licensed under the GPLv2.
5 */
7 #include "strlist.h"
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
13 static
14 struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry)
16 const char *s = entry;
17 struct rb_node *rc = NULL;
18 struct strlist *strlist = container_of(rblist, struct strlist, rblist);
19 struct str_node *snode = malloc(sizeof(*snode));
21 if (snode != NULL) {
22 if (strlist->dupstr) {
23 s = strdup(s);
24 if (s == NULL)
25 goto out_delete;
27 snode->s = s;
28 rc = &snode->rb_node;
31 return rc;
33 out_delete:
34 free(snode);
35 return NULL;
38 static void str_node__delete(struct str_node *snode, bool dupstr)
40 if (dupstr)
41 free((void *)snode->s);
42 free(snode);
45 static
46 void strlist__node_delete(struct rblist *rblist, struct rb_node *rb_node)
48 struct strlist *slist = container_of(rblist, struct strlist, rblist);
49 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
51 str_node__delete(snode, slist->dupstr);
54 static int strlist__node_cmp(struct rb_node *rb_node, const void *entry)
56 const char *str = entry;
57 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
59 return strcmp(snode->s, str);
62 int strlist__add(struct strlist *slist, const char *new_entry)
64 return rblist__add_node(&slist->rblist, new_entry);
67 int strlist__load(struct strlist *slist, const char *filename)
69 char entry[1024];
70 int err;
71 FILE *fp = fopen(filename, "r");
73 if (fp == NULL)
74 return errno;
76 while (fgets(entry, sizeof(entry), fp) != NULL) {
77 const size_t len = strlen(entry);
79 if (len == 0)
80 continue;
81 entry[len - 1] = '\0';
83 err = strlist__add(slist, entry);
84 if (err != 0)
85 goto out;
88 err = 0;
89 out:
90 fclose(fp);
91 return err;
94 void strlist__remove(struct strlist *slist, struct str_node *snode)
96 rblist__remove_node(&slist->rblist, &snode->rb_node);
99 struct str_node *strlist__find(struct strlist *slist, const char *entry)
101 struct str_node *snode = NULL;
102 struct rb_node *rb_node = rblist__find(&slist->rblist, entry);
104 if (rb_node)
105 snode = container_of(rb_node, struct str_node, rb_node);
107 return snode;
110 static int strlist__parse_list_entry(struct strlist *slist, const char *s)
112 if (strncmp(s, "file://", 7) == 0)
113 return strlist__load(slist, s + 7);
115 return strlist__add(slist, s);
118 int strlist__parse_list(struct strlist *slist, const char *s)
120 char *sep;
121 int err;
123 while ((sep = strchr(s, ',')) != NULL) {
124 *sep = '\0';
125 err = strlist__parse_list_entry(slist, s);
126 *sep = ',';
127 if (err != 0)
128 return err;
129 s = sep + 1;
132 return *s ? strlist__parse_list_entry(slist, s) : 0;
135 struct strlist *strlist__new(bool dupstr, const char *list)
137 struct strlist *slist = malloc(sizeof(*slist));
139 if (slist != NULL) {
140 rblist__init(&slist->rblist);
141 slist->rblist.node_cmp = strlist__node_cmp;
142 slist->rblist.node_new = strlist__node_new;
143 slist->rblist.node_delete = strlist__node_delete;
145 slist->dupstr = dupstr;
146 if (list && strlist__parse_list(slist, list) != 0)
147 goto out_error;
150 return slist;
151 out_error:
152 free(slist);
153 return NULL;
156 void strlist__delete(struct strlist *slist)
158 if (slist != NULL)
159 rblist__delete(&slist->rblist);
162 struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx)
164 struct str_node *snode = NULL;
165 struct rb_node *rb_node;
167 rb_node = rblist__entry(&slist->rblist, idx);
168 if (rb_node)
169 snode = container_of(rb_node, struct str_node, rb_node);
171 return snode;