StringStream.h
1/*
2 * Copyright (C) 2007 Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef PT_STRINGSTREAM_H
30#define PT_STRINGSTREAM_H
31
32#include <Pt/Api.h>
33#include <Pt/String.h>
34#include <Pt/StreamBuffer.h>
35#include <Pt/IOStream.h>
36#include <sstream>
37
38namespace Pt {
39
44class PT_API StringBuffer : public BasicStreamBuffer<Char>
45{
46 public:
47 typedef BasicStreamBuffer<Char> Base;
48
50 typedef Char char_type;
51
53 typedef typename std::char_traits<Char> traits_type;
54
56 typedef typename traits_type::int_type int_type;
57
59 typedef typename traits_type::pos_type pos_type;
60
62 typedef typename traits_type::off_type off_type;
63
64 public:
65 explicit StringBuffer(std::ios::openmode mode = std::ios::in|std::ios::out);
66
67 explicit StringBuffer(const Pt::String& str,
68 std::ios::openmode mode = std::ios::in|std::ios::out);
69
70 ~StringBuffer();
71
72 Pt::String str() const;
73
74 void str(const Pt::String& str);
75
76#if __cplusplus >= 201103L
77 void str(Pt::String&& s);
78
79 void swap(StringBuffer& other);
80#endif
81
82 protected:
83 virtual std::streamsize showfull() override;
84
85 virtual std::streamsize showmanyc() override;
86
87 virtual int sync() override;
88
89 virtual int_type pbackfail(int_type ch = traits_type::eof()) override;
90
91 virtual int_type underflow() override;
92
93 virtual int_type overflow( int_type ch = traits_type::eof() ) override;
94
95 virtual std::streamsize xsgetn(char_type* s, std::streamsize n) override;
96
97 virtual std::streamsize xsputn(const char_type* s, std::streamsize n) override;
98
99 virtual pos_type seekoff(off_type off, std::ios_base::seekdir way,
100 std::ios_base::openmode m = std::ios_base::in|std::ios_base::out) override;
101
102 virtual pos_type seekpos(pos_type sp,
103 std::ios_base::openmode m = std::ios_base::in|std::ios_base::out) override;
104
105 private:
106 std::ios_base::openmode _mode;
107 Pt::String _str;
108 std::streamsize _hwm;
109};
110
111#if defined(PT_WITH_STD_STRINGSTREAM)
112
113namespace Pt {
114
115typedef std::basic_stringstream<Pt::Char> StringStream;
116typedef std::basic_istringstream<Pt::Char> IStringStream;
117typedef std::basic_ostringstream<Pt::Char> OStringStream;
118
119#else
120
125class PT_API IStringStream : public std::basic_istream<Pt::Char>
126{
127 public:
128 typedef Pt::Char char_type;
129 typedef std::char_traits<Pt::Char> traits_type;
130 typedef std::allocator<Pt::Char> allocator_type;
131 typedef traits_type::int_type int_type;
132 typedef traits_type::pos_type pos_type;
133 typedef traits_type::off_type off_type;
134
135 public:
136 explicit IStringStream(std::ios_base::openmode mode = ios_base::in);
137
138 explicit IStringStream(const Pt::String& str,
139 std::ios_base::openmode mode = std::ios_base::in);
140
141 StringBuffer* rdbuf() const
142 { return const_cast<Pt::StringBuffer*>(&_buffer); }
143
144 Pt::String str() const
145 { return _buffer.str(); }
146
147 void str(const Pt::String& str)
148 { _buffer.str(str); }
149
150 private:
151 Pt::StringBuffer _buffer;
152};
153
158class PT_API OStringStream : public std::basic_ostream<Pt::Char>
159{
160 public:
161 typedef Pt::Char char_type;
162 typedef std::char_traits<Pt::Char> traits_type;
163 typedef std::allocator<Pt::Char> allocator_type;
164 typedef traits_type::int_type int_type;
165 typedef traits_type::pos_type pos_type;
166 typedef traits_type::off_type off_type;
167
168 public:
169 explicit OStringStream(std::ios_base::openmode mode = ios_base::out);
170
171 explicit OStringStream(const Pt::String& str,
172 std::ios_base::openmode mode = std::ios_base::out);
173
174 StringBuffer* rdbuf() const
175 { return const_cast<Pt::StringBuffer*>(&_buffer); }
176
177 Pt::String str() const
178 { return _buffer.str(); }
179
180 void str(const Pt::String& str)
181 { _buffer.str(str); }
182
183 private:
184 Pt::StringBuffer _buffer;
185};
186
191class PT_API StringStream : public std::basic_iostream<Pt::Char>
192{
193 public:
194 typedef Pt::Char char_type;
195 typedef std::char_traits<Pt::Char> traits_type;
196 typedef std::allocator<Pt::Char> allocator_type;
197 typedef traits_type::int_type int_type;
198 typedef traits_type::pos_type pos_type;
199 typedef traits_type::off_type off_type;
200
201 public:
202 explicit StringStream(std::ios_base::openmode mode = ios_base::in | ios_base::out);
203
204 explicit StringStream(const Pt::String& str,
205 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
206
207 StringBuffer* rdbuf() const
208 { return const_cast<Pt::StringBuffer*>(&_buffer); }
209
210 Pt::String str() const
211 { return _buffer.str(); }
212
213 void str(const Pt::String& str)
214 { _buffer.str(str); }
215
216 private:
217 Pt::StringBuffer _buffer;
218};
219
220#endif // PT_WITH_STD_STRINGSTREAM
221
222} // namespace Pt
223
224#endif
BasicStreamBuffer()
Definition StreamBuffer.h:139
Unicode string input stream.
Definition StringStream.h:126
Unicode string output stream.
Definition StringStream.h:159
Unicode string stream buffer.
Definition StringStream.h:45
traits_type::off_type off_type
Stream offset type.
Definition StringStream.h:62
Char char_type
Internal character type.
Definition StringStream.h:50
std::char_traits< Char > traits_type
Internal character traits.
Definition StringStream.h:53
traits_type::pos_type pos_type
Stream position type.
Definition StringStream.h:59
traits_type::int_type int_type
Integer type.
Definition StringStream.h:56
virtual std::streamsize showfull() override
Returns the number of characters buffered for output.
Unicode string stream.
Definition StringStream.h:192
Unicode capable basic_string.
Definition Api-String.h:67
Core module.
Definition Allocator.h:33
Unicode character type.
Definition String.h:67