2 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Licensed under the GPLv2.
13 static struct str_node *str_node__new(const char *s, bool dupstr)
15 struct str_node *self = malloc(sizeof(*self));
33 static void str_node__delete(struct str_node *self, bool dupstr)
36 free((void *)self->s);
40 int strlist__add(struct strlist *self, const char *new_entry)
42 struct rb_node **p = &self->entries.rb_node;
43 struct rb_node *parent = NULL;
50 sn = rb_entry(parent, struct str_node, rb_node);
51 rc = strcmp(sn->s, new_entry);
61 sn = str_node__new(new_entry, self->dupstr);
65 rb_link_node(&sn->rb_node, parent, p);
66 rb_insert_color(&sn->rb_node, &self->entries);
71 int strlist__load(struct strlist *self, const char *filename)
75 FILE *fp = fopen(filename, "r");
80 while (fgets(entry, sizeof(entry), fp) != NULL) {
81 const size_t len = strlen(entry);
85 entry[len - 1] = '\0';
87 err = strlist__add(self, entry);
98 void strlist__remove(struct strlist *self, struct str_node *sn)
100 rb_erase(&sn->rb_node, &self->entries);
101 str_node__delete(sn, self->dupstr);
104 bool strlist__has_entry(struct strlist *self, const char *entry)
106 struct rb_node **p = &self->entries.rb_node;
107 struct rb_node *parent = NULL;
114 sn = rb_entry(parent, struct str_node, rb_node);
115 rc = strcmp(sn->s, entry);
128 static int strlist__parse_list_entry(struct strlist *self, const char *s)
130 if (strncmp(s, "file://", 7) == 0)
131 return strlist__load(self, s + 7);
133 return strlist__add(self, s);
136 int strlist__parse_list(struct strlist *self, const char *s)
141 while ((sep = strchr(s, ',')) != NULL) {
143 err = strlist__parse_list_entry(self, s);
150 return *s ? strlist__parse_list_entry(self, s) : 0;
153 struct strlist *strlist__new(bool dupstr, const char *slist)
155 struct strlist *self = malloc(sizeof(*self));
158 self->entries = RB_ROOT;
159 self->dupstr = dupstr;
160 if (slist && strlist__parse_list(self, slist) != 0)
170 void strlist__delete(struct strlist *self)
173 struct str_node *pos;
174 struct rb_node *next = rb_first(&self->entries);
177 pos = rb_entry(next, struct str_node, rb_node);
178 next = rb_next(&pos->rb_node);
179 strlist__remove(self, pos);
181 self->entries = RB_ROOT;