blob: a5f17a9dc3196dcbfe0da5f3964edcc744c867e9 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRSTREAM
11#define _LIBCPP_STRSTREAM
12
13/*
14 strstream synopsis
15
16class strstreambuf
17 : public basic_streambuf<char>
18{
19public:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +010020 explicit strstreambuf(streamsize alsize_arg = 0); // before C++20
21 strstreambuf() : strstreambuf(0) {} // C++20
22 explicit strstreambuf(streamsize alsize_arg); // C++20
23
Howard Hinnantc51e1022010-05-11 19:42:16 +000024 strstreambuf(void* (*palloc_arg)(size_t), void (*pfree_arg)(void*));
Bruce Mitchener170d8972020-11-24 12:53:53 -050025 strstreambuf(char* gnext_arg, streamsize n, char* pbeg_arg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +000026 strstreambuf(const char* gnext_arg, streamsize n);
27
Bruce Mitchener170d8972020-11-24 12:53:53 -050028 strstreambuf(signed char* gnext_arg, streamsize n, signed char* pbeg_arg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +000029 strstreambuf(const signed char* gnext_arg, streamsize n);
Bruce Mitchener170d8972020-11-24 12:53:53 -050030 strstreambuf(unsigned char* gnext_arg, streamsize n, unsigned char* pbeg_arg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 strstreambuf(const unsigned char* gnext_arg, streamsize n);
32
33 strstreambuf(strstreambuf&& rhs);
34 strstreambuf& operator=(strstreambuf&& rhs);
35
36 virtual ~strstreambuf();
37
38 void swap(strstreambuf& rhs);
39
40 void freeze(bool freezefl = true);
41 char* str();
Howard Hinnantc566dc32010-05-11 21:36:01 +000042 int pcount() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +000043
44protected:
45 virtual int_type overflow (int_type c = EOF);
46 virtual int_type pbackfail(int_type c = EOF);
47 virtual int_type underflow();
48 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
49 ios_base::openmode which = ios_base::in | ios_base::out);
50 virtual pos_type seekpos(pos_type sp,
51 ios_base::openmode which = ios_base::in | ios_base::out);
52 virtual streambuf* setbuf(char* s, streamsize n);
53
54private:
55 typedef T1 strstate; // exposition only
56 static const strstate allocated; // exposition only
57 static const strstate constant; // exposition only
58 static const strstate dynamic; // exposition only
59 static const strstate frozen; // exposition only
60 strstate strmode; // exposition only
61 streamsize alsize; // exposition only
62 void* (*palloc)(size_t); // exposition only
63 void (*pfree)(void*); // exposition only
64};
65
66class istrstream
67 : public basic_istream<char>
68{
69public:
70 explicit istrstream(const char* s);
71 explicit istrstream(char* s);
72 istrstream(const char* s, streamsize n);
73 istrstream(char* s, streamsize n);
74
75 virtual ~istrstream();
76
77 strstreambuf* rdbuf() const;
78 char *str();
79
80private:
Howard Hinnantc566dc32010-05-11 21:36:01 +000081 strstreambuf sb; // exposition only
Howard Hinnantc51e1022010-05-11 19:42:16 +000082};
83
84class ostrstream
85 : public basic_ostream<char>
86{
87public:
88 ostrstream();
89 ostrstream(char* s, int n, ios_base::openmode mode = ios_base::out);
90
91 virtual ~ostrstream();
92
93 strstreambuf* rdbuf() const;
94 void freeze(bool freezefl = true);
95 char* str();
96 int pcount() const;
97
98private:
Howard Hinnantc566dc32010-05-11 21:36:01 +000099 strstreambuf sb; // exposition only
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100};
101
102class strstream
103 : public basic_iostream<char>
104{
105public:
106 // Types
107 typedef char char_type;
108 typedef char_traits<char>::int_type int_type;
109 typedef char_traits<char>::pos_type pos_type;
110 typedef char_traits<char>::off_type off_type;
111
112 // constructors/destructor
113 strstream();
114 strstream(char* s, int n, ios_base::openmode mode = ios_base::in | ios_base::out);
115
116 virtual ~strstream();
117
118 // Members:
119 strstreambuf* rdbuf() const;
120 void freeze(bool freezefl = true);
121 int pcount() const;
122 char* str();
123
124private:
Howard Hinnantc566dc32010-05-11 21:36:01 +0000125 strstreambuf sb; // exposition only
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126};
127
128} // std
129
130*/
131
132#include <__config>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133#include <istream>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400134#include <ostream>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000136#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000138#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139
140_LIBCPP_BEGIN_NAMESPACE_STD
141
Howard Hinnant8331b762013-03-06 23:30:19 +0000142class _LIBCPP_TYPE_VIS strstreambuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143 : public streambuf
144{
145public:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100146#ifndef _LIBCPP_CXX03_LANG
147 strstreambuf() : strstreambuf(0) {}
148 explicit strstreambuf(streamsize __alsize);
149#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150 explicit strstreambuf(streamsize __alsize = 0);
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100151#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*));
Bruce Mitchener170d8972020-11-24 12:53:53 -0500153 strstreambuf(char* __gnext, streamsize __n, char* __pbeg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154 strstreambuf(const char* __gnext, streamsize __n);
155
Bruce Mitchener170d8972020-11-24 12:53:53 -0500156 strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157 strstreambuf(const signed char* __gnext, streamsize __n);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500158 strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 strstreambuf(const unsigned char* __gnext, streamsize __n);
160
Eric Fiselier78ccf772017-04-18 23:38:41 +0000161#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163 strstreambuf(strstreambuf&& __rhs);
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165 strstreambuf& operator=(strstreambuf&& __rhs);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400166#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167
168 virtual ~strstreambuf();
169
170 void swap(strstreambuf& __rhs);
171
172 void freeze(bool __freezefl = true);
173 char* str();
Howard Hinnantc566dc32010-05-11 21:36:01 +0000174 int pcount() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175
176protected:
177 virtual int_type overflow (int_type __c = EOF);
178 virtual int_type pbackfail(int_type __c = EOF);
179 virtual int_type underflow();
180 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
181 ios_base::openmode __which = ios_base::in | ios_base::out);
182 virtual pos_type seekpos(pos_type __sp,
183 ios_base::openmode __which = ios_base::in | ios_base::out);
184
185private:
186 typedef unsigned __mode_type;
187 static const __mode_type __allocated = 0x01;
188 static const __mode_type __constant = 0x02;
189 static const __mode_type __dynamic = 0x04;
190 static const __mode_type __frozen = 0x08;
191 static const streamsize __default_alsize = 4096;
192
193 __mode_type __strmode_;
194 streamsize __alsize_;
195 void* (*__palloc_)(size_t);
196 void (*__pfree_)(void*);
197
198 void __init(char* __gnext, streamsize __n, char* __pbeg);
199};
200
Eric Fiselier78ccf772017-04-18 23:38:41 +0000201#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000202
203inline _LIBCPP_INLINE_VISIBILITY
204strstreambuf::strstreambuf(strstreambuf&& __rhs)
205 : streambuf(__rhs),
206 __strmode_(__rhs.__strmode_),
207 __alsize_(__rhs.__alsize_),
208 __palloc_(__rhs.__palloc_),
209 __pfree_(__rhs.__pfree_)
210{
211 __rhs.setg(nullptr, nullptr, nullptr);
212 __rhs.setp(nullptr, nullptr);
213}
214
215inline _LIBCPP_INLINE_VISIBILITY
216strstreambuf&
217strstreambuf::operator=(strstreambuf&& __rhs)
218{
219 if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0)
220 {
221 if (__pfree_)
222 __pfree_(eback());
223 else
224 delete [] eback();
225 }
226 streambuf::operator=(__rhs);
227 __strmode_ = __rhs.__strmode_;
228 __alsize_ = __rhs.__alsize_;
229 __palloc_ = __rhs.__palloc_;
230 __pfree_ = __rhs.__pfree_;
231 __rhs.setg(nullptr, nullptr, nullptr);
232 __rhs.setp(nullptr, nullptr);
233 return *this;
234}
235
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400236#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000237
Howard Hinnant8331b762013-03-06 23:30:19 +0000238class _LIBCPP_TYPE_VIS istrstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 : public istream
240{
241public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243 explicit istrstream(const char* __s)
244 : istream(&__sb_), __sb_(__s, 0) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246 explicit istrstream(char* __s)
247 : istream(&__sb_), __sb_(__s, 0) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249 istrstream(const char* __s, streamsize __n)
250 : istream(&__sb_), __sb_(__s, __n) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252 istrstream(char* __s, streamsize __n)
253 : istream(&__sb_), __sb_(__s, __n) {}
254
Eric Fiselier78ccf772017-04-18 23:38:41 +0000255#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257 istrstream(istrstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000258 : istream(_VSTD::move(__rhs)),
259 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260 {
261 istream::set_rdbuf(&__sb_);
262 }
263
Howard Hinnant186dca82010-09-23 17:31:07 +0000264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265 istrstream& operator=(istrstream&& __rhs)
266 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000267 istream::operator=(_VSTD::move(__rhs));
268 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269 return *this;
270 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400271#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272
273 virtual ~istrstream();
274
Howard Hinnant186dca82010-09-23 17:31:07 +0000275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276 void swap(istrstream& __rhs)
277 {
278 istream::swap(__rhs);
279 __sb_.swap(__rhs.__sb_);
280 }
281
Howard Hinnant186dca82010-09-23 17:31:07 +0000282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285 char *str() {return __sb_.str();}
286
287private:
288 strstreambuf __sb_;
289};
290
Howard Hinnant8331b762013-03-06 23:30:19 +0000291class _LIBCPP_TYPE_VIS ostrstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292 : public ostream
293{
294public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296 ostrstream()
297 : ostream(&__sb_) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299 ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
300 : ostream(&__sb_),
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500301 __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 {}
303
Eric Fiselier78ccf772017-04-18 23:38:41 +0000304#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306 ostrstream(ostrstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000307 : ostream(_VSTD::move(__rhs)),
308 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309 {
310 ostream::set_rdbuf(&__sb_);
311 }
312
Howard Hinnant186dca82010-09-23 17:31:07 +0000313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314 ostrstream& operator=(ostrstream&& __rhs)
315 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000316 ostream::operator=(_VSTD::move(__rhs));
317 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318 return *this;
319 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400320#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321
322 virtual ~ostrstream();
323
Howard Hinnant186dca82010-09-23 17:31:07 +0000324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325 void swap(ostrstream& __rhs)
326 {
327 ostream::swap(__rhs);
328 __sb_.swap(__rhs.__sb_);
329 }
330
Howard Hinnant186dca82010-09-23 17:31:07 +0000331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000334 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336 char* str() {return __sb_.str();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338 int pcount() const {return __sb_.pcount();}
339
340private:
Howard Hinnantc566dc32010-05-11 21:36:01 +0000341 strstreambuf __sb_; // exposition only
Howard Hinnantc51e1022010-05-11 19:42:16 +0000342};
343
Howard Hinnant8331b762013-03-06 23:30:19 +0000344class _LIBCPP_TYPE_VIS strstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345 : public iostream
346{
347public:
348 // Types
349 typedef char char_type;
350 typedef char_traits<char>::int_type int_type;
351 typedef char_traits<char>::pos_type pos_type;
352 typedef char_traits<char>::off_type off_type;
353
354 // constructors/destructor
Howard Hinnant186dca82010-09-23 17:31:07 +0000355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356 strstream()
357 : iostream(&__sb_) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000359 strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
360 : iostream(&__sb_),
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500361 __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000362 {}
363
Eric Fiselier78ccf772017-04-18 23:38:41 +0000364#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366 strstream(strstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000367 : iostream(_VSTD::move(__rhs)),
368 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369 {
370 iostream::set_rdbuf(&__sb_);
371 }
372
Howard Hinnant186dca82010-09-23 17:31:07 +0000373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374 strstream& operator=(strstream&& __rhs)
375 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000376 iostream::operator=(_VSTD::move(__rhs));
377 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 return *this;
379 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400380#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382 virtual ~strstream();
383
Howard Hinnant186dca82010-09-23 17:31:07 +0000384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385 void swap(strstream& __rhs)
386 {
387 iostream::swap(__rhs);
388 __sb_.swap(__rhs.__sb_);
389 }
390
391 // Members:
Howard Hinnant186dca82010-09-23 17:31:07 +0000392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397 int pcount() const {return __sb_.pcount();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 char* str() {return __sb_.str();}
400
401private:
Howard Hinnantc566dc32010-05-11 21:36:01 +0000402 strstreambuf __sb_; // exposition only
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403};
404
405_LIBCPP_END_NAMESPACE_STD
406
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400407#endif // _LIBCPP_STRSTREAM