ini-file-parser.c revision a97605e07ad7f44f2f65e15be64880e61a39ab43
1/* $Id$ */ 2 3/*** 4 This file is part of avahi. 5 6 avahi is free software; you can redistribute it and/or modify it 7 under the terms of the GNU Lesser General Public License as 8 published by the Free Software Foundation; either version 2.1 of the 9 License, or (at your option) any later version. 10 11 avahi is distributed in the hope that it will be useful, but WITHOUT 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General 14 Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with avahi; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 19 USA. 20***/ 21 22#ifdef HAVE_CONFIG_H 23#include <config.h> 24#endif 25 26#include <stdio.h> 27#include <string.h> 28#include <errno.h> 29#include <ctype.h> 30 31#include <avahi-common/malloc.h> 32#include <avahi-core/log.h> 33 34#include "ini-file-parser.h" 35 36AvahiIniFile* avahi_ini_file_load(const char *fname) { 37 AvahiIniFile *f; 38 FILE *fo; 39 AvahiIniFileGroup *group = NULL; 40 unsigned line; 41 42 assert(fname); 43 44 if (!(fo = fopen(fname, "r"))) { 45 avahi_log_error("Failed to open file '%s': %s", fname, strerror(errno)); 46 return NULL; 47 } 48 49 f = avahi_new(AvahiIniFile, 1); 50 AVAHI_LLIST_HEAD_INIT(AvahiIniFileGroup, f->groups); 51 f->n_groups = 0; 52 53 line = 0; 54 while (!feof(fo)) { 55 char ln[256], *s, *e; 56 AvahiIniFilePair *pair; 57 58 if (!(fgets(ln, sizeof(ln), fo))) 59 break; 60 61 line++; 62 63 s = ln + strspn(ln, " \t"); 64 s[strcspn(s, "\r\n")] = 0; 65 66 /* Skip comments and empty lines */ 67 if (*s == '#' || *s == '%' || *s == 0) 68 continue; 69 70 if (*s == '[') { 71 /* new group */ 72 73 if (!(e = strchr(s, ']'))) { 74 avahi_log_error("Unclosed group header in %s:%u: <%s>", fname, line, s); 75 goto fail; 76 } 77 78 *e = 0; 79 80 group = avahi_new(AvahiIniFileGroup, 1); 81 group->name = avahi_strdup(s+1); 82 group->n_pairs = 0; 83 AVAHI_LLIST_HEAD_INIT(AvahiIniFilePair, group->pairs); 84 85 AVAHI_LLIST_PREPEND(AvahiIniFileGroup, groups, f->groups, group); 86 f->n_groups++; 87 } else { 88 89 /* Normal assignment */ 90 if (!(e = strchr(s, '='))) { 91 avahi_log_error("Missing assignment in %s:%u: <%s>", fname, line, s); 92 goto fail; 93 } 94 95 if (!group) { 96 avahi_log_error("Assignment outside group in %s:%u <%s>", fname, line, s); 97 goto fail; 98 } 99 100 /* Split the key and the value */ 101 *(e++) = 0; 102 103 pair = avahi_new(AvahiIniFilePair, 1); 104 pair->key = avahi_strdup(s); 105 pair->value = avahi_strdup(e); 106 107 AVAHI_LLIST_PREPEND(AvahiIniFilePair, pairs, group->pairs, pair); 108 group->n_pairs++; 109 } 110 } 111 112 fclose(fo); 113 114 return f; 115 116fail: 117 118 if (fo) 119 fclose(fo); 120 121 if (f) 122 avahi_ini_file_free(f); 123 124 return NULL; 125} 126 127void avahi_ini_file_free(AvahiIniFile *f) { 128 AvahiIniFileGroup *g; 129 assert(f); 130 131 while ((g = f->groups)) { 132 AvahiIniFilePair *p; 133 134 while ((p = g->pairs)) { 135 avahi_free(p->key); 136 avahi_free(p->value); 137 138 AVAHI_LLIST_REMOVE(AvahiIniFilePair, pairs, g->pairs, p); 139 avahi_free(p); 140 } 141 142 avahi_free(g->name); 143 144 AVAHI_LLIST_REMOVE(AvahiIniFileGroup, groups, f->groups, g); 145 avahi_free(g); 146 } 147 148 avahi_free(f); 149} 150 151char** avahi_split_csv(const char *t) { 152 unsigned n_comma = 0; 153 const char *p; 154 char **r, **i; 155 156 for (p = t; *p; p++) 157 if (*p == ',') 158 n_comma++; 159 160 i = r = avahi_new(char*, n_comma+2); 161 162 for (;;) { 163 size_t n, l = strcspn(t, ","); 164 const char *c; 165 166 /* Ignore leading blanks */ 167 for (c = t, n = l; isblank(*c); c++, n--); 168 169 /* Ignore trailing blanks */ 170 for (; n > 0 && isblank(c[n-1]); n--); 171 172 *(i++) = avahi_strndup(c, n); 173 174 t += l; 175 176 if (*t == 0) 177 break; 178 179 assert(*t == ','); 180 t++; 181 } 182 183 *i = NULL; 184 185 return r; 186} 187 188void avahi_strfreev(char **p) { 189 char **i; 190 191 if (!p) 192 return; 193 194 for (i = p; *i; i++) 195 avahi_free(*i); 196 197 avahi_free(p); 198} 199