archive/iso9660: convert structs to classes

This commit is contained in:
Max Kellermann 2013-11-24 22:41:23 +01:00
parent 6396e23a2a
commit dbda35ffe1

View File

@ -41,11 +41,11 @@
#define CEILING(x, y) ((x+(y-1))/y) #define CEILING(x, y) ((x+(y-1))/y)
class Iso9660ArchiveFile final : public ArchiveFile { class Iso9660ArchiveFile final : public ArchiveFile {
public:
RefCount ref; RefCount ref;
iso9660_t *iso; iso9660_t *iso;
public:
Iso9660ArchiveFile(iso9660_t *_iso) Iso9660ArchiveFile(iso9660_t *_iso)
:ArchiveFile(iso9660_archive_plugin), iso(_iso) {} :ArchiveFile(iso9660_archive_plugin), iso(_iso) {}
@ -53,11 +53,19 @@ public:
iso9660_close(iso); iso9660_close(iso);
} }
void Ref() {
ref.Increment();
}
void Unref() { void Unref() {
if (ref.Decrement()) if (ref.Decrement())
delete this; delete this;
} }
long SeekRead(void *ptr, lsn_t start, long int i_size) const {
return iso9660_iso_seek_read(iso, ptr, start, i_size);
}
void Visit(const char *path, ArchiveVisitor &visitor); void Visit(const char *path, ArchiveVisitor &visitor);
virtual void Close() override { virtual void Close() override {
@ -131,31 +139,35 @@ Iso9660ArchiveFile::Visit(ArchiveVisitor &visitor)
/* single archive handling */ /* single archive handling */
struct Iso9660InputStream { class Iso9660InputStream {
InputStream base; InputStream base;
Iso9660ArchiveFile *archive; Iso9660ArchiveFile *archive;
iso9660_stat_t *statbuf; iso9660_stat_t *statbuf;
size_t max_blocks;
public:
Iso9660InputStream(Iso9660ArchiveFile &_archive, const char *uri, Iso9660InputStream(Iso9660ArchiveFile &_archive, const char *uri,
Mutex &mutex, Cond &cond, Mutex &mutex, Cond &cond,
iso9660_stat_t *_statbuf) iso9660_stat_t *_statbuf)
:base(iso9660_input_plugin, uri, mutex, cond), :base(iso9660_input_plugin, uri, mutex, cond),
archive(&_archive), statbuf(_statbuf), archive(&_archive), statbuf(_statbuf) {
max_blocks(CEILING(statbuf->size, ISO_BLOCKSIZE)) {
base.ready = true; base.ready = true;
base.size = statbuf->size; base.size = statbuf->size;
archive->ref.Increment(); archive->Ref();
} }
~Iso9660InputStream() { ~Iso9660InputStream() {
free(statbuf); free(statbuf);
archive->Unref(); archive->Unref();
} }
InputStream *Get() {
return &base;
}
size_t Read(void *ptr, size_t size, Error &error);
}; };
InputStream * InputStream *
@ -173,7 +185,7 @@ Iso9660ArchiveFile::OpenStream(const char *pathname,
Iso9660InputStream *iis = Iso9660InputStream *iis =
new Iso9660InputStream(*this, pathname, mutex, cond, new Iso9660InputStream(*this, pathname, mutex, cond,
statbuf); statbuf);
return &iis->base; return iis->Get();
} }
static void static void
@ -184,15 +196,12 @@ iso9660_input_close(InputStream *is)
delete iis; delete iis;
} }
inline size_t
static size_t Iso9660InputStream::Read(void *ptr, size_t size, Error &error)
iso9660_input_read(InputStream *is, void *ptr, size_t size,
Error &error)
{ {
Iso9660InputStream *iis = (Iso9660InputStream *)is;
int readed = 0; int readed = 0;
int no_blocks, cur_block; int no_blocks, cur_block;
size_t left_bytes = iis->statbuf->size - is->offset; size_t left_bytes = statbuf->size - base.offset;
size = (size * ISO_BLOCKSIZE) / ISO_BLOCKSIZE; size = (size * ISO_BLOCKSIZE) / ISO_BLOCKSIZE;
@ -205,10 +214,10 @@ iso9660_input_read(InputStream *is, void *ptr, size_t size,
if (no_blocks == 0) if (no_blocks == 0)
return 0; return 0;
cur_block = is->offset / ISO_BLOCKSIZE; cur_block = base.offset / ISO_BLOCKSIZE;
readed = iso9660_iso_seek_read (iis->archive->iso, ptr, readed = archive->SeekRead(ptr, statbuf->lsn + cur_block,
iis->statbuf->lsn + cur_block, no_blocks); no_blocks);
if (readed != no_blocks * ISO_BLOCKSIZE) { if (readed != no_blocks * ISO_BLOCKSIZE) {
error.Format(iso9660_domain, error.Format(iso9660_domain,
@ -220,10 +229,18 @@ iso9660_input_read(InputStream *is, void *ptr, size_t size,
readed = left_bytes; readed = left_bytes;
} }
is->offset += readed; base.offset += readed;
return readed; return readed;
} }
static size_t
iso9660_input_read(InputStream *is, void *ptr, size_t size,
Error &error)
{
Iso9660InputStream *iis = (Iso9660InputStream *)is;
return iis->Read(ptr, size, error);
}
static bool static bool
iso9660_input_eof(InputStream *is) iso9660_input_eof(InputStream *is)
{ {