]> code.delx.au - pulseaudio/blob - src/polypcore/conf-parser.c
d59bc55568b74f5d101dbd391b8e4e7cad809322
[pulseaudio] / src / polypcore / conf-parser.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; 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 <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 #include <polyp/xmalloc.h>
32
33 #include <polypcore/core-error.h>
34 #include <polypcore/log.h>
35 #include <polypcore/core-util.h>
36
37 #include "conf-parser.h"
38
39 #define WHITESPACE " \t\n"
40 #define COMMENTS "#;\n"
41
42 /* Run the user supplied parser for an assignment */
43 static int next_assignment(const char *filename, unsigned line, const pa_config_item *t, const char *lvalue, const char *rvalue, void *userdata) {
44 assert(filename && t && lvalue && rvalue);
45
46 for (; t->parse; t++)
47 if (!strcmp(lvalue, t->lvalue))
48 return t->parse(filename, line, lvalue, rvalue, t->data, userdata);
49
50 pa_log(__FILE__": [%s:%u] Unknown lvalue '%s'.", filename, line, lvalue);
51
52 return -1;
53 }
54
55 /* Returns non-zero when c is contained in s */
56 static int in_string(char c, const char *s) {
57 assert(s);
58
59 for (; *s; s++)
60 if (*s == c)
61 return 1;
62
63 return 0;
64 }
65
66 /* Remove all whitepsapce from the beginning and the end of *s. *s may
67 * be modified. */
68 static char *strip(char *s) {
69 char *b = s+strspn(s, WHITESPACE);
70 char *e, *l = NULL;
71
72 for (e = b; *e; e++)
73 if (!in_string(*e, WHITESPACE))
74 l = e;
75
76 if (l)
77 *(l+1) = 0;
78
79 return b;
80 }
81
82 /* Parse a variable assignment line */
83 static int parse_line(const char *filename, unsigned line, const pa_config_item *t, char *l, void *userdata) {
84 char *e, *c, *b = l+strspn(l, WHITESPACE);
85
86 if ((c = strpbrk(b, COMMENTS)))
87 *c = 0;
88
89 if (!*b)
90 return 0;
91
92 if (!(e = strchr(b, '='))) {
93 pa_log(__FILE__": [%s:%u] Missing '='.", filename, line);
94 return -1;
95 }
96
97 *e = 0;
98 e++;
99
100 return next_assignment(filename, line, t, strip(b), strip(e), userdata);
101 }
102
103 /* Go through the file and parse each line */
104 int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, void *userdata) {
105 int r = -1;
106 unsigned line = 0;
107 int do_close = !f;
108 assert(filename && t);
109
110 if (!f && !(f = fopen(filename, "r"))) {
111 if (errno == ENOENT) {
112 r = 0;
113 goto finish;
114 }
115
116 pa_log_warn(__FILE__": WARNING: failed to open configuration file '%s': %s",
117 filename, pa_cstrerror(errno));
118 goto finish;
119 }
120
121 while (!feof(f)) {
122 char l[256];
123 if (!fgets(l, sizeof(l), f)) {
124 if (feof(f))
125 break;
126
127 pa_log_warn(__FILE__": WARNING: failed to read configuration file '%s': %s",
128 filename, pa_cstrerror(errno));
129 goto finish;
130 }
131
132 if (parse_line(filename, ++line, t, l, userdata) < 0)
133 goto finish;
134 }
135
136 r = 0;
137
138 finish:
139
140 if (do_close && f)
141 fclose(f);
142
143 return r;
144 }
145
146 int pa_config_parse_int(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
147 int *i = data;
148 int32_t k;
149 assert(filename && lvalue && rvalue && data);
150
151 if (pa_atoi(rvalue, &k) < 0) {
152 pa_log(__FILE__": [%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
153 return -1;
154 }
155
156 *i = (int) k;
157 return 0;
158 }
159
160 int pa_config_parse_bool(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
161 int *b = data, k;
162 assert(filename && lvalue && rvalue && data);
163
164 if ((k = pa_parse_boolean(rvalue)) < 0) {
165 pa_log(__FILE__": [%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
166 return -1;
167 }
168
169 *b = k;
170
171 return 0;
172 }
173
174 int pa_config_parse_string(const char *filename, PA_GCC_UNUSED unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
175 char **s = data;
176 assert(filename && lvalue && rvalue && data);
177
178 pa_xfree(*s);
179 *s = *rvalue ? pa_xstrdup(rvalue) : NULL;
180 return 0;
181 }