Commit c97685fe authored by Max Kellermann's avatar Max Kellermann

TagFile: use decoder_plugins_try()

.. instead of decoder_plugin_from_suffix(). This reduces overhead by walking the array only once.
parent 358b6710
...@@ -29,58 +29,71 @@ ...@@ -29,58 +29,71 @@
#include <assert.h> #include <assert.h>
bool class TagFileScan {
tag_file_scan(Path path_fs, const Path path_fs;
const struct tag_handler *handler, void *handler_ctx) const char *const suffix;
{
assert(!path_fs.IsNull());
assert(handler != nullptr);
/* check if there's a suffix and a plugin */
const char *suffix = uri_get_suffix(path_fs.c_str());
if (suffix == nullptr)
return false;
const struct DecoderPlugin *plugin = const tag_handler &handler;
decoder_plugin_from_suffix(suffix, nullptr); void *handler_ctx;
if (plugin == nullptr)
return false;
InputStream *is = nullptr;
Mutex mutex; Mutex mutex;
Cond cond; Cond cond;
InputStream *is;
do { public:
/* load file tag */ TagFileScan(Path _path_fs, const char *_suffix,
if (plugin->ScanFile(path_fs.c_str(), const tag_handler &_handler, void *_handler_ctx)
*handler, handler_ctx)) :path_fs(_path_fs), suffix(_suffix),
break; handler(_handler), handler_ctx(_handler_ctx) ,
is(nullptr) {}
/* fall back to stream tag */ ~TagFileScan() {
if (plugin->scan_stream != nullptr) { if (is != nullptr)
/* open the InputStream (if not already is->Close();
open) */ }
if (is == nullptr)
bool ScanFile(const DecoderPlugin &plugin) {
return plugin.ScanFile(path_fs.c_str(), handler, handler_ctx);
}
bool ScanStream(const DecoderPlugin &plugin) {
if (plugin.scan_stream == nullptr)
return false;
/* open the InputStream (if not already open) */
if (is == nullptr) {
is = InputStream::Open(path_fs.c_str(), is = InputStream::Open(path_fs.c_str(),
mutex, cond, mutex, cond,
IgnoreError()); IgnoreError());
if (is == nullptr)
return false;
}
/* now try the stream_tag() method */ /* now try the stream_tag() method */
if (is != nullptr) { return plugin.ScanStream(*is, handler, handler_ctx);
if (plugin->ScanStream(*is,
*handler, handler_ctx))
break;
is->LockRewind(IgnoreError());
} }
bool Scan(const DecoderPlugin &plugin) {
return plugin.SupportsSuffix(suffix) &&
(ScanFile(plugin) || ScanStream(plugin));
} }
};
bool
tag_file_scan(Path path_fs,
const struct tag_handler *handler, void *handler_ctx)
{
assert(!path_fs.IsNull());
assert(handler != nullptr);
plugin = decoder_plugin_from_suffix(suffix, plugin); /* check if there's a suffix and a plugin */
} while (plugin != nullptr);
if (is != nullptr) const char *suffix = uri_get_suffix(path_fs.c_str());
is->Close(); if (suffix == nullptr)
return false;
return plugin != nullptr; TagFileScan tfs(path_fs, suffix, *handler, handler_ctx);
return decoder_plugins_try([&](const DecoderPlugin &plugin){
return tfs.Scan(plugin);
});
} }
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