1 /*
2 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Licensed under the GPLv2.
5 */
6
7 #include "strlist.h"
8 #include "util.h"
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 static
strlist__node_new(struct rblist * rblist,const void * entry)15 struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry)
16 {
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));
21
22 if (snode != NULL) {
23 if (strlist->dupstr) {
24 s = strdup(s);
25 if (s == NULL)
26 goto out_delete;
27 }
28 snode->s = s;
29 rc = &snode->rb_node;
30 }
31
32 return rc;
33
34 out_delete:
35 free(snode);
36 return NULL;
37 }
38
str_node__delete(struct str_node * snode,bool dupstr)39 static void str_node__delete(struct str_node *snode, bool dupstr)
40 {
41 if (dupstr)
42 zfree((char **)&snode->s);
43 free(snode);
44 }
45
46 static
strlist__node_delete(struct rblist * rblist,struct rb_node * rb_node)47 void strlist__node_delete(struct rblist *rblist, struct rb_node *rb_node)
48 {
49 struct strlist *slist = container_of(rblist, struct strlist, rblist);
50 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
51
52 str_node__delete(snode, slist->dupstr);
53 }
54
strlist__node_cmp(struct rb_node * rb_node,const void * entry)55 static int strlist__node_cmp(struct rb_node *rb_node, const void *entry)
56 {
57 const char *str = entry;
58 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
59
60 return strcmp(snode->s, str);
61 }
62
strlist__add(struct strlist * slist,const char * new_entry)63 int strlist__add(struct strlist *slist, const char *new_entry)
64 {
65 return rblist__add_node(&slist->rblist, new_entry);
66 }
67
strlist__load(struct strlist * slist,const char * filename)68 int strlist__load(struct strlist *slist, const char *filename)
69 {
70 char entry[1024];
71 int err;
72 FILE *fp = fopen(filename, "r");
73
74 if (fp == NULL)
75 return errno;
76
77 while (fgets(entry, sizeof(entry), fp) != NULL) {
78 const size_t len = strlen(entry);
79
80 if (len == 0)
81 continue;
82 entry[len - 1] = '\0';
83
84 err = strlist__add(slist, entry);
85 if (err != 0)
86 goto out;
87 }
88
89 err = 0;
90 out:
91 fclose(fp);
92 return err;
93 }
94
strlist__remove(struct strlist * slist,struct str_node * snode)95 void strlist__remove(struct strlist *slist, struct str_node *snode)
96 {
97 rblist__remove_node(&slist->rblist, &snode->rb_node);
98 }
99
strlist__find(struct strlist * slist,const char * entry)100 struct str_node *strlist__find(struct strlist *slist, const char *entry)
101 {
102 struct str_node *snode = NULL;
103 struct rb_node *rb_node = rblist__find(&slist->rblist, entry);
104
105 if (rb_node)
106 snode = container_of(rb_node, struct str_node, rb_node);
107
108 return snode;
109 }
110
strlist__parse_list_entry(struct strlist * slist,const char * s)111 static int strlist__parse_list_entry(struct strlist *slist, const char *s)
112 {
113 if (strncmp(s, "file://", 7) == 0)
114 return strlist__load(slist, s + 7);
115
116 return strlist__add(slist, s);
117 }
118
strlist__parse_list(struct strlist * slist,const char * s)119 int strlist__parse_list(struct strlist *slist, const char *s)
120 {
121 char *sep;
122 int err;
123
124 while ((sep = strchr(s, ',')) != NULL) {
125 *sep = '\0';
126 err = strlist__parse_list_entry(slist, s);
127 *sep = ',';
128 if (err != 0)
129 return err;
130 s = sep + 1;
131 }
132
133 return *s ? strlist__parse_list_entry(slist, s) : 0;
134 }
135
strlist__new(bool dupstr,const char * list)136 struct strlist *strlist__new(bool dupstr, const char *list)
137 {
138 struct strlist *slist = malloc(sizeof(*slist));
139
140 if (slist != NULL) {
141 rblist__init(&slist->rblist);
142 slist->rblist.node_cmp = strlist__node_cmp;
143 slist->rblist.node_new = strlist__node_new;
144 slist->rblist.node_delete = strlist__node_delete;
145
146 slist->dupstr = dupstr;
147 if (list && strlist__parse_list(slist, list) != 0)
148 goto out_error;
149 }
150
151 return slist;
152 out_error:
153 free(slist);
154 return NULL;
155 }
156
strlist__delete(struct strlist * slist)157 void strlist__delete(struct strlist *slist)
158 {
159 if (slist != NULL)
160 rblist__delete(&slist->rblist);
161 }
162
strlist__entry(const struct strlist * slist,unsigned int idx)163 struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx)
164 {
165 struct str_node *snode = NULL;
166 struct rb_node *rb_node;
167
168 rb_node = rblist__entry(&slist->rblist, idx);
169 if (rb_node)
170 snode = container_of(rb_node, struct str_node, rb_node);
171
172 return snode;
173 }
174