OW_DataStreams.hpp

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

Generated on Thu Feb 9 08:47:58 2006 for openwbem by  doxygen 1.4.6