From eb8922f3468fe664211911c49077a4e3442b7703 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 29 Jan 2013 17:23:58 +0100 Subject: test/{read_conf,run_filter}: convert to C++ --- test/read_conf.c | 72 ------------------- test/read_conf.cxx | 72 +++++++++++++++++++ test/run_filter.c | 192 -------------------------------------------------- test/run_filter.cxx | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 268 insertions(+), 264 deletions(-) delete mode 100644 test/read_conf.c create mode 100644 test/read_conf.cxx delete mode 100644 test/run_filter.c create mode 100644 test/run_filter.cxx (limited to 'test') diff --git a/test/read_conf.c b/test/read_conf.c deleted file mode 100644 index 4f6005c6f..000000000 --- a/test/read_conf.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2003-2011 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "conf.h" - -#include - -#include - -static void -my_log_func(G_GNUC_UNUSED const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, G_GNUC_UNUSED gpointer user_data) -{ - if (log_level > G_LOG_LEVEL_WARNING) - return; - - g_printerr("%s\n", message); -} - -int main(int argc, char **argv) -{ - if (argc != 3) { - g_printerr("Usage: read_conf FILE SETTING\n"); - return 1; - } - - const char *path = argv[1]; - const char *name = argv[2]; - - g_log_set_default_handler(my_log_func, NULL); - - config_global_init(); - - GError *error = NULL; - bool success = config_read_file(path, &error); - if (!success) { - g_printerr("%s:", error->message); - g_error_free(error); - return 1; - } - - const char *value = config_get_string(name, NULL); - int ret; - if (value != NULL) { - g_print("%s\n", value); - ret = 0; - } else { - g_printerr("No such setting: %s\n", name); - ret = 2; - } - - config_global_finish(); - return ret; -} diff --git a/test/read_conf.cxx b/test/read_conf.cxx new file mode 100644 index 000000000..859c8949d --- /dev/null +++ b/test/read_conf.cxx @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2003-2013 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "conf.h" + +#include + +#include + +static void +my_log_func(G_GNUC_UNUSED const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, G_GNUC_UNUSED gpointer user_data) +{ + if (log_level > G_LOG_LEVEL_WARNING) + return; + + g_printerr("%s\n", message); +} + +int main(int argc, char **argv) +{ + if (argc != 3) { + g_printerr("Usage: read_conf FILE SETTING\n"); + return 1; + } + + const char *path = argv[1]; + const char *name = argv[2]; + + g_log_set_default_handler(my_log_func, NULL); + + config_global_init(); + + GError *error = NULL; + bool success = config_read_file(path, &error); + if (!success) { + g_printerr("%s:", error->message); + g_error_free(error); + return 1; + } + + const char *value = config_get_string(name, NULL); + int ret; + if (value != NULL) { + g_print("%s\n", value); + ret = 0; + } else { + g_printerr("No such setting: %s\n", name); + ret = 2; + } + + config_global_finish(); + return ret; +} diff --git a/test/run_filter.c b/test/run_filter.c deleted file mode 100644 index 7a4bd9d80..000000000 --- a/test/run_filter.c +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (C) 2003-2013 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "conf.h" -#include "audio_parser.h" -#include "audio_format.h" -#include "filter_plugin.h" -#include "pcm_volume.h" -#include "mixer_control.h" -#include "stdbin.h" - -#include - -#include -#include -#include -#include - -bool -mixer_set_volume(G_GNUC_UNUSED struct mixer *mixer, - G_GNUC_UNUSED unsigned volume, G_GNUC_UNUSED GError **error_r) -{ - return true; -} - -static void -my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level, - const gchar *message, G_GNUC_UNUSED gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} - -static const struct config_param * -find_named_config_block(const char *block, const char *name) -{ - const struct config_param *param = NULL; - - while ((param = config_get_next_param(block, param)) != NULL) { - const char *current_name = - config_get_block_string(param, "name", NULL); - if (current_name != NULL && strcmp(current_name, name) == 0) - return param; - } - - return NULL; -} - -static struct filter * -load_filter(const char *name) -{ - const struct config_param *param; - struct filter *filter; - GError *error = NULL; - - param = find_named_config_block("filter", name); - if (param == NULL) { - g_printerr("No such configured filter: %s\n", name); - return false; - } - - filter = filter_configured_new(param, &error); - if (filter == NULL) { - g_printerr("Failed to load filter: %s\n", error->message); - g_error_free(error); - return NULL; - } - - return filter; -} - -int main(int argc, char **argv) -{ - struct audio_format audio_format; - struct audio_format_string af_string; - bool success; - GError *error = NULL; - struct filter *filter; - const struct audio_format *out_audio_format; - char buffer[4096]; - - if (argc < 3 || argc > 4) { - g_printerr("Usage: run_filter CONFIG NAME [FORMAT] message); - g_error_free(error); - return 1; - } - - /* parse the audio format */ - - if (argc > 3) { - success = audio_format_parse(&audio_format, argv[3], - false, &error); - if (!success) { - g_printerr("Failed to parse audio format: %s\n", - error->message); - g_error_free(error); - return 1; - } - } - - /* initialize the filter */ - - filter = load_filter(argv[2]); - if (filter == NULL) - return 1; - - /* open the filter */ - - out_audio_format = filter_open(filter, &audio_format, &error); - if (out_audio_format == NULL) { - g_printerr("Failed to open filter: %s\n", error->message); - g_error_free(error); - filter_free(filter); - return 1; - } - - g_printerr("audio_format=%s\n", - audio_format_to_string(out_audio_format, &af_string)); - - /* play */ - - while (true) { - ssize_t nbytes; - size_t length; - const void *dest; - - nbytes = read(0, buffer, sizeof(buffer)); - if (nbytes <= 0) - break; - - dest = filter_filter(filter, buffer, (size_t)nbytes, - &length, &error); - if (dest == NULL) { - g_printerr("Filter failed: %s\n", error->message); - filter_close(filter); - filter_free(filter); - return 1; - } - - nbytes = write(1, dest, length); - if (nbytes < 0) { - g_printerr("Failed to write: %s\n", g_strerror(errno)); - filter_close(filter); - filter_free(filter); - return 1; - } - } - - /* cleanup and exit */ - - filter_close(filter); - filter_free(filter); - - config_global_finish(); - - return 0; -} diff --git a/test/run_filter.cxx b/test/run_filter.cxx new file mode 100644 index 000000000..990a57df0 --- /dev/null +++ b/test/run_filter.cxx @@ -0,0 +1,196 @@ +/* + * Copyright (C) 2003-2013 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "conf.h" + +extern "C" { +#include "audio_parser.h" +} + +#include "audio_format.h" +#include "filter_plugin.h" +#include "pcm_volume.h" +#include "mixer_control.h" +#include "stdbin.h" + +#include + +#include +#include +#include +#include + +bool +mixer_set_volume(G_GNUC_UNUSED struct mixer *mixer, + G_GNUC_UNUSED unsigned volume, G_GNUC_UNUSED GError **error_r) +{ + return true; +} + +static void +my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level, + const gchar *message, G_GNUC_UNUSED gpointer user_data) +{ + if (log_domain != NULL) + g_printerr("%s: %s\n", log_domain, message); + else + g_printerr("%s\n", message); +} + +static const struct config_param * +find_named_config_block(const char *block, const char *name) +{ + const struct config_param *param = NULL; + + while ((param = config_get_next_param(block, param)) != NULL) { + const char *current_name = + config_get_block_string(param, "name", NULL); + if (current_name != NULL && strcmp(current_name, name) == 0) + return param; + } + + return NULL; +} + +static struct filter * +load_filter(const char *name) +{ + const struct config_param *param; + struct filter *filter; + GError *error = NULL; + + param = find_named_config_block("filter", name); + if (param == NULL) { + g_printerr("No such configured filter: %s\n", name); + return nullptr; + } + + filter = filter_configured_new(param, &error); + if (filter == NULL) { + g_printerr("Failed to load filter: %s\n", error->message); + g_error_free(error); + return NULL; + } + + return filter; +} + +int main(int argc, char **argv) +{ + struct audio_format audio_format; + struct audio_format_string af_string; + bool success; + GError *error = NULL; + struct filter *filter; + const struct audio_format *out_audio_format; + char buffer[4096]; + + if (argc < 3 || argc > 4) { + g_printerr("Usage: run_filter CONFIG NAME [FORMAT] message); + g_error_free(error); + return 1; + } + + /* parse the audio format */ + + if (argc > 3) { + success = audio_format_parse(&audio_format, argv[3], + false, &error); + if (!success) { + g_printerr("Failed to parse audio format: %s\n", + error->message); + g_error_free(error); + return 1; + } + } + + /* initialize the filter */ + + filter = load_filter(argv[2]); + if (filter == NULL) + return 1; + + /* open the filter */ + + out_audio_format = filter_open(filter, &audio_format, &error); + if (out_audio_format == NULL) { + g_printerr("Failed to open filter: %s\n", error->message); + g_error_free(error); + filter_free(filter); + return 1; + } + + g_printerr("audio_format=%s\n", + audio_format_to_string(out_audio_format, &af_string)); + + /* play */ + + while (true) { + ssize_t nbytes; + size_t length; + const void *dest; + + nbytes = read(0, buffer, sizeof(buffer)); + if (nbytes <= 0) + break; + + dest = filter_filter(filter, buffer, (size_t)nbytes, + &length, &error); + if (dest == NULL) { + g_printerr("Filter failed: %s\n", error->message); + filter_close(filter); + filter_free(filter); + return 1; + } + + nbytes = write(1, dest, length); + if (nbytes < 0) { + g_printerr("Failed to write: %s\n", g_strerror(errno)); + filter_close(filter); + filter_free(filter); + return 1; + } + } + + /* cleanup and exit */ + + filter_close(filter); + filter_free(filter); + + config_global_finish(); + + return 0; +} -- cgit v1.2.3