]> git.meshlink.io Git - catta/blob - avahi-daemon/ini-file-parser.c
* implement ini file parser
[catta] / avahi-daemon / ini-file-parser.c
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 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <avahi-common/malloc.h>
27 #include <avahi-core/log.h>
28
29 #include "ini-file-parser.h"
30
31 AvahiIniFile* avahi_ini_file_load(const char *fname) {
32     AvahiIniFile *f;
33     FILE *fo;
34     AvahiIniFileGroup *group = NULL;
35     unsigned line;
36     
37     assert(fname);
38
39     if (!(fo = fopen(fname, "r"))) {
40         avahi_log_error("Failed to open file '%s': %s", fname, strerror(errno));
41         return NULL;
42     }
43     
44     f = avahi_new(AvahiIniFile, 1);
45     AVAHI_LLIST_HEAD_INIT(AvahiIniFileGroup, f->groups);
46     f->n_groups = 0;
47
48     line = 0;
49     while (!feof(fo)) {
50         char ln[256], *s, *e;
51         AvahiIniFilePair *pair;
52         
53         if (!(fgets(ln, sizeof(ln), fo)))
54             break;
55
56         line++;
57         
58         s = ln + strspn(ln, " \t");
59         s[strcspn(s, "\r\n")] = 0;
60
61         /* Skip comments and empty lines */
62         if (*s == '#' || *s == '%' || *s == 0)
63             continue;
64
65         if (*s == '[') {
66             /* new group */
67             
68             if (!(e = strchr(s, ']'))) {
69                 avahi_log_error("Unclosed group header in %s:%u: <%s>", fname, line, s);
70                 goto fail;
71             }
72
73             *e = 0;
74             
75             group = avahi_new(AvahiIniFileGroup, 1);
76             group->name = avahi_strdup(s+1);
77             group->n_pairs = 0;
78             AVAHI_LLIST_HEAD_INIT(AvahiIniFilePair, group->pairs);
79             
80             AVAHI_LLIST_PREPEND(AvahiIniFileGroup, groups, f->groups, group);
81             f->n_groups++;
82         } else {
83
84             /* Normal assignment */
85             if (!(e = strchr(s, '='))) {
86                 avahi_log_error("Missing assignment in %s:%u: <%s>", fname, line, s);
87                 goto fail;
88             }
89             
90             if (!group) {
91                 avahi_log_error("Assignment outside group in %s:%u <%s>", fname, line, s);
92                 goto fail;
93             }
94             
95             /* Split the key and the value */
96             *(e++) = 0;
97             
98             pair = avahi_new(AvahiIniFilePair, 1);
99             pair->key = avahi_strdup(s);
100             pair->value = avahi_strdup(e);
101             
102             AVAHI_LLIST_PREPEND(AvahiIniFilePair, pairs, group->pairs, pair);
103             group->n_pairs++;
104         }
105     }
106     
107     fclose(fo);
108         
109     return f;
110
111 fail:
112
113     if (fo)
114         fclose(fo);
115
116     if (f)
117         avahi_ini_file_free(f);
118
119     return NULL;
120 }
121
122 void avahi_ini_file_free(AvahiIniFile *f) {
123     AvahiIniFileGroup *g;
124     assert(f);
125
126     while ((g = f->groups)) {
127         AvahiIniFilePair *p;
128         
129         while ((p = g->pairs)) {
130             avahi_free(p->key);
131             avahi_free(p->value);
132
133             AVAHI_LLIST_REMOVE(AvahiIniFilePair, pairs, g->pairs, p);
134             avahi_free(p);
135         }
136
137         avahi_free(g->name);
138
139         AVAHI_LLIST_REMOVE(AvahiIniFileGroup, groups, f->groups, g);
140         avahi_free(g);
141     }
142
143     avahi_free(f);
144 }
145
146 char** avahi_split_csv(const char *t) {
147     unsigned n_comma = 0;
148     const char *p;
149     char **r, **i;
150
151     for (p = t; *p; p++)
152         if (*p == ',')
153             n_comma++;
154
155     i = r = avahi_new(char*, n_comma+2);
156
157     for (;;) {
158         size_t l = strcspn(t, ",");
159
160         *(i++) = avahi_strndup(t, l);
161
162         t += l;
163
164         if (*t == 0)
165             break;
166
167         assert(*t == ',');
168         t++;
169     }
170
171     *i = NULL;
172
173     return r;
174 }
175
176 void avahi_strfreev(char **p) {
177     char **i;
178
179     if (!p)
180         return;
181     
182     for (i = p; *i; i++)
183         avahi_free(*i);
184
185     avahi_free(p);
186 }