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