TagFile.cxx 2.63 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "TagFile.hxx"
21
#include "tag/Generic.hxx"
22 23
#include "tag/Handler.hxx"
#include "tag/Builder.hxx"
24
#include "fs/Path.hxx"
25 26
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "input/InputStream.hxx"
28
#include "input/LocalOpen.hxx"
29

30
#include <exception>
31

32 33
#include <assert.h>

34 35 36 37
class TagFileScan {
	const Path path_fs;
	const char *const suffix;

38
	TagHandler &handler;
39 40

	Mutex mutex;
41
	InputStreamPtr is;
42 43 44

public:
	TagFileScan(Path _path_fs, const char *_suffix,
45
		    TagHandler &_handler) noexcept
46
		:path_fs(_path_fs), suffix(_suffix),
47
		 handler(_handler),
48 49
		 is(nullptr) {}

Max Kellermann's avatar
Max Kellermann committed
50
	bool ScanFile(const DecoderPlugin &plugin) noexcept {
51
		return plugin.ScanFile(path_fs, handler);
52 53
	}

54
	bool ScanStream(const DecoderPlugin &plugin) {
55 56 57 58 59
		if (plugin.scan_stream == nullptr)
			return false;

		/* open the InputStream (if not already open) */
		if (is == nullptr) {
60
			is = OpenLocalInputStream(path_fs, mutex);
61
		} else {
62
			is->LockRewind();
63
		}
64 65

		/* now try the stream_tag() method */
66
		return plugin.ScanStream(*is, handler);
67 68
	}

69
	bool Scan(const DecoderPlugin &plugin) {
70 71 72 73 74
		return plugin.SupportsSuffix(suffix) &&
			(ScanFile(plugin) || ScanStream(plugin));
	}
};

75
bool
76
ScanFileTagsNoGeneric(Path path_fs, TagHandler &handler)
77
{
78
	assert(!path_fs.IsNull());
79 80 81

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

82
	const auto *suffix = path_fs.GetSuffix();
83
	if (suffix == nullptr)
84 85
		return false;

86 87
	const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8();

88
	TagFileScan tfs(path_fs, suffix_utf8.c_str(), handler);
89 90 91
	return decoder_plugins_try([&](const DecoderPlugin &plugin){
			return tfs.Scan(plugin);
		});
92
}
93 94

bool
95
ScanFileTagsWithGeneric(Path path, TagBuilder &builder,
96
			AudioFormat *audio_format)
97
{
98
	FullTagHandler h(builder, audio_format);
99

100
	if (!ScanFileTagsNoGeneric(path, h))
101 102
		return false;

103
	if (builder.empty())
104
		ScanGenericTags(path, h);
105 106 107

	return true;
}