TagFile.cxx 2.91 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
21
#include "TagFile.hxx"
22
#include "tag/Generic.hxx"
23 24
#include "tag/Handler.hxx"
#include "tag/Builder.hxx"
25
#include "fs/Path.hxx"
26 27
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "input/InputStream.hxx"
29
#include "input/LocalOpen.hxx"
30
#include "thread/Cond.hxx"
31

32 33
#include <stdexcept>

34 35
#include <assert.h>

36 37 38 39
class TagFileScan {
	const Path path_fs;
	const char *const suffix;

40
	const TagHandler &handler;
41 42 43 44
	void *handler_ctx;

	Mutex mutex;
	Cond cond;
45
	InputStreamPtr is;
46 47 48

public:
	TagFileScan(Path _path_fs, const char *_suffix,
49
		    const TagHandler &_handler, void *_handler_ctx)
50 51 52 53 54
		:path_fs(_path_fs), suffix(_suffix),
		 handler(_handler), handler_ctx(_handler_ctx) ,
		 is(nullptr) {}

	bool ScanFile(const DecoderPlugin &plugin) {
55
		return plugin.ScanFile(path_fs, handler, handler_ctx);
56 57 58 59 60 61 62 63
	}

	bool ScanStream(const DecoderPlugin &plugin) {
		if (plugin.scan_stream == nullptr)
			return false;

		/* open the InputStream (if not already open) */
		if (is == nullptr) {
64 65 66 67
			try {
				is = OpenLocalInputStream(path_fs,
							  mutex, cond);
			} catch (const std::runtime_error &) {
68
				return false;
69
			}
70 71 72 73 74 75
		} else {
			try {
				is->LockRewind();
			} catch (const std::runtime_error &) {
			}
		}
76 77 78 79 80 81 82 83 84 85 86

		/* 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));
	}
};

87
bool
88
tag_file_scan(Path path_fs, const TagHandler &handler, void *handler_ctx)
89
{
90
	assert(!path_fs.IsNull());
91 92 93

	/* check if there's a suffix and a plugin */

94
	const auto *suffix = path_fs.GetSuffix();
95
	if (suffix == nullptr)
96 97
		return false;

98 99 100
	const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8();

	TagFileScan tfs(path_fs, suffix_utf8.c_str(), handler, handler_ctx);
101 102 103
	return decoder_plugins_try([&](const DecoderPlugin &plugin){
			return tfs.Scan(plugin);
		});
104
}
105 106 107 108 109 110 111 112 113 114 115 116

bool
tag_file_scan(Path path, TagBuilder &builder)
{
	if (!tag_file_scan(path, full_tag_handler, &builder))
		return false;

	if (builder.IsEmpty())
		ScanGenericTags(path, full_tag_handler, &builder);

	return true;
}