]>
code.delx.au - pulseaudio/blob - src/polypcore/conf-parser.c
4 This file is part of polypaudio.
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.
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.
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
31 #include <polypcore/log.h>
32 #include <polypcore/util.h>
33 #include <polypcore/xmalloc.h>
35 #include "conf-parser.h"
37 #define WHITESPACE " \t\n"
38 #define COMMENTS "#;\n"
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
);
45 if (!strcmp(lvalue
, t
->lvalue
))
46 return t
->parse(filename
, line
, lvalue
, rvalue
, t
->data
, userdata
);
48 pa_log(__FILE__
": [%s:%u] Unknown lvalue '%s'.", filename
, line
, lvalue
);
53 /* Returns non-zero when c is contained in s */
54 static int in_string(char c
, const char *s
) {
64 /* Remove all whitepsapce from the beginning and the end of *s. *s may
66 static char *strip(char *s
) {
67 char *b
= s
+strspn(s
, WHITESPACE
);
71 if (!in_string(*e
, WHITESPACE
))
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
);
84 if ((c
= strpbrk(b
, COMMENTS
)))
90 if (!(e
= strchr(b
, '='))) {
91 pa_log(__FILE__
": [%s:%u] Missing '='.", filename
, line
);
98 return next_assignment(filename
, line
, t
, strip(b
), strip(e
), userdata
);
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
) {
106 assert(filename
&& t
);
108 if (!f
&& !(f
= fopen(filename
, "r"))) {
109 if (errno
== ENOENT
) {
114 pa_log(__FILE__
": WARNING: failed to open configuration file '%s': %s", filename
, strerror(errno
));
120 if (!fgets(l
, sizeof(l
), f
)) {
124 pa_log(__FILE__
": WARNING: failed to read configuration file '%s': %s", filename
, strerror(errno
));
128 if (parse_line(filename
, ++line
, t
, l
, userdata
) < 0)
142 int pa_config_parse_int(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
145 assert(filename
&& lvalue
&& rvalue
&& data
);
147 if (pa_atoi(rvalue
, &k
) < 0) {
148 pa_log(__FILE__
": [%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
156 int pa_config_parse_bool(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
158 assert(filename
&& lvalue
&& rvalue
&& data
);
160 if ((k
= pa_parse_boolean(rvalue
)) < 0) {
161 pa_log(__FILE__
": [%s:%u] Failed to parse boolean value: %s", filename
, line
, rvalue
);
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
) {
172 assert(filename
&& lvalue
&& rvalue
&& data
);
175 *s
= *rvalue
? pa_xstrdup(rvalue
) : NULL
;