blob: 1904bb773f7b590036e15f1e926f197cd247c6df [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>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100135#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000137#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500138# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000139#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140
141_LIBCPP_BEGIN_NAMESPACE_STD
142
Howard Hinnant8331b762013-03-06 23:30:19 +0000143class _LIBCPP_TYPE_VIS strstreambuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144 : public streambuf
145{
146public:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100147#ifndef _LIBCPP_CXX03_LANG
148 strstreambuf() : strstreambuf(0) {}
149 explicit strstreambuf(streamsize __alsize);
150#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151 explicit strstreambuf(streamsize __alsize = 0);
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100152#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153 strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*));
Bruce Mitchener170d8972020-11-24 12:53:53 -0500154 strstreambuf(char* __gnext, streamsize __n, char* __pbeg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155 strstreambuf(const char* __gnext, streamsize __n);
156
Bruce Mitchener170d8972020-11-24 12:53:53 -0500157 strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 strstreambuf(const signed char* __gnext, streamsize __n);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500159 strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160 strstreambuf(const unsigned char* __gnext, streamsize __n);
161
Eric Fiselier78ccf772017-04-18 23:38:41 +0000162#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164 strstreambuf(strstreambuf&& __rhs);
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 strstreambuf& operator=(strstreambuf&& __rhs);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400167#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169 virtual ~strstreambuf();
170
171 void swap(strstreambuf& __rhs);
172
173 void freeze(bool __freezefl = true);
174 char* str();
Howard Hinnantc566dc32010-05-11 21:36:01 +0000175 int pcount() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176
177protected:
178 virtual int_type overflow (int_type __c = EOF);
179 virtual int_type pbackfail(int_type __c = EOF);
180 virtual int_type underflow();
181 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
182 ios_base::openmode __which = ios_base::in | ios_base::out);
183 virtual pos_type seekpos(pos_type __sp,
184 ios_base::openmode __which = ios_base::in | ios_base::out);
185
186private:
187 typedef unsigned __mode_type;
188 static const __mode_type __allocated = 0x01;
189 static const __mode_type __constant = 0x02;
190 static const __mode_type __dynamic = 0x04;
191 static const __mode_type __frozen = 0x08;
192 static const streamsize __default_alsize = 4096;
193
194 __mode_type __strmode_;
195 streamsize __alsize_;
196 void* (*__palloc_)(size_t);
197 void (*__pfree_)(void*);
198
199 void __init(char* __gnext, streamsize __n, char* __pbeg);
200};
201
Eric Fiselier78ccf772017-04-18 23:38:41 +0000202#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000203
204inline _LIBCPP_INLINE_VISIBILITY
205strstreambuf::strstreambuf(strstreambuf&& __rhs)
206 : streambuf(__rhs),
207 __strmode_(__rhs.__strmode_),
208 __alsize_(__rhs.__alsize_),
209 __palloc_(__rhs.__palloc_),
210 __pfree_(__rhs.__pfree_)
211{
212 __rhs.setg(nullptr, nullptr, nullptr);
213 __rhs.setp(nullptr, nullptr);
214}
215
216inline _LIBCPP_INLINE_VISIBILITY
217strstreambuf&
218strstreambuf::operator=(strstreambuf&& __rhs)
219{
220 if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0)
221 {
222 if (__pfree_)
223 __pfree_(eback());
224 else
225 delete [] eback();
226 }
227 streambuf::operator=(__rhs);
228 __strmode_ = __rhs.__strmode_;
229 __alsize_ = __rhs.__alsize_;
230 __palloc_ = __rhs.__palloc_;
231 __pfree_ = __rhs.__pfree_;
232 __rhs.setg(nullptr, nullptr, nullptr);
233 __rhs.setp(nullptr, nullptr);
234 return *this;
235}
236
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400237#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000238
Howard Hinnant8331b762013-03-06 23:30:19 +0000239class _LIBCPP_TYPE_VIS istrstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240 : public istream
241{
242public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 explicit istrstream(const char* __s)
245 : istream(&__sb_), __sb_(__s, 0) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 explicit istrstream(char* __s)
248 : istream(&__sb_), __sb_(__s, 0) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250 istrstream(const char* __s, streamsize __n)
251 : istream(&__sb_), __sb_(__s, __n) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000253 istrstream(char* __s, streamsize __n)
254 : istream(&__sb_), __sb_(__s, __n) {}
255
Eric Fiselier78ccf772017-04-18 23:38:41 +0000256#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258 istrstream(istrstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000259 : istream(_VSTD::move(__rhs)),
260 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261 {
262 istream::set_rdbuf(&__sb_);
263 }
264
Howard Hinnant186dca82010-09-23 17:31:07 +0000265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266 istrstream& operator=(istrstream&& __rhs)
267 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000268 istream::operator=(_VSTD::move(__rhs));
269 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270 return *this;
271 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400272#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273
274 virtual ~istrstream();
275
Howard Hinnant186dca82010-09-23 17:31:07 +0000276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277 void swap(istrstream& __rhs)
278 {
279 istream::swap(__rhs);
280 __sb_.swap(__rhs.__sb_);
281 }
282
Howard Hinnant186dca82010-09-23 17:31:07 +0000283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286 char *str() {return __sb_.str();}
287
288private:
289 strstreambuf __sb_;
290};
291
Howard Hinnant8331b762013-03-06 23:30:19 +0000292class _LIBCPP_TYPE_VIS ostrstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293 : public ostream
294{
295public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297 ostrstream()
298 : ostream(&__sb_) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
301 : ostream(&__sb_),
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500302 __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303 {}
304
Eric Fiselier78ccf772017-04-18 23:38:41 +0000305#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 ostrstream(ostrstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000308 : ostream(_VSTD::move(__rhs)),
309 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310 {
311 ostream::set_rdbuf(&__sb_);
312 }
313
Howard Hinnant186dca82010-09-23 17:31:07 +0000314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315 ostrstream& operator=(ostrstream&& __rhs)
316 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000317 ostream::operator=(_VSTD::move(__rhs));
318 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319 return *this;
320 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400321#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322
323 virtual ~ostrstream();
324
Howard Hinnant186dca82010-09-23 17:31:07 +0000325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 void swap(ostrstream& __rhs)
327 {
328 ostream::swap(__rhs);
329 __sb_.swap(__rhs.__sb_);
330 }
331
Howard Hinnant186dca82010-09-23 17:31:07 +0000332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000333 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000335 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337 char* str() {return __sb_.str();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 int pcount() const {return __sb_.pcount();}
340
341private:
Howard Hinnantc566dc32010-05-11 21:36:01 +0000342 strstreambuf __sb_; // exposition only
Howard Hinnantc51e1022010-05-11 19:42:16 +0000343};
344
Howard Hinnant8331b762013-03-06 23:30:19 +0000345class _LIBCPP_TYPE_VIS strstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000346 : public iostream
347{
348public:
349 // Types
350 typedef char char_type;
351 typedef char_traits<char>::int_type int_type;
352 typedef char_traits<char>::pos_type pos_type;
353 typedef char_traits<char>::off_type off_type;
354
355 // constructors/destructor
Howard Hinnant186dca82010-09-23 17:31:07 +0000356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357 strstream()
358 : iostream(&__sb_) {}
Howard Hinnant186dca82010-09-23 17:31:07 +0000359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360 strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
361 : iostream(&__sb_),
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500362 __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363 {}
364
Eric Fiselier78ccf772017-04-18 23:38:41 +0000365#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367 strstream(strstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000368 : iostream(_VSTD::move(__rhs)),
369 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 {
371 iostream::set_rdbuf(&__sb_);
372 }
373
Howard Hinnant186dca82010-09-23 17:31:07 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375 strstream& operator=(strstream&& __rhs)
376 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000377 iostream::operator=(_VSTD::move(__rhs));
378 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379 return *this;
380 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400381#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382
383 virtual ~strstream();
384
Howard Hinnant186dca82010-09-23 17:31:07 +0000385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386 void swap(strstream& __rhs)
387 {
388 iostream::swap(__rhs);
389 __sb_.swap(__rhs.__sb_);
390 }
391
392 // Members:
Howard Hinnant186dca82010-09-23 17:31:07 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 int pcount() const {return __sb_.pcount();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400 char* str() {return __sb_.str();}
401
402private:
Howard Hinnantc566dc32010-05-11 21:36:01 +0000403 strstreambuf __sb_; // exposition only
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404};
405
406_LIBCPP_END_NAMESPACE_STD
407
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400408#endif // _LIBCPP_STRSTREAM