diff --git a/sound/outputs/openal_out.cpp b/sound/outputs/openal_out.cpp index e6d0b3ae7..f0298feb2 100644 --- a/sound/outputs/openal_out.cpp +++ b/sound/outputs/openal_out.cpp @@ -2,26 +2,14 @@ #include #include "../../stream/filters/buffer_stream.h" +#include "../../tools/str_exception.h" using namespace Mangle::Sound; // ---- Helper functions and classes ---- -class OpenAL_Exception : public std::exception -{ - std::string msg; - - public: - - OpenAL_Exception(const std::string &m) : msg(m) {} - ~OpenAL_Exception() throw() {} - virtual const char* what() const throw() { return msg.c_str(); } -}; - static void fail(const std::string &msg) -{ - throw OpenAL_Exception("OpenAL exception: " + msg); -} +{ throw str_exception("OpenAL exception: " + msg); } static void checkALError(const std::string &msg) { diff --git a/sound/sources/audiere_source.cpp b/sound/sources/audiere_source.cpp index 8a6b57ada..6a0776402 100644 --- a/sound/sources/audiere_source.cpp +++ b/sound/sources/audiere_source.cpp @@ -1,25 +1,12 @@ #include "audiere_source.h" #include "../../stream/clients/audiere_file.h" +#include "../../tools/str_exception.h" using namespace Mangle::Stream; -// Exception handling -class Audiere_Exception : public std::exception -{ - std::string msg; - - public: - - Audiere_Exception(const std::string &m) : msg(m) {} - ~Audiere_Exception() throw() {} - virtual const char* what() const throw() { return msg.c_str(); } -}; - static void fail(const std::string &msg) -{ - throw Audiere_Exception("Audiere exception: " + msg); -} +{ throw str_exception("Audiere exception: " + msg); } using namespace audiere; using namespace Mangle::Sound; diff --git a/sound/sources/ffmpeg_source.cpp b/sound/sources/ffmpeg_source.cpp index 372d1766d..5724689de 100644 --- a/sound/sources/ffmpeg_source.cpp +++ b/sound/sources/ffmpeg_source.cpp @@ -1,5 +1,6 @@ #include "ffmpeg_source.h" -#include + +#include "../../tools/str_exception.h" using namespace Mangle::Sound; @@ -7,30 +8,8 @@ using namespace Mangle::Sound; // streams operated from the same thread. static uint8_t outBuf[AVCODEC_MAX_AUDIO_FRAME_SIZE]; -/// FFmpeg exception. -class FFM_Exception : public std::exception -{ - std::string msg; - - public: - - FFM_Exception(const std::string &m); - ~FFM_Exception() throw(); - virtual const char* what() const throw(); -}; - -FFM_Exception::FFM_Exception(const std::string &m) - : msg(m) {} - -const char* FFM_Exception::what() const throw() -{ return msg.c_str(); } - -FFM_Exception::~FFM_Exception() throw() {} - static void fail(const std::string &msg) -{ - throw FFM_Exception("FFMpeg exception: " + msg); -} +{ throw str_exception("FFMpeg exception: " + msg); } // --- Loader --- diff --git a/stream/servers/file_stream.h b/stream/servers/file_stream.h index 012cb3a78..4aeb7eeac 100644 --- a/stream/servers/file_stream.h +++ b/stream/servers/file_stream.h @@ -18,6 +18,9 @@ class FileStream : public StdStream : StdStream(&file) { file.open(name.c_str(), std::ios::binary); + + if(file.fail()) + throw str_exception("FileStream: failed to open file " + name); } ~FileStream() { file.close(); } }; diff --git a/stream/servers/std_stream.h b/stream/servers/std_stream.h index d4b56a9ae..657d9303b 100644 --- a/stream/servers/std_stream.h +++ b/stream/servers/std_stream.h @@ -3,18 +3,20 @@ #include "../stream.h" #include +#include "../../tools/str_exception.h" namespace Mangle { namespace Stream { /** Simplest wrapper for std::istream. - - TODO: No error checking yet. */ class StdStream : public Stream { std::istream *inf; + static void fail(const std::string &msg) + { throw str_exception("StdStream: " + msg); } + public: StdStream(std::istream *_inf) : inf(_inf) @@ -27,11 +29,17 @@ class StdStream : public Stream size_t read(void* buf, size_t len) { inf->read((char*)buf, len); + if(inf->fail()) + fail("error reading from stream"); return inf->gcount(); } void seek(size_t pos) - { inf->seekg(pos); } + { + inf->seekg(pos); + if(inf->fail()) + fail("seek error"); + } size_t tell() const // Hack around the fact that ifstream->tellg() isn't const @@ -44,6 +52,10 @@ class StdStream : public Stream inf->seekg(0, std::ios::end); size_t res = inf->tellg(); inf->seekg(pos); + + if(inf->fail()) + fail("could not get stream size"); + return res; } diff --git a/tools/str_exception.h b/tools/str_exception.h new file mode 100644 index 000000000..39120a3f9 --- /dev/null +++ b/tools/str_exception.h @@ -0,0 +1,19 @@ +#ifndef __STR_EXCEPTION_H +#define __STR_EXCEPTION_H + +#include +#include + +/// A simple exception that takes and holds a string +class str_exception : public std::exception +{ + std::string msg; + + public: + + str_exception(const std::string &m) : msg(m) {} + ~str_exception() throw() {} + const char* what() const throw() { return msg.c_str(); } +}; + +#endif