From fc7d3f64c007672e4c01172ccb1c46d083a8abd5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 9 Sep 2016 15:37:06 +0200 Subject: input/Plugin: migrate open() from class Error to C++ exceptions --- src/lib/ffmpeg/Error.cxx | 8 ++++++++ src/lib/ffmpeg/Error.hxx | 5 +++++ src/lib/nfs/FileReader.cxx | 23 ++++++++--------------- src/lib/nfs/FileReader.hxx | 6 +++++- 4 files changed, 26 insertions(+), 16 deletions(-) (limited to 'src/lib') diff --git a/src/lib/ffmpeg/Error.cxx b/src/lib/ffmpeg/Error.cxx index 9dc193bfa..92608300f 100644 --- a/src/lib/ffmpeg/Error.cxx +++ b/src/lib/ffmpeg/Error.cxx @@ -26,6 +26,14 @@ extern "C" { #include } +std::runtime_error +MakeFfmpegError(int errnum) +{ + char msg[256]; + av_strerror(errnum, msg, sizeof(msg)); + return std::runtime_error(msg); +} + void SetFfmpegError(Error &error, int errnum) { diff --git a/src/lib/ffmpeg/Error.hxx b/src/lib/ffmpeg/Error.hxx index 7dc7fc550..3a6eb876d 100644 --- a/src/lib/ffmpeg/Error.hxx +++ b/src/lib/ffmpeg/Error.hxx @@ -20,8 +20,13 @@ #ifndef MPD_FFMPEG_ERROR_HXX #define MPD_FFMPEG_ERROR_HXX +#include + class Error; +std::runtime_error +MakeFfmpegError(int errnum); + void SetFfmpegError(Error &error, int errnum); diff --git a/src/lib/nfs/FileReader.cxx b/src/lib/nfs/FileReader.cxx index 523690de7..54b59e09a 100644 --- a/src/lib/nfs/FileReader.cxx +++ b/src/lib/nfs/FileReader.cxx @@ -91,23 +91,19 @@ NfsFileReader::DeferClose() BlockingCall(io_thread_get(), [this](){ Close(); }); } -bool -NfsFileReader::Open(const char *uri, Error &error) +void +NfsFileReader::Open(const char *uri) { assert(state == State::INITIAL); - if (!StringStartsWith(uri, "nfs://")) { - error.Set(nfs_domain, "Malformed nfs:// URI"); - return false; - } + if (!StringStartsWith(uri, "nfs://")) + throw std::runtime_error("Malformed nfs:// URI"); uri += 6; const char *slash = strchr(uri, '/'); - if (slash == nullptr) { - error.Set(nfs_domain, "Malformed nfs:// URI"); - return false; - } + if (slash == nullptr) + throw std::runtime_error("Malformed nfs:// URI"); server = std::string(uri, slash); @@ -121,10 +117,8 @@ NfsFileReader::Open(const char *uri, Error &error) path = new_path; } else { slash = strrchr(uri + 1, '/'); - if (slash == nullptr || slash[1] == 0) { - error.Set(nfs_domain, "Malformed nfs:// URI"); - return false; - } + if (slash == nullptr || slash[1] == 0) + throw std::runtime_error("Malformed nfs:// URI"); export_name = std::string(uri, slash); path = slash; @@ -132,7 +126,6 @@ NfsFileReader::Open(const char *uri, Error &error) state = State::DEFER; DeferredMonitor::Schedule(); - return true; } bool diff --git a/src/lib/nfs/FileReader.hxx b/src/lib/nfs/FileReader.hxx index ab88299cc..177fb66f2 100644 --- a/src/lib/nfs/FileReader.hxx +++ b/src/lib/nfs/FileReader.hxx @@ -61,7 +61,11 @@ public: void Close(); void DeferClose(); - bool Open(const char *uri, Error &error); + /** + * Throws std::runtime_error on error. + */ + void Open(const char *uri); + bool Read(uint64_t offset, size_t size, Error &error); void CancelRead(); -- cgit v1.2.3