TagFile.cxx 2.75 KB
Newer Older
1
/*
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) {}

50
	bool ScanFile(const DecoderPlugin &plugin) noexcept {
51
		return plugin.ScanFile(path_fs, handler);
52 53
	}

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

		/* open the InputStream (if not already open) */
		if (is == nullptr) {
60
			try {
61
				is = OpenLocalInputStream(path_fs, mutex);
62
			} catch (...) {
63
				return false;
64
			}
65 66 67
		} else {
			try {
				is->LockRewind();
68
			} catch (...) {
69 70
			}
		}
71 72

		/* now try the stream_tag() method */
73
		return plugin.ScanStream(*is, handler);
74 75
	}

76
	bool Scan(const DecoderPlugin &plugin) noexcept {
77 78 79 80 81
		return plugin.SupportsSuffix(suffix) &&
			(ScanFile(plugin) || ScanStream(plugin));
	}
};

82
bool
83
ScanFileTagsNoGeneric(Path path_fs, TagHandler &handler) noexcept
84
{
85
	assert(!path_fs.IsNull());
86 87 88

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

89
	const auto *suffix = path_fs.GetSuffix();
90
	if (suffix == nullptr)
91 92
		return false;

93 94
	const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8();

95
	TagFileScan tfs(path_fs, suffix_utf8.c_str(), handler);
96 97 98
	return decoder_plugins_try([&](const DecoderPlugin &plugin){
			return tfs.Scan(plugin);
		});
99
}
100 101

bool
102 103
ScanFileTagsWithGeneric(Path path, TagBuilder &builder,
			AudioFormat *audio_format) noexcept
104
{
105
	FullTagHandler h(builder, audio_format);
106

107
	if (!ScanFileTagsNoGeneric(path, h))
108 109
		return false;

110
	if (builder.empty())
111
		ScanGenericTags(path, h);
112 113 114

	return true;
}