blocxx
StringStream.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_STRINGSTREAM_HPP_INCLUDE_GUARD_
40 #define BLOCXX_STRINGSTREAM_HPP_INCLUDE_GUARD_
41 #include "blocxx/BLOCXX_config.h"
42 #include "blocxx/StringBuffer.hpp"
44 #if defined(BLOCXX_HAVE_OSTREAM) && defined(BLOCXX_HAVE_ISTREAM)
45 #include <istream>
46 #include <ostream>
47 #else
48 #include <iostream>
49 #endif
50 #ifdef BLOCXX_HAVE_STREAMBUF
51 #include <streambuf>
52 #else
53 #include <streambuf.h>
54 #endif
55 
56 namespace BLOCXX_NAMESPACE
57 {
58 
60 class IStringStreamBuf : public std::streambuf
61 {
62 public:
64  : std::streambuf()
65  , m_buf(s)
66  {
67  setg(const_cast<char*>(reinterpret_cast<const char*>(m_buf.c_str())),
68  const_cast<char*>(reinterpret_cast<const char*>(m_buf.c_str())),
69  const_cast<char*>(reinterpret_cast<const char*>(m_buf.c_str()+m_buf.length())));
70  }
71 protected:
72  int underflow()
73  {
74  return (gptr() < egptr()) ? static_cast<unsigned char>(*gptr()) : EOF; // need a static_cast so a -1 doesn't turn into an EOF
75  }
76 private:
78 };
81 {
82 public:
83  IStringStreamBase(const String& s) : m_buf(s) {}
85 };
86 
88 class BLOCXX_COMMON_API IStringStream : private IStringStreamBase, public std::istream
89 {
90 public:
91  IStringStream(const String& s);
92  ~IStringStream();
93 private:
94  // not implemented
96  IStringStream& operator=(const IStringStream&);
97 };
98 
100 class BLOCXX_COMMON_API OStringStreamBuf : public BaseStreamBuffer
101 {
102 public:
103  OStringStreamBuf(size_t size);
104  virtual ~OStringStreamBuf();
105  String toString() const;
106  // After calling releaseString(), this OStringStream is unusable
107  String releaseString();
108  size_t length() const;
109  const char* c_str() const;
110  void reset();
111 protected:
112  virtual int buffer_to_device(const char *c, int n);
113 private:
115  friend class OStringStream;
116 };
118 class BLOCXX_COMMON_API OStringStreamBase
119 {
120 public:
121  OStringStreamBase(size_t sz);
123 };
125 class BLOCXX_COMMON_API OStringStream : private OStringStreamBase, public std::ostream
126 {
127 public:
128  OStringStream(size_t size = 256);
129  ~OStringStream();
131  OStringStream& operator=(const OStringStream&);
132  String toString() const;
133  // After calling releaseString(), this OStringStream is unusable
134  String releaseString();
135  size_t length() const;
136  const char* c_str() const;
137  void reset();
138 };
139 
140 } // end namespace BLOCXX_NAMESPACE
141 
142 #endif