strstream 11 KB

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