#ifndef INCLUDED_BOBCAT_IOSTREAMBUF_
#define INCLUDED_BOBCAT_IOSTREAMBUF_

#include <fstream>
#include <streambuf>

namespace FBB
{

class IOStreambuf: public std::streambuf
{
    char d_buf;
    std::istream *d_in;
    std::ostream *d_out;

    public:
        IOStreambuf(std::istream &in, std::ostream &out);
        IOStreambuf();

        virtual ~IOStreambuf();

        void open(std::istream &in, std::ostream &out);

    protected:
        std::streamsize pXsputn(char const *buffer, std::streamsize n);
        int pSync();
        pos_type pSeekoff(off_type offset, std::ios::seekdir way,
                                 std::ios::openmode mode = 
                                                std::ios::in | std::ios::out);
        pos_type pSeekpos(pos_type offset, 
                                 std::ios::openmode mode = 
                                                std::ios::in | std::ios::out);

    private:
        virtual int underflow();
        virtual pos_type seekoff(off_type offset, std::ios::seekdir way,
                                 std::ios::openmode mode = 
                                                std::ios::in | std::ios::out);
        virtual pos_type seekpos(pos_type offset, 
                                 std::ios::openmode mode = 
                                                std::ios::in | std::ios::out);
        virtual int sync();
        virtual int overflow(int c);
        virtual std::streamsize xsputn(char const *buffer, std::streamsize n);
};


inline IOStreambuf::IOStreambuf(std::istream &in, std::ostream &out)
{        
    open(in, out);
}

inline IOStreambuf::IOStreambuf()
:
    d_in(0),
    d_out(0)
{}        

inline IOStreambuf::pos_type IOStreambuf::pSeekpos(pos_type offset, 
                         std::ios::openmode mode)
{
    return seekoff(offset, std::ios::beg, mode);
}

inline std::streamsize IOStreambuf::pXsputn(char const *buffer, 
                                           std::streamsize n)
{
    return d_out->write(buffer, n) ? n : 0;
}

} // namespace FBB

#endif
