diff options
author | Max Kellermann <max@musicpd.org> | 2016-09-09 15:37:06 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2016-09-09 18:15:01 +0200 |
commit | fc7d3f64c007672e4c01172ccb1c46d083a8abd5 (patch) | |
tree | 3c72f16ee6cc919a17558678a1e270568182b8af /src/archive | |
parent | 63ab7767a37a99b5651f32e067aeb5fcbb7bd033 (diff) |
input/Plugin: migrate open() from class Error to C++ exceptions
Diffstat (limited to 'src/archive')
-rw-r--r-- | src/archive/ArchiveFile.hxx | 5 | ||||
-rw-r--r-- | src/archive/plugins/Bzip2ArchivePlugin.cxx | 41 | ||||
-rw-r--r-- | src/archive/plugins/Iso9660ArchivePlugin.cxx | 17 | ||||
-rw-r--r-- | src/archive/plugins/ZzipArchivePlugin.cxx | 17 |
4 files changed, 31 insertions, 49 deletions
diff --git a/src/archive/ArchiveFile.hxx b/src/archive/ArchiveFile.hxx index 3e9e7a208..b7335072c 100644 --- a/src/archive/ArchiveFile.hxx +++ b/src/archive/ArchiveFile.hxx @@ -51,11 +51,12 @@ public: /** * Opens an InputStream of a file within the archive. * + * Throws std::runtime_error on error. + * * @param path the path within the archive */ virtual InputStream *OpenStream(const char *path, - Mutex &mutex, Cond &cond, - Error &error) = 0; + Mutex &mutex, Cond &cond) = 0; }; #endif diff --git a/src/archive/plugins/Bzip2ArchivePlugin.cxx b/src/archive/plugins/Bzip2ArchivePlugin.cxx index 63ff2841c..1601b8c42 100644 --- a/src/archive/plugins/Bzip2ArchivePlugin.cxx +++ b/src/archive/plugins/Bzip2ArchivePlugin.cxx @@ -36,6 +36,8 @@ #include <bzlib.h> +#include <stdexcept> + #include <stddef.h> class Bzip2ArchiveFile final : public ArchiveFile { @@ -74,9 +76,8 @@ public: visitor.VisitArchiveEntry(name.c_str()); } - virtual InputStream *OpenStream(const char *path, - Mutex &mutex, Cond &cond, - Error &error) override; + InputStream *OpenStream(const char *path, + Mutex &mutex, Cond &cond) override; }; class Bzip2InputStream final : public InputStream { @@ -93,13 +94,12 @@ public: Mutex &mutex, Cond &cond); ~Bzip2InputStream(); - bool Open(Error &error); - /* virtual methods from InputStream */ bool IsEOF() override; size_t Read(void *ptr, size_t size, Error &error) override; private: + void Open(); bool FillBuffer(Error &error); }; @@ -107,8 +107,8 @@ static constexpr Domain bz2_domain("bz2"); /* single archive handling allocation helpers */ -inline bool -Bzip2InputStream::Open(Error &error) +inline void +Bzip2InputStream::Open() { bzstream.bzalloc = nullptr; bzstream.bzfree = nullptr; @@ -118,27 +118,20 @@ Bzip2InputStream::Open(Error &error) bzstream.avail_in = 0; int ret = BZ2_bzDecompressInit(&bzstream, 0, 0); - if (ret != BZ_OK) { - error.Set(bz2_domain, ret, - "BZ2_bzDecompressInit() has failed"); - return false; - } + if (ret != BZ_OK) + throw std::runtime_error("BZ2_bzDecompressInit() has failed"); SetReady(); - return true; } /* archive open && listing routine */ static ArchiveFile * -bz2_open(Path pathname, Error &error) +bz2_open(Path pathname, gcc_unused Error &error) { static Mutex mutex; static Cond cond; - auto is = OpenLocalInputStream(pathname, mutex, cond, error); - if (is == nullptr) - return nullptr; - + auto is = OpenLocalInputStream(pathname, mutex, cond); return new Bzip2ArchiveFile(pathname, std::move(is)); } @@ -150,6 +143,7 @@ Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context, :InputStream(_uri, _mutex, _cond), archive(&_context) { + Open(); archive->Ref(); } @@ -161,16 +155,9 @@ Bzip2InputStream::~Bzip2InputStream() InputStream * Bzip2ArchiveFile::OpenStream(const char *path, - Mutex &mutex, Cond &cond, - Error &error) + Mutex &mutex, Cond &cond) { - Bzip2InputStream *bis = new Bzip2InputStream(*this, path, mutex, cond); - if (!bis->Open(error)) { - delete bis; - return nullptr; - } - - return bis; + return new Bzip2InputStream(*this, path, mutex, cond); } inline bool diff --git a/src/archive/plugins/Iso9660ArchivePlugin.cxx b/src/archive/plugins/Iso9660ArchivePlugin.cxx index d077e3aef..44e3ae71a 100644 --- a/src/archive/plugins/Iso9660ArchivePlugin.cxx +++ b/src/archive/plugins/Iso9660ArchivePlugin.cxx @@ -29,6 +29,7 @@ #include "input/InputStream.hxx" #include "fs/Path.hxx" #include "util/RefCount.hxx" +#include "util/RuntimeError.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" @@ -77,9 +78,8 @@ public: virtual void Visit(ArchiveVisitor &visitor) override; - virtual InputStream *OpenStream(const char *path, - Mutex &mutex, Cond &cond, - Error &error) override; + InputStream *OpenStream(const char *path, + Mutex &mutex, Cond &cond) override; }; static constexpr Domain iso9660_domain("iso9660"); @@ -175,15 +175,12 @@ public: InputStream * Iso9660ArchiveFile::OpenStream(const char *pathname, - Mutex &mutex, Cond &cond, - Error &error) + Mutex &mutex, Cond &cond) { auto statbuf = iso9660_ifs_stat_translate(iso, pathname); - if (statbuf == nullptr) { - error.Format(iso9660_domain, - "not found in the ISO file: %s", pathname); - return nullptr; - } + if (statbuf == nullptr) + throw FormatRuntimeError("not found in the ISO file: %s", + pathname); return new Iso9660InputStream(*this, pathname, mutex, cond, statbuf); diff --git a/src/archive/plugins/ZzipArchivePlugin.cxx b/src/archive/plugins/ZzipArchivePlugin.cxx index e83edb09b..45ae02cc3 100644 --- a/src/archive/plugins/ZzipArchivePlugin.cxx +++ b/src/archive/plugins/ZzipArchivePlugin.cxx @@ -29,6 +29,7 @@ #include "input/InputStream.hxx" #include "fs/Path.hxx" #include "util/RefCount.hxx" +#include "util/RuntimeError.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" @@ -58,9 +59,8 @@ public: virtual void Visit(ArchiveVisitor &visitor) override; - virtual InputStream *OpenStream(const char *path, - Mutex &mutex, Cond &cond, - Error &error) override; + InputStream *OpenStream(const char *path, + Mutex &mutex, Cond &cond) override; }; static constexpr Domain zzip_domain("zzip"); @@ -129,15 +129,12 @@ struct ZzipInputStream final : public InputStream { InputStream * ZzipArchiveFile::OpenStream(const char *pathname, - Mutex &mutex, Cond &cond, - Error &error) + Mutex &mutex, Cond &cond) { ZZIP_FILE *_file = zzip_file_open(dir, pathname, 0); - if (_file == nullptr) { - error.Format(zzip_domain, "not found in the ZIP file: %s", - pathname); - return nullptr; - } + if (_file == nullptr) + throw FormatRuntimeError("not found in the ZIP file: %s", + pathname); return new ZzipInputStream(*this, pathname, mutex, cond, |