OW_StringStream.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_STRINGSTREAM_HPP_INCLUDE_GUARD_
00037 #define OW_STRINGSTREAM_HPP_INCLUDE_GUARD_
00038 #include "OW_config.h"
00039 #include "OW_StringBuffer.hpp"
00040 #include "OW_BaseStreamBuffer.hpp"
00041 #if defined(OW_HAVE_OSTREAM) && defined(OW_HAVE_ISTREAM)
00042 #include <istream>
00043 #include <ostream>
00044 #else
00045 #include <iostream>
00046 #endif
00047 #ifdef OW_HAVE_STREAMBUF
00048 #include <streambuf>
00049 #else
00050 #include <streambuf.h>
00051 #endif
00052 
00053 namespace OW_NAMESPACE
00054 {
00055 
00057 class IStringStreamBuf : public std::streambuf
00058 {
00059 public:
00060    IStringStreamBuf(const String& s)
00061       : std::streambuf()
00062       , m_buf(s)
00063    {
00064       setg(const_cast<char*>(reinterpret_cast<const char*>(m_buf.c_str())),
00065          const_cast<char*>(reinterpret_cast<const char*>(m_buf.c_str())),
00066          const_cast<char*>(reinterpret_cast<const char*>(m_buf.c_str()+m_buf.length())));
00067    }
00068 protected:
00069    int underflow()
00070    {
00071       return (gptr() < egptr()) ? static_cast<unsigned char>(*gptr()) : EOF;  // need a static_cast so a -1 doesn't turn into an EOF
00072    }
00073 private:
00074    String m_buf;
00075 };
00077 class IStringStreamBase
00078 {
00079 public:
00080    IStringStreamBase(const String& s) : m_buf(s) {}
00081    mutable IStringStreamBuf m_buf;
00082 };
00083 
00085 class IStringStream : private IStringStreamBase, public std::istream
00086 {
00087 public:
00088    IStringStream(const String& s);
00089    ~IStringStream();
00090    void reset();
00091 private:
00092    // not implemented
00093    IStringStream(const IStringStream&);
00094    IStringStream& operator=(const IStringStream&);
00095 };
00096 
00098 class OW_COMMON_API OStringStreamBuf : public BaseStreamBuffer
00099 {
00100 public:
00101    OStringStreamBuf(size_t size);
00102    virtual ~OStringStreamBuf();
00103    String toString() const;
00104    // After calling releaseString(), this OStringStream is unusable
00105    String releaseString();
00106    size_t length() const;
00107    const char* c_str() const;
00108    void reset();
00109 protected:
00110    virtual int buffer_to_device(const char *c, int n);
00111 private:
00112    StringBuffer m_buf;
00113    friend class OStringStream;
00114 };
00116 class OW_COMMON_API OStringStreamBase
00117 {
00118 public:
00119    OStringStreamBase(size_t sz);
00120    mutable OStringStreamBuf m_buf;
00121 };
00123 class OW_COMMON_API OStringStream : private OStringStreamBase, public std::ostream
00124 {
00125 public:
00126    OStringStream(size_t size = 256);
00127    ~OStringStream();
00128    OStringStream(const OStringStream&);
00129    OStringStream& operator=(const OStringStream&);  
00130    String toString() const;
00131    // After calling releaseString(), this OStringStream is unusable
00132    String releaseString();
00133    size_t length() const;
00134    const char* c_str() const;
00135    void reset();
00136 };
00137 
00138 } // end namespace OW_NAMESPACE
00139 
00140 #endif

Generated on Thu Feb 9 08:48:16 2006 for openwbem by  doxygen 1.4.6