1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// 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 7// 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: 20 explicit strstreambuf(streamsize alsize_arg = 0); // before C++20 21 strstreambuf() : strstreambuf(0) {} // C++20 22 explicit strstreambuf(streamsize alsize_arg); // C++20 23 24 strstreambuf(void* (*palloc_arg)(size_t), void (*pfree_arg)(void*)); 25 strstreambuf(char* gnext_arg, streamsize n, char* pbeg_arg = nullptr); 26 strstreambuf(const char* gnext_arg, streamsize n); 27 28 strstreambuf(signed char* gnext_arg, streamsize n, signed char* pbeg_arg = nullptr); 29 strstreambuf(const signed char* gnext_arg, streamsize n); 30 strstreambuf(unsigned char* gnext_arg, streamsize n, unsigned char* pbeg_arg = nullptr); 31 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(); 42 int pcount() const; 43 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: 81 strstreambuf sb; // exposition only 82}; 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: 99 strstreambuf sb; // exposition only 100}; 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: 125 strstreambuf sb; // exposition only 126}; 127 128} // std 129 130*/ 131 132#include <__assert> // all public C++ headers provide the assertion handler 133#include <__config> 134#include <istream> 135#include <ostream> 136#include <version> 137 138#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 139# pragma GCC system_header 140#endif 141 142_LIBCPP_BEGIN_NAMESPACE_STD 143 144class _LIBCPP_TYPE_VIS strstreambuf 145 : public streambuf 146{ 147public: 148#ifndef _LIBCPP_CXX03_LANG 149 strstreambuf() : strstreambuf(0) {} 150 explicit strstreambuf(streamsize __alsize); 151#else 152 explicit strstreambuf(streamsize __alsize = 0); 153#endif 154 strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*)); 155 strstreambuf(char* __gnext, streamsize __n, char* __pbeg = nullptr); 156 strstreambuf(const char* __gnext, streamsize __n); 157 158 strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = nullptr); 159 strstreambuf(const signed char* __gnext, streamsize __n); 160 strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = nullptr); 161 strstreambuf(const unsigned char* __gnext, streamsize __n); 162 163#ifndef _LIBCPP_CXX03_LANG 164 _LIBCPP_INLINE_VISIBILITY 165 strstreambuf(strstreambuf&& __rhs); 166 _LIBCPP_INLINE_VISIBILITY 167 strstreambuf& operator=(strstreambuf&& __rhs); 168#endif // _LIBCPP_CXX03_LANG 169 170 ~strstreambuf() override; 171 172 void swap(strstreambuf& __rhs); 173 174 void freeze(bool __freezefl = true); 175 char* str(); 176 int pcount() const; 177 178protected: 179 int_type overflow (int_type __c = EOF) override; 180 int_type pbackfail(int_type __c = EOF) override; 181 int_type underflow() override; 182 pos_type seekoff(off_type __off, ios_base::seekdir __way, 183 ios_base::openmode __which = ios_base::in | ios_base::out) override; 184 pos_type seekpos(pos_type __sp, 185 ios_base::openmode __which = ios_base::in | ios_base::out) override; 186 187private: 188 typedef unsigned __mode_type; 189 static const __mode_type __allocated = 0x01; 190 static const __mode_type __constant = 0x02; 191 static const __mode_type __dynamic = 0x04; 192 static const __mode_type __frozen = 0x08; 193 static const streamsize __default_alsize = 4096; 194 195 __mode_type __strmode_; 196 streamsize __alsize_; 197 void* (*__palloc_)(size_t); 198 void (*__pfree_)(void*); 199 200 void __init(char* __gnext, streamsize __n, char* __pbeg); 201}; 202 203#ifndef _LIBCPP_CXX03_LANG 204 205inline _LIBCPP_INLINE_VISIBILITY 206strstreambuf::strstreambuf(strstreambuf&& __rhs) 207 : streambuf(__rhs), 208 __strmode_(__rhs.__strmode_), 209 __alsize_(__rhs.__alsize_), 210 __palloc_(__rhs.__palloc_), 211 __pfree_(__rhs.__pfree_) 212{ 213 __rhs.setg(nullptr, nullptr, nullptr); 214 __rhs.setp(nullptr, nullptr); 215} 216 217inline _LIBCPP_INLINE_VISIBILITY 218strstreambuf& 219strstreambuf::operator=(strstreambuf&& __rhs) 220{ 221 if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0) 222 { 223 if (__pfree_) 224 __pfree_(eback()); 225 else 226 delete [] eback(); 227 } 228 streambuf::operator=(__rhs); 229 __strmode_ = __rhs.__strmode_; 230 __alsize_ = __rhs.__alsize_; 231 __palloc_ = __rhs.__palloc_; 232 __pfree_ = __rhs.__pfree_; 233 __rhs.setg(nullptr, nullptr, nullptr); 234 __rhs.setp(nullptr, nullptr); 235 return *this; 236} 237 238#endif // _LIBCPP_CXX03_LANG 239 240class _LIBCPP_TYPE_VIS istrstream 241 : public istream 242{ 243public: 244 _LIBCPP_INLINE_VISIBILITY 245 explicit istrstream(const char* __s) 246 : istream(&__sb_), __sb_(__s, 0) {} 247 _LIBCPP_INLINE_VISIBILITY 248 explicit istrstream(char* __s) 249 : istream(&__sb_), __sb_(__s, 0) {} 250 _LIBCPP_INLINE_VISIBILITY 251 istrstream(const char* __s, streamsize __n) 252 : istream(&__sb_), __sb_(__s, __n) {} 253 _LIBCPP_INLINE_VISIBILITY 254 istrstream(char* __s, streamsize __n) 255 : istream(&__sb_), __sb_(__s, __n) {} 256 257#ifndef _LIBCPP_CXX03_LANG 258 _LIBCPP_INLINE_VISIBILITY 259 istrstream(istrstream&& __rhs) 260 : istream(_VSTD::move(__rhs)), 261 __sb_(_VSTD::move(__rhs.__sb_)) 262 { 263 istream::set_rdbuf(&__sb_); 264 } 265 266 _LIBCPP_INLINE_VISIBILITY 267 istrstream& operator=(istrstream&& __rhs) 268 { 269 __sb_ = _VSTD::move(__rhs.__sb_); 270 istream::operator=(_VSTD::move(__rhs)); 271 return *this; 272 } 273#endif // _LIBCPP_CXX03_LANG 274 275 ~istrstream() override; 276 277 _LIBCPP_INLINE_VISIBILITY 278 void swap(istrstream& __rhs) 279 { 280 istream::swap(__rhs); 281 __sb_.swap(__rhs.__sb_); 282 } 283 284 _LIBCPP_INLINE_VISIBILITY 285 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);} 286 _LIBCPP_INLINE_VISIBILITY 287 char *str() {return __sb_.str();} 288 289private: 290 strstreambuf __sb_; 291}; 292 293class _LIBCPP_TYPE_VIS ostrstream 294 : public ostream 295{ 296public: 297 _LIBCPP_INLINE_VISIBILITY 298 ostrstream() 299 : ostream(&__sb_) {} 300 _LIBCPP_INLINE_VISIBILITY 301 ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out) 302 : ostream(&__sb_), 303 __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0)) 304 {} 305 306#ifndef _LIBCPP_CXX03_LANG 307 _LIBCPP_INLINE_VISIBILITY 308 ostrstream(ostrstream&& __rhs) 309 : ostream(_VSTD::move(__rhs)), 310 __sb_(_VSTD::move(__rhs.__sb_)) 311 { 312 ostream::set_rdbuf(&__sb_); 313 } 314 315 _LIBCPP_INLINE_VISIBILITY 316 ostrstream& operator=(ostrstream&& __rhs) 317 { 318 __sb_ = _VSTD::move(__rhs.__sb_); 319 ostream::operator=(_VSTD::move(__rhs)); 320 return *this; 321 } 322#endif // _LIBCPP_CXX03_LANG 323 324 ~ostrstream() override; 325 326 _LIBCPP_INLINE_VISIBILITY 327 void swap(ostrstream& __rhs) 328 { 329 ostream::swap(__rhs); 330 __sb_.swap(__rhs.__sb_); 331 } 332 333 _LIBCPP_INLINE_VISIBILITY 334 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);} 335 _LIBCPP_INLINE_VISIBILITY 336 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);} 337 _LIBCPP_INLINE_VISIBILITY 338 char* str() {return __sb_.str();} 339 _LIBCPP_INLINE_VISIBILITY 340 int pcount() const {return __sb_.pcount();} 341 342private: 343 strstreambuf __sb_; // exposition only 344}; 345 346class _LIBCPP_TYPE_VIS strstream 347 : public iostream 348{ 349public: 350 // Types 351 typedef char char_type; 352 typedef char_traits<char>::int_type int_type; 353 typedef char_traits<char>::pos_type pos_type; 354 typedef char_traits<char>::off_type off_type; 355 356 // constructors/destructor 357 _LIBCPP_INLINE_VISIBILITY 358 strstream() 359 : iostream(&__sb_) {} 360 _LIBCPP_INLINE_VISIBILITY 361 strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out) 362 : iostream(&__sb_), 363 __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0)) 364 {} 365 366#ifndef _LIBCPP_CXX03_LANG 367 _LIBCPP_INLINE_VISIBILITY 368 strstream(strstream&& __rhs) 369 : iostream(_VSTD::move(__rhs)), 370 __sb_(_VSTD::move(__rhs.__sb_)) 371 { 372 iostream::set_rdbuf(&__sb_); 373 } 374 375 _LIBCPP_INLINE_VISIBILITY 376 strstream& operator=(strstream&& __rhs) 377 { 378 __sb_ = _VSTD::move(__rhs.__sb_); 379 iostream::operator=(_VSTD::move(__rhs)); 380 return *this; 381 } 382#endif // _LIBCPP_CXX03_LANG 383 384 ~strstream() override; 385 386 _LIBCPP_INLINE_VISIBILITY 387 void swap(strstream& __rhs) 388 { 389 iostream::swap(__rhs); 390 __sb_.swap(__rhs.__sb_); 391 } 392 393 // Members: 394 _LIBCPP_INLINE_VISIBILITY 395 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);} 396 _LIBCPP_INLINE_VISIBILITY 397 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);} 398 _LIBCPP_INLINE_VISIBILITY 399 int pcount() const {return __sb_.pcount();} 400 _LIBCPP_INLINE_VISIBILITY 401 char* str() {return __sb_.str();} 402 403private: 404 strstreambuf __sb_; // exposition only 405}; 406 407_LIBCPP_END_NAMESPACE_STD 408 409#endif // _LIBCPP_STRSTREAM 410