4 This file is part of avahi.
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.
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.
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
27 #include <avahi-common/malloc.h>
28 #include <avahi-core/log.h>
30 #include "ini-file-parser.h"
32 AvahiIniFile* avahi_ini_file_load(const char *fname) {
35 AvahiIniFileGroup *group = NULL;
40 if (!(fo = fopen(fname, "r"))) {
41 avahi_log_error("Failed to open file '%s': %s", fname, strerror(errno));
45 f = avahi_new(AvahiIniFile, 1);
46 AVAHI_LLIST_HEAD_INIT(AvahiIniFileGroup, f->groups);
52 AvahiIniFilePair *pair;
54 if (!(fgets(ln, sizeof(ln), fo)))
59 s = ln + strspn(ln, " \t");
60 s[strcspn(s, "\r\n")] = 0;
62 /* Skip comments and empty lines */
63 if (*s == '#' || *s == '%' || *s == 0)
69 if (!(e = strchr(s, ']'))) {
70 avahi_log_error("Unclosed group header in %s:%u: <%s>", fname, line, s);
76 group = avahi_new(AvahiIniFileGroup, 1);
77 group->name = avahi_strdup(s+1);
79 AVAHI_LLIST_HEAD_INIT(AvahiIniFilePair, group->pairs);
81 AVAHI_LLIST_PREPEND(AvahiIniFileGroup, groups, f->groups, group);
85 /* Normal assignment */
86 if (!(e = strchr(s, '='))) {
87 avahi_log_error("Missing assignment in %s:%u: <%s>", fname, line, s);
92 avahi_log_error("Assignment outside group in %s:%u <%s>", fname, line, s);
96 /* Split the key and the value */
99 pair = avahi_new(AvahiIniFilePair, 1);
100 pair->key = avahi_strdup(s);
101 pair->value = avahi_strdup(e);
103 AVAHI_LLIST_PREPEND(AvahiIniFilePair, pairs, group->pairs, pair);
118 avahi_ini_file_free(f);
123 void avahi_ini_file_free(AvahiIniFile *f) {
124 AvahiIniFileGroup *g;
127 while ((g = f->groups)) {
130 while ((p = g->pairs)) {
132 avahi_free(p->value);
134 AVAHI_LLIST_REMOVE(AvahiIniFilePair, pairs, g->pairs, p);
140 AVAHI_LLIST_REMOVE(AvahiIniFileGroup, groups, f->groups, g);
147 char** avahi_split_csv(const char *t) {
148 unsigned n_comma = 0;
156 i = r = avahi_new(char*, n_comma+2);
159 size_t n, l = strcspn(t, ",");
162 /* Ignore leading blanks */
163 for (c = t, n = l; isblank(*c); c++, n--);
165 /* Ignore trailing blanks */
166 for (; n > 0 && isblank(c[n-1]); n--);
168 *(i++) = avahi_strndup(c, n);
184 void avahi_strfreev(char **p) {