ios.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. //===-------------------------- ios.cpp -----------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "__config"
  10. #if !defined(_LIBCPP_SGX_CONFIG)
  11. #include "ios"
  12. #include <stdlib.h>
  13. #include "__locale"
  14. #include "algorithm"
  15. #include "include/config_elast.h"
  16. #include "istream"
  17. #include "limits"
  18. #include "memory"
  19. #include "new"
  20. #include "streambuf"
  21. #include "string"
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template class basic_ios<char>;
  24. template class basic_ios<wchar_t>;
  25. template class basic_streambuf<char>;
  26. template class basic_streambuf<wchar_t>;
  27. template class basic_istream<char>;
  28. template class basic_istream<wchar_t>;
  29. template class basic_ostream<char>;
  30. template class basic_ostream<wchar_t>;
  31. template class basic_iostream<char>;
  32. class _LIBCPP_HIDDEN __iostream_category
  33. : public __do_message
  34. {
  35. public:
  36. virtual const char* name() const _NOEXCEPT;
  37. virtual string message(int ev) const;
  38. };
  39. const char*
  40. __iostream_category::name() const _NOEXCEPT
  41. {
  42. return "iostream";
  43. }
  44. string
  45. __iostream_category::message(int ev) const
  46. {
  47. if (ev != static_cast<int>(io_errc::stream)
  48. #ifdef _LIBCPP_ELAST
  49. && ev <= _LIBCPP_ELAST
  50. #endif // _LIBCPP_ELAST
  51. )
  52. return __do_message::message(ev);
  53. return string("unspecified iostream_category error");
  54. }
  55. const error_category&
  56. iostream_category() _NOEXCEPT
  57. {
  58. static __iostream_category s;
  59. return s;
  60. }
  61. // ios_base::failure
  62. ios_base::failure::failure(const string& msg, const error_code& ec)
  63. : system_error(ec, msg)
  64. {
  65. }
  66. ios_base::failure::failure(const char* msg, const error_code& ec)
  67. : system_error(ec, msg)
  68. {
  69. }
  70. ios_base::failure::~failure() throw()
  71. {
  72. }
  73. // ios_base locale
  74. const ios_base::fmtflags ios_base::boolalpha;
  75. const ios_base::fmtflags ios_base::dec;
  76. const ios_base::fmtflags ios_base::fixed;
  77. const ios_base::fmtflags ios_base::hex;
  78. const ios_base::fmtflags ios_base::internal;
  79. const ios_base::fmtflags ios_base::left;
  80. const ios_base::fmtflags ios_base::oct;
  81. const ios_base::fmtflags ios_base::right;
  82. const ios_base::fmtflags ios_base::scientific;
  83. const ios_base::fmtflags ios_base::showbase;
  84. const ios_base::fmtflags ios_base::showpoint;
  85. const ios_base::fmtflags ios_base::showpos;
  86. const ios_base::fmtflags ios_base::skipws;
  87. const ios_base::fmtflags ios_base::unitbuf;
  88. const ios_base::fmtflags ios_base::uppercase;
  89. const ios_base::fmtflags ios_base::adjustfield;
  90. const ios_base::fmtflags ios_base::basefield;
  91. const ios_base::fmtflags ios_base::floatfield;
  92. const ios_base::iostate ios_base::badbit;
  93. const ios_base::iostate ios_base::eofbit;
  94. const ios_base::iostate ios_base::failbit;
  95. const ios_base::iostate ios_base::goodbit;
  96. const ios_base::openmode ios_base::app;
  97. const ios_base::openmode ios_base::ate;
  98. const ios_base::openmode ios_base::binary;
  99. const ios_base::openmode ios_base::in;
  100. const ios_base::openmode ios_base::out;
  101. const ios_base::openmode ios_base::trunc;
  102. void
  103. ios_base::__call_callbacks(event ev)
  104. {
  105. for (size_t i = __event_size_; i;)
  106. {
  107. --i;
  108. __fn_[i](ev, *this, __index_[i]);
  109. }
  110. }
  111. // locale
  112. locale
  113. ios_base::imbue(const locale& newloc)
  114. {
  115. static_assert(sizeof(locale) == sizeof(__loc_), "");
  116. locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
  117. locale oldloc = loc_storage;
  118. loc_storage = newloc;
  119. __call_callbacks(imbue_event);
  120. return oldloc;
  121. }
  122. locale
  123. ios_base::getloc() const
  124. {
  125. const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
  126. return loc_storage;
  127. }
  128. // xalloc
  129. #if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
  130. atomic<int> ios_base::__xindex_ = ATOMIC_VAR_INIT(0);
  131. #else
  132. int ios_base::__xindex_ = 0;
  133. #endif
  134. template <typename _Tp>
  135. static size_t __ios_new_cap(size_t __req_size, size_t __current_cap)
  136. { // Precondition: __req_size > __current_cap
  137. const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp);
  138. if (__req_size < mx/2)
  139. return _VSTD::max(2 * __current_cap, __req_size);
  140. else
  141. return mx;
  142. }
  143. int
  144. ios_base::xalloc()
  145. {
  146. return __xindex_++;
  147. }
  148. long&
  149. ios_base::iword(int index)
  150. {
  151. size_t req_size = static_cast<size_t>(index)+1;
  152. if (req_size > __iarray_cap_)
  153. {
  154. size_t newcap = __ios_new_cap<long>(req_size, __iarray_cap_);
  155. long* iarray = static_cast<long*>(realloc(__iarray_, newcap * sizeof(long)));
  156. if (iarray == 0)
  157. {
  158. setstate(badbit);
  159. static long error;
  160. error = 0;
  161. return error;
  162. }
  163. __iarray_ = iarray;
  164. for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p)
  165. *p = 0;
  166. __iarray_cap_ = newcap;
  167. }
  168. __iarray_size_ = max<size_t>(__iarray_size_, req_size);
  169. return __iarray_[index];
  170. }
  171. void*&
  172. ios_base::pword(int index)
  173. {
  174. size_t req_size = static_cast<size_t>(index)+1;
  175. if (req_size > __parray_cap_)
  176. {
  177. size_t newcap = __ios_new_cap<void *>(req_size, __iarray_cap_);
  178. void** parray = static_cast<void**>(realloc(__parray_, newcap * sizeof(void *)));
  179. if (parray == 0)
  180. {
  181. setstate(badbit);
  182. static void* error;
  183. error = 0;
  184. return error;
  185. }
  186. __parray_ = parray;
  187. for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p)
  188. *p = 0;
  189. __parray_cap_ = newcap;
  190. }
  191. __parray_size_ = max<size_t>(__parray_size_, req_size);
  192. return __parray_[index];
  193. }
  194. // register_callback
  195. void
  196. ios_base::register_callback(event_callback fn, int index)
  197. {
  198. size_t req_size = __event_size_ + 1;
  199. if (req_size > __event_cap_)
  200. {
  201. size_t newcap = __ios_new_cap<event_callback>(req_size, __event_cap_);
  202. event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newcap * sizeof(event_callback)));
  203. if (fns == 0)
  204. setstate(badbit);
  205. __fn_ = fns;
  206. int* indxs = static_cast<int *>(realloc(__index_, newcap * sizeof(int)));
  207. if (indxs == 0)
  208. setstate(badbit);
  209. __index_ = indxs;
  210. __event_cap_ = newcap;
  211. }
  212. __fn_[__event_size_] = fn;
  213. __index_[__event_size_] = index;
  214. ++__event_size_;
  215. }
  216. ios_base::~ios_base()
  217. {
  218. __call_callbacks(erase_event);
  219. locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
  220. loc_storage.~locale();
  221. free(__fn_);
  222. free(__index_);
  223. free(__iarray_);
  224. free(__parray_);
  225. }
  226. // iostate
  227. void
  228. ios_base::clear(iostate state)
  229. {
  230. if (__rdbuf_)
  231. __rdstate_ = state;
  232. else
  233. __rdstate_ = state | badbit;
  234. #ifndef _LIBCPP_NO_EXCEPTIONS
  235. if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)
  236. throw failure("ios_base::clear");
  237. #endif // _LIBCPP_NO_EXCEPTIONS
  238. }
  239. // init
  240. void
  241. ios_base::init(void* sb)
  242. {
  243. __rdbuf_ = sb;
  244. __rdstate_ = __rdbuf_ ? goodbit : badbit;
  245. __exceptions_ = goodbit;
  246. __fmtflags_ = skipws | dec;
  247. __width_ = 0;
  248. __precision_ = 6;
  249. __fn_ = 0;
  250. __index_ = 0;
  251. __event_size_ = 0;
  252. __event_cap_ = 0;
  253. __iarray_ = 0;
  254. __iarray_size_ = 0;
  255. __iarray_cap_ = 0;
  256. __parray_ = 0;
  257. __parray_size_ = 0;
  258. __parray_cap_ = 0;
  259. ::new(&__loc_) locale;
  260. }
  261. void
  262. ios_base::copyfmt(const ios_base& rhs)
  263. {
  264. // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
  265. // Don't alter *this until all needed resources are acquired
  266. unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
  267. unique_ptr<int, void (*)(void*)> new_ints(0, free);
  268. unique_ptr<long, void (*)(void*)> new_longs(0, free);
  269. unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
  270. if (__event_cap_ < rhs.__event_size_)
  271. {
  272. size_t newesize = sizeof(event_callback) * rhs.__event_size_;
  273. new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
  274. #ifndef _LIBCPP_NO_EXCEPTIONS
  275. if (!new_callbacks)
  276. throw bad_alloc();
  277. #endif // _LIBCPP_NO_EXCEPTIONS
  278. size_t newisize = sizeof(int) * rhs.__event_size_;
  279. new_ints.reset(static_cast<int *>(malloc(newisize)));
  280. #ifndef _LIBCPP_NO_EXCEPTIONS
  281. if (!new_ints)
  282. throw bad_alloc();
  283. #endif // _LIBCPP_NO_EXCEPTIONS
  284. }
  285. if (__iarray_cap_ < rhs.__iarray_size_)
  286. {
  287. size_t newsize = sizeof(long) * rhs.__iarray_size_;
  288. new_longs.reset(static_cast<long*>(malloc(newsize)));
  289. #ifndef _LIBCPP_NO_EXCEPTIONS
  290. if (!new_longs)
  291. throw bad_alloc();
  292. #endif // _LIBCPP_NO_EXCEPTIONS
  293. }
  294. if (__parray_cap_ < rhs.__parray_size_)
  295. {
  296. size_t newsize = sizeof(void*) * rhs.__parray_size_;
  297. new_pointers.reset(static_cast<void**>(malloc(newsize)));
  298. #ifndef _LIBCPP_NO_EXCEPTIONS
  299. if (!new_pointers)
  300. throw bad_alloc();
  301. #endif // _LIBCPP_NO_EXCEPTIONS
  302. }
  303. // Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_
  304. __fmtflags_ = rhs.__fmtflags_;
  305. __precision_ = rhs.__precision_;
  306. __width_ = rhs.__width_;
  307. locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
  308. const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
  309. lhs_loc = rhs_loc;
  310. if (__event_cap_ < rhs.__event_size_)
  311. {
  312. free(__fn_);
  313. __fn_ = new_callbacks.release();
  314. free(__index_);
  315. __index_ = new_ints.release();
  316. __event_cap_ = rhs.__event_size_;
  317. }
  318. for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_)
  319. {
  320. __fn_[__event_size_] = rhs.__fn_[__event_size_];
  321. __index_[__event_size_] = rhs.__index_[__event_size_];
  322. }
  323. if (__iarray_cap_ < rhs.__iarray_size_)
  324. {
  325. free(__iarray_);
  326. __iarray_ = new_longs.release();
  327. __iarray_cap_ = rhs.__iarray_size_;
  328. }
  329. for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
  330. __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
  331. if (__parray_cap_ < rhs.__parray_size_)
  332. {
  333. free(__parray_);
  334. __parray_ = new_pointers.release();
  335. __parray_cap_ = rhs.__parray_size_;
  336. }
  337. for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
  338. __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
  339. }
  340. void
  341. ios_base::move(ios_base& rhs)
  342. {
  343. // *this is uninitialized
  344. __fmtflags_ = rhs.__fmtflags_;
  345. __precision_ = rhs.__precision_;
  346. __width_ = rhs.__width_;
  347. __rdstate_ = rhs.__rdstate_;
  348. __exceptions_ = rhs.__exceptions_;
  349. __rdbuf_ = 0;
  350. locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
  351. ::new(&__loc_) locale(rhs_loc);
  352. __fn_ = rhs.__fn_;
  353. rhs.__fn_ = 0;
  354. __index_ = rhs.__index_;
  355. rhs.__index_ = 0;
  356. __event_size_ = rhs.__event_size_;
  357. rhs.__event_size_ = 0;
  358. __event_cap_ = rhs.__event_cap_;
  359. rhs.__event_cap_ = 0;
  360. __iarray_ = rhs.__iarray_;
  361. rhs.__iarray_ = 0;
  362. __iarray_size_ = rhs.__iarray_size_;
  363. rhs.__iarray_size_ = 0;
  364. __iarray_cap_ = rhs.__iarray_cap_;
  365. rhs.__iarray_cap_ = 0;
  366. __parray_ = rhs.__parray_;
  367. rhs.__parray_ = 0;
  368. __parray_size_ = rhs.__parray_size_;
  369. rhs.__parray_size_ = 0;
  370. __parray_cap_ = rhs.__parray_cap_;
  371. rhs.__parray_cap_ = 0;
  372. }
  373. void
  374. ios_base::swap(ios_base& rhs) _NOEXCEPT
  375. {
  376. _VSTD::swap(__fmtflags_, rhs.__fmtflags_);
  377. _VSTD::swap(__precision_, rhs.__precision_);
  378. _VSTD::swap(__width_, rhs.__width_);
  379. _VSTD::swap(__rdstate_, rhs.__rdstate_);
  380. _VSTD::swap(__exceptions_, rhs.__exceptions_);
  381. locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
  382. locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
  383. _VSTD::swap(lhs_loc, rhs_loc);
  384. _VSTD::swap(__fn_, rhs.__fn_);
  385. _VSTD::swap(__index_, rhs.__index_);
  386. _VSTD::swap(__event_size_, rhs.__event_size_);
  387. _VSTD::swap(__event_cap_, rhs.__event_cap_);
  388. _VSTD::swap(__iarray_, rhs.__iarray_);
  389. _VSTD::swap(__iarray_size_, rhs.__iarray_size_);
  390. _VSTD::swap(__iarray_cap_, rhs.__iarray_cap_);
  391. _VSTD::swap(__parray_, rhs.__parray_);
  392. _VSTD::swap(__parray_size_, rhs.__parray_size_);
  393. _VSTD::swap(__parray_cap_, rhs.__parray_cap_);
  394. }
  395. void
  396. ios_base::__set_badbit_and_consider_rethrow()
  397. {
  398. __rdstate_ |= badbit;
  399. #ifndef _LIBCPP_NO_EXCEPTIONS
  400. if (__exceptions_ & badbit)
  401. throw;
  402. #endif // _LIBCPP_NO_EXCEPTIONS
  403. }
  404. void
  405. ios_base::__set_failbit_and_consider_rethrow()
  406. {
  407. __rdstate_ |= failbit;
  408. #ifndef _LIBCPP_NO_EXCEPTIONS
  409. if (__exceptions_ & failbit)
  410. throw;
  411. #endif // _LIBCPP_NO_EXCEPTIONS
  412. }
  413. bool
  414. ios_base::sync_with_stdio(bool sync)
  415. {
  416. static bool previous_state = true;
  417. bool r = previous_state;
  418. previous_state = sync;
  419. return r;
  420. }
  421. _LIBCPP_END_NAMESPACE_STD
  422. #endif // !defined(_LIBCPP_SGX_CONFIG)