From f5c0b0d3168d6c34010f86a4b6878b7181d10237 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 29 Jan 2013 23:26:51 +0100 Subject: ArchiveFile: convert to a class --- Makefile.am | 2 +- src/ArchiveFile.hxx | 31 +++++++++++++++++++++++++++++++ src/ArchiveInternal.hxx | 34 ---------------------------------- src/ArchivePlugin.cxx | 36 +++++++++++++++--------------------- src/ArchivePlugin.hxx | 18 +++++++++--------- src/UpdateArchive.cxx | 3 +-- src/archive/Bzip2ArchivePlugin.cxx | 23 ++++++++++------------- src/archive/Iso9660ArchivePlugin.cxx | 22 +++++++++------------- src/archive/ZzipArchivePlugin.cxx | 21 +++++++++------------ src/input/ArchiveInputPlugin.cxx | 3 +-- test/visit_archive.cxx | 4 +++- 11 files changed, 89 insertions(+), 108 deletions(-) create mode 100644 src/ArchiveFile.hxx delete mode 100644 src/ArchiveInternal.hxx diff --git a/Makefile.am b/Makefile.am index ad6b14a3c..3a1c72a9e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -399,7 +399,7 @@ libarchive_a_SOURCES = \ src/ArchiveList.cxx src/ArchiveList.hxx \ src/ArchivePlugin.cxx src/ArchivePlugin.hxx \ src/ArchiveVisitor.hxx \ - src/ArchiveInternal.hxx \ + src/ArchiveFile.hxx \ src/input/ArchiveInputPlugin.cxx src/input/ArchiveInputPlugin.hxx libarchive_a_CPPFLAGS = $(AM_CPPFLAGS) \ $(BZ2_CFLAGS) \ diff --git a/src/ArchiveFile.hxx b/src/ArchiveFile.hxx new file mode 100644 index 000000000..52e5a4910 --- /dev/null +++ b/src/ArchiveFile.hxx @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +#ifndef MPD_ARCHIVE_FILE_HXX +#define MPD_ARCHIVE_FILE_HXX + +class ArchiveFile { +public: + const struct archive_plugin &plugin; + + ArchiveFile(const struct archive_plugin &_plugin) + :plugin(_plugin) {} +}; + +#endif diff --git a/src/ArchiveInternal.hxx b/src/ArchiveInternal.hxx deleted file mode 100644 index f0bf2e108..000000000 --- a/src/ArchiveInternal.hxx +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2003-2013 The Music Player Daemon Project - * 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. - */ - -#ifndef MPD_ARCHIVE_INTERNAL_HXX -#define MPD_ARCHIVE_INTERNAL_HXX - -struct archive_file { - const struct archive_plugin *plugin; -}; - -static inline void -archive_file_init(struct archive_file *archive_file, - const struct archive_plugin *plugin) -{ - archive_file->plugin = plugin; -} - -#endif diff --git a/src/ArchivePlugin.cxx b/src/ArchivePlugin.cxx index 95f49a2f8..db4bb1b58 100644 --- a/src/ArchivePlugin.cxx +++ b/src/ArchivePlugin.cxx @@ -18,28 +18,25 @@ */ #include "ArchivePlugin.hxx" -#include "ArchiveInternal.hxx" +#include "ArchiveFile.hxx" #include -struct archive_file * +ArchiveFile * archive_file_open(const struct archive_plugin *plugin, const char *path, GError **error_r) { - struct archive_file *file; - assert(plugin != NULL); assert(plugin->open != NULL); assert(path != NULL); assert(error_r == NULL || *error_r == NULL); - file = plugin->open(path, error_r); + ArchiveFile *file = plugin->open(path, error_r); if (file != NULL) { - assert(file->plugin != NULL); - assert(file->plugin->close != NULL); - assert(file->plugin->visit != nullptr); - assert(file->plugin->open_stream != NULL); + assert(file->plugin.close != NULL); + assert(file->plugin.visit != nullptr); + assert(file->plugin.open_stream != NULL); assert(error_r == NULL || *error_r == NULL); } else { assert(error_r == NULL || *error_r != NULL); @@ -49,34 +46,31 @@ archive_file_open(const struct archive_plugin *plugin, const char *path, } void -archive_file_close(struct archive_file *file) +archive_file_close(ArchiveFile *file) { assert(file != NULL); - assert(file->plugin != NULL); - assert(file->plugin->close != NULL); + assert(file->plugin.close != NULL); - file->plugin->close(file); + file->plugin.close(file); } void -archive_file_visit(archive_file *file, ArchiveVisitor &visitor) +archive_file_visit(ArchiveFile *file, ArchiveVisitor &visitor) { assert(file != NULL); - assert(file->plugin != NULL); - assert(file->plugin->visit != nullptr); + assert(file->plugin.visit != nullptr); - file->plugin->visit(file, visitor); + file->plugin.visit(file, visitor); } struct input_stream * -archive_file_open_stream(struct archive_file *file, const char *path, +archive_file_open_stream(ArchiveFile *file, const char *path, Mutex &mutex, Cond &cond, GError **error_r) { assert(file != NULL); - assert(file->plugin != NULL); - assert(file->plugin->open_stream != NULL); + assert(file->plugin.open_stream != NULL); - return file->plugin->open_stream(file, path, mutex, cond, + return file->plugin.open_stream(file, path, mutex, cond, error_r); } diff --git a/src/ArchivePlugin.hxx b/src/ArchivePlugin.hxx index 007a4f15a..6d043dfac 100644 --- a/src/ArchivePlugin.hxx +++ b/src/ArchivePlugin.hxx @@ -25,7 +25,7 @@ #include "gerror.h" struct input_stream; -struct archive_file; +class ArchiveFile; class ArchiveVisitor; struct archive_plugin { @@ -49,12 +49,12 @@ struct archive_plugin { * returns pointer to handle used is all operations with this archive * or NULL when opening fails */ - struct archive_file *(*open)(const char *path_fs, GError **error_r); + ArchiveFile *(*open)(const char *path_fs, GError **error_r); /** * Visit all entries inside this archive. */ - void (*visit)(archive_file *af, ArchiveVisitor &visitor); + void (*visit)(ArchiveFile *af, ArchiveVisitor &visitor); /** * Opens an input_stream of a file within the archive. @@ -63,7 +63,7 @@ struct archive_plugin { * @param error_r location to store the error occurring, or * NULL to ignore errors */ - struct input_stream *(*open_stream)(struct archive_file *af, + struct input_stream *(*open_stream)(ArchiveFile *af, const char *path, Mutex &mutex, Cond &cond, GError **error_r); @@ -71,7 +71,7 @@ struct archive_plugin { /** * closes archive file. */ - void (*close)(struct archive_file *); + void (*close)(ArchiveFile *); /** * suffixes handled by this plugin. @@ -80,18 +80,18 @@ struct archive_plugin { const char *const*suffixes; }; -struct archive_file * +ArchiveFile * archive_file_open(const struct archive_plugin *plugin, const char *path, GError **error_r); void -archive_file_close(struct archive_file *file); +archive_file_close(ArchiveFile *file); void -archive_file_visit(archive_file *file, ArchiveVisitor &visitor); +archive_file_visit(ArchiveFile *file, ArchiveVisitor &visitor); struct input_stream * -archive_file_open_stream(struct archive_file *file, const char *path, +archive_file_open_stream(ArchiveFile *file, const char *path, Mutex &mutex, Cond &cond, GError **error_r); diff --git a/src/UpdateArchive.cxx b/src/UpdateArchive.cxx index 88d67fbc0..41a73b421 100644 --- a/src/UpdateArchive.cxx +++ b/src/UpdateArchive.cxx @@ -101,8 +101,7 @@ update_archive_file2(Directory *parent, const char *name, /* open archive */ GError *error = NULL; - struct archive_file *file = archive_file_open(plugin, path_fs.c_str(), - &error); + ArchiveFile *file = archive_file_open(plugin, path_fs.c_str(), &error); if (file == NULL) { g_warning("%s", error->message); g_error_free(error); diff --git a/src/archive/Bzip2ArchivePlugin.cxx b/src/archive/Bzip2ArchivePlugin.cxx index f93718128..daa04b975 100644 --- a/src/archive/Bzip2ArchivePlugin.cxx +++ b/src/archive/Bzip2ArchivePlugin.cxx @@ -23,8 +23,8 @@ #include "config.h" #include "Bzip2ArchivePlugin.hxx" -#include "ArchiveInternal.hxx" #include "ArchivePlugin.hxx" +#include "ArchiveFile.hxx" #include "ArchiveVisitor.hxx" #include "InputInternal.hxx" #include "InputStream.hxx" @@ -42,17 +42,14 @@ #define BZ2_bzDecompress bzDecompress #endif -struct Bzip2ArchiveFile { - struct archive_file base; - +class Bzip2ArchiveFile : public ArchiveFile { +public: RefCount ref; char *name; struct input_stream *istream; - Bzip2ArchiveFile() { - archive_file_init(&base, &bz2_archive_plugin); - } + Bzip2ArchiveFile():ArchiveFile(bz2_archive_plugin) {} void Unref() { if (!ref.Decrement()) @@ -123,7 +120,7 @@ Bzip2InputStream::Close() /* archive open && listing routine */ -static struct archive_file * +static ArchiveFile * bz2_open(const char *pathname, GError **error_r) { Bzip2ArchiveFile *context = new Bzip2ArchiveFile(); @@ -147,11 +144,11 @@ bz2_open(const char *pathname, GError **error_r) context->name[len - 4] = 0; //remove .bz2 suffix } - return &context->base; + return context; } static void -bz2_visit(archive_file *file, ArchiveVisitor &visitor) +bz2_visit(ArchiveFile *file, ArchiveVisitor &visitor) { Bzip2ArchiveFile *context = (Bzip2ArchiveFile *) file; @@ -159,7 +156,7 @@ bz2_visit(archive_file *file, ArchiveVisitor &visitor) } static void -bz2_close(struct archive_file *file) +bz2_close(ArchiveFile *file) { Bzip2ArchiveFile *context = (Bzip2ArchiveFile *) file; @@ -178,11 +175,11 @@ Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context, const char *uri, Bzip2InputStream::~Bzip2InputStream() { - bz2_close(&archive->base); + archive->Unref(); } static struct input_stream * -bz2_open_stream(struct archive_file *file, const char *path, +bz2_open_stream(ArchiveFile *file, const char *path, Mutex &mutex, Cond &cond, GError **error_r) { diff --git a/src/archive/Iso9660ArchivePlugin.cxx b/src/archive/Iso9660ArchivePlugin.cxx index 344cdac6a..21e2fa41c 100644 --- a/src/archive/Iso9660ArchivePlugin.cxx +++ b/src/archive/Iso9660ArchivePlugin.cxx @@ -23,8 +23,8 @@ #include "config.h" #include "Iso9660ArchivePlugin.hxx" -#include "ArchiveInternal.hxx" #include "ArchivePlugin.hxx" +#include "ArchiveFile.hxx" #include "ArchiveVisitor.hxx" #include "InputInternal.hxx" #include "InputStream.hxx" @@ -41,17 +41,14 @@ #define CEILING(x, y) ((x+(y-1))/y) -struct Iso9660ArchiveFile { - struct archive_file base; - +class Iso9660ArchiveFile : public ArchiveFile { +public: RefCount ref; iso9660_t *iso; Iso9660ArchiveFile(iso9660_t *_iso) - :iso(_iso) { - archive_file_init(&base, &iso9660_archive_plugin); - } + :ArchiveFile(iso9660_archive_plugin), iso(_iso) {} ~Iso9660ArchiveFile() { iso9660_close(iso); @@ -107,7 +104,7 @@ Iso9660ArchiveFile::Visit(const char *psz_path, ArchiveVisitor &visitor) _cdio_list_free (entlist, true); } -static struct archive_file * +static ArchiveFile * iso9660_archive_open(const char *pathname, GError **error_r) { /* open archive */ @@ -118,12 +115,11 @@ iso9660_archive_open(const char *pathname, GError **error_r) return NULL; } - Iso9660ArchiveFile *archive = new Iso9660ArchiveFile(iso); - return &archive->base; + return new Iso9660ArchiveFile(iso); } static void -iso9660_archive_visit(archive_file *file, ArchiveVisitor &visitor) +iso9660_archive_visit(ArchiveFile *file, ArchiveVisitor &visitor) { Iso9660ArchiveFile *context = (Iso9660ArchiveFile *)file; @@ -132,7 +128,7 @@ iso9660_archive_visit(archive_file *file, ArchiveVisitor &visitor) } static void -iso9660_archive_close(struct archive_file *file) +iso9660_archive_close(ArchiveFile *file) { Iso9660ArchiveFile *context = (Iso9660ArchiveFile *)file; @@ -170,7 +166,7 @@ struct Iso9660InputStream { }; static struct input_stream * -iso9660_archive_open_stream(struct archive_file *file, const char *pathname, +iso9660_archive_open_stream(ArchiveFile *file, const char *pathname, Mutex &mutex, Cond &cond, GError **error_r) { diff --git a/src/archive/ZzipArchivePlugin.cxx b/src/archive/ZzipArchivePlugin.cxx index 1b089a484..ba001bdd5 100644 --- a/src/archive/ZzipArchivePlugin.cxx +++ b/src/archive/ZzipArchivePlugin.cxx @@ -23,8 +23,8 @@ #include "config.h" #include "ZzipArchivePlugin.hxx" -#include "ArchiveInternal.hxx" #include "ArchivePlugin.hxx" +#include "ArchiveFile.hxx" #include "ArchiveVisitor.hxx" #include "InputInternal.hxx" #include "InputStream.hxx" @@ -35,16 +35,13 @@ #include #include -struct ZzipArchiveFile { - struct archive_file base; - +class ZzipArchiveFile : public ArchiveFile { +public: RefCount ref; ZZIP_DIR *dir; - ZzipArchiveFile() { - archive_file_init(&base, &zzip_archive_plugin); - } + ZzipArchiveFile():ArchiveFile(zzip_archive_plugin) {} void Unref() { if (!ref.Decrement()) @@ -69,7 +66,7 @@ zzip_quark(void) /* archive open && listing routine */ -static struct archive_file * +static ArchiveFile * zzip_archive_open(const char *pathname, GError **error_r) { ZzipArchiveFile *context = new ZzipArchiveFile(); @@ -82,7 +79,7 @@ zzip_archive_open(const char *pathname, GError **error_r) return NULL; } - return &context->base; + return context; } inline void @@ -98,7 +95,7 @@ ZzipArchiveFile::Visit(ArchiveVisitor &visitor) } static void -zzip_archive_visit(archive_file *file, ArchiveVisitor &visitor) +zzip_archive_visit(ArchiveFile *file, ArchiveVisitor &visitor) { ZzipArchiveFile *context = (ZzipArchiveFile *) file; @@ -106,7 +103,7 @@ zzip_archive_visit(archive_file *file, ArchiveVisitor &visitor) } static void -zzip_archive_close(struct archive_file *file) +zzip_archive_close(ArchiveFile *file) { ZzipArchiveFile *context = (ZzipArchiveFile *) file; @@ -145,7 +142,7 @@ struct ZzipInputStream { }; static struct input_stream * -zzip_archive_open_stream(struct archive_file *file, +zzip_archive_open_stream(ArchiveFile *file, const char *pathname, Mutex &mutex, Cond &cond, GError **error_r) diff --git a/src/input/ArchiveInputPlugin.cxx b/src/input/ArchiveInputPlugin.cxx index 01a854523..fde817da7 100644 --- a/src/input/ArchiveInputPlugin.cxx +++ b/src/input/ArchiveInputPlugin.cxx @@ -40,7 +40,6 @@ input_archive_open(const char *pathname, GError **error_r) { const struct archive_plugin *arplug; - struct archive_file *file; char *archive, *filename, *suffix, *pname; struct input_stream *is; @@ -63,7 +62,7 @@ input_archive_open(const char *pathname, return NULL; } - file = archive_file_open(arplug, archive, error_r); + auto file = archive_file_open(arplug, archive, error_r); if (file == NULL) { g_free(pname); return NULL; diff --git a/test/visit_archive.cxx b/test/visit_archive.cxx index b8cc2e019..eba715c57 100644 --- a/test/visit_archive.cxx +++ b/test/visit_archive.cxx @@ -25,6 +25,7 @@ #include "InputInit.hxx" #include "ArchiveList.hxx" #include "ArchivePlugin.hxx" +#include "ArchiveFile.hxx" #include "ArchiveVisitor.hxx" #include "fs/Path.hxx" @@ -97,10 +98,11 @@ main(int argc, char **argv) int result = EXIT_SUCCESS; - archive_file *file = archive_file_open(plugin, path.c_str(), &error); + ArchiveFile *file = archive_file_open(plugin, path.c_str(), &error); if (file != nullptr) { MyArchiveVisitor visitor; archive_file_visit(file, visitor); + archive_file_close(file); } else { fprintf(stderr, "%s\n", error->message); g_error_free(error); -- cgit v1.2.3