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
00036 #ifndef OW_DATA_STREAMS_HPP_INCLUDE_GUARD_
00037 #define OW_DATA_STREAMS_HPP_INCLUDE_GUARD_
00038 #include "OW_config.h"
00039 #include "OW_Types.hpp"
00040 #if defined(OW_HAVE_ISTREAM) && defined(OW_HAVE_OSTREAM)
00041 #include <istream>
00042 #include <ostream>
00043 #else
00044 #include <iostream>
00045 #endif
00046 #if defined(OW_HAVE_STREAMBUF)
00047 #include <streambuf>
00048 #elif defined(OW_HAVE_STREAMBUF_H)
00049 #include <streambuf.h>
00050 #endif
00051 #include <vector>
00052
00053
00054
00055
00056 namespace OW_NAMESPACE
00057 {
00058
00060 class OW_COMMON_API DataIStreamBuf : public std::streambuf
00061 {
00062 public:
00063 DataIStreamBuf(int dataLen, const unsigned char* data) :
00064 std::streambuf()
00065 {
00066 setg(const_cast<char*>(reinterpret_cast<const char*>(data)),
00067 const_cast<char*>(reinterpret_cast<const char*>(data)),
00068 const_cast<char*>(reinterpret_cast<const char*>(data+dataLen)));
00069 }
00070 protected:
00071 int underflow()
00072 {
00073 return (gptr() < egptr()) ? static_cast<unsigned char>(*gptr()) : EOF;
00074 }
00075 };
00077 class OW_COMMON_API DataIStreamBase
00078 {
00079 protected:
00080 DataIStreamBase(int dataLen, const unsigned char* data) : m_strbuf(dataLen, data) {}
00081 DataIStreamBuf m_strbuf;
00082 };
00084 class OW_COMMON_API DataIStream : private DataIStreamBase, public std::istream
00085 {
00086 public:
00087 DataIStream(int dataLen, const unsigned char* data)
00088 : DataIStreamBase(dataLen, data)
00089 , std::basic_istream<char, std::char_traits<char> >(&m_strbuf) {}
00090 };
00092 class OW_COMMON_API DataOStreamBuf : public std::streambuf
00093 {
00094 public:
00095 DataOStreamBuf(size_t initialSize = 256);
00096 const unsigned char* getData() const { return &m_bfr[0]; }
00097 int length() const { return m_bfr.size(); }
00098 void clear() { m_bfr.clear(); }
00099 protected:
00100 virtual int overflow(int c);
00101 virtual std::streamsize xsputn(const char* s, std::streamsize n);
00102 private:
00103
00104 #ifdef OW_WIN32
00105 #pragma warning (push)
00106 #pragma warning (disable: 4251)
00107 #endif
00108
00109 std::vector<unsigned char> m_bfr;
00110
00111 #ifdef OW_WIN32
00112 #pragma warning (pop)
00113 #endif
00114
00115 };
00117 class OW_COMMON_API DataOStreamBase
00118 {
00119 protected:
00120 DataOStreamBase(size_t initialSize = 256)
00121 : m_buf(initialSize) {}
00122
00123 DataOStreamBuf m_buf;
00124 };
00126 class OW_COMMON_API DataOStream : private DataOStreamBase, public std::ostream
00127 {
00128 public:
00129 DataOStream(size_t initialSize = 256)
00130 : DataOStreamBase(initialSize)
00131 , std::basic_ostream<char, std::char_traits<char> >(&m_buf)
00132 {}
00133 const unsigned char* getData() const { return m_buf.getData(); }
00134 int length() const { return m_buf.length(); }
00135 void clearData() { m_buf.clear(); }
00136 };
00137
00138 }
00139
00140 #endif