strstream 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #ifndef _LIBCPP_STRSTREAM
  10. #define _LIBCPP_STRSTREAM
  11. /*
  12. strstream synopsis
  13. class strstreambuf
  14. : public basic_streambuf<char>
  15. {
  16. public:
  17. explicit strstreambuf(streamsize alsize_arg = 0); // before C++20
  18. strstreambuf() : strstreambuf(0) {} // C++20
  19. explicit strstreambuf(streamsize alsize_arg); // C++20
  20. strstreambuf(void* (*palloc_arg)(size_t), void (*pfree_arg)(void*));
  21. strstreambuf(char* gnext_arg, streamsize n, char* pbeg_arg = nullptr);
  22. strstreambuf(const char* gnext_arg, streamsize n);
  23. strstreambuf(signed char* gnext_arg, streamsize n, signed char* pbeg_arg = nullptr);
  24. strstreambuf(const signed char* gnext_arg, streamsize n);
  25. strstreambuf(unsigned char* gnext_arg, streamsize n, unsigned char* pbeg_arg = nullptr);
  26. strstreambuf(const unsigned char* gnext_arg, streamsize n);
  27. strstreambuf(strstreambuf&& rhs);
  28. strstreambuf& operator=(strstreambuf&& rhs);
  29. virtual ~strstreambuf();
  30. void swap(strstreambuf& rhs);
  31. void freeze(bool freezefl = true);
  32. char* str();
  33. int pcount() const;
  34. protected:
  35. virtual int_type overflow (int_type c = EOF);
  36. virtual int_type pbackfail(int_type c = EOF);
  37. virtual int_type underflow();
  38. virtual pos_type seekoff(off_type off, ios_base::seekdir way,
  39. ios_base::openmode which = ios_base::in | ios_base::out);
  40. virtual pos_type seekpos(pos_type sp,
  41. ios_base::openmode which = ios_base::in | ios_base::out);
  42. virtual streambuf* setbuf(char* s, streamsize n);
  43. private:
  44. typedef T1 strstate; // exposition only
  45. static const strstate allocated; // exposition only
  46. static const strstate constant; // exposition only
  47. static const strstate dynamic; // exposition only
  48. static const strstate frozen; // exposition only
  49. strstate strmode; // exposition only
  50. streamsize alsize; // exposition only
  51. void* (*palloc)(size_t); // exposition only
  52. void (*pfree)(void*); // exposition only
  53. };
  54. class istrstream
  55. : public basic_istream<char>
  56. {
  57. public:
  58. explicit istrstream(const char* s);
  59. explicit istrstream(char* s);
  60. istrstream(const char* s, streamsize n);
  61. istrstream(char* s, streamsize n);
  62. virtual ~istrstream();
  63. strstreambuf* rdbuf() const;
  64. char *str();
  65. private:
  66. strstreambuf sb; // exposition only
  67. };
  68. class ostrstream
  69. : public basic_ostream<char>
  70. {
  71. public:
  72. ostrstream();
  73. ostrstream(char* s, int n, ios_base::openmode mode = ios_base::out);
  74. virtual ~ostrstream();
  75. strstreambuf* rdbuf() const;
  76. void freeze(bool freezefl = true);
  77. char* str();
  78. int pcount() const;
  79. private:
  80. strstreambuf sb; // exposition only
  81. };
  82. class strstream
  83. : public basic_iostream<char>
  84. {
  85. public:
  86. // Types
  87. typedef char char_type;
  88. typedef char_traits<char>::int_type int_type;
  89. typedef char_traits<char>::pos_type pos_type;
  90. typedef char_traits<char>::off_type off_type;
  91. // constructors/destructor
  92. strstream();
  93. strstream(char* s, int n, ios_base::openmode mode = ios_base::in | ios_base::out);
  94. virtual ~strstream();
  95. // Members:
  96. strstreambuf* rdbuf() const;
  97. void freeze(bool freezefl = true);
  98. int pcount() const;
  99. char* str();
  100. private:
  101. strstreambuf sb; // exposition only
  102. };
  103. } // std
  104. */
  105. #include <__config>
  106. #include <istream>
  107. #include <ostream>
  108. #include <version>
  109. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  110. # pragma GCC system_header
  111. #endif
  112. _LIBCPP_BEGIN_NAMESPACE_STD
  113. class _LIBCPP_TYPE_VIS strstreambuf
  114. : public streambuf
  115. {
  116. public:
  117. #ifndef _LIBCPP_CXX03_LANG
  118. strstreambuf() : strstreambuf(0) {}
  119. explicit strstreambuf(streamsize __alsize);
  120. #else
  121. explicit strstreambuf(streamsize __alsize = 0);
  122. #endif
  123. strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*));
  124. strstreambuf(char* __gnext, streamsize __n, char* __pbeg = nullptr);
  125. strstreambuf(const char* __gnext, streamsize __n);
  126. strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = nullptr);
  127. strstreambuf(const signed char* __gnext, streamsize __n);
  128. strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = nullptr);
  129. strstreambuf(const unsigned char* __gnext, streamsize __n);
  130. #ifndef _LIBCPP_CXX03_LANG
  131. _LIBCPP_INLINE_VISIBILITY
  132. strstreambuf(strstreambuf&& __rhs);
  133. _LIBCPP_INLINE_VISIBILITY
  134. strstreambuf& operator=(strstreambuf&& __rhs);
  135. #endif // _LIBCPP_CXX03_LANG
  136. virtual ~strstreambuf();
  137. void swap(strstreambuf& __rhs);
  138. void freeze(bool __freezefl = true);
  139. char* str();
  140. int pcount() const;
  141. protected:
  142. virtual int_type overflow (int_type __c = EOF);
  143. virtual int_type pbackfail(int_type __c = EOF);
  144. virtual int_type underflow();
  145. virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
  146. ios_base::openmode __which = ios_base::in | ios_base::out);
  147. virtual pos_type seekpos(pos_type __sp,
  148. ios_base::openmode __which = ios_base::in | ios_base::out);
  149. private:
  150. typedef unsigned __mode_type;
  151. static const __mode_type __allocated = 0x01;
  152. static const __mode_type __constant = 0x02;
  153. static const __mode_type __dynamic = 0x04;
  154. static const __mode_type __frozen = 0x08;
  155. static const streamsize __default_alsize = 4096;
  156. __mode_type __strmode_;
  157. streamsize __alsize_;
  158. void* (*__palloc_)(size_t);
  159. void (*__pfree_)(void*);
  160. void __init(char* __gnext, streamsize __n, char* __pbeg);
  161. };
  162. #ifndef _LIBCPP_CXX03_LANG
  163. inline _LIBCPP_INLINE_VISIBILITY
  164. strstreambuf::strstreambuf(strstreambuf&& __rhs)
  165. : streambuf(__rhs),
  166. __strmode_(__rhs.__strmode_),
  167. __alsize_(__rhs.__alsize_),
  168. __palloc_(__rhs.__palloc_),
  169. __pfree_(__rhs.__pfree_)
  170. {
  171. __rhs.setg(nullptr, nullptr, nullptr);
  172. __rhs.setp(nullptr, nullptr);
  173. }
  174. inline _LIBCPP_INLINE_VISIBILITY
  175. strstreambuf&
  176. strstreambuf::operator=(strstreambuf&& __rhs)
  177. {
  178. if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0)
  179. {
  180. if (__pfree_)
  181. __pfree_(eback());
  182. else
  183. delete [] eback();
  184. }
  185. streambuf::operator=(__rhs);
  186. __strmode_ = __rhs.__strmode_;
  187. __alsize_ = __rhs.__alsize_;
  188. __palloc_ = __rhs.__palloc_;
  189. __pfree_ = __rhs.__pfree_;
  190. __rhs.setg(nullptr, nullptr, nullptr);
  191. __rhs.setp(nullptr, nullptr);
  192. return *this;
  193. }
  194. #endif // _LIBCPP_CXX03_LANG
  195. class _LIBCPP_TYPE_VIS istrstream
  196. : public istream
  197. {
  198. public:
  199. _LIBCPP_INLINE_VISIBILITY
  200. explicit istrstream(const char* __s)
  201. : istream(&__sb_), __sb_(__s, 0) {}
  202. _LIBCPP_INLINE_VISIBILITY
  203. explicit istrstream(char* __s)
  204. : istream(&__sb_), __sb_(__s, 0) {}
  205. _LIBCPP_INLINE_VISIBILITY
  206. istrstream(const char* __s, streamsize __n)
  207. : istream(&__sb_), __sb_(__s, __n) {}
  208. _LIBCPP_INLINE_VISIBILITY
  209. istrstream(char* __s, streamsize __n)
  210. : istream(&__sb_), __sb_(__s, __n) {}
  211. #ifndef _LIBCPP_CXX03_LANG
  212. _LIBCPP_INLINE_VISIBILITY
  213. istrstream(istrstream&& __rhs)
  214. : istream(_VSTD::move(__rhs)),
  215. __sb_(_VSTD::move(__rhs.__sb_))
  216. {
  217. istream::set_rdbuf(&__sb_);
  218. }
  219. _LIBCPP_INLINE_VISIBILITY
  220. istrstream& operator=(istrstream&& __rhs)
  221. {
  222. istream::operator=(_VSTD::move(__rhs));
  223. __sb_ = _VSTD::move(__rhs.__sb_);
  224. return *this;
  225. }
  226. #endif // _LIBCPP_CXX03_LANG
  227. virtual ~istrstream();
  228. _LIBCPP_INLINE_VISIBILITY
  229. void swap(istrstream& __rhs)
  230. {
  231. istream::swap(__rhs);
  232. __sb_.swap(__rhs.__sb_);
  233. }
  234. _LIBCPP_INLINE_VISIBILITY
  235. strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
  236. _LIBCPP_INLINE_VISIBILITY
  237. char *str() {return __sb_.str();}
  238. private:
  239. strstreambuf __sb_;
  240. };
  241. class _LIBCPP_TYPE_VIS ostrstream
  242. : public ostream
  243. {
  244. public:
  245. _LIBCPP_INLINE_VISIBILITY
  246. ostrstream()
  247. : ostream(&__sb_) {}
  248. _LIBCPP_INLINE_VISIBILITY
  249. ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
  250. : ostream(&__sb_),
  251. __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0))
  252. {}
  253. #ifndef _LIBCPP_CXX03_LANG
  254. _LIBCPP_INLINE_VISIBILITY
  255. ostrstream(ostrstream&& __rhs)
  256. : ostream(_VSTD::move(__rhs)),
  257. __sb_(_VSTD::move(__rhs.__sb_))
  258. {
  259. ostream::set_rdbuf(&__sb_);
  260. }
  261. _LIBCPP_INLINE_VISIBILITY
  262. ostrstream& operator=(ostrstream&& __rhs)
  263. {
  264. ostream::operator=(_VSTD::move(__rhs));
  265. __sb_ = _VSTD::move(__rhs.__sb_);
  266. return *this;
  267. }
  268. #endif // _LIBCPP_CXX03_LANG
  269. virtual ~ostrstream();
  270. _LIBCPP_INLINE_VISIBILITY
  271. void swap(ostrstream& __rhs)
  272. {
  273. ostream::swap(__rhs);
  274. __sb_.swap(__rhs.__sb_);
  275. }
  276. _LIBCPP_INLINE_VISIBILITY
  277. strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
  278. _LIBCPP_INLINE_VISIBILITY
  279. void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
  280. _LIBCPP_INLINE_VISIBILITY
  281. char* str() {return __sb_.str();}
  282. _LIBCPP_INLINE_VISIBILITY
  283. int pcount() const {return __sb_.pcount();}
  284. private:
  285. strstreambuf __sb_; // exposition only
  286. };
  287. class _LIBCPP_TYPE_VIS strstream
  288. : public iostream
  289. {
  290. public:
  291. // Types
  292. typedef char char_type;
  293. typedef char_traits<char>::int_type int_type;
  294. typedef char_traits<char>::pos_type pos_type;
  295. typedef char_traits<char>::off_type off_type;
  296. // constructors/destructor
  297. _LIBCPP_INLINE_VISIBILITY
  298. strstream()
  299. : iostream(&__sb_) {}
  300. _LIBCPP_INLINE_VISIBILITY
  301. strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
  302. : iostream(&__sb_),
  303. __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0))
  304. {}
  305. #ifndef _LIBCPP_CXX03_LANG
  306. _LIBCPP_INLINE_VISIBILITY
  307. strstream(strstream&& __rhs)
  308. : iostream(_VSTD::move(__rhs)),
  309. __sb_(_VSTD::move(__rhs.__sb_))
  310. {
  311. iostream::set_rdbuf(&__sb_);
  312. }
  313. _LIBCPP_INLINE_VISIBILITY
  314. strstream& operator=(strstream&& __rhs)
  315. {
  316. iostream::operator=(_VSTD::move(__rhs));
  317. __sb_ = _VSTD::move(__rhs.__sb_);
  318. return *this;
  319. }
  320. #endif // _LIBCPP_CXX03_LANG
  321. virtual ~strstream();
  322. _LIBCPP_INLINE_VISIBILITY
  323. void swap(strstream& __rhs)
  324. {
  325. iostream::swap(__rhs);
  326. __sb_.swap(__rhs.__sb_);
  327. }
  328. // Members:
  329. _LIBCPP_INLINE_VISIBILITY
  330. strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
  331. _LIBCPP_INLINE_VISIBILITY
  332. void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
  333. _LIBCPP_INLINE_VISIBILITY
  334. int pcount() const {return __sb_.pcount();}
  335. _LIBCPP_INLINE_VISIBILITY
  336. char* str() {return __sb_.str();}
  337. private:
  338. strstreambuf __sb_; // exposition only
  339. };
  340. _LIBCPP_END_NAMESPACE_STD
  341. #endif // _LIBCPP_STRSTREAM