]> code.delx.au - pulseaudio/blob - src/polypcore/conf-parser.c
Cleaned up the includes after the restructuring. Indicate which headers are
[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 <polypcore/log.h>
32 #include <polypcore/util.h>
33 #include <polypcore/xmalloc.h>
34
35 #include "conf-parser.h"
36
37 #define WHITESPACE " \t\n"
38 #define COMMENTS "#;\n"
39
40 /* Run the user supplied parser for an assignment */
41 static int next_assignment(const char *filename, unsigned line, const pa_config_item *t, const char *lvalue, const char *rvalue, void *userdata) {
42 assert(filename && t && lvalue && rvalue);
43
44 for (; t->parse; t++)
45 if (!strcmp(lvalue, t->lvalue))
46 return t->parse(filename, line, lvalue, rvalue, t->data, userdata);
47
48 pa_log(__FILE__": [%s:%u] Unknown lvalue '%s'.\n", filename, line, lvalue);
49
50 return -1;
51 }
52
53 /* Returns non-zero when c is contained in s */
54 static int in_string(char c, const char *s) {
55 assert(s);
56
57 for (; *s; s++)
58 if (*s == c)
59 return 1;
60
61 return 0;
62 }
63
64 /* Remove all whitepsapce from the beginning and the end of *s. *s may
65 * be modified. */
66 static char *strip(char *s) {
67 char *b = s+strspn(s, WHITESPACE);
68 char *e, *l = NULL;
69
70 for (e = b; *e; e++)
71 if (!in_string(*e, WHITESPACE))
72 l = e;
73
74 if (l)
75 *(l+1) = 0;
76
77 return b;
78 }
79
80 /* Parse a variable assignment line */
81 static int parse_line(const char *filename, unsigned line, const pa_config_item *t, char *l, void *userdata) {
82 char *e, *c, *b = l+strspn(l, WHITESPACE);
83
84 if ((c = strpbrk(b, COMMENTS)))
85 *c = 0;
86
87 if (!*b)
88 return 0;
89
90 if (!(e = strchr(b, '='))) {
91 pa_log(__FILE__": [%s:%u] Missing '='.\n", filename, line);
92 return -1;
93 }
94
95 *e = 0;
96 e++;
97
98 return next_assignment(filename, line, t, strip(b), strip(e), userdata);
99 }
100
101 /* Go through the file and parse each line */
102 int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, void *userdata) {
103 int r = -1;
104 unsigned line = 0;
105 int do_close = !f;
106 assert(filename && t);
107
108 if (!f && !(f = fopen(filename, "r"))) {
109 if (errno == ENOENT) {
110 r = 0;
111 goto finish;
112 }
113
114 pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s\n", filename, strerror(errno));
115 goto finish;
116 }
117
118 while (!feof(f)) {
119 char l[256];
120 if (!fgets(l, sizeof(l), f)) {
121 if (feof(f))
122 break;
123
124 pa_log(__FILE__": WARNING: failed to read configuration file '%s': %s\n", filename, strerror(errno));
125 goto finish;
126 }
127
128 if (parse_line(filename, ++line, t, l, userdata) < 0)
129 goto finish;
130 }
131
132 r = 0;
133
134 finish:
135
136 if (do_close && f)
137 fclose(f);
138
139 return r;
140 }
141
142 int pa_config_parse_int(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
143 int *i = data;
144 int32_t k;
145 assert(filename && lvalue && rvalue && data);
146
147 if (pa_atoi(rvalue, &k) < 0) {
148 pa_log(__FILE__": [%s:%u] Failed to parse numeric value: %s\n", filename, line, rvalue);
149 return -1;
150 }
151
152 *i = (int) k;
153 return 0;
154 }
155
156 int pa_config_parse_bool(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
157 int *b = data, k;
158 assert(filename && lvalue && rvalue && data);
159
160 if ((k = pa_parse_boolean(rvalue)) < 0) {
161 pa_log(__FILE__": [%s:%u] Failed to parse boolean value: %s\n", filename, line, rvalue);
162 return -1;
163 }
164
165 *b = k;
166
167 return 0;
168 }
169
170 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) {
171 char **s = data;
172 assert(filename && lvalue && rvalue && data);
173
174 pa_xfree(*s);
175 *s = *rvalue ? pa_xstrdup(rvalue) : NULL;
176 return 0;
177 }