Boost logo

Boost :

From: Guy (ignis_at_[hidden])
Date: 2002-11-04 15:02:08


Hi,

I am implementing classes to read/write binary data as bytes from files and
memory.

1. I borrowed ideas from iostream, but the classes don't inherit or relate
to it.

2. I removed the << and >> operators and kept only the write(), put(),
read(), get() functions
and added functions for integer writing.

3. I added a size() function to return the size in case the stream is a
file/memory, if used for sockets it should
be undefined.

4. The classes are meant only for systems with binary file support.

5. The prototypes follow below

////////////////////////////////////////////////////////////////////////
// <bios.hpp>

class binary_ios {
public:
    enum iostate { goodbit = 0x00, eofbit = 0x01, failbit = 0x02, badbit =
0x04 };

    enum openmode { in = 0x01, out = 0x02, ate = 0x04, app = 0x08, trunc =
0x10 };

    enum seekdir { beg = 0, cur = 1, end = 2 };

    enum byteorder { big_endian = 0, little_endian = 1; } // default to
network order (big_endian)

    // failure
    class failure : public std::exception {
    public:
        explicit failure(const std::string &msg);
        virtual ~failure();
        virtual const char* what() const throw();
    private:
        std::string m_msg;
    }

    // state
    operator void*() const;
    bool operator!() const;

    iostate rdstate() const;
    void clear(iostate state = goodbit);
    void setstate(iostate state);

    bool good() const;
    bool eof() const;
    bool fail() const;
    bool bad() const;

    iostate exceptions() const;

    void exceptions(iostate except);

    // byte order
    byteotder setbyteorder(byteorder order);
    byteorder getbyteorder() const;

    // Constructor/destructor
    explicit binary_ios(bstreambuf *sb);
    virtual ~binary_ios() {}

    // Buffer
    bstreambuf* rdbuf() const;
    bstreambuf* rdbuf(bstreambuf *sb);

protected:
    binary_ios();
    void init(bstreambuf *buf);

private:
    binary_ios(const binary_ios&);
    binary_ios& operator=(const binary_ios&);
};

////////////////////////////////////////////////////////////////////////
// <bstream.hpp>

class bstreambuf {
public:
    typedef unsigned char byte_type;
    typedef /* int type */ intptr_type;

    typedef /* int type */ pos_type; // prefered 64bit type for large files
    typedef /* int type */ off_type;
    typedef intptr_type size_type;

    // destructor
    virtual ~bstreambuf();

    // buffer and positioning
    bstreambuf* pubsetbuf(byte_type *s, size_type n);
    pos_type pubseekoff(off_type off, binary_io::seekdir way,
binary_io::openmode which = binary_io::in | binary_io::out);
    pos_type pubseekpos(pos_type pos, binary_io::openmode which =
binary_io::in | binary_io::out);
    int pubsync();
    pos_type pubsize();

    // Get area
    size_type in_avail();
    int snextb();
    int sbumpb();
    int sgetb();
    size_type sgetn(byte_type *s, size_type n);

    // Putback
    int sputbackb(byte_type b);
    int sungetb();

    // Put area
    int sputb(byte_type b);
    size_type sputn(const byte_type *s, size_type n);

protected:
    bstreambuf();

    // Get area
    byte_type* eback() const;
    byte_type* gptr() const;
    byte_type* egptr() const;

    void gbump(int n);
    void setg(byte_type *gbeg, byte_type *gnext, byte_type *gend);

    // Put area
    byte_type* pbase() const;
    byte_type* pptr() const;
    byte_type* epptr() const;

    void pbump(int n);
    void setp(byte_type *pbeg, byte_type *pend);

    // Virtual functions

    // Buffer managment and positioning
    virtual bstreambuf* setbuf(byte_type *s, size_type n);
    virtual pos_type seekoff(off_type off, binary_io::seekdir way,
binary_io::openmode which = binary_io::in | binary_io::out);
    virtual pos_type seekpos(pos_type pos, binary_io::openmode which =
binary_io::in | binary_io::out);
    virtual pos_type size();

    virtual int sync();

    // Get area
    virtual size_type showmanyc();
    virtual size_type xsgetn(byte_type *s, size_type n);
    virtual int underflow();
    virtual int uflow();

    // Putback
    virtual int pbackfail(int b = EOF);

    // Put area
    virtual size_type xsputn(const byte_type *s, size_type n);
    virtual int overflow(int b = EOF);
};

class bistream : public virtual binary_ios {
public:
    typedef unsigned char byte_type;
    typedef /* 8 bit */ uint_least8_type;
    typedef /* 16 bit */ uint_least16_type;
    typedef /* 32 bit */ uint_least32_type;
    typedef /* 64 bit */ uint_least64_type;
    typedef /* int type */ intptr_type;

    typedef /* int type */ pos_type;
    typedef /* int type */ off_type;
    typedef intptr_type size_type;

    // Constructor/destructor
    explicit bistream(bstreambuf *sb);
    virtual ~bistream();

    // returns number of bytes read
    size_type gcount() const;

    //
    int sync();

    // byte input
    bistream& read(byte_type *s, size_type n);
    bistream& read(bstreambuf *s, size_type n);

    bistream& get(byte_type &b);
    bistream& peek(byte_type &b);
    bistream& putback(byte_type b);
    bistream& unget();

    // integer input
    bistream& get8(uint_least8_type &val);
    bistream& get16(uint_least16_type &val);
    bistream& get32(uint_least32_type &val);
    bistream& get64(uint_least64_type &val);

    // character input
    bistream& get8(char &c);
    bistream& get16(wchar_t &c);
    bistream& get32(wchar_t &c);

    // seeking, position, size
    pos_type tellg();
    pos_type seekg(pos_type);
    pos_type seekg(off_type, bio::seekdir);
    pos_type size();
};

class bostream : public virtual binary_ios {
public:
    typedef unsigned char byte_type;
    typedef /* 8 bit */ uint_least8_type;
    typedef /* 16 bit */ uint_least16_type;
    typedef /* 32 bit */ uint_least32_type;
    typedef /* 64 bit */ uint_least64_type;
    typedef /* int type */ intptr_type;

    typedef /* int type */ pos_type;
    typedef /* int type */ off_type;
    typedef intptr_type size_type;

    // constructor/destructor
    explicit bostream(bstreambuf *sb);
    virtual ~bostream();

    //
    bostream& flush();

    // byte output
    bostream& put(byte_type b);
    bostream& write(const byte_type *s, size_type n);

    // integer output
    bostream& put8(uint_least8_type val);
    bostream& put16(uint_least16_type val);
    bostream& put32(uint_least32_type val);
    bostream& put64(uint_least64_type val);

    // character output
    bostream& put8(char c);
    bostream& put16(wchar_t c);
    bostream& put32(wchar_t c);

    // position
    pos_type tellp();
    pos_type seekp(pos_type pos);
    pos_type seekp(off_type off, bio::seekdir way);
};

class bstream : public bistream, public bostream {
public:
    // constructor/destructor
    explicit bstream(bstreambuf *sb);
    virtual ~bstream();
};

////////////////////////////////////////////////////////////////////////
// <bfstream>

class binaryfile_buf : public bstreambuf {
    // uses operating system functions to handle files
};

class bfistream : public bistream {
public:
    explicit bfistream(const char *name, binary_ios::openmode mode =
binary_ios::in);
    ~bfistream();

    void open(const char *name, binary_ios::openmode mode = binary_ios::in);
    void close();
};

// same for bfostream, bfstream

////////////////////////////////////////////////////////////////////////
// <bmstream>

class binarymemory_buf : public bstreambuf {
    // works with bytes
};

class bmistream : public bistream {
public:
    // types
    typedef unsigned char byte_type;
    typedef /* 8 bit */ uint_least8_type;
    typedef /* 16 bit */ uint_least16_type;
    typedef /* 32 bit */ uint_least32_type;
    typedef /* 64 bit */ uint_least64_type;
    typedef /* int type */ intptr_type;

    typedef /* int type */ pos_type;
    typedef /* int type */ off_type;
    typedef intptr_type size_type;

    // constructor/destructor
    explicit bmistream(binary_ios::openmode = binary_ios::in);
    explicit bmistream(byte_type *buf, size_type n, binary_ios::openmode =
binary_ios::in);
    ~bmistream();

    void bytes(const byte_type *buf, size_type n);
    const byte_type* bytes() const;
};

// same for bmostream, bmstream


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk