aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2009-02-11 18:00:41 +0100
committerMax Kellermann <max@duempel.org>2009-02-11 18:00:41 +0100
commit5090cf6484f5e7464aeba54d19500cc334a80fad (patch)
tree1e6cc079a0b23128a529aea3f6042581027828c9
parent5484aaee5f9407e61d77e81236b4942c12b94556 (diff)
downloadmpd-5090cf6484f5e7464aeba54d19500cc334a80fad.tar.gz
mpd-5090cf6484f5e7464aeba54d19500cc334a80fad.tar.xz
mpd-5090cf6484f5e7464aeba54d19500cc334a80fad.zip
audio: replaced parseAudioConfig() with audio_format_parse()
Added audio_format_parse() in a separate library, with a modern interface: return a GError instead of logging errors. This allows the caller to deal with the error.
-rw-r--r--src/Makefile.am2
-rw-r--r--src/audio.c65
-rw-r--r--src/audio.h2
-rw-r--r--src/audio_parser.c98
-rw-r--r--src/audio_parser.h36
-rw-r--r--src/output_init.c13
6 files changed, 153 insertions, 63 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 98fe8bea7..2e7b769d0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -35,6 +35,7 @@ mpd_headers = \
ack.h \
audio.h \
audio_format.h \
+ audio_parser.h \
audioOutput.h \
output_internal.h \
output_api.h \
@@ -136,6 +137,7 @@ mpd_SOURCES = \
$(mpd_headers) \
notify.c \
audio.c \
+ audio_parser.c \
audioOutput.c \
output_api.c \
output_list.c \
diff --git a/src/audio.c b/src/audio.c
index d3e66fdb2..eef8d2de8 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -18,6 +18,7 @@
#include "audio.h"
#include "audio_format.h"
+#include "audio_parser.h"
#include "output_api.h"
#include "output_control.h"
#include "output_internal.h"
@@ -44,67 +45,17 @@ void getOutputAudioFormat(const struct audio_format *inAudioFormat,
void initAudioConfig(void)
{
const struct config_param *param = config_get_param(CONF_AUDIO_OUTPUT_FORMAT);
+ GError *error = NULL;
+ bool ret;
if (NULL == param || NULL == param->value)
return;
- if (0 != parseAudioConfig(&configured_audio_format, param->value)) {
- g_error("error parsing \"%s\" at line %i\n",
- CONF_AUDIO_OUTPUT_FORMAT, param->line);
- }
-}
-
-int parseAudioConfig(struct audio_format *audioFormat, char *conf)
-{
- char *test;
-
- memset(audioFormat, 0, sizeof(*audioFormat));
-
- audioFormat->sample_rate = strtol(conf, &test, 10);
-
- if (*test != ':') {
- g_warning("error parsing audio output format: %s\n", conf);
- return -1;
- }
-
- if (audioFormat->sample_rate <= 0) {
- g_warning("sample rate %u is not >= 0\n",
- audioFormat->sample_rate);
- return -1;
- }
-
- audioFormat->bits = (uint8_t)strtoul(test + 1, &test, 10);
-
- if (*test != ':') {
- g_warning("error parsing audio output format: %s\n", conf);
- return -1;
- }
-
- if (audioFormat->bits != 16 && audioFormat->bits != 24 &&
- audioFormat->bits != 8) {
- g_warning("bits %u can not be used for audio output\n",
- audioFormat->bits);
- return -1;
- }
-
- audioFormat->channels = (uint8_t)strtoul(test + 1, &test, 10);
-
- if (*test != '\0') {
- g_warning("error parsing audio output format: %s\n", conf);
- return -1;
- }
-
- switch (audioFormat->channels) {
- case 1:
- case 2:
- break;
- default:
- g_warning("channels %u can not be used for audio output\n",
- audioFormat->channels);
- return -1;
- }
-
- return 0;
+ ret = audio_format_parse(&configured_audio_format, param->value,
+ &error);
+ if (!ret)
+ g_error("error parsing \"%s\" at line %i: %s",
+ CONF_AUDIO_OUTPUT_FORMAT, param->line, error->message);
}
void finishAudioConfig(void)
diff --git a/src/audio.h b/src/audio.h
index 865890485..588655b05 100644
--- a/src/audio.h
+++ b/src/audio.h
@@ -26,8 +26,6 @@ struct audio_format;
void getOutputAudioFormat(const struct audio_format *inFormat,
struct audio_format *outFormat);
-int parseAudioConfig(struct audio_format *audioFormat, char *conf);
-
/* make sure initPlayerData is called before this function!! */
void initAudioConfig(void);
diff --git a/src/audio_parser.c b/src/audio_parser.c
new file mode 100644
index 000000000..807a279b3
--- /dev/null
+++ b/src/audio_parser.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2003-2009 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ * Parser functions for audio related objects.
+ *
+ */
+
+#include "audio_parser.h"
+#include "audio_format.h"
+
+#include <stdlib.h>
+
+/**
+ * The GLib quark used for errors reported by this library.
+ */
+static inline GQuark
+audio_parser_quark(void)
+{
+ return g_quark_from_static_string("audio_parser");
+}
+
+bool
+audio_format_parse(struct audio_format *dest, const char *src, GError **error)
+{
+ char *endptr;
+ unsigned long value;
+
+ audio_format_clear(dest);
+
+ /* parse sample rate */
+
+ value = strtoul(src, &endptr, 10);
+ if (endptr == src) {
+ g_set_error(error, audio_parser_quark(), 0,
+ "Sample rate missing");
+ return false;
+ } else if (*endptr != ':') {
+ g_set_error(error, audio_parser_quark(), 0,
+ "Sample format missing");
+ return false;
+ } else if (value <= 0 || value > G_MAXINT32) {
+ g_set_error(error, audio_parser_quark(), 0,
+ "Invalid sample rate: %lu", value);
+ return false;
+ }
+
+ dest->sample_rate = value;
+
+ /* parse sample format */
+
+ src = endptr + 1;
+ value = strtoul(src, &endptr, 10);
+ if (endptr == src) {
+ g_set_error(error, audio_parser_quark(), 0,
+ "Sample format missing");
+ return false;
+ } else if (*endptr != ':') {
+ g_set_error(error, audio_parser_quark(), 0,
+ "Channel count missing");
+ return false;
+ } else if (value != 16 && value != 24 && value != 8) {
+ g_set_error(error, audio_parser_quark(), 0,
+ "Invalid sample format: %lu", value);
+ return false;
+ }
+
+ dest->bits = value;
+
+ /* parse channel count */
+
+ src = endptr + 1;
+ value = strtoul(src, &endptr, 10);
+ if (*endptr != 0 || (value != 1 && value != 2)) {
+ g_set_error(error, audio_parser_quark(), 0,
+ "Invalid channel count: %s", src);
+ return false;
+ }
+
+ dest->channels = value;
+
+ return true;
+}
diff --git a/src/audio_parser.h b/src/audio_parser.h
new file mode 100644
index 000000000..251ce7cef
--- /dev/null
+++ b/src/audio_parser.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2003-2009 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ * Parser functions for audio related objects.
+ *
+ */
+
+#ifndef AUDIO_PARSER_H
+#define AUDIO_PARSER_H
+
+#include <glib.h>
+
+#include <stdbool.h>
+
+struct audio_format;
+
+bool
+audio_format_parse(struct audio_format *dest, const char *src, GError **error);
+
+#endif
diff --git a/src/output_init.c b/src/output_init.c
index a3231f4d2..b7b088396 100644
--- a/src/output_init.c
+++ b/src/output_init.c
@@ -20,7 +20,7 @@
#include "output_api.h"
#include "output_internal.h"
#include "output_list.h"
-#include "audio.h"
+#include "audio_parser.h"
#include <glib.h>
@@ -94,9 +94,14 @@ audio_output_init(struct audio_output *ao, const struct config_param *param)
pcm_convert_init(&ao->convert_state);
if (format) {
- if (0 != parseAudioConfig(&ao->config_audio_format, format)) {
- g_error("error parsing format at line %i\n", bp->line);
- }
+ GError *error = NULL;
+ bool ret;
+
+ ret = audio_format_parse(&ao->config_audio_format, format,
+ &error);
+ if (!ret)
+ g_error("error parsing format at line %i: %s",
+ bp->line, error->message);
} else
audio_format_clear(&ao->config_audio_format);