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
26 #include <avahi-common/malloc.h>
27 #include <avahi-core/log.h>
29 #include "ini-file-parser.h"
31 AvahiIniFile* avahi_ini_file_load(const char *fname) {
34 AvahiIniFileGroup *group = NULL;
39 if (!(fo = fopen(fname, "r"))) {
40 avahi_log_error("Failed to open file '%s': %s", fname, strerror(errno));
44 f = avahi_new(AvahiIniFile, 1);
45 AVAHI_LLIST_HEAD_INIT(AvahiIniFileGroup, f->groups);
51 AvahiIniFilePair *pair;
53 if (!(fgets(ln, sizeof(ln), fo)))
58 s = ln + strspn(ln, " \t");
59 s[strcspn(s, "\r\n")] = 0;
61 /* Skip comments and empty lines */
62 if (*s == '#' || *s == '%' || *s == 0)
68 if (!(e = strchr(s, ']'))) {
69 avahi_log_error("Unclosed group header in %s:%u: <%s>", fname, line, s);
75 group = avahi_new(AvahiIniFileGroup, 1);
76 group->name = avahi_strdup(s+1);
78 AVAHI_LLIST_HEAD_INIT(AvahiIniFilePair, group->pairs);
80 AVAHI_LLIST_PREPEND(AvahiIniFileGroup, groups, f->groups, group);
84 /* Normal assignment */
85 if (!(e = strchr(s, '='))) {
86 avahi_log_error("Missing assignment in %s:%u: <%s>", fname, line, s);
91 avahi_log_error("Assignment outside group in %s:%u <%s>", fname, line, s);
95 /* Split the key and the value */
98 pair = avahi_new(AvahiIniFilePair, 1);
99 pair->key = avahi_strdup(s);
100 pair->value = avahi_strdup(e);
102 AVAHI_LLIST_PREPEND(AvahiIniFilePair, pairs, group->pairs, pair);
117 avahi_ini_file_free(f);
122 void avahi_ini_file_free(AvahiIniFile *f) {
123 AvahiIniFileGroup *g;
126 while ((g = f->groups)) {
129 while ((p = g->pairs)) {
131 avahi_free(p->value);
133 AVAHI_LLIST_REMOVE(AvahiIniFilePair, pairs, g->pairs, p);
139 AVAHI_LLIST_REMOVE(AvahiIniFileGroup, groups, f->groups, g);
146 char** avahi_split_csv(const char *t) {
147 unsigned n_comma = 0;
155 i = r = avahi_new(char*, n_comma+2);
158 size_t l = strcspn(t, ",");
160 *(i++) = avahi_strndup(t, l);
176 void avahi_strfreev(char **p) {