InputStream: add static method OpenReady()

Merge some duplicate code.
This commit is contained in:
Max Kellermann 2013-12-29 18:08:49 +01:00
parent ea9aff1d3f
commit aeb2baa495
11 changed files with 47 additions and 62 deletions

View File

@ -57,6 +57,28 @@ InputStream::Open(const char *url,
return nullptr; return nullptr;
} }
InputStream *
InputStream::OpenReady(const char *uri,
Mutex &mutex, Cond &cond,
Error &error)
{
InputStream *is = Open(uri, mutex, cond, error);
if (is == nullptr)
return nullptr;
mutex.lock();
is->WaitReady();
bool success = is->Check(error);
mutex.unlock();
if (!success) {
is->Close();
is = nullptr;
}
return is;
}
bool bool
InputStream::Check(Error &error) InputStream::Check(Error &error)
{ {

View File

@ -118,6 +118,15 @@ struct InputStream {
static InputStream *Open(const char *uri, Mutex &mutex, Cond &cond, static InputStream *Open(const char *uri, Mutex &mutex, Cond &cond,
Error &error); Error &error);
/**
* Just like Open(), but waits for the stream to become ready.
* It is a wrapper for Open(), WaitReady() and Check().
*/
gcc_malloc gcc_nonnull_all
static InputStream *OpenReady(const char *uri,
Mutex &mutex, Cond &cond,
Error &error);
/** /**
* Close the input stream and free resources. * Close the input stream and free resources.
* *

View File

@ -41,7 +41,7 @@ playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond,
} }
Error error; Error error;
InputStream *is = InputStream::Open(uri, mutex, cond, error); InputStream *is = InputStream::OpenReady(uri, mutex, cond, error);
if (is == nullptr) { if (is == nullptr) {
if (error.IsDefined()) if (error.IsDefined())
FormatError(error, "Failed to open %s", uri); FormatError(error, "Failed to open %s", uri);

View File

@ -269,9 +269,7 @@ playlist_list_open_stream_suffix(InputStream &is, const char *suffix)
SongEnumerator * SongEnumerator *
playlist_list_open_stream(InputStream &is, const char *uri) playlist_list_open_stream(InputStream &is, const char *uri)
{ {
const char *suffix; assert(is.ready);
is.LockWaitReady();
const char *const mime = is.GetMimeType(); const char *const mime = is.GetMimeType();
if (mime != nullptr) { if (mime != nullptr) {
@ -280,7 +278,7 @@ playlist_list_open_stream(InputStream &is, const char *uri)
return playlist; return playlist;
} }
suffix = uri != nullptr ? uri_get_suffix(uri) : nullptr; const char *suffix = uri != nullptr ? uri_get_suffix(uri) : nullptr;
if (suffix != nullptr) { if (suffix != nullptr) {
auto playlist = playlist_list_open_stream_suffix(is, suffix); auto playlist = playlist_list_open_stream_suffix(is, suffix);
if (playlist != nullptr) if (playlist != nullptr)
@ -317,7 +315,7 @@ playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
return nullptr; return nullptr;
Error error; Error error;
InputStream *is = InputStream::Open(path_fs, mutex, cond, error); InputStream *is = InputStream::OpenReady(path_fs, mutex, cond, error);
if (is == nullptr) { if (is == nullptr) {
if (error.IsDefined()) if (error.IsDefined())
LogError(error); LogError(error);
@ -325,8 +323,6 @@ playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
return nullptr; return nullptr;
} }
is->LockWaitReady();
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; *is_r = is;

View File

@ -62,9 +62,9 @@ public:
/* open the InputStream (if not already open) */ /* open the InputStream (if not already open) */
if (is == nullptr) { if (is == nullptr) {
is = InputStream::Open(path_fs.c_str(), is = InputStream::OpenReady(path_fs.c_str(),
mutex, cond, mutex, cond,
IgnoreError()); IgnoreError());
if (is == nullptr) if (is == nullptr)
return false; return false;
} else } else

View File

@ -146,7 +146,7 @@ bz2_open(const char *pathname, Error &error)
{ {
static Mutex mutex; static Mutex mutex;
static Cond cond; static Cond cond;
InputStream *is = InputStream::Open(pathname, mutex, cond, error); InputStream *is = InputStream::OpenReady(pathname, mutex, cond, error);
if (is == nullptr) if (is == nullptr)
return nullptr; return nullptr;

View File

@ -254,8 +254,8 @@ soundcloud_parse_json(const char *url, yajl_handle hand,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
Error error; Error error;
InputStream *input_stream = InputStream::Open(url, mutex, cond, InputStream *input_stream = InputStream::OpenReady(url, mutex, cond,
error); error);
if (input_stream == nullptr) { if (input_stream == nullptr) {
if (error.IsDefined()) if (error.IsDefined())
LogError(error); LogError(error);
@ -263,7 +263,6 @@ soundcloud_parse_json(const char *url, yajl_handle hand,
} }
mutex.lock(); mutex.lock();
input_stream->WaitReady();
yajl_status stat; yajl_status stat;
int done = 0; int done = 0;

View File

@ -92,7 +92,7 @@ int main(int argc, char **argv)
if (playlist == NULL) { if (playlist == NULL) {
/* open the stream and wait until it becomes ready */ /* open the stream and wait until it becomes ready */
is = InputStream::Open(uri, mutex, cond, error); is = InputStream::OpenReady(uri, mutex, cond, error);
if (is == NULL) { if (is == NULL) {
if (error.IsDefined()) if (error.IsDefined())
LogError(error); LogError(error);
@ -102,8 +102,6 @@ int main(int argc, char **argv)
return 2; return 2;
} }
is->LockWaitReady();
/* open the playlist */ /* open the playlist */
playlist = playlist_list_open_stream(*is, uri); playlist = playlist_list_open_stream(*is, uri);

View File

@ -49,23 +49,6 @@ dump_text_file(TextInputStream &is)
static int static int
dump_input_stream(InputStream &is) dump_input_stream(InputStream &is)
{ {
Error error;
is.Lock();
/* wait until the stream becomes ready */
is.WaitReady();
if (!is.Check(error)) {
LogError(error);
is.Unlock();
return EXIT_FAILURE;
}
/* read data and tags from the stream */
is.Unlock();
{ {
TextInputStream tis(is); TextInputStream tis(is);
dump_text_file(tis); dump_text_file(tis);
@ -73,6 +56,7 @@ dump_input_stream(InputStream &is)
is.Lock(); is.Lock();
Error error;
if (!is.Check(error)) { if (!is.Check(error)) {
LogError(error); LogError(error);
is.Unlock(); is.Unlock();
@ -121,7 +105,7 @@ int main(int argc, char **argv)
Mutex mutex; Mutex mutex;
Cond cond; Cond cond;
InputStream *is = InputStream::Open(argv[1], mutex, cond, error); InputStream *is = InputStream::OpenReady(argv[1], mutex, cond, error);
if (is != NULL) { if (is != NULL) {
ret = dump_input_stream(*is); ret = dump_input_stream(*is);
is->Close(); is->Close();

View File

@ -113,26 +113,13 @@ int main(int argc, char **argv)
Mutex mutex; Mutex mutex;
Cond cond; Cond cond;
InputStream *is = InputStream::Open(path, mutex, cond, InputStream *is = InputStream::OpenReady(path, mutex, cond,
error); error);
if (is == NULL) { if (is == NULL) {
FormatError(error, "Failed to open %s", path); FormatError(error, "Failed to open %s", path);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
mutex.lock();
is->WaitReady();
if (!is->Check(error)) {
mutex.unlock();
FormatError(error, "Failed to read %s", path);
return EXIT_FAILURE;
}
mutex.unlock();
success = plugin->ScanStream(*is, print_handler, nullptr); success = plugin->ScanStream(*is, print_handler, nullptr);
is->Close(); is->Close();
} }

View File

@ -50,16 +50,6 @@ dump_input_stream(InputStream *is)
is->Lock(); is->Lock();
/* wait until the stream becomes ready */
is->WaitReady();
if (!is->Check(error)) {
LogError(error);
is->Unlock();
return EXIT_FAILURE;
}
/* print meta data */ /* print meta data */
if (!is->mime.empty()) if (!is->mime.empty())
@ -139,7 +129,7 @@ int main(int argc, char **argv)
Mutex mutex; Mutex mutex;
Cond cond; Cond cond;
is = InputStream::Open(argv[1], mutex, cond, error); is = InputStream::OpenReady(argv[1], mutex, cond, error);
if (is != NULL) { if (is != NULL) {
ret = dump_input_stream(is); ret = dump_input_stream(is);
is->Close(); is->Close();