Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / tools / perf / util / strlist.c
blob0d3dfcb919b44936dfa42f2f437176f833afa39c
1 /*
2 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Licensed under the GPLv2.
5 */
7 #include "strlist.h"
8 #include "util.h"
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 static
15 struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry)
17 const char *s = entry;
18 struct rb_node *rc = NULL;
19 struct strlist *strlist = container_of(rblist, struct strlist, rblist);
20 struct str_node *snode = malloc(sizeof(*snode));
22 if (snode != NULL) {
23 if (strlist->dupstr) {
24 s = strdup(s);
25 if (s == NULL)
26 goto out_delete;
28 snode->s = s;
29 rc = &snode->rb_node;
32 return rc;
34 out_delete:
35 free(snode);
36 return NULL;
39 static void str_node__delete(struct str_node *snode, bool dupstr)
41 if (dupstr)
42 zfree((char **)&snode->s);
43 free(snode);
46 static
47 void strlist__node_delete(struct rblist *rblist, struct rb_node *rb_node)
49 struct strlist *slist = container_of(rblist, struct strlist, rblist);
50 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
52 str_node__delete(snode, slist->dupstr);
55 static int strlist__node_cmp(struct rb_node *rb_node, const void *entry)
57 const char *str = entry;
58 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
60 return strcmp(snode->s, str);
63 int strlist__add(struct strlist *slist, const char *new_entry)
65 return rblist__add_node(&slist->rblist, new_entry);
68 int strlist__load(struct strlist *slist, const char *filename)
70 char entry[1024];
71 int err;
72 FILE *fp = fopen(filename, "r");
74 if (fp == NULL)
75 return -errno;
77 while (fgets(entry, sizeof(entry), fp) != NULL) {
78 const size_t len = strlen(entry);
80 if (len == 0)
81 continue;
82 entry[len - 1] = '\0';
84 err = strlist__add(slist, entry);
85 if (err != 0)
86 goto out;
89 err = 0;
90 out:
91 fclose(fp);
92 return err;
95 void strlist__remove(struct strlist *slist, struct str_node *snode)
97 rblist__remove_node(&slist->rblist, &snode->rb_node);
100 struct str_node *strlist__find(struct strlist *slist, const char *entry)
102 struct str_node *snode = NULL;
103 struct rb_node *rb_node = rblist__find(&slist->rblist, entry);
105 if (rb_node)
106 snode = container_of(rb_node, struct str_node, rb_node);
108 return snode;
111 static int strlist__parse_list_entry(struct strlist *slist, const char *s,
112 const char *subst_dir)
114 int err;
115 char *subst = NULL;
117 if (strncmp(s, "file://", 7) == 0)
118 return strlist__load(slist, s + 7);
120 if (subst_dir) {
121 err = -ENOMEM;
122 if (asprintf(&subst, "%s/%s", subst_dir, s) < 0)
123 goto out;
125 if (access(subst, F_OK) == 0) {
126 err = strlist__load(slist, subst);
127 goto out;
130 if (slist->file_only) {
131 err = -ENOENT;
132 goto out;
136 err = strlist__add(slist, s);
137 out:
138 free(subst);
139 return err;
142 static int strlist__parse_list(struct strlist *slist, const char *s, const char *subst_dir)
144 char *sep;
145 int err;
147 while ((sep = strchr(s, ',')) != NULL) {
148 *sep = '\0';
149 err = strlist__parse_list_entry(slist, s, subst_dir);
150 *sep = ',';
151 if (err != 0)
152 return err;
153 s = sep + 1;
156 return *s ? strlist__parse_list_entry(slist, s, subst_dir) : 0;
159 struct strlist *strlist__new(const char *list, const struct strlist_config *config)
161 struct strlist *slist = malloc(sizeof(*slist));
163 if (slist != NULL) {
164 bool dupstr = true;
165 bool file_only = false;
166 const char *dirname = NULL;
168 if (config) {
169 dupstr = !config->dont_dupstr;
170 dirname = config->dirname;
171 file_only = config->file_only;
174 rblist__init(&slist->rblist);
175 slist->rblist.node_cmp = strlist__node_cmp;
176 slist->rblist.node_new = strlist__node_new;
177 slist->rblist.node_delete = strlist__node_delete;
179 slist->dupstr = dupstr;
180 slist->file_only = file_only;
182 if (list && strlist__parse_list(slist, list, dirname) != 0)
183 goto out_error;
186 return slist;
187 out_error:
188 free(slist);
189 return NULL;
192 void strlist__delete(struct strlist *slist)
194 if (slist != NULL)
195 rblist__delete(&slist->rblist);
198 struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx)
200 struct str_node *snode = NULL;
201 struct rb_node *rb_node;
203 rb_node = rblist__entry(&slist->rblist, idx);
204 if (rb_node)
205 snode = container_of(rb_node, struct str_node, rb_node);
207 return snode;