input/InputStream: migrate from class Error to C++ exceptions

This commit is contained in:
Max Kellermann
2016-09-09 18:47:42 +02:00
parent 597e59f10d
commit 8c744efd56
64 changed files with 440 additions and 473 deletions

View File

@@ -30,8 +30,6 @@
#include "input/LocalOpen.hxx"
#include "thread/Cond.hxx"
#include "util/RefCount.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "fs/Path.hxx"
#include <bzlib.h>
@@ -96,15 +94,13 @@ public:
/* virtual methods from InputStream */
bool IsEOF() override;
size_t Read(void *ptr, size_t size, Error &error) override;
size_t Read(void *ptr, size_t size) override;
private:
void Open();
bool FillBuffer(Error &error);
bool FillBuffer();
};
static constexpr Domain bz2_domain("bz2");
/* single archive handling allocation helpers */
inline void
@@ -161,13 +157,12 @@ Bzip2ArchiveFile::OpenStream(const char *path,
}
inline bool
Bzip2InputStream::FillBuffer(Error &error)
Bzip2InputStream::FillBuffer()
{
if (bzstream.avail_in > 0)
return true;
size_t count = archive->istream->Read(buffer, sizeof(buffer),
error);
size_t count = archive->istream->Read(buffer, sizeof(buffer));
if (count == 0)
return false;
@@ -177,7 +172,7 @@ Bzip2InputStream::FillBuffer(Error &error)
}
size_t
Bzip2InputStream::Read(void *ptr, size_t length, Error &error)
Bzip2InputStream::Read(void *ptr, size_t length)
{
int bz_result;
size_t nbytes = 0;
@@ -189,7 +184,7 @@ Bzip2InputStream::Read(void *ptr, size_t length, Error &error)
bzstream.avail_out = length;
do {
if (!FillBuffer(error))
if (!FillBuffer())
return 0;
bz_result = BZ2_bzDecompress(&bzstream);
@@ -199,11 +194,8 @@ Bzip2InputStream::Read(void *ptr, size_t length, Error &error)
break;
}
if (bz_result != BZ_OK) {
error.Set(bz2_domain, bz_result,
"BZ2_bzDecompress() has failed");
return 0;
}
if (bz_result != BZ_OK)
throw std::runtime_error("BZ2_bzDecompress() has failed");
} while (bzstream.avail_out == length);
nbytes = length - bzstream.avail_out;

View File

@@ -30,8 +30,6 @@
#include "fs/Path.hxx"
#include "util/RefCount.hxx"
#include "util/RuntimeError.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include <cdio/iso9660.h>
@@ -82,8 +80,6 @@ public:
Mutex &mutex, Cond &cond) override;
};
static constexpr Domain iso9660_domain("iso9660");
/* archive open && listing routine */
inline void
@@ -167,7 +163,7 @@ public:
/* virtual methods from InputStream */
bool IsEOF() override;
size_t Read(void *ptr, size_t size, Error &error) override;
size_t Read(void *ptr, size_t size) override;
};
InputStream *
@@ -184,7 +180,7 @@ Iso9660ArchiveFile::OpenStream(const char *pathname,
}
size_t
Iso9660InputStream::Read(void *ptr, size_t read_size, Error &error)
Iso9660InputStream::Read(void *ptr, size_t read_size)
{
int readed = 0;
int no_blocks, cur_block;
@@ -204,12 +200,10 @@ Iso9660InputStream::Read(void *ptr, size_t read_size, Error &error)
readed = archive.SeekRead(ptr, statbuf->lsn + cur_block,
no_blocks);
if (readed != no_blocks * ISO_BLOCKSIZE) {
error.Format(iso9660_domain,
"error reading ISO file at lsn %lu",
(unsigned long)cur_block);
return 0;
}
if (readed != no_blocks * ISO_BLOCKSIZE)
throw FormatRuntimeError("error reading ISO file at lsn %lu",
(unsigned long)cur_block);
if (left_bytes < read_size) {
readed = left_bytes;
}

View File

@@ -30,8 +30,6 @@
#include "fs/Path.hxx"
#include "util/RefCount.hxx"
#include "util/RuntimeError.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include <zzip/zzip.h>
@@ -63,8 +61,6 @@ public:
Mutex &mutex, Cond &cond) override;
};
static constexpr Domain zzip_domain("zzip");
/* archive open && listing routine */
static ArchiveFile *
@@ -121,8 +117,8 @@ struct ZzipInputStream final : public InputStream {
/* virtual methods from InputStream */
bool IsEOF() override;
size_t Read(void *ptr, size_t size, Error &error) override;
bool Seek(offset_type offset, Error &error) override;
size_t Read(void *ptr, size_t size) override;
void Seek(offset_type offset) override;
};
InputStream *
@@ -140,13 +136,11 @@ ZzipArchiveFile::OpenStream(const char *pathname,
}
size_t
ZzipInputStream::Read(void *ptr, size_t read_size, Error &error)
ZzipInputStream::Read(void *ptr, size_t read_size)
{
int ret = zzip_file_read(file, ptr, read_size);
if (ret < 0) {
error.Set(zzip_domain, "zzip_file_read() has failed");
return 0;
}
if (ret < 0)
throw std::runtime_error("zzip_file_read() has failed");
offset = zzip_tell(file);
return ret;
@@ -158,17 +152,14 @@ ZzipInputStream::IsEOF()
return offset_type(zzip_tell(file)) == size;
}
bool
ZzipInputStream::Seek(offset_type new_offset, Error &error)
void
ZzipInputStream::Seek(offset_type new_offset)
{
zzip_off_t ofs = zzip_seek(file, new_offset, SEEK_SET);
if (ofs < 0) {
error.Set(zzip_domain, "zzip_seek() has failed");
return false;
}
if (ofs < 0)
throw std::runtime_error("zzip_seek() has failed");
offset = ofs;
return true;
}
/* exported structures */