strstream 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_PUSH_MACROS
  113. #include <__undef_macros>
  114. _LIBCPP_BEGIN_NAMESPACE_STD
  115. class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI strstreambuf : public streambuf {
  116. public:
  117. #ifndef _LIBCPP_CXX03_LANG
  118. _LIBCPP_HIDE_FROM_ABI 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_HIDE_FROM_ABI strstreambuf(strstreambuf&& __rhs);
  132. _LIBCPP_HIDE_FROM_ABI strstreambuf& operator=(strstreambuf&& __rhs);
  133. #endif // _LIBCPP_CXX03_LANG
  134. ~strstreambuf() override;
  135. void swap(strstreambuf& __rhs);
  136. void freeze(bool __freezefl = true);
  137. char* str();
  138. int pcount() const;
  139. protected:
  140. int_type overflow(int_type __c = EOF) override;
  141. int_type pbackfail(int_type __c = EOF) override;
  142. int_type underflow() override;
  143. pos_type
  144. seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out) override;
  145. pos_type seekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out) override;
  146. private:
  147. typedef unsigned __mode_type;
  148. static const __mode_type __allocated = 0x01;
  149. static const __mode_type __constant = 0x02;
  150. static const __mode_type __dynamic = 0x04;
  151. static const __mode_type __frozen = 0x08;
  152. static const streamsize __default_alsize = 4096;
  153. __mode_type __strmode_;
  154. streamsize __alsize_;
  155. void* (*__palloc_)(size_t);
  156. void (*__pfree_)(void*);
  157. void __init(char* __gnext, streamsize __n, char* __pbeg);
  158. };
  159. #ifndef _LIBCPP_CXX03_LANG
  160. inline _LIBCPP_HIDE_FROM_ABI strstreambuf::strstreambuf(strstreambuf&& __rhs)
  161. : streambuf(__rhs),
  162. __strmode_(__rhs.__strmode_),
  163. __alsize_(__rhs.__alsize_),
  164. __palloc_(__rhs.__palloc_),
  165. __pfree_(__rhs.__pfree_) {
  166. __rhs.setg(nullptr, nullptr, nullptr);
  167. __rhs.setp(nullptr, nullptr);
  168. }
  169. inline _LIBCPP_HIDE_FROM_ABI strstreambuf& strstreambuf::operator=(strstreambuf&& __rhs) {
  170. if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0) {
  171. if (__pfree_)
  172. __pfree_(eback());
  173. else
  174. delete[] eback();
  175. }
  176. streambuf::operator=(__rhs);
  177. __strmode_ = __rhs.__strmode_;
  178. __alsize_ = __rhs.__alsize_;
  179. __palloc_ = __rhs.__palloc_;
  180. __pfree_ = __rhs.__pfree_;
  181. __rhs.setg(nullptr, nullptr, nullptr);
  182. __rhs.setp(nullptr, nullptr);
  183. return *this;
  184. }
  185. #endif // _LIBCPP_CXX03_LANG
  186. class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI istrstream : public istream {
  187. public:
  188. _LIBCPP_HIDE_FROM_ABI explicit istrstream(const char* __s) : istream(&__sb_), __sb_(__s, 0) {}
  189. _LIBCPP_HIDE_FROM_ABI explicit istrstream(char* __s) : istream(&__sb_), __sb_(__s, 0) {}
  190. _LIBCPP_HIDE_FROM_ABI istrstream(const char* __s, streamsize __n) : istream(&__sb_), __sb_(__s, __n) {}
  191. _LIBCPP_HIDE_FROM_ABI istrstream(char* __s, streamsize __n) : istream(&__sb_), __sb_(__s, __n) {}
  192. #ifndef _LIBCPP_CXX03_LANG
  193. _LIBCPP_HIDE_FROM_ABI istrstream(istrstream&& __rhs) // extension
  194. : istream(std::move(static_cast<istream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
  195. istream::set_rdbuf(&__sb_);
  196. }
  197. _LIBCPP_HIDE_FROM_ABI istrstream& operator=(istrstream&& __rhs) {
  198. __sb_ = std::move(__rhs.__sb_);
  199. istream::operator=(std::move(__rhs));
  200. return *this;
  201. }
  202. #endif // _LIBCPP_CXX03_LANG
  203. ~istrstream() override;
  204. _LIBCPP_HIDE_FROM_ABI void swap(istrstream& __rhs) {
  205. istream::swap(__rhs);
  206. __sb_.swap(__rhs.__sb_);
  207. }
  208. _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
  209. _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
  210. private:
  211. strstreambuf __sb_;
  212. };
  213. class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI ostrstream : public ostream {
  214. public:
  215. _LIBCPP_HIDE_FROM_ABI ostrstream() : ostream(&__sb_) {}
  216. _LIBCPP_HIDE_FROM_ABI ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
  217. : ostream(&__sb_), __sb_(__s, __n, __s + (__mode & ios::app ? std::strlen(__s) : 0)) {}
  218. #ifndef _LIBCPP_CXX03_LANG
  219. _LIBCPP_HIDE_FROM_ABI ostrstream(ostrstream&& __rhs) // extension
  220. : ostream(std::move(static_cast<ostream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
  221. ostream::set_rdbuf(&__sb_);
  222. }
  223. _LIBCPP_HIDE_FROM_ABI ostrstream& operator=(ostrstream&& __rhs) {
  224. __sb_ = std::move(__rhs.__sb_);
  225. ostream::operator=(std::move(__rhs));
  226. return *this;
  227. }
  228. #endif // _LIBCPP_CXX03_LANG
  229. ~ostrstream() override;
  230. _LIBCPP_HIDE_FROM_ABI void swap(ostrstream& __rhs) {
  231. ostream::swap(__rhs);
  232. __sb_.swap(__rhs.__sb_);
  233. }
  234. _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
  235. _LIBCPP_HIDE_FROM_ABI void freeze(bool __freezefl = true) { __sb_.freeze(__freezefl); }
  236. _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
  237. _LIBCPP_HIDE_FROM_ABI int pcount() const { return __sb_.pcount(); }
  238. private:
  239. strstreambuf __sb_; // exposition only
  240. };
  241. class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI strstream : public iostream {
  242. public:
  243. // Types
  244. typedef char char_type;
  245. typedef char_traits<char>::int_type int_type;
  246. typedef char_traits<char>::pos_type pos_type;
  247. typedef char_traits<char>::off_type off_type;
  248. // constructors/destructor
  249. _LIBCPP_HIDE_FROM_ABI strstream() : iostream(&__sb_) {}
  250. _LIBCPP_HIDE_FROM_ABI strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
  251. : iostream(&__sb_), __sb_(__s, __n, __s + (__mode & ios::app ? std::strlen(__s) : 0)) {}
  252. #ifndef _LIBCPP_CXX03_LANG
  253. _LIBCPP_HIDE_FROM_ABI strstream(strstream&& __rhs) // extension
  254. : iostream(std::move(static_cast<iostream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
  255. iostream::set_rdbuf(&__sb_);
  256. }
  257. _LIBCPP_HIDE_FROM_ABI strstream& operator=(strstream&& __rhs) {
  258. __sb_ = std::move(__rhs.__sb_);
  259. iostream::operator=(std::move(__rhs));
  260. return *this;
  261. }
  262. #endif // _LIBCPP_CXX03_LANG
  263. ~strstream() override;
  264. _LIBCPP_HIDE_FROM_ABI void swap(strstream& __rhs) {
  265. iostream::swap(__rhs);
  266. __sb_.swap(__rhs.__sb_);
  267. }
  268. // Members:
  269. _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
  270. _LIBCPP_HIDE_FROM_ABI void freeze(bool __freezefl = true) { __sb_.freeze(__freezefl); }
  271. _LIBCPP_HIDE_FROM_ABI int pcount() const { return __sb_.pcount(); }
  272. _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
  273. private:
  274. strstreambuf __sb_; // exposition only
  275. };
  276. _LIBCPP_END_NAMESPACE_STD
  277. _LIBCPP_POP_MACROS
  278. #endif // _LIBCPP_STRSTREAM