TagFile.cxx 2.94 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 23 24
#include "tag/Generic.hxx"
#include "tag/TagHandler.hxx"
#include "tag/TagBuilder.hxx"
25
#include "fs/Path.hxx"
26
#include "util/Error.hxx"
27 28
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "input/InputStream.hxx"
30
#include "input/LocalOpen.hxx"
31
#include "thread/Cond.hxx"
32

33 34
#include <stdexcept>

35 36
#include <assert.h>

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

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

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

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

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

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

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

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

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

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

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

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

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

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;
}