Added tools/str_exception.h and better error handling for StdStream and FileStream

actorid
Nicolay Korslund 15 years ago
parent 5e70bc7bd7
commit 86089816a7

@ -2,26 +2,14 @@
#include <assert.h>
#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)
{

@ -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;

@ -1,5 +1,6 @@
#include "ffmpeg_source.h"
#include <exception>
#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 ---

@ -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(); }
};

@ -3,18 +3,20 @@
#include "../stream.h"
#include <iostream>
#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;
}

@ -0,0 +1,19 @@
#ifndef __STR_EXCEPTION_H
#define __STR_EXCEPTION_H
#include <exception>
#include <string>
/// 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
Loading…
Cancel
Save