00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00039 #ifndef BLOCXX_DATA_STREAMS_HPP_INCLUDE_GUARD_
00040 #define BLOCXX_DATA_STREAMS_HPP_INCLUDE_GUARD_
00041 #include "blocxx/BLOCXX_config.h"
00042 #include "blocxx/Types.hpp"
00043 #if defined(BLOCXX_HAVE_ISTREAM) && defined(BLOCXX_HAVE_OSTREAM)
00044 #include <istream>
00045 #include <ostream>
00046 #else
00047 #include <iostream>
00048 #endif
00049 #if defined(BLOCXX_HAVE_STREAMBUF)
00050 #include <streambuf>
00051 #elif defined(BLOCXX_HAVE_STREAMBUF_H)
00052 #include <streambuf.h>
00053 #endif
00054 #include <vector>
00055
00056
00057
00058
00059 namespace BLOCXX_NAMESPACE
00060 {
00061
00063 class BLOCXX_COMMON_API DataIStreamBuf : public std::streambuf
00064 {
00065 public:
00066 DataIStreamBuf(int dataLen, const unsigned char* data) :
00067 std::streambuf()
00068 {
00069 setg(const_cast<char*>(reinterpret_cast<const char*>(data)),
00070 const_cast<char*>(reinterpret_cast<const char*>(data)),
00071 const_cast<char*>(reinterpret_cast<const char*>(data+dataLen)));
00072 }
00073 protected:
00074 virtual int underflow();
00075
00076 virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which);
00077 virtual pos_type seekpos(pos_type sp, std::ios_base::openmode which);
00078 };
00080 class BLOCXX_COMMON_API DataIStreamBase
00081 {
00082 protected:
00083 DataIStreamBase(int dataLen, const unsigned char* data) : m_strbuf(dataLen, data) {}
00084 DataIStreamBuf m_strbuf;
00085 };
00087 class BLOCXX_COMMON_API DataIStream : private DataIStreamBase, public std::istream
00088 {
00089 public:
00090 DataIStream(int dataLen, const unsigned char* data)
00091 : DataIStreamBase(dataLen, data)
00092 , std::basic_istream<char, std::char_traits<char> >(&m_strbuf) {}
00093 };
00095 class BLOCXX_COMMON_API DataOStreamBuf : public std::streambuf
00096 {
00097 public:
00098 DataOStreamBuf(size_t initialSize = 256);
00099 const unsigned char* getData() const { return &m_bfr[0]; }
00100 int length() const { return m_bfr.size(); }
00101 void clear() { m_bfr.clear(); }
00102 protected:
00103 virtual int overflow(int c);
00104 virtual std::streamsize xsputn(const char* s, std::streamsize n);
00105 private:
00106
00107 #ifdef BLOCXX_WIN32
00108 #pragma warning (push)
00109 #pragma warning (disable: 4251)
00110 #endif
00111
00112 std::vector<unsigned char> m_bfr;
00113
00114 #ifdef BLOCXX_WIN32
00115 #pragma warning (pop)
00116 #endif
00117
00118 };
00120 class BLOCXX_COMMON_API DataOStreamBase
00121 {
00122 protected:
00123 DataOStreamBase(size_t initialSize = 256)
00124 : m_buf(initialSize) {}
00125
00126 DataOStreamBuf m_buf;
00127 };
00129 class BLOCXX_COMMON_API DataOStream : private DataOStreamBase, public std::ostream
00130 {
00131 public:
00132 DataOStream(size_t initialSize = 256)
00133 : DataOStreamBase(initialSize)
00134 , std::basic_ostream<char, std::char_traits<char> >(&m_buf)
00135 {}
00136 const unsigned char* getData() const { return m_buf.getData(); }
00137 int length() const { return m_buf.length(); }
00138 void clearData() { m_buf.clear(); }
00139 };
00140
00141 }
00142
00143 #endif