From 9f0fb8f6a8f58ff9a760d40bccfadd010b9ddef9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 29 Jan 2013 21:21:07 +0100 Subject: ArchivePlugin: replace scan_reset(), scan_next() with visit() Add the interface ArchiveVisitor. --- src/archive/Bzip2ArchivePlugin.cxx | 22 +++------------- src/archive/Iso9660ArchivePlugin.cxx | 45 +++++++------------------------ src/archive/ZzipArchivePlugin.cxx | 51 ++++++++++++------------------------ 3 files changed, 31 insertions(+), 87 deletions(-) (limited to 'src/archive') diff --git a/src/archive/Bzip2ArchivePlugin.cxx b/src/archive/Bzip2ArchivePlugin.cxx index 4b0d5223..be7388d5 100644 --- a/src/archive/Bzip2ArchivePlugin.cxx +++ b/src/archive/Bzip2ArchivePlugin.cxx @@ -25,6 +25,7 @@ #include "Bzip2ArchivePlugin.hxx" #include "ArchiveInternal.hxx" #include "ArchivePlugin.hxx" +#include "ArchiveVisitor.hxx" #include "InputInternal.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" @@ -47,7 +48,6 @@ struct Bzip2ArchiveFile { struct refcount ref; char *name; - bool reset; struct input_stream *istream; Bzip2ArchiveFile() { @@ -152,24 +152,11 @@ bz2_open(const char *pathname, GError **error_r) } static void -bz2_scan_reset(struct archive_file *file) +bz2_visit(archive_file *file, ArchiveVisitor &visitor) { Bzip2ArchiveFile *context = (Bzip2ArchiveFile *) file; - context->reset = true; -} - -static const char * -bz2_scan_next(struct archive_file *file) -{ - Bzip2ArchiveFile *context = (Bzip2ArchiveFile *) file; - const char *name = NULL; - - if (context->reset) { - name = context->name; - context->reset = false; - } - return name; + visitor.VisitArchiveEntry(context->name); } static void @@ -318,8 +305,7 @@ const struct archive_plugin bz2_archive_plugin = { nullptr, nullptr, bz2_open, - bz2_scan_reset, - bz2_scan_next, + bz2_visit, bz2_open_stream, bz2_close, bz2_extensions, diff --git a/src/archive/Iso9660ArchivePlugin.cxx b/src/archive/Iso9660ArchivePlugin.cxx index fe752ff3..895087ef 100644 --- a/src/archive/Iso9660ArchivePlugin.cxx +++ b/src/archive/Iso9660ArchivePlugin.cxx @@ -25,6 +25,7 @@ #include "Iso9660ArchivePlugin.hxx" #include "ArchiveInternal.hxx" #include "ArchivePlugin.hxx" +#include "ArchiveVisitor.hxx" #include "InputInternal.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" @@ -46,22 +47,14 @@ struct Iso9660ArchiveFile { struct refcount ref; iso9660_t *iso; - GSList *list; - GSList *iter; Iso9660ArchiveFile(iso9660_t *_iso) - :iso(_iso), list(nullptr) { + :iso(_iso) { archive_file_init(&base, &iso9660_archive_plugin); refcount_init(&ref); } ~Iso9660ArchiveFile() { - //free list - for (GSList *tmp = list; tmp != NULL; tmp = g_slist_next(tmp)) - g_free(tmp->data); - g_slist_free(list); - - //close archive iso9660_close(iso); } @@ -70,7 +63,7 @@ struct Iso9660ArchiveFile { delete this; } - void CollectRecursive(const char *path); + void Visit(const char *path, ArchiveVisitor &visitor); }; extern const struct input_plugin iso9660_input_plugin; @@ -83,8 +76,8 @@ iso9660_quark(void) /* archive open && listing routine */ -void -Iso9660ArchiveFile::CollectRecursive(const char *psz_path) +inline void +Iso9660ArchiveFile::Visit(const char *psz_path, ArchiveVisitor &visitor) { CdioList_t *entlist; CdioListNode_t *entnode; @@ -105,11 +98,11 @@ Iso9660ArchiveFile::CollectRecursive(const char *psz_path) if (iso9660_stat_s::_STAT_DIR == statbuf->type ) { if (strcmp(statbuf->filename, ".") && strcmp(statbuf->filename, "..")) { strcat(pathname, "/"); - CollectRecursive(pathname); + Visit(pathname, visitor); } } else { //remove leading / - list = g_slist_prepend(list, g_strdup(pathname + 1)); + visitor.VisitArchiveEntry(pathname + 1); } } _cdio_list_free (entlist, true); @@ -127,33 +120,16 @@ iso9660_archive_open(const char *pathname, GError **error_r) } Iso9660ArchiveFile *archive = new Iso9660ArchiveFile(iso); - archive->CollectRecursive("/"); return &archive->base; } static void -iso9660_archive_scan_reset(struct archive_file *file) -{ - Iso9660ArchiveFile *context = - (Iso9660ArchiveFile *)file; - - //reset iterator - context->iter = context->list; -} - -static const char * -iso9660_archive_scan_next(struct archive_file *file) +iso9660_archive_visit(archive_file *file, ArchiveVisitor &visitor) { Iso9660ArchiveFile *context = (Iso9660ArchiveFile *)file; - const char *data = NULL; - if (context->iter != NULL) { - ///fetch data and goto next - data = (const char *)context->iter->data; - context->iter = g_slist_next(context->iter); - } - return data; + context->Visit("/", visitor); } static void @@ -296,8 +272,7 @@ const struct archive_plugin iso9660_archive_plugin = { nullptr, nullptr, iso9660_archive_open, - iso9660_archive_scan_reset, - iso9660_archive_scan_next, + iso9660_archive_visit, iso9660_archive_open_stream, iso9660_archive_close, iso9660_archive_extensions, diff --git a/src/archive/ZzipArchivePlugin.cxx b/src/archive/ZzipArchivePlugin.cxx index 81bf91f0..4075e217 100644 --- a/src/archive/ZzipArchivePlugin.cxx +++ b/src/archive/ZzipArchivePlugin.cxx @@ -25,6 +25,7 @@ #include "ZzipArchivePlugin.hxx" #include "ArchiveInternal.hxx" #include "ArchivePlugin.hxx" +#include "ArchiveVisitor.hxx" #include "InputInternal.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" @@ -40,8 +41,6 @@ struct ZzipArchiveFile { struct refcount ref; ZZIP_DIR *dir; - GSList *list; - GSList *iter; ZzipArchiveFile() { archive_file_init(&base, &zzip_archive_plugin); @@ -52,17 +51,13 @@ struct ZzipArchiveFile { if (!refcount_dec(&ref)) return; - if (list) { - //free list - for (GSList *tmp = list; tmp != NULL; tmp = g_slist_next(tmp)) - g_free(tmp->data); - g_slist_free(list); - } //close archive zzip_dir_close (dir); delete this; } + + void Visit(ArchiveVisitor &visitor); }; extern const struct input_plugin zzip_input_plugin; @@ -79,10 +74,8 @@ static struct archive_file * zzip_archive_open(const char *pathname, GError **error_r) { ZzipArchiveFile *context = new ZzipArchiveFile(); - ZZIP_DIRENT dirent; // open archive - context->list = NULL; context->dir = zzip_dir_open(pathname, NULL); if (context->dir == NULL) { g_set_error(error_r, zzip_quark(), 0, @@ -90,36 +83,27 @@ zzip_archive_open(const char *pathname, GError **error_r) return NULL; } - while (zzip_dir_read(context->dir, &dirent)) { - //add only files - if (dirent.st_size > 0) { - context->list = g_slist_prepend(context->list, - g_strdup(dirent.d_name)); - } - } - return &context->base; } -static void -zzip_archive_scan_reset(struct archive_file *file) +inline void +ZzipArchiveFile::Visit(ArchiveVisitor &visitor) { - ZzipArchiveFile *context = (ZzipArchiveFile *) file; - //reset iterator - context->iter = context->list; + zzip_rewinddir(dir); + + ZZIP_DIRENT dirent; + while (zzip_dir_read(dir, &dirent)) + //add only files + if (dirent.st_size > 0) + visitor.VisitArchiveEntry(dirent.d_name); } -static const char * -zzip_archive_scan_next(struct archive_file *file) +static void +zzip_archive_visit(archive_file *file, ArchiveVisitor &visitor) { ZzipArchiveFile *context = (ZzipArchiveFile *) file; - const char *data = NULL; - if (context->iter != NULL) { - ///fetch data and goto next - data = (const char *)context->iter->data; - context->iter = g_slist_next(context->iter); - } - return data; + + context->Visit(visitor); } static void @@ -260,8 +244,7 @@ const struct archive_plugin zzip_archive_plugin = { nullptr, nullptr, zzip_archive_open, - zzip_archive_scan_reset, - zzip_archive_scan_next, + zzip_archive_visit, zzip_archive_open_stream, zzip_archive_close, zzip_archive_extensions, -- cgit v1.2.3