ArchiveFile: convert to a class

This commit is contained in:
Max Kellermann 2013-01-29 23:26:51 +01:00
parent ba51045d9e
commit f5c0b0d316
10 changed files with 66 additions and 85 deletions

View File

@ -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) \

View File

@ -17,18 +17,15 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_ARCHIVE_INTERNAL_HXX
#define MPD_ARCHIVE_INTERNAL_HXX
#ifndef MPD_ARCHIVE_FILE_HXX
#define MPD_ARCHIVE_FILE_HXX
struct archive_file {
const struct archive_plugin *plugin;
class ArchiveFile {
public:
const struct archive_plugin &plugin;
ArchiveFile(const struct archive_plugin &_plugin)
:plugin(_plugin) {}
};
static inline void
archive_file_init(struct archive_file *archive_file,
const struct archive_plugin *plugin)
{
archive_file->plugin = plugin;
}
#endif

View File

@ -18,28 +18,25 @@
*/
#include "ArchivePlugin.hxx"
#include "ArchiveInternal.hxx"
#include "ArchiveFile.hxx"
#include <assert.h>
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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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 <glib.h>
#include <string.h>
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)

View File

@ -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;

View File

@ -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);