]>
code.delx.au - pulseaudio/blob - src/pulsecore/conf-parser.c
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/core-error.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/macro.h>
39 #include "conf-parser.h"
41 #define WHITESPACE " \t\n"
42 #define COMMENTS "#;\n"
44 /* Run the user supplied parser for an assignment */
45 static int next_assignment(const char *filename
, unsigned line
, const pa_config_item
*t
, const char *lvalue
, const char *rvalue
, void *userdata
) {
52 if (!strcmp(lvalue
, t
->lvalue
))
53 return t
->parse(filename
, line
, lvalue
, rvalue
, t
->data
, userdata
);
55 pa_log("[%s:%u] Unknown lvalue '%s'.", filename
, line
, lvalue
);
60 /* Returns non-zero when c is contained in s */
61 static int in_string(char c
, const char *s
) {
71 /* Remove all whitepsapce from the beginning and the end of *s. *s may
73 static char *strip(char *s
) {
74 char *b
= s
+strspn(s
, WHITESPACE
);
78 if (!in_string(*e
, WHITESPACE
))
87 /* Parse a variable assignment line */
88 static int parse_line(const char *filename
, unsigned line
, const pa_config_item
*t
, char *l
, void *userdata
) {
89 char *e
, *c
, *b
= l
+strspn(l
, WHITESPACE
);
91 if ((c
= strpbrk(b
, COMMENTS
)))
97 if (!(e
= strchr(b
, '='))) {
98 pa_log("[%s:%u] Missing '='.", filename
, line
);
105 return next_assignment(filename
, line
, t
, strip(b
), strip(e
), userdata
);
108 /* Go through the file and parse each line */
109 int pa_config_parse(const char *filename
, FILE *f
, const pa_config_item
*t
, void *userdata
) {
117 if (!f
&& !(f
= fopen(filename
, "r"))) {
118 if (errno
== ENOENT
) {
123 pa_log_warn("Failed to open configuration file '%s': %s",
124 filename
, pa_cstrerror(errno
));
130 if (!fgets(l
, sizeof(l
), f
)) {
134 pa_log_warn("Failed to read configuration file '%s': %s",
135 filename
, pa_cstrerror(errno
));
139 if (parse_line(filename
, ++line
, t
, l
, userdata
) < 0)
153 int pa_config_parse_int(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
162 if (pa_atoi(rvalue
, &k
) < 0) {
163 pa_log("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
171 int pa_config_parse_bool(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
179 if ((k
= pa_parse_boolean(rvalue
)) < 0) {
180 pa_log("[%s:%u] Failed to parse boolean value: %s", filename
, line
, rvalue
);
189 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
) {
198 *s
= *rvalue
? pa_xstrdup(rvalue
) : NULL
;