aboutsummaryrefslogtreecommitdiffstats
path: root/src/TagFile.cxx
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/TagFile.cxx103
1 files changed, 57 insertions, 46 deletions
diff --git a/src/TagFile.cxx b/src/TagFile.cxx
index 785a74987..84faa848a 100644
--- a/src/TagFile.cxx
+++ b/src/TagFile.cxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * 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
@@ -22,65 +22,76 @@
#include "fs/Path.hxx"
#include "util/UriUtil.hxx"
#include "util/Error.hxx"
-#include "DecoderList.hxx"
-#include "DecoderPlugin.hxx"
-#include "InputStream.hxx"
+#include "decoder/DecoderList.hxx"
+#include "decoder/DecoderPlugin.hxx"
+#include "input/InputStream.hxx"
#include "thread/Cond.hxx"
#include <assert.h>
-bool
-tag_file_scan(Path path_fs,
- const struct tag_handler *handler, void *handler_ctx)
-{
- assert(!path_fs.IsNull());
- assert(handler != nullptr);
+class TagFileScan {
+ const Path path_fs;
+ const char *const suffix;
- /* check if there's a suffix and a plugin */
+ const tag_handler &handler;
+ void *handler_ctx;
- const char *suffix = uri_get_suffix(path_fs.c_str());
- if (suffix == nullptr)
- return false;
-
- const struct DecoderPlugin *plugin =
- decoder_plugin_from_suffix(suffix, nullptr);
- if (plugin == nullptr)
- return false;
-
- InputStream *is = nullptr;
Mutex mutex;
Cond cond;
+ InputStream *is;
+
+public:
+ TagFileScan(Path _path_fs, const char *_suffix,
+ const tag_handler &_handler, void *_handler_ctx)
+ :path_fs(_path_fs), suffix(_suffix),
+ handler(_handler), handler_ctx(_handler_ctx) ,
+ is(nullptr) {}
- do {
- /* load file tag */
- if (plugin->ScanFile(path_fs.c_str(),
- *handler, handler_ctx))
- break;
+ ~TagFileScan() {
+ delete is;
+ }
- /* fall back to stream tag */
- if (plugin->scan_stream != nullptr) {
- /* open the InputStream (if not already
- open) */
+ bool ScanFile(const DecoderPlugin &plugin) {
+ return plugin.ScanFile(path_fs, 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::OpenReady(path_fs.c_str(),
+ mutex, cond,
+ IgnoreError());
if (is == nullptr)
- is = InputStream::Open(path_fs.c_str(),
- mutex, cond,
- IgnoreError());
+ return false;
+ } else
+ is->LockRewind(IgnoreError());
+
+ /* now try the stream_tag() method */
+ return plugin.ScanStream(*is, handler, handler_ctx);
+ }
- /* now try the stream_tag() method */
- if (is != nullptr) {
- if (plugin->ScanStream(*is,
- *handler, handler_ctx))
- break;
+ bool Scan(const DecoderPlugin &plugin) {
+ return plugin.SupportsSuffix(suffix) &&
+ (ScanFile(plugin) || ScanStream(plugin));
+ }
+};
- is->LockRewind(IgnoreError());
- }
- }
+bool
+tag_file_scan(Path path_fs, const tag_handler &handler, void *handler_ctx)
+{
+ assert(!path_fs.IsNull());
- plugin = decoder_plugin_from_suffix(suffix, plugin);
- } while (plugin != nullptr);
+ /* check if there's a suffix and a plugin */
- if (is != nullptr)
- is->Close();
+ const char *suffix = uri_get_suffix(path_fs.c_str());
+ 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);
+ });
}