Commit 8549ccfd authored by Max Kellermann's avatar Max Kellermann

playlist/CloseSongEnumerator: new wrapper class

Simplifies a lot of code, because we don't need to return both the SongEnumerator and the InputStream.
parent ffd16b55
...@@ -1150,6 +1150,8 @@ endif ...@@ -1150,6 +1150,8 @@ endif
libplaylist_plugins_a_SOURCES = \ libplaylist_plugins_a_SOURCES = \
src/playlist/PlaylistPlugin.hxx \ src/playlist/PlaylistPlugin.hxx \
src/playlist/SongEnumerator.hxx \ src/playlist/SongEnumerator.hxx \
src/playlist/CloseSongEnumerator.cxx \
src/playlist/CloseSongEnumerator.hxx \
src/playlist/MemorySongEnumerator.cxx \ src/playlist/MemorySongEnumerator.cxx \
src/playlist/MemorySongEnumerator.hxx \ src/playlist/MemorySongEnumerator.hxx \
src/playlist/plugins/ExtM3uPlaylistPlugin.cxx \ src/playlist/plugins/ExtM3uPlaylistPlugin.cxx \
......
/*
* Copyright (C) 2003-2014 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 "CloseSongEnumerator.hxx"
#include "input/InputStream.hxx"
CloseSongEnumerator::~CloseSongEnumerator()
{
delete other;
is->Close();
}
DetachedSong *
CloseSongEnumerator::NextSong()
{
return other->NextSong();
}
/*
* Copyright (C) 2003-2014 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.
*/
#ifndef MPD_CLOSE_SONG_ENUMERATOR_HXX
#define MPD_CLOSE_SONG_ENUMERATOR_HXX
#include "SongEnumerator.hxx"
#include "Compiler.h"
struct InputStream;
/**
* A #SongEnumerator wrapper that closes an #InputStream automatically
* after deleting the #SongEnumerator
*/
class CloseSongEnumerator final : public SongEnumerator {
SongEnumerator *const other;
InputStream *const is;
public:
gcc_nonnull_all
CloseSongEnumerator(SongEnumerator *_other, InputStream *const _is)
:other(_other), is(_is) {}
virtual ~CloseSongEnumerator();
virtual DetachedSong *NextSong() override;
};
#endif
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "PlaylistAny.hxx" #include "PlaylistAny.hxx"
#include "PlaylistMapper.hxx" #include "PlaylistMapper.hxx"
#include "PlaylistRegistry.hxx" #include "PlaylistRegistry.hxx"
#include "CloseSongEnumerator.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "input/InputStream.hxx" #include "input/InputStream.hxx"
...@@ -29,16 +30,13 @@ ...@@ -29,16 +30,13 @@
#include <assert.h> #include <assert.h>
static SongEnumerator * static SongEnumerator *
playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond, playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond)
InputStream **is_r)
{ {
assert(uri_has_scheme(uri)); assert(uri_has_scheme(uri));
SongEnumerator *playlist = playlist_list_open_uri(uri, mutex, cond); SongEnumerator *playlist = playlist_list_open_uri(uri, mutex, cond);
if (playlist != nullptr) { if (playlist != nullptr)
*is_r = nullptr;
return playlist; return playlist;
}
Error error; Error error;
InputStream *is = InputStream::OpenReady(uri, mutex, cond, error); InputStream *is = InputStream::OpenReady(uri, mutex, cond, error);
...@@ -55,15 +53,13 @@ playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond, ...@@ -55,15 +53,13 @@ playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond,
return nullptr; return nullptr;
} }
*is_r = is; return new CloseSongEnumerator(playlist, is);
return playlist;
} }
SongEnumerator * SongEnumerator *
playlist_open_any(const char *uri, Mutex &mutex, Cond &cond, playlist_open_any(const char *uri, Mutex &mutex, Cond &cond)
InputStream **is_r)
{ {
return uri_has_scheme(uri) return uri_has_scheme(uri)
? playlist_open_remote(uri, mutex, cond, is_r) ? playlist_open_remote(uri, mutex, cond)
: playlist_mapper_open(uri, mutex, cond, is_r); : playlist_mapper_open(uri, mutex, cond);
} }
...@@ -23,19 +23,13 @@ ...@@ -23,19 +23,13 @@
class Mutex; class Mutex;
class Cond; class Cond;
class SongEnumerator; class SongEnumerator;
struct InputStream;
/** /**
* Opens a playlist from the specified URI, which can be either an * Opens a playlist from the specified URI, which can be either an
* absolute remote URI (with a scheme) or a relative path to the * absolute remote URI (with a scheme) or a relative path to the
* music orplaylist directory. * music orplaylist directory.
*
* @param is_r on success, an input_stream object may be returned
* here, which must be closed after the playlist_provider object is
* freed
*/ */
SongEnumerator * SongEnumerator *
playlist_open_any(const char *uri, Mutex &mutex, Cond &cond, playlist_open_any(const char *uri, Mutex &mutex, Cond &cond);
InputStream **is_r);
#endif #endif
...@@ -28,14 +28,11 @@ ...@@ -28,14 +28,11 @@
#include <assert.h> #include <assert.h>
static SongEnumerator * static SongEnumerator *
playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond, playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond)
InputStream **is_r)
{ {
auto playlist = playlist_list_open_uri(path_fs, mutex, cond); auto playlist = playlist_list_open_uri(path_fs, mutex, cond);
if (playlist != nullptr) if (playlist == nullptr)
*is_r = nullptr; playlist = playlist_list_open_path(path_fs, mutex, cond);
else
playlist = playlist_list_open_path(path_fs, mutex, cond, is_r);
return playlist; return playlist;
} }
...@@ -44,8 +41,7 @@ playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond, ...@@ -44,8 +41,7 @@ playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
* Load a playlist from the configured playlist directory. * Load a playlist from the configured playlist directory.
*/ */
static SongEnumerator * static SongEnumerator *
playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond, playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond)
InputStream **is_r)
{ {
assert(spl_valid_name(uri)); assert(spl_valid_name(uri));
...@@ -61,7 +57,7 @@ playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond, ...@@ -61,7 +57,7 @@ playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond,
AllocatedPath::Build(playlist_directory_fs, uri_fs); AllocatedPath::Build(playlist_directory_fs, uri_fs);
assert(!path_fs.IsNull()); assert(!path_fs.IsNull());
return playlist_open_path(path_fs.c_str(), mutex, cond, is_r); return playlist_open_path(path_fs.c_str(), mutex, cond);
} }
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
...@@ -70,8 +66,7 @@ playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond, ...@@ -70,8 +66,7 @@ playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond,
* Load a playlist from the configured music directory. * Load a playlist from the configured music directory.
*/ */
static SongEnumerator * static SongEnumerator *
playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond, playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond)
InputStream **is_r)
{ {
assert(uri_safe_local(uri)); assert(uri_safe_local(uri));
...@@ -79,26 +74,24 @@ playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond, ...@@ -79,26 +74,24 @@ playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond,
if (path.IsNull()) if (path.IsNull())
return nullptr; return nullptr;
return playlist_open_path(path.c_str(), mutex, cond, is_r); return playlist_open_path(path.c_str(), mutex, cond);
} }
#endif #endif
SongEnumerator * SongEnumerator *
playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond, playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond)
InputStream **is_r)
{ {
if (spl_valid_name(uri)) { if (spl_valid_name(uri)) {
auto playlist = playlist_open_in_playlist_dir(uri, mutex, cond, auto playlist = playlist_open_in_playlist_dir(uri,
is_r); mutex, cond);
if (playlist != nullptr) if (playlist != nullptr)
return playlist; return playlist;
} }
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
if (uri_safe_local(uri)) { if (uri_safe_local(uri)) {
auto playlist = playlist_open_in_music_dir(uri, mutex, cond, auto playlist = playlist_open_in_music_dir(uri, mutex, cond);
is_r);
if (playlist != nullptr) if (playlist != nullptr)
return playlist; return playlist;
} }
......
...@@ -23,18 +23,12 @@ ...@@ -23,18 +23,12 @@
class Mutex; class Mutex;
class Cond; class Cond;
class SongEnumerator; class SongEnumerator;
struct InputStream;
/** /**
* Opens a playlist from an URI relative to the playlist or music * Opens a playlist from an URI relative to the playlist or music
* directory. * directory.
*
* @param is_r on success, an input_stream object may be returned
* here, which must be closed after the playlist_provider object is
* freed
*/ */
SongEnumerator * SongEnumerator *
playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond, playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond);
InputStream **is_r);
#endif #endif
...@@ -22,9 +22,9 @@ ...@@ -22,9 +22,9 @@
#include "PlaylistAny.hxx" #include "PlaylistAny.hxx"
#include "PlaylistSong.hxx" #include "PlaylistSong.hxx"
#include "Playlist.hxx" #include "Playlist.hxx"
#include "input/InputStream.hxx"
#include "SongEnumerator.hxx" #include "SongEnumerator.hxx"
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx" #include "thread/Cond.hxx"
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
...@@ -72,8 +72,7 @@ playlist_open_into_queue(const char *uri, ...@@ -72,8 +72,7 @@ playlist_open_into_queue(const char *uri,
Mutex mutex; Mutex mutex;
Cond cond; Cond cond;
InputStream *is; auto playlist = playlist_open_any(uri, mutex, cond);
auto playlist = playlist_open_any(uri, mutex, cond, &is);
if (playlist == nullptr) if (playlist == nullptr)
return PlaylistResult::NO_SUCH_LIST; return PlaylistResult::NO_SUCH_LIST;
...@@ -82,9 +81,5 @@ playlist_open_into_queue(const char *uri, ...@@ -82,9 +81,5 @@ playlist_open_into_queue(const char *uri,
start_index, end_index, start_index, end_index,
dest, pc, loader); dest, pc, loader);
delete playlist; delete playlist;
if (is != nullptr)
is->Close();
return result; return result;
} }
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "PlaylistRegistry.hxx" #include "PlaylistRegistry.hxx"
#include "PlaylistPlugin.hxx" #include "PlaylistPlugin.hxx"
#include "CloseSongEnumerator.hxx"
#include "plugins/ExtM3uPlaylistPlugin.hxx" #include "plugins/ExtM3uPlaylistPlugin.hxx"
#include "plugins/M3uPlaylistPlugin.hxx" #include "plugins/M3uPlaylistPlugin.hxx"
#include "plugins/XspfPlaylistPlugin.hxx" #include "plugins/XspfPlaylistPlugin.hxx"
...@@ -279,8 +280,7 @@ playlist_suffix_supported(const char *suffix) ...@@ -279,8 +280,7 @@ playlist_suffix_supported(const char *suffix)
} }
SongEnumerator * SongEnumerator *
playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond, playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond)
InputStream **is_r)
{ {
const char *suffix; const char *suffix;
...@@ -301,7 +301,7 @@ playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond, ...@@ -301,7 +301,7 @@ playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
auto playlist = playlist_list_open_stream_suffix(*is, suffix); auto playlist = playlist_list_open_stream_suffix(*is, suffix);
if (playlist != nullptr) if (playlist != nullptr)
*is_r = is; playlist = new CloseSongEnumerator(playlist, is);
else else
is->Close(); is->Close();
......
...@@ -77,7 +77,6 @@ playlist_suffix_supported(const char *suffix); ...@@ -77,7 +77,6 @@ playlist_suffix_supported(const char *suffix);
* @return a playlist, or nullptr on error * @return a playlist, or nullptr on error
*/ */
SongEnumerator * SongEnumerator *
playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond, playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond);
InputStream **is_r);
#endif #endif
...@@ -23,10 +23,10 @@ ...@@ -23,10 +23,10 @@
#include "PlaylistSong.hxx" #include "PlaylistSong.hxx"
#include "SongEnumerator.hxx" #include "SongEnumerator.hxx"
#include "SongPrint.hxx" #include "SongPrint.hxx"
#include "input/InputStream.hxx"
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
#include "SongLoader.hxx" #include "SongLoader.hxx"
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx" #include "thread/Cond.hxx"
static void static void
...@@ -59,16 +59,11 @@ playlist_file_print(Client &client, const char *uri, bool detail) ...@@ -59,16 +59,11 @@ playlist_file_print(Client &client, const char *uri, bool detail)
Mutex mutex; Mutex mutex;
Cond cond; Cond cond;
InputStream *is; SongEnumerator *playlist = playlist_open_any(uri, mutex, cond);
SongEnumerator *playlist = playlist_open_any(uri, mutex, cond, &is);
if (playlist == nullptr) if (playlist == nullptr)
return false; return false;
playlist_provider_print(client, uri, *playlist, detail); playlist_provider_print(client, uri, *playlist, detail);
delete playlist; delete playlist;
if (is != nullptr)
is->Close();
return true; return true;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment