2010-01-01 15:39:11 +00:00
|
|
|
#ifndef MANGLE_STREAM_STDIOSERVER_H
|
|
|
|
#define MANGLE_STREAM_STDIOSERVER_H
|
2009-12-31 13:48:34 +00:00
|
|
|
|
2010-06-03 18:13:27 +00:00
|
|
|
#include "../stream.hpp"
|
2009-12-31 13:48:34 +00:00
|
|
|
#include <iostream>
|
2010-09-02 20:19:44 +00:00
|
|
|
#include <stdexcept>
|
2009-12-31 13:48:34 +00:00
|
|
|
|
|
|
|
namespace Mangle {
|
|
|
|
namespace Stream {
|
|
|
|
|
2010-08-04 10:20:46 +00:00
|
|
|
/** Simple wrapper for std::istream.
|
2009-12-31 13:48:34 +00:00
|
|
|
*/
|
|
|
|
class StdStream : public Stream
|
|
|
|
{
|
|
|
|
std::istream *inf;
|
|
|
|
|
2010-01-02 12:06:37 +00:00
|
|
|
static void fail(const std::string &msg)
|
2010-09-02 20:19:44 +00:00
|
|
|
{ throw std::runtime_error("StdStream: " + msg); }
|
2010-01-02 12:06:37 +00:00
|
|
|
|
2009-12-31 13:48:34 +00:00
|
|
|
public:
|
|
|
|
StdStream(std::istream *_inf)
|
|
|
|
: inf(_inf)
|
|
|
|
{
|
|
|
|
isSeekable = true;
|
|
|
|
hasPosition = true;
|
|
|
|
hasSize = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t read(void* buf, size_t len)
|
|
|
|
{
|
|
|
|
inf->read((char*)buf, len);
|
2010-08-17 11:17:39 +00:00
|
|
|
if(inf->bad())
|
2010-01-02 12:06:37 +00:00
|
|
|
fail("error reading from stream");
|
2009-12-31 13:48:34 +00:00
|
|
|
return inf->gcount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void seek(size_t pos)
|
2010-01-02 12:06:37 +00:00
|
|
|
{
|
2010-09-13 08:52:26 +00:00
|
|
|
inf->clear();
|
2010-01-02 12:06:37 +00:00
|
|
|
inf->seekg(pos);
|
|
|
|
if(inf->fail())
|
|
|
|
fail("seek error");
|
|
|
|
}
|
2009-12-31 13:48:34 +00:00
|
|
|
|
|
|
|
size_t tell() const
|
|
|
|
// Hack around the fact that ifstream->tellg() isn't const
|
|
|
|
{ return ((StdStream*)this)->inf->tellg(); }
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
// Use the standard iostream size hack, terrible as it is.
|
|
|
|
std::streampos pos = inf->tellg();
|
2010-01-01 15:39:11 +00:00
|
|
|
inf->seekg(0, std::ios::end);
|
2009-12-31 13:48:34 +00:00
|
|
|
size_t res = inf->tellg();
|
|
|
|
inf->seekg(pos);
|
2010-01-02 12:06:37 +00:00
|
|
|
|
|
|
|
if(inf->fail())
|
|
|
|
fail("could not get stream size");
|
|
|
|
|
2009-12-31 13:48:34 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool eof() const
|
|
|
|
{ return inf->eof(); }
|
|
|
|
};
|
|
|
|
|
2009-12-31 14:37:01 +00:00
|
|
|
typedef boost::shared_ptr<StdStream> StdStreamPtr;
|
|
|
|
|
2009-12-31 13:48:34 +00:00
|
|
|
}} // namespaces
|
|
|
|
#endif
|