blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 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 // The classes and functions defined in this file are not meant for general 00057 // use, they are internal implementation details. They may change at any time. 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 } // end namespace BLOCXX_NAMESPACE 00142 00143 #endif