blob: 71f9d102b96fc4aa9a94176151008083e275199f [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"
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03008#include "util.h"
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03009#include <errno.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
David Ahernee8dd3c2012-07-30 22:31:33 -060014static
15struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030016{
David Ahernee8dd3c2012-07-30 22:31:33 -060017 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));
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030021
David Ahernee8dd3c2012-07-30 22:31:33 -060022 if (snode != NULL) {
23 if (strlist->dupstr) {
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030024 s = strdup(s);
25 if (s == NULL)
26 goto out_delete;
27 }
David Ahernee8dd3c2012-07-30 22:31:33 -060028 snode->s = s;
29 rc = &snode->rb_node;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030030 }
31
David Ahernee8dd3c2012-07-30 22:31:33 -060032 return rc;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030033
34out_delete:
David Ahernee8dd3c2012-07-30 22:31:33 -060035 free(snode);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030036 return NULL;
37}
38
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030039static void str_node__delete(struct str_node *snode, bool dupstr)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030040{
41 if (dupstr)
Namhyung Kim7d16c632014-01-09 23:07:59 +090042 zfree((char **)&snode->s);
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030043 free(snode);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030044}
45
David Ahernee8dd3c2012-07-30 22:31:33 -060046static
47void 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
55static 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
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030063int strlist__add(struct strlist *slist, const char *new_entry)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030064{
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030065 return rblist__add_node(&slist->rblist, new_entry);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030066}
67
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030068int strlist__load(struct strlist *slist, const char *filename)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030069{
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
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -030084 err = strlist__add(slist, entry);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030085 if (err != 0)
86 goto out;
87 }
88
89 err = 0;
90out:
91 fclose(fp);
92 return err;
93}
94
David Ahernee8dd3c2012-07-30 22:31:33 -060095void strlist__remove(struct strlist *slist, struct str_node *snode)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030096{
Suzuki K. Poulose45922812012-08-29 11:30:07 +053097 rblist__remove_node(&slist->rblist, &snode->rb_node);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030098}
99
David Ahernee8dd3c2012-07-30 22:31:33 -0600100struct str_node *strlist__find(struct strlist *slist, const char *entry)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300101{
David Ahernee8dd3c2012-07-30 22:31:33 -0600102 struct str_node *snode = NULL;
103 struct rb_node *rb_node = rblist__find(&slist->rblist, entry);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300104
David Ahernee8dd3c2012-07-30 22:31:33 -0600105 if (rb_node)
106 snode = container_of(rb_node, struct str_node, rb_node);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300107
David Ahernee8dd3c2012-07-30 22:31:33 -0600108 return snode;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300109}
110
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300111static int strlist__parse_list_entry(struct strlist *slist, const char *s)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300112{
113 if (strncmp(s, "file://", 7) == 0)
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300114 return strlist__load(slist, s + 7);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300115
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300116 return strlist__add(slist, s);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300117}
118
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300119int strlist__parse_list(struct strlist *slist, const char *s)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300120{
121 char *sep;
122 int err;
123
124 while ((sep = strchr(s, ',')) != NULL) {
125 *sep = '\0';
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300126 err = strlist__parse_list_entry(slist, s);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300127 *sep = ',';
128 if (err != 0)
129 return err;
130 s = sep + 1;
131 }
132
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300133 return *s ? strlist__parse_list_entry(slist, s) : 0;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300134}
135
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300136struct strlist *strlist__new(bool dupstr, const char *list)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300137{
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300138 struct strlist *slist = malloc(sizeof(*slist));
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300139
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300140 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;
David Ahernee8dd3c2012-07-30 22:31:33 -0600145
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300146 slist->dupstr = dupstr;
Ananth N Mavinakayanahalli79146a62013-03-12 14:32:17 +0530147 if (list && strlist__parse_list(slist, list) != 0)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300148 goto out_error;
149 }
150
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300151 return slist;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300152out_error:
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300153 free(slist);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300154 return NULL;
155}
156
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300157void strlist__delete(struct strlist *slist)
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300158{
Arnaldo Carvalho de Melod8639f02013-01-24 21:59:59 -0300159 if (slist != NULL)
160 rblist__delete(&slist->rblist);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300161}
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300162
David Ahernee8dd3c2012-07-30 22:31:33 -0600163struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx)
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300164{
David Ahernee8dd3c2012-07-30 22:31:33 -0600165 struct str_node *snode = NULL;
166 struct rb_node *rb_node;
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300167
David Ahernee8dd3c2012-07-30 22:31:33 -0600168 rb_node = rblist__entry(&slist->rblist, idx);
169 if (rb_node)
170 snode = container_of(rb_node, struct str_node, rb_node);
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300171
David Ahernee8dd3c2012-07-30 22:31:33 -0600172 return snode;
Arnaldo Carvalho de Melo27d0fd42009-07-11 12:18:34 -0300173}