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

20
#include "config.h"
21 22
#include "ArchiveList.hxx"
#include "ArchivePlugin.hxx"
23
#include "util/StringUtil.hxx"
24 25 26
#include "plugins/Bzip2ArchivePlugin.hxx"
#include "plugins/Iso9660ArchivePlugin.hxx"
#include "plugins/ZzipArchivePlugin.hxx"
27

28
#include <cassert>
29
#include <iterator>
30 31 32

#include <string.h>

33
const ArchivePlugin *const archive_plugins[] = {
34
#ifdef ENABLE_BZ2
35
	&bz2_archive_plugin,
36
#endif
37
#ifdef ENABLE_ZZIP
38
	&zzip_archive_plugin,
39
#endif
40
#ifdef ENABLE_ISO9660
41
	&iso9660_archive_plugin,
42
#endif
43
	nullptr
44 45 46
};

/** which plugins have been initialized successfully? */
47
static bool archive_plugins_enabled[std::size(archive_plugins) - 1];
48

49 50 51 52
#define archive_plugins_for_each_enabled(plugin) \
	archive_plugins_for_each(plugin) \
		if (archive_plugins_enabled[archive_plugin_iterator - archive_plugins])

53
const ArchivePlugin *
54
archive_plugin_from_suffix(const char *suffix) noexcept
55
{
56
	assert(suffix != nullptr);
57

58
	archive_plugins_for_each_enabled(plugin)
59
		if (plugin->suffixes != nullptr &&
60
		    StringArrayContainsCase(plugin->suffixes, suffix))
61
			return plugin;
62

63
	return nullptr;
64 65
}

66
const ArchivePlugin *
67
archive_plugin_from_name(const char *name) noexcept
68
{
69 70
	archive_plugins_for_each_enabled(plugin)
		if (strcmp(plugin->name, name) == 0)
71 72
			return plugin;

73
	return nullptr;
74 75
}

76
void archive_plugin_init_all()
77
{
78
	for (unsigned i = 0; archive_plugins[i] != nullptr; ++i) {
79
		const ArchivePlugin *plugin = archive_plugins[i];
80
		if (plugin->init == nullptr || archive_plugins[i]->init())
81 82 83 84
			archive_plugins_enabled[i] = true;
	}
}

85 86
void
archive_plugin_deinit_all() noexcept
87
{
88
	archive_plugins_for_each_enabled(plugin)
89
		if (plugin->finish != nullptr)
90
			plugin->finish();
91 92
}