From 4d1174515636f4ff2e6c388d50f052adb4a265af Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 2 Sep 2019 20:19:47 +0200 Subject: [PATCH] playlist/Plugin: add SupportsSuffix(), SupportsMimeType() --- src/playlist/PlaylistPlugin.cxx | 43 +++++++++++++++++++++++++++++++ src/playlist/PlaylistPlugin.hxx | 20 ++++++++++++++ src/playlist/PlaylistRegistry.cxx | 17 +++++------- src/playlist/meson.build | 1 + 4 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 src/playlist/PlaylistPlugin.cxx diff --git a/src/playlist/PlaylistPlugin.cxx b/src/playlist/PlaylistPlugin.cxx new file mode 100644 index 000000000..73ed0f233 --- /dev/null +++ b/src/playlist/PlaylistPlugin.cxx @@ -0,0 +1,43 @@ +/* + * Copyright 2003-2019 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 "PlaylistPlugin.hxx" +#include "util/StringUtil.hxx" +#include "util/StringView.hxx" + +bool +PlaylistPlugin::SupportsScheme(StringView scheme) const noexcept +{ + return schemes != nullptr && + StringArrayContainsCase(schemes, scheme); +} + +bool +PlaylistPlugin::SupportsSuffix(const char *suffix) const noexcept +{ + return suffixes != nullptr && + StringArrayContainsCase(suffixes, suffix); +} + +bool +PlaylistPlugin::SupportsMimeType(StringView mime_type) const noexcept +{ + return mime_types != nullptr && + StringArrayContainsCase(mime_types, mime_type); +} diff --git a/src/playlist/PlaylistPlugin.hxx b/src/playlist/PlaylistPlugin.hxx index d34c4dd36..4661ed967 100644 --- a/src/playlist/PlaylistPlugin.hxx +++ b/src/playlist/PlaylistPlugin.hxx @@ -22,9 +22,11 @@ #include "input/Ptr.hxx" #include "thread/Mutex.hxx" +#include "util/Compiler.h" struct ConfigBlock; struct Tag; +struct StringView; class SongEnumerator; struct PlaylistPlugin { @@ -101,6 +103,24 @@ struct PlaylistPlugin { copy.mime_types = _mime_types; return copy; } + + /** + * Does the plugin announce the specified URI scheme? + */ + gcc_pure gcc_nonnull_all + bool SupportsScheme(StringView scheme) const noexcept; + + /** + * Does the plugin announce the specified file name suffix? + */ + gcc_pure gcc_nonnull_all + bool SupportsSuffix(const char *suffix) const noexcept; + + /** + * Does the plugin announce the specified MIME type? + */ + gcc_pure gcc_nonnull_all + bool SupportsMimeType(StringView mime_type) const noexcept; }; /** diff --git a/src/playlist/PlaylistRegistry.cxx b/src/playlist/PlaylistRegistry.cxx index 6aeca95eb..ed06a8408 100644 --- a/src/playlist/PlaylistRegistry.cxx +++ b/src/playlist/PlaylistRegistry.cxx @@ -33,7 +33,6 @@ #include "plugins/EmbeddedCuePlaylistPlugin.hxx" #include "input/InputStream.hxx" #include "util/MimeType.hxx" -#include "util/StringUtil.hxx" #include "util/StringView.hxx" #include "util/UriExtract.hxx" #include "config/Data.hxx" @@ -122,8 +121,7 @@ playlist_list_open_uri_scheme(const char *uri, Mutex &mutex, assert(!tried[i]); if (playlist_plugins_enabled[i] && plugin->open_uri != nullptr && - plugin->schemes != nullptr && - StringArrayContainsCase(plugin->schemes, scheme)) { + plugin->SupportsScheme(scheme)) { auto playlist = plugin->open_uri(uri, mutex); if (playlist) return playlist; @@ -150,8 +148,8 @@ playlist_list_open_uri_suffix(const char *uri, Mutex &mutex, const auto *plugin = playlist_plugins[i]; if (playlist_plugins_enabled[i] && !tried[i] && - plugin->open_uri != nullptr && plugin->suffixes != nullptr && - StringArrayContainsCase(plugin->suffixes, suffix)) { + plugin->open_uri != nullptr && + plugin->SupportsSuffix(suffix)) { auto playlist = plugin->open_uri(uri, mutex); if (playlist != nullptr) return playlist; @@ -183,8 +181,7 @@ playlist_list_open_stream_mime2(InputStreamPtr &&is, StringView mime) { playlist_plugins_for_each_enabled(plugin) { if (plugin->open_stream != nullptr && - plugin->mime_types != nullptr && - StringArrayContainsCase(plugin->mime_types, mime)) { + plugin->SupportsMimeType(mime)) { /* rewind the stream, so each plugin gets a fresh start */ try { @@ -233,8 +230,7 @@ playlist_list_open_stream_suffix(InputStreamPtr &&is, const char *suffix) playlist_plugins_for_each_enabled(plugin) { if (plugin->open_stream != nullptr && - plugin->suffixes != nullptr && - StringArrayContainsCase(plugin->suffixes, suffix)) { + plugin->SupportsSuffix(suffix)) { /* rewind the stream, so each plugin gets a fresh start */ try { @@ -284,8 +280,7 @@ playlist_suffix_supported(const char *suffix) noexcept assert(suffix != nullptr); playlist_plugins_for_each_enabled(plugin) { - if (plugin->suffixes != nullptr && - StringArrayContainsCase(plugin->suffixes, suffix)) + if (plugin->SupportsSuffix(suffix)) return true; } diff --git a/src/playlist/meson.build b/src/playlist/meson.build index ed9882d8f..1266784be 100644 --- a/src/playlist/meson.build +++ b/src/playlist/meson.build @@ -1,5 +1,6 @@ playlist_api = static_library( 'playlist_api', + 'PlaylistPlugin.cxx', 'MemorySongEnumerator.cxx', include_directories: inc, )