2012-02-12 15:20:38 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2012-02-12 15:20:38 +01:00
|
|
|
* 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"
|
2013-01-26 01:18:12 +01:00
|
|
|
#include "TagFile.hxx"
|
2013-10-26 15:14:54 +02:00
|
|
|
#include "fs/Path.hxx"
|
2013-04-08 23:30:21 +02:00
|
|
|
#include "util/UriUtil.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "decoder/DecoderList.hxx"
|
|
|
|
#include "decoder/DecoderPlugin.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2014-10-02 21:43:40 +02:00
|
|
|
#include "input/LocalOpen.hxx"
|
2013-10-17 10:06:31 +02:00
|
|
|
#include "thread/Cond.hxx"
|
2012-02-12 15:20:38 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2013-12-29 16:13:11 +01:00
|
|
|
class TagFileScan {
|
|
|
|
const Path path_fs;
|
|
|
|
const char *const suffix;
|
|
|
|
|
|
|
|
const tag_handler &handler;
|
|
|
|
void *handler_ctx;
|
|
|
|
|
|
|
|
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) {}
|
|
|
|
|
|
|
|
~TagFileScan() {
|
2014-05-11 16:59:19 +02:00
|
|
|
delete is;
|
2013-12-29 16:13:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScanFile(const DecoderPlugin &plugin) {
|
2014-02-07 18:52:19 +01:00
|
|
|
return plugin.ScanFile(path_fs, handler, handler_ctx);
|
2013-12-29 16:13:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScanStream(const DecoderPlugin &plugin) {
|
|
|
|
if (plugin.scan_stream == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* open the InputStream (if not already open) */
|
|
|
|
if (is == nullptr) {
|
2014-10-02 21:43:40 +02:00
|
|
|
is = OpenLocalInputStream(path_fs,
|
|
|
|
mutex, cond,
|
|
|
|
IgnoreError());
|
2013-12-29 16:13:11 +01:00
|
|
|
if (is == nullptr)
|
|
|
|
return false;
|
2013-12-29 17:09:40 +01:00
|
|
|
} else
|
|
|
|
is->LockRewind(IgnoreError());
|
2013-12-29 16:13:11 +01:00
|
|
|
|
|
|
|
/* now try the stream_tag() method */
|
|
|
|
return plugin.ScanStream(*is, handler, handler_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scan(const DecoderPlugin &plugin) {
|
|
|
|
return plugin.SupportsSuffix(suffix) &&
|
|
|
|
(ScanFile(plugin) || ScanStream(plugin));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-12 15:20:38 +01:00
|
|
|
bool
|
2013-12-29 16:16:04 +01:00
|
|
|
tag_file_scan(Path path_fs, const tag_handler &handler, void *handler_ctx)
|
2012-02-12 15:20:38 +01:00
|
|
|
{
|
2013-10-26 15:14:54 +02:00
|
|
|
assert(!path_fs.IsNull());
|
2012-02-12 15:20:38 +01:00
|
|
|
|
|
|
|
/* check if there's a suffix and a plugin */
|
|
|
|
|
2013-10-26 15:14:54 +02:00
|
|
|
const char *suffix = uri_get_suffix(path_fs.c_str());
|
2013-10-19 18:19:03 +02:00
|
|
|
if (suffix == nullptr)
|
2012-02-12 15:20:38 +01:00
|
|
|
return false;
|
|
|
|
|
2013-12-29 16:16:04 +01:00
|
|
|
TagFileScan tfs(path_fs, suffix, handler, handler_ctx);
|
2013-12-29 16:13:11 +01:00
|
|
|
return decoder_plugins_try([&](const DecoderPlugin &plugin){
|
|
|
|
return tfs.Scan(plugin);
|
|
|
|
});
|
2012-02-12 15:20:38 +01:00
|
|
|
}
|