archive_list: add _for_each() macros
This commit is contained in:
parent
48da345e79
commit
9ebbdb9b0b
@ -28,7 +28,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
static const struct archive_plugin *const archive_plugins[] = {
|
const struct archive_plugin *const archive_plugins[] = {
|
||||||
#ifdef HAVE_BZ2
|
#ifdef HAVE_BZ2
|
||||||
&bz2_archive_plugin,
|
&bz2_archive_plugin,
|
||||||
#endif
|
#endif
|
||||||
@ -44,44 +44,38 @@ static const struct archive_plugin *const archive_plugins[] = {
|
|||||||
/** which plugins have been initialized successfully? */
|
/** which plugins have been initialized successfully? */
|
||||||
static bool archive_plugins_enabled[G_N_ELEMENTS(archive_plugins) - 1];
|
static bool archive_plugins_enabled[G_N_ELEMENTS(archive_plugins) - 1];
|
||||||
|
|
||||||
|
#define archive_plugins_for_each_enabled(plugin) \
|
||||||
|
archive_plugins_for_each(plugin) \
|
||||||
|
if (archive_plugins_enabled[archive_plugin_iterator - archive_plugins])
|
||||||
|
|
||||||
const struct archive_plugin *
|
const struct archive_plugin *
|
||||||
archive_plugin_from_suffix(const char *suffix)
|
archive_plugin_from_suffix(const char *suffix)
|
||||||
{
|
{
|
||||||
if (suffix == NULL)
|
if (suffix == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for (unsigned i = 0; archive_plugins[i] != NULL; ++i) {
|
archive_plugins_for_each_enabled(plugin)
|
||||||
const struct archive_plugin *plugin = archive_plugins[i];
|
if (plugin->suffixes != NULL &&
|
||||||
if (archive_plugins_enabled[i] &&
|
string_array_contains(plugin->suffixes, suffix))
|
||||||
plugin->suffixes != NULL &&
|
|
||||||
string_array_contains(plugin->suffixes, suffix)) {
|
|
||||||
++i;
|
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct archive_plugin *
|
const struct archive_plugin *
|
||||||
archive_plugin_from_name(const char *name)
|
archive_plugin_from_name(const char *name)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; archive_plugins[i] != NULL; ++i) {
|
archive_plugins_for_each_enabled(plugin)
|
||||||
const struct archive_plugin *plugin = archive_plugins[i];
|
if (strcmp(plugin->name, name) == 0)
|
||||||
if (archive_plugins_enabled[i] &&
|
|
||||||
strcmp(plugin->name, name) == 0)
|
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void archive_plugin_print_all_suffixes(FILE * fp)
|
void archive_plugin_print_all_suffixes(FILE * fp)
|
||||||
{
|
{
|
||||||
const char *const*suffixes;
|
archive_plugins_for_each(plugin) {
|
||||||
|
const char *const*suffixes = plugin->suffixes;
|
||||||
for (unsigned i = 0; archive_plugins[i] != NULL; ++i) {
|
|
||||||
const struct archive_plugin *plugin = archive_plugins[i];
|
|
||||||
|
|
||||||
suffixes = plugin->suffixes;
|
|
||||||
while (suffixes && *suffixes) {
|
while (suffixes && *suffixes) {
|
||||||
fprintf(fp, "%s ", *suffixes);
|
fprintf(fp, "%s ", *suffixes);
|
||||||
suffixes++;
|
suffixes++;
|
||||||
@ -102,10 +96,8 @@ void archive_plugin_init_all(void)
|
|||||||
|
|
||||||
void archive_plugin_deinit_all(void)
|
void archive_plugin_deinit_all(void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; archive_plugins[i] != NULL; ++i) {
|
archive_plugins_for_each_enabled(plugin)
|
||||||
const struct archive_plugin *plugin = archive_plugins[i];
|
if (plugin->finish != NULL)
|
||||||
if (archive_plugins_enabled[i] && plugin->finish != NULL)
|
plugin->finish();
|
||||||
archive_plugins[i]->finish();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,14 @@
|
|||||||
|
|
||||||
struct archive_plugin;
|
struct archive_plugin;
|
||||||
|
|
||||||
|
extern const struct archive_plugin *const archive_plugins[];
|
||||||
|
|
||||||
|
#define archive_plugins_for_each(plugin) \
|
||||||
|
for (const struct archive_plugin *plugin, \
|
||||||
|
*const*archive_plugin_iterator = &archive_plugins[0]; \
|
||||||
|
(plugin = *archive_plugin_iterator) != NULL; \
|
||||||
|
++archive_plugin_iterator)
|
||||||
|
|
||||||
/* interface for using plugins */
|
/* interface for using plugins */
|
||||||
|
|
||||||
const struct archive_plugin *
|
const struct archive_plugin *
|
||||||
|
Loading…
Reference in New Issue
Block a user