blob: 155d8b7078a723ddfec5030048a72b6500840019 [file] [log] [blame]
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001/*
2 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Licensed under the GPLv2.
5 */
6
7#include "strlist.h"
8#include <errno.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
David Ahernee8dd3c2012-07-30 22:31:33 -060013static
14struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030015{
David Ahernee8dd3c2012-07-30 22:31:33 -060016 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));
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030020
David Ahernee8dd3c2012-07-30 22:31:33 -060021 if (snode != NULL) {
22 if (strlist->dupstr) {
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030023 s = strdup(s);
24 if (s == NULL)
25 goto out_delete;
26 }
David Ahernee8dd3c2012-07-30 22:31:33 -060027 snode->s = s;
28 rc = &snode->rb_node;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030029 }
30
David Ahernee8dd3c2012-07-30 22:31:33 -060031 return rc;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030032
33out_delete:
David Ahernee8dd3c2012-07-30 22:31:33 -060034 free(snode);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030035 return NULL;
36}
37
38static void str_node__delete(struct str_node *self, bool dupstr)
39{
40 if (dupstr)
41 free((void *)self->s);
42 free(self);
43}
44
David Ahernee8dd3c2012-07-30 22:31:33 -060045static
46void strlist__node_delete(struct rblist *rblist, struct rb_node *rb_node)
47{
48 struct strlist *slist = container_of(rblist, struct strlist, rblist);
49 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
50
51 str_node__delete(snode, slist->dupstr);
52}
53
54static int strlist__node_cmp(struct rb_node *rb_node, const void *entry)
55{
56 const char *str = entry;
57 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
58
59 return strcmp(snode->s, str);
60}
61
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030062int strlist__add(struct strlist *self, const char *new_entry)
63{
David Ahernee8dd3c2012-07-30 22:31:33 -060064 return rblist__add_node(&self->rblist, new_entry);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030065}
66
67int strlist__load(struct strlist *self, const char *filename)
68{
69 char entry[1024];
70 int err;
71 FILE *fp = fopen(filename, "r");
72
73 if (fp == NULL)
74 return errno;
75
76 while (fgets(entry, sizeof(entry), fp) != NULL) {
77 const size_t len = strlen(entry);
78
79 if (len == 0)
80 continue;
81 entry[len - 1] = '\0';
82
83 err = strlist__add(self, entry);
84 if (err != 0)
85 goto out;
86 }
87
88 err = 0;
89out:
90 fclose(fp);
91 return err;
92}
93
David Ahernee8dd3c2012-07-30 22:31:33 -060094void strlist__remove(struct strlist *slist, struct str_node *snode)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030095{
Suzuki K. Poulose45922812012-08-29 11:30:07 +053096 rblist__remove_node(&slist->rblist, &snode->rb_node);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030097}
98
David Ahernee8dd3c2012-07-30 22:31:33 -060099struct str_node *strlist__find(struct strlist *slist, const char *entry)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300100{
David Ahernee8dd3c2012-07-30 22:31:33 -0600101 struct str_node *snode = NULL;
102 struct rb_node *rb_node = rblist__find(&slist->rblist, entry);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300103
David Ahernee8dd3c2012-07-30 22:31:33 -0600104 if (rb_node)
105 snode = container_of(rb_node, struct str_node, rb_node);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300106
David Ahernee8dd3c2012-07-30 22:31:33 -0600107 return snode;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300108}
109
110static int strlist__parse_list_entry(struct strlist *self, const char *s)
111{
112 if (strncmp(s, "file://", 7) == 0)
113 return strlist__load(self, s + 7);
114
115 return strlist__add(self, s);
116}
117
118int strlist__parse_list(struct strlist *self, const char *s)
119{
120 char *sep;
121 int err;
122
123 while ((sep = strchr(s, ',')) != NULL) {
124 *sep = '\0';
125 err = strlist__parse_list_entry(self, s);
126 *sep = ',';
127 if (err != 0)
128 return err;
129 s = sep + 1;
130 }
131
132 return *s ? strlist__parse_list_entry(self, s) : 0;
133}
134
135struct strlist *strlist__new(bool dupstr, const char *slist)
136{
137 struct strlist *self = malloc(sizeof(*self));
138
139 if (self != NULL) {
David Ahernee8dd3c2012-07-30 22:31:33 -0600140 rblist__init(&self->rblist);
141 self->rblist.node_cmp = strlist__node_cmp;
142 self->rblist.node_new = strlist__node_new;
143 self->rblist.node_delete = strlist__node_delete;
144
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300145 self->dupstr = dupstr;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300146 if (slist && strlist__parse_list(self, slist) != 0)
147 goto out_error;
148 }
149
150 return self;
151out_error:
152 free(self);
153 return NULL;
154}
155
156void strlist__delete(struct strlist *self)
157{
David Ahernee8dd3c2012-07-30 22:31:33 -0600158 if (self != NULL)
159 rblist__delete(&self->rblist);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300160}
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300161
David Ahernee8dd3c2012-07-30 22:31:33 -0600162struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx)
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300163{
David Ahernee8dd3c2012-07-30 22:31:33 -0600164 struct str_node *snode = NULL;
165 struct rb_node *rb_node;
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300166
David Ahernee8dd3c2012-07-30 22:31:33 -0600167 rb_node = rblist__entry(&slist->rblist, idx);
168 if (rb_node)
169 snode = container_of(rb_node, struct str_node, rb_node);
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300170
David Ahernee8dd3c2012-07-30 22:31:33 -0600171 return snode;
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300172}