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