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