2 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Licensed under the GPLv2.
14 struct rb_node
*strlist__node_new(struct rblist
*rblist
, const void *entry
)
16 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
));
22 if (strlist
->dupstr
) {
38 static void str_node__delete(struct str_node
*snode
, bool dupstr
)
41 free((void *)snode
->s
);
46 void strlist__node_delete(struct rblist
*rblist
, struct rb_node
*rb_node
)
48 struct strlist
*slist
= container_of(rblist
, struct strlist
, rblist
);
49 struct str_node
*snode
= container_of(rb_node
, struct str_node
, rb_node
);
51 str_node__delete(snode
, slist
->dupstr
);
54 static int strlist__node_cmp(struct rb_node
*rb_node
, const void *entry
)
56 const char *str
= entry
;
57 struct str_node
*snode
= container_of(rb_node
, struct str_node
, rb_node
);
59 return strcmp(snode
->s
, str
);
62 int strlist__add(struct strlist
*slist
, const char *new_entry
)
64 return rblist__add_node(&slist
->rblist
, new_entry
);
67 int strlist__load(struct strlist
*slist
, const char *filename
)
71 FILE *fp
= fopen(filename
, "r");
76 while (fgets(entry
, sizeof(entry
), fp
) != NULL
) {
77 const size_t len
= strlen(entry
);
81 entry
[len
- 1] = '\0';
83 err
= strlist__add(slist
, entry
);
94 void strlist__remove(struct strlist
*slist
, struct str_node
*snode
)
96 rblist__remove_node(&slist
->rblist
, &snode
->rb_node
);
99 struct str_node
*strlist__find(struct strlist
*slist
, const char *entry
)
101 struct str_node
*snode
= NULL
;
102 struct rb_node
*rb_node
= rblist__find(&slist
->rblist
, entry
);
105 snode
= container_of(rb_node
, struct str_node
, rb_node
);
110 static int strlist__parse_list_entry(struct strlist
*slist
, const char *s
)
112 if (strncmp(s
, "file://", 7) == 0)
113 return strlist__load(slist
, s
+ 7);
115 return strlist__add(slist
, s
);
118 int strlist__parse_list(struct strlist
*slist
, const char *s
)
123 while ((sep
= strchr(s
, ',')) != NULL
) {
125 err
= strlist__parse_list_entry(slist
, s
);
132 return *s
? strlist__parse_list_entry(slist
, s
) : 0;
135 struct strlist
*strlist__new(bool dupstr
, const char *list
)
137 struct strlist
*slist
= malloc(sizeof(*slist
));
140 rblist__init(&slist
->rblist
);
141 slist
->rblist
.node_cmp
= strlist__node_cmp
;
142 slist
->rblist
.node_new
= strlist__node_new
;
143 slist
->rblist
.node_delete
= strlist__node_delete
;
145 slist
->dupstr
= dupstr
;
146 if (list
&& strlist__parse_list(slist
, list
) != 0)
156 void strlist__delete(struct strlist
*slist
)
159 rblist__delete(&slist
->rblist
);
162 struct str_node
*strlist__entry(const struct strlist
*slist
, unsigned int idx
)
164 struct str_node
*snode
= NULL
;
165 struct rb_node
*rb_node
;
167 rb_node
= rblist__entry(&slist
->rblist
, idx
);
169 snode
= container_of(rb_node
, struct str_node
, rb_node
);