2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <avahi-common/malloc.h>
30 #include <avahi-core/log.h>
32 #include "ini-file-parser.h"
34 AvahiIniFile* avahi_ini_file_load(const char *fname) {
37 AvahiIniFileGroup *group = NULL;
42 if (!(fo = fopen(fname, "r"))) {
43 avahi_log_error("Failed to open file '%s': %s", fname, strerror(errno));
47 f = avahi_new(AvahiIniFile, 1);
48 AVAHI_LLIST_HEAD_INIT(AvahiIniFileGroup, f->groups);
54 AvahiIniFilePair *pair;
56 if (!(fgets(ln, sizeof(ln), fo)))
61 s = ln + strspn(ln, " \t");
62 s[strcspn(s, "\r\n")] = 0;
64 /* Skip comments and empty lines */
65 if (*s == '#' || *s == '%' || *s == 0)
71 if (!(e = strchr(s, ']'))) {
72 avahi_log_error("Unclosed group header in %s:%u: <%s>", fname, line, s);
78 group = avahi_new(AvahiIniFileGroup, 1);
79 group->name = avahi_strdup(s+1);
81 AVAHI_LLIST_HEAD_INIT(AvahiIniFilePair, group->pairs);
83 AVAHI_LLIST_PREPEND(AvahiIniFileGroup, groups, f->groups, group);
87 /* Normal assignment */
88 if (!(e = strchr(s, '='))) {
89 avahi_log_error("Missing assignment in %s:%u: <%s>", fname, line, s);
94 avahi_log_error("Assignment outside group in %s:%u <%s>", fname, line, s);
98 /* Split the key and the value */
101 pair = avahi_new(AvahiIniFilePair, 1);
102 pair->key = avahi_strdup(s);
103 pair->value = avahi_strdup(e);
105 AVAHI_LLIST_PREPEND(AvahiIniFilePair, pairs, group->pairs, pair);
120 avahi_ini_file_free(f);
125 void avahi_ini_file_free(AvahiIniFile *f) {
126 AvahiIniFileGroup *g;
129 while ((g = f->groups)) {
132 while ((p = g->pairs)) {
134 avahi_free(p->value);
136 AVAHI_LLIST_REMOVE(AvahiIniFilePair, pairs, g->pairs, p);
142 AVAHI_LLIST_REMOVE(AvahiIniFileGroup, groups, f->groups, g);
149 char** avahi_split_csv(const char *t) {
150 unsigned n_comma = 0;
158 i = r = avahi_new(char*, n_comma+2);
161 size_t n, l = strcspn(t, ",");
164 /* Ignore leading blanks */
165 for (c = t, n = l; isblank(*c); c++, n--);
167 /* Ignore trailing blanks */
168 for (; n > 0 && isblank(c[n-1]); n--);
170 *(i++) = avahi_strndup(c, n);
186 void avahi_strfreev(char **p) {