blob: 6e60365475c5b32092f16b2c6d02e8cbfaaf671c [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- strstream --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STRSTREAM
12#define _LIBCPP_STRSTREAM
13
14/*
15 strstream synopsis
16
17class strstreambuf
18 : public basic_streambuf<char>
19{
20public:
21 explicit strstreambuf(streamsize alsize_arg = 0);
22 strstreambuf(void* (*palloc_arg)(size_t), void (*pfree_arg)(void*));
23 strstreambuf(char* gnext_arg, streamsize n, char* pbeg_arg = 0);
24 strstreambuf(const char* gnext_arg, streamsize n);
25
26 strstreambuf(signed char* gnext_arg, streamsize n, signed char* pbeg_arg = 0);
27 strstreambuf(const signed char* gnext_arg, streamsize n);
28 strstreambuf(unsigned char* gnext_arg, streamsize n, unsigned char* pbeg_arg = 0);
29 strstreambuf(const unsigned char* gnext_arg, streamsize n);
30
31 strstreambuf(strstreambuf&& rhs);
32 strstreambuf& operator=(strstreambuf&& rhs);
33
34 virtual ~strstreambuf();
35
36 void swap(strstreambuf& rhs);
37
38 void freeze(bool freezefl = true);
39 char* str();
Howard Hinnantf5256e12010-05-11 21:36:01 +000040 int pcount() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041
42protected:
43 virtual int_type overflow (int_type c = EOF);
44 virtual int_type pbackfail(int_type c = EOF);
45 virtual int_type underflow();
46 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
47 ios_base::openmode which = ios_base::in | ios_base::out);
48 virtual pos_type seekpos(pos_type sp,
49 ios_base::openmode which = ios_base::in | ios_base::out);
50 virtual streambuf* setbuf(char* s, streamsize n);
51
52private:
53 typedef T1 strstate; // exposition only
54 static const strstate allocated; // exposition only
55 static const strstate constant; // exposition only
56 static const strstate dynamic; // exposition only
57 static const strstate frozen; // exposition only
58 strstate strmode; // exposition only
59 streamsize alsize; // exposition only
60 void* (*palloc)(size_t); // exposition only
61 void (*pfree)(void*); // exposition only
62};
63
64class istrstream
65 : public basic_istream<char>
66{
67public:
68 explicit istrstream(const char* s);
69 explicit istrstream(char* s);
70 istrstream(const char* s, streamsize n);
71 istrstream(char* s, streamsize n);
72
73 virtual ~istrstream();
74
75 strstreambuf* rdbuf() const;
76 char *str();
77
78private:
Howard Hinnantf5256e12010-05-11 21:36:01 +000079 strstreambuf sb; // exposition only
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080};
81
82class ostrstream
83 : public basic_ostream<char>
84{
85public:
86 ostrstream();
87 ostrstream(char* s, int n, ios_base::openmode mode = ios_base::out);
88
89 virtual ~ostrstream();
90
91 strstreambuf* rdbuf() const;
92 void freeze(bool freezefl = true);
93 char* str();
94 int pcount() const;
95
96private:
Howard Hinnantf5256e12010-05-11 21:36:01 +000097 strstreambuf sb; // exposition only
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098};
99
100class strstream
101 : public basic_iostream<char>
102{
103public:
104 // Types
105 typedef char char_type;
106 typedef char_traits<char>::int_type int_type;
107 typedef char_traits<char>::pos_type pos_type;
108 typedef char_traits<char>::off_type off_type;
109
110 // constructors/destructor
111 strstream();
112 strstream(char* s, int n, ios_base::openmode mode = ios_base::in | ios_base::out);
113
114 virtual ~strstream();
115
116 // Members:
117 strstreambuf* rdbuf() const;
118 void freeze(bool freezefl = true);
119 int pcount() const;
120 char* str();
121
122private:
Howard Hinnantf5256e12010-05-11 21:36:01 +0000123 strstreambuf sb; // exposition only
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124};
125
126} // std
127
128*/
129
130#include <__config>
131#include <ostream>
132#include <istream>
133
134#pragma GCC system_header
135
136_LIBCPP_BEGIN_NAMESPACE_STD
137
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000138class _LIBCPP_VISIBLE strstreambuf
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139 : public streambuf
140{
141public:
142 explicit strstreambuf(streamsize __alsize = 0);
143 strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*));
144 strstreambuf(char* __gnext, streamsize __n, char* __pbeg = 0);
145 strstreambuf(const char* __gnext, streamsize __n);
146
147 strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = 0);
148 strstreambuf(const signed char* __gnext, streamsize __n);
149 strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = 0);
150 strstreambuf(const unsigned char* __gnext, streamsize __n);
151
Howard Hinnant73d21a42010-09-04 23:28:19 +0000152#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000153 strstreambuf(strstreambuf&& __rhs);
154 strstreambuf& operator=(strstreambuf&& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000155#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156
157 virtual ~strstreambuf();
158
159 void swap(strstreambuf& __rhs);
160
161 void freeze(bool __freezefl = true);
162 char* str();
Howard Hinnantf5256e12010-05-11 21:36:01 +0000163 int pcount() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164
165protected:
166 virtual int_type overflow (int_type __c = EOF);
167 virtual int_type pbackfail(int_type __c = EOF);
168 virtual int_type underflow();
169 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
170 ios_base::openmode __which = ios_base::in | ios_base::out);
171 virtual pos_type seekpos(pos_type __sp,
172 ios_base::openmode __which = ios_base::in | ios_base::out);
173
174private:
175 typedef unsigned __mode_type;
176 static const __mode_type __allocated = 0x01;
177 static const __mode_type __constant = 0x02;
178 static const __mode_type __dynamic = 0x04;
179 static const __mode_type __frozen = 0x08;
180 static const streamsize __default_alsize = 4096;
181
182 __mode_type __strmode_;
183 streamsize __alsize_;
184 void* (*__palloc_)(size_t);
185 void (*__pfree_)(void*);
186
187 void __init(char* __gnext, streamsize __n, char* __pbeg);
188};
189
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000190class _LIBCPP_VISIBLE istrstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191 : public istream
192{
193public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195 explicit istrstream(const char* __s)
196 : istream(&__sb_), __sb_(__s, 0) {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198 explicit istrstream(char* __s)
199 : istream(&__sb_), __sb_(__s, 0) {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201 istrstream(const char* __s, streamsize __n)
202 : istream(&__sb_), __sb_(__s, __n) {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204 istrstream(char* __s, streamsize __n)
205 : istream(&__sb_), __sb_(__s, __n) {}
206
Howard Hinnant73d21a42010-09-04 23:28:19 +0000207#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209 istrstream(istrstream&& __rhs)
210 : istream(_STD::move(__rhs)),
211 __sb_(_STD::move(__rhs.__sb_))
212 {
213 istream::set_rdbuf(&__sb_);
214 }
215
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 istrstream& operator=(istrstream&& __rhs)
218 {
219 istream::operator=(_STD::move(__rhs));
220 __sb_ = _STD::move(__rhs.__sb_);
221 return *this;
222 }
Howard Hinnant73d21a42010-09-04 23:28:19 +0000223#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000224
225 virtual ~istrstream();
226
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228 void swap(istrstream& __rhs)
229 {
230 istream::swap(__rhs);
231 __sb_.swap(__rhs.__sb_);
232 }
233
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237 char *str() {return __sb_.str();}
238
239private:
240 strstreambuf __sb_;
241};
242
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000243class _LIBCPP_VISIBLE ostrstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244 : public ostream
245{
246public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248 ostrstream()
249 : ostream(&__sb_) {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000251 ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
252 : ostream(&__sb_),
253 __sb_(__s, __n, __s + (__mode & ios::app ? strlen(__s) : 0))
254 {}
255
Howard Hinnant73d21a42010-09-04 23:28:19 +0000256#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258 ostrstream(ostrstream&& __rhs)
259 : ostream(_STD::move(__rhs)),
260 __sb_(_STD::move(__rhs.__sb_))
261 {
262 ostream::set_rdbuf(&__sb_);
263 }
264
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266 ostrstream& operator=(ostrstream&& __rhs)
267 {
268 ostream::operator=(_STD::move(__rhs));
269 __sb_ = _STD::move(__rhs.__sb_);
270 return *this;
271 }
Howard Hinnant73d21a42010-09-04 23:28:19 +0000272#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273
274 virtual ~ostrstream();
275
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277 void swap(ostrstream& __rhs)
278 {
279 ostream::swap(__rhs);
280 __sb_.swap(__rhs.__sb_);
281 }
282
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288 char* str() {return __sb_.str();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290 int pcount() const {return __sb_.pcount();}
291
292private:
Howard Hinnantf5256e12010-05-11 21:36:01 +0000293 strstreambuf __sb_; // exposition only
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294};
295
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000296class _LIBCPP_VISIBLE strstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000297 : public iostream
298{
299public:
300 // Types
301 typedef char char_type;
302 typedef char_traits<char>::int_type int_type;
303 typedef char_traits<char>::pos_type pos_type;
304 typedef char_traits<char>::off_type off_type;
305
306 // constructors/destructor
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308 strstream()
309 : iostream(&__sb_) {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
312 : iostream(&__sb_),
313 __sb_(__s, __n, __s + (__mode & ios::app ? strlen(__s) : 0))
314 {}
315
Howard Hinnant73d21a42010-09-04 23:28:19 +0000316#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000318 strstream(strstream&& __rhs)
319 : iostream(_STD::move(__rhs)),
320 __sb_(_STD::move(__rhs.__sb_))
321 {
322 iostream::set_rdbuf(&__sb_);
323 }
324
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326 strstream& operator=(strstream&& __rhs)
327 {
328 iostream::operator=(_STD::move(__rhs));
329 __sb_ = _STD::move(__rhs.__sb_);
330 return *this;
331 }
Howard Hinnant73d21a42010-09-04 23:28:19 +0000332#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333
334 virtual ~strstream();
335
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 void swap(strstream& __rhs)
338 {
339 iostream::swap(__rhs);
340 __sb_.swap(__rhs.__sb_);
341 }
342
343 // Members:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349 int pcount() const {return __sb_.pcount();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351 char* str() {return __sb_.str();}
352
353private:
Howard Hinnantf5256e12010-05-11 21:36:01 +0000354 strstreambuf __sb_; // exposition only
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355};
356
357_LIBCPP_END_NAMESPACE_STD
358
359#endif // _LIBCPP_STRSTREAM