strstream 11 KB

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