]>
code.delx.au - pulseaudio/blob - src/pulsecore/conf-parser.c
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio 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 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
37 #include "conf-parser.h"
39 #define WHITESPACE " \t\n"
40 #define COMMENTS "#;\n"
42 /* Run the user supplied parser for an assignment */
43 static int next_assignment(const char *filename
, unsigned line
, const pa_config_item
*t
, const char *lvalue
, const char *rvalue
, void *userdata
) {
50 if (!strcmp(lvalue
, t
->lvalue
))
51 return t
->parse(filename
, line
, lvalue
, rvalue
, t
->data
, userdata
);
53 pa_log("[%s:%u] Unknown lvalue '%s'.", filename
, line
, lvalue
);
58 /* Returns non-zero when c is contained in s */
59 static int in_string(char c
, const char *s
) {
69 /* Remove all whitepsapce from the beginning and the end of *s. *s may
71 static char *strip(char *s
) {
72 char *b
= s
+strspn(s
, WHITESPACE
);
76 if (!in_string(*e
, WHITESPACE
))
85 /* Parse a variable assignment line */
86 static int parse_line(const char *filename
, unsigned line
, const pa_config_item
*t
, char *l
, void *userdata
) {
87 char *e
, *c
, *b
= l
+strspn(l
, WHITESPACE
);
89 if ((c
= strpbrk(b
, COMMENTS
)))
95 if (!(e
= strchr(b
, '='))) {
96 pa_log("[%s:%u] Missing '='.", filename
, line
);
103 return next_assignment(filename
, line
, t
, strip(b
), strip(e
), userdata
);
106 /* Go through the file and parse each line */
107 int pa_config_parse(const char *filename
, FILE *f
, const pa_config_item
*t
, void *userdata
) {
115 if (!f
&& !(f
= fopen(filename
, "r"))) {
116 if (errno
== ENOENT
) {
121 pa_log_warn("Failed to open configuration file '%s': %s",
122 filename
, pa_cstrerror(errno
));
128 if (!fgets(l
, sizeof(l
), f
)) {
132 pa_log_warn("Failed to read configuration file '%s': %s",
133 filename
, pa_cstrerror(errno
));
137 if (parse_line(filename
, ++line
, t
, l
, userdata
) < 0)
151 int pa_config_parse_int(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
160 if (pa_atoi(rvalue
, &k
) < 0) {
161 pa_log("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
169 int pa_config_parse_size(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
178 if (pa_atou(rvalue
, &k
) < 0) {
179 pa_log("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
187 int pa_config_parse_bool(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
196 if ((k
= pa_parse_boolean(rvalue
)) < 0) {
197 pa_log("[%s:%u] Failed to parse boolean value: %s", filename
, line
, rvalue
);
206 int pa_config_parse_string(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
215 *s
= *rvalue
? pa_xstrdup(rvalue
) : NULL
;