Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 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 <errno.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 13 | static |
| 14 | struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry) |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 15 | { |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 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)); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 20 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 21 | if (snode != NULL) { |
| 22 | if (strlist->dupstr) { |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 23 | s = strdup(s); |
| 24 | if (s == NULL) |
| 25 | goto out_delete; |
| 26 | } |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 27 | snode->s = s; |
| 28 | rc = &snode->rb_node; |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 29 | } |
| 30 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 31 | return rc; |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 32 | |
| 33 | out_delete: |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 34 | free(snode); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | static 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 Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 45 | static |
| 46 | void 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 | |
| 54 | static 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 Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 62 | int strlist__add(struct strlist *self, const char *new_entry) |
| 63 | { |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 64 | return rblist__add_node(&self->rblist, new_entry); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int 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; |
| 89 | out: |
| 90 | fclose(fp); |
| 91 | return err; |
| 92 | } |
| 93 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 94 | void strlist__remove(struct strlist *slist, struct str_node *snode) |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 95 | { |
Suzuki K. Poulose | 4592281 | 2012-08-29 11:30:07 +0530 | [diff] [blame^] | 96 | rblist__remove_node(&slist->rblist, &snode->rb_node); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 97 | } |
| 98 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 99 | struct str_node *strlist__find(struct strlist *slist, const char *entry) |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 100 | { |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 101 | struct str_node *snode = NULL; |
| 102 | struct rb_node *rb_node = rblist__find(&slist->rblist, entry); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 103 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 104 | if (rb_node) |
| 105 | snode = container_of(rb_node, struct str_node, rb_node); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 106 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 107 | return snode; |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static 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 | |
| 118 | int 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 | |
| 135 | struct strlist *strlist__new(bool dupstr, const char *slist) |
| 136 | { |
| 137 | struct strlist *self = malloc(sizeof(*self)); |
| 138 | |
| 139 | if (self != NULL) { |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 140 | 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 Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 145 | self->dupstr = dupstr; |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 146 | if (slist && strlist__parse_list(self, slist) != 0) |
| 147 | goto out_error; |
| 148 | } |
| 149 | |
| 150 | return self; |
| 151 | out_error: |
| 152 | free(self); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | void strlist__delete(struct strlist *self) |
| 157 | { |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 158 | if (self != NULL) |
| 159 | rblist__delete(&self->rblist); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 160 | } |
Arnaldo Carvalho de Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 161 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 162 | struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx) |
Arnaldo Carvalho de Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 163 | { |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 164 | struct str_node *snode = NULL; |
| 165 | struct rb_node *rb_node; |
Arnaldo Carvalho de Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 166 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 167 | 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 Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 170 | |
David Ahern | ee8dd3c | 2012-07-30 22:31:33 -0600 | [diff] [blame] | 171 | return snode; |
Arnaldo Carvalho de Melo | 27d0fd4 | 2009-07-11 12:18:34 -0300 | [diff] [blame] | 172 | } |