ArchiveList.cxx 2.59 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 21
#include "ArchiveList.hxx"
#include "ArchivePlugin.hxx"
22
#include "archive/Features.h"
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
constexpr const ArchivePlugin *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 47
static constexpr std::size_t n_archive_plugins = std::size(archive_plugins) - 1;

48
/** which plugins have been initialized successfully? */
49 50 51
/* the std::max() is just here to avoid a zero-sized array, which is
   forbidden in C++ */
static bool archive_plugins_enabled[std::max(n_archive_plugins, std::size_t(1))];
52

53 54 55 56
#define archive_plugins_for_each_enabled(plugin) \
	archive_plugins_for_each(plugin) \
		if (archive_plugins_enabled[archive_plugin_iterator - archive_plugins])

57
const ArchivePlugin *
58
archive_plugin_from_suffix(std::string_view suffix) noexcept
59
{
60
	archive_plugins_for_each_enabled(plugin)
61
		if (plugin->suffixes != nullptr &&
62
		    StringArrayContainsCase(plugin->suffixes, suffix))
63
			return plugin;
64

65
	return nullptr;
66 67
}

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

75
	return nullptr;
76 77
}

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

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