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 
38 namespace Pt {
39 
44 class PT_API StringBuffer : public BasicStreamBuffer<Char>
45 {
46  public:
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  protected:
77  virtual std::streamsize showfull() override;
78 
79  virtual std::streamsize showmanyc() override;
80 
81  virtual int sync() override;
82 
83  virtual int_type pbackfail(int_type ch = traits_type::eof()) override;
84 
85  virtual int_type underflow() override;
86 
87  virtual int_type overflow( int_type ch = traits_type::eof() ) override;
88 
89  virtual std::streamsize xsgetn(char_type* s, std::streamsize n) override;
90 
91  virtual std::streamsize xsputn(const char_type* s, std::streamsize n) override;
92 
93  virtual pos_type seekoff(off_type off, std::ios_base::seekdir way,
94  std::ios_base::openmode m = std::ios_base::in|std::ios_base::out) override;
95 
96  virtual pos_type seekpos(pos_type sp,
97  std::ios_base::openmode m = std::ios_base::in|std::ios_base::out) override;
98 
99  private:
100  std::ios_base::openmode _mode;
101  Pt::String _str;
102  mutable char_type* _hm = nullptr;
103 };
104 
105 } // namespace Pt
106 
107 //#define PT_STRINGSTREAM_BUILTIN 1
108 
109 #if defined(_MSC_VER) && __cplusplus >= 202002L
110 
111 namespace Pt {
112 
113 class PT_API IStringStream : public std::basic_istream<Pt::Char>
114 {
115  public:
116  typedef Pt::Char char_type;
117  typedef std::char_traits<Pt::Char> traits_type;
118  typedef std::allocator<Pt::Char> allocator_type;
119  typedef traits_type::int_type int_type;
120  typedef traits_type::pos_type pos_type;
121  typedef traits_type::off_type off_type;
122 
123  public:
124  explicit IStringStream(std::ios_base::openmode mode = ios_base::in);
125 
126  explicit IStringStream(const Pt::String& str,
127  std::ios_base::openmode mode = std::ios_base::in);
128 
129  StringBuffer* rdbuf() const
130  { return const_cast<Pt::StringBuffer*>(&_buffer); }
131 
132  Pt::String str() const
133  { return _buffer.str(); }
134 
135  void str(const Pt::String& str)
136  { _buffer.str(str); }
137 
138  private:
139  Pt::StringBuffer _buffer;
140 };
141 
142 
143 class PT_API OStringStream : public std::basic_ostream<Pt::Char>
144 {
145  public:
146  typedef Pt::Char char_type;
147  typedef std::char_traits<Pt::Char> traits_type;
148  typedef std::allocator<Pt::Char> allocator_type;
149  typedef traits_type::int_type int_type;
150  typedef traits_type::pos_type pos_type;
151  typedef traits_type::off_type off_type;
152 
153  public:
154  explicit OStringStream(std::ios_base::openmode mode = ios_base::out);
155 
156  explicit OStringStream(const Pt::String& str,
157  std::ios_base::openmode mode = std::ios_base::out);
158 
159  StringBuffer* rdbuf() const
160  { return const_cast<Pt::StringBuffer*>(&_buffer); }
161 
162  Pt::String str() const
163  { return _buffer.str(); }
164 
165  void str(const Pt::String& str)
166  { _buffer.str(str); }
167 
168  private:
169  Pt::StringBuffer _buffer;
170 };
171 
172 
173 class PT_API StringStream : public std::basic_iostream<Pt::Char>
174 {
175  public:
176  typedef Pt::Char char_type;
177  typedef std::char_traits<Pt::Char> traits_type;
178  typedef std::allocator<Pt::Char> allocator_type;
179  typedef traits_type::int_type int_type;
180  typedef traits_type::pos_type pos_type;
181  typedef traits_type::off_type off_type;
182 
183  public:
184  explicit StringStream(std::ios_base::openmode mode = ios_base::in | ios_base::out);
185 
186  explicit StringStream(const Pt::String& str,
187  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
188 
189  StringBuffer* rdbuf() const
190  { return const_cast<Pt::StringBuffer*>(&_buffer); }
191 
192  Pt::String str() const
193  { return _buffer.str(); }
194 
195  void str(const Pt::String& str)
196  { _buffer.str(str); }
197 
198  private:
199  Pt::StringBuffer _buffer;
200 };
201 
202 } // namespace Pt
203 
204 #else
205 
206 namespace Pt {
207 
220 typedef std::basic_stringstream<Pt::Char> StringStream;
221 
234 typedef std::basic_istringstream<Pt::Char> IStringStream;
235 
248 typedef std::basic_ostringstream<Pt::Char> OStringStream;
249 
250 } // namespace Pt
251 
252 #endif
253 
254 #endif
Unicode string stream buffer.
Definition: StringStream.h:44
traits_type::off_type off_type
Stream offset type.
Definition: StringStream.h:62
Buffer for input and output streams.
Definition: StreamBuffer.h:51
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
std::char_traits< Char > traits_type
Internal character traits.
Definition: StringStream.h:53
Unicode character type.
Definition: String.h:66
Unicode capable basic_string.
Definition: String.h:42
Char char_type
Internal character type.
Definition: StringStream.h:50