iomanip 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // Standard stream manipulators -*- C++ -*-
  2. // Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/iomanip
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 27.6.3 Standard manipulators
  25. //
  26. #ifndef _GLIBCXX_IOMANIP
  27. #define _GLIBCXX_IOMANIP 1
  28. #pragma GCC system_header
  29. #include <bits/c++config.h>
  30. #include <iosfwd>
  31. #include <bits/ios_base.h>
  32. #if __cplusplus >= 201103L
  33. #include <locale>
  34. #if __cplusplus > 201103L
  35. #include <bits/quoted_string.h>
  36. #endif
  37. #endif
  38. namespace std _GLIBCXX_VISIBILITY(default)
  39. {
  40. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  41. // [27.6.3] standard manipulators
  42. // Also see DR 183.
  43. struct _Resetiosflags { ios_base::fmtflags _M_mask; };
  44. /**
  45. * @brief Manipulator for @c setf.
  46. * @param __mask A format flags mask.
  47. *
  48. * Sent to a stream object, this manipulator resets the specified flags,
  49. * via @e stream.setf(0,__mask).
  50. */
  51. inline _Resetiosflags
  52. resetiosflags(ios_base::fmtflags __mask)
  53. { return { __mask }; }
  54. template<typename _CharT, typename _Traits>
  55. inline basic_istream<_CharT, _Traits>&
  56. operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
  57. {
  58. __is.setf(ios_base::fmtflags(0), __f._M_mask);
  59. return __is;
  60. }
  61. template<typename _CharT, typename _Traits>
  62. inline basic_ostream<_CharT, _Traits>&
  63. operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
  64. {
  65. __os.setf(ios_base::fmtflags(0), __f._M_mask);
  66. return __os;
  67. }
  68. struct _Setiosflags { ios_base::fmtflags _M_mask; };
  69. /**
  70. * @brief Manipulator for @c setf.
  71. * @param __mask A format flags mask.
  72. *
  73. * Sent to a stream object, this manipulator sets the format flags
  74. * to @a __mask.
  75. */
  76. inline _Setiosflags
  77. setiosflags(ios_base::fmtflags __mask)
  78. { return { __mask }; }
  79. template<typename _CharT, typename _Traits>
  80. inline basic_istream<_CharT, _Traits>&
  81. operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
  82. {
  83. __is.setf(__f._M_mask);
  84. return __is;
  85. }
  86. template<typename _CharT, typename _Traits>
  87. inline basic_ostream<_CharT, _Traits>&
  88. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
  89. {
  90. __os.setf(__f._M_mask);
  91. return __os;
  92. }
  93. struct _Setbase { int _M_base; };
  94. /**
  95. * @brief Manipulator for @c setf.
  96. * @param __base A numeric base.
  97. *
  98. * Sent to a stream object, this manipulator changes the
  99. * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
  100. * is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
  101. */
  102. inline _Setbase
  103. setbase(int __base)
  104. { return { __base }; }
  105. template<typename _CharT, typename _Traits>
  106. inline basic_istream<_CharT, _Traits>&
  107. operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
  108. {
  109. __is.setf(__f._M_base == 8 ? ios_base::oct :
  110. __f._M_base == 10 ? ios_base::dec :
  111. __f._M_base == 16 ? ios_base::hex :
  112. ios_base::fmtflags(0), ios_base::basefield);
  113. return __is;
  114. }
  115. template<typename _CharT, typename _Traits>
  116. inline basic_ostream<_CharT, _Traits>&
  117. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
  118. {
  119. __os.setf(__f._M_base == 8 ? ios_base::oct :
  120. __f._M_base == 10 ? ios_base::dec :
  121. __f._M_base == 16 ? ios_base::hex :
  122. ios_base::fmtflags(0), ios_base::basefield);
  123. return __os;
  124. }
  125. template<typename _CharT>
  126. struct _Setfill { _CharT _M_c; };
  127. /**
  128. * @brief Manipulator for @c fill.
  129. * @param __c The new fill character.
  130. *
  131. * Sent to a stream object, this manipulator calls @c fill(__c) for that
  132. * object.
  133. */
  134. template<typename _CharT>
  135. inline _Setfill<_CharT>
  136. setfill(_CharT __c)
  137. { return { __c }; }
  138. template<typename _CharT, typename _Traits>
  139. inline basic_istream<_CharT, _Traits>&
  140. operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
  141. {
  142. __is.fill(__f._M_c);
  143. return __is;
  144. }
  145. template<typename _CharT, typename _Traits>
  146. inline basic_ostream<_CharT, _Traits>&
  147. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
  148. {
  149. __os.fill(__f._M_c);
  150. return __os;
  151. }
  152. struct _Setprecision { int _M_n; };
  153. /**
  154. * @brief Manipulator for @c precision.
  155. * @param __n The new precision.
  156. *
  157. * Sent to a stream object, this manipulator calls @c precision(__n) for
  158. * that object.
  159. */
  160. inline _Setprecision
  161. setprecision(int __n)
  162. { return { __n }; }
  163. template<typename _CharT, typename _Traits>
  164. inline basic_istream<_CharT, _Traits>&
  165. operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
  166. {
  167. __is.precision(__f._M_n);
  168. return __is;
  169. }
  170. template<typename _CharT, typename _Traits>
  171. inline basic_ostream<_CharT, _Traits>&
  172. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
  173. {
  174. __os.precision(__f._M_n);
  175. return __os;
  176. }
  177. struct _Setw { int _M_n; };
  178. /**
  179. * @brief Manipulator for @c width.
  180. * @param __n The new width.
  181. *
  182. * Sent to a stream object, this manipulator calls @c width(__n) for
  183. * that object.
  184. */
  185. inline _Setw
  186. setw(int __n)
  187. { return { __n }; }
  188. template<typename _CharT, typename _Traits>
  189. inline basic_istream<_CharT, _Traits>&
  190. operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
  191. {
  192. __is.width(__f._M_n);
  193. return __is;
  194. }
  195. template<typename _CharT, typename _Traits>
  196. inline basic_ostream<_CharT, _Traits>&
  197. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
  198. {
  199. __os.width(__f._M_n);
  200. return __os;
  201. }
  202. #if __cplusplus >= 201103L
  203. template<typename _MoneyT>
  204. struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
  205. /**
  206. * @brief Extended manipulator for extracting money.
  207. * @param __mon Either long double or a specialization of @c basic_string.
  208. * @param __intl A bool indicating whether international format
  209. * is to be used.
  210. *
  211. * Sent to a stream object, this manipulator extracts @a __mon.
  212. */
  213. template<typename _MoneyT>
  214. inline _Get_money<_MoneyT>
  215. get_money(_MoneyT& __mon, bool __intl = false)
  216. { return { __mon, __intl }; }
  217. template<typename _CharT, typename _Traits, typename _MoneyT>
  218. basic_istream<_CharT, _Traits>&
  219. operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
  220. {
  221. typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
  222. if (__cerb)
  223. {
  224. ios_base::iostate __err = ios_base::goodbit;
  225. __try
  226. {
  227. typedef istreambuf_iterator<_CharT, _Traits> _Iter;
  228. typedef money_get<_CharT, _Iter> _MoneyGet;
  229. const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
  230. __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
  231. __is, __err, __f._M_mon);
  232. }
  233. __catch(__cxxabiv1::__forced_unwind&)
  234. {
  235. __is._M_setstate(ios_base::badbit);
  236. __throw_exception_again;
  237. }
  238. __catch(...)
  239. { __is._M_setstate(ios_base::badbit); }
  240. if (__err)
  241. __is.setstate(__err);
  242. }
  243. return __is;
  244. }
  245. template<typename _MoneyT>
  246. struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
  247. /**
  248. * @brief Extended manipulator for inserting money.
  249. * @param __mon Either long double or a specialization of @c basic_string.
  250. * @param __intl A bool indicating whether international format
  251. * is to be used.
  252. *
  253. * Sent to a stream object, this manipulator inserts @a __mon.
  254. */
  255. template<typename _MoneyT>
  256. inline _Put_money<_MoneyT>
  257. put_money(const _MoneyT& __mon, bool __intl = false)
  258. { return { __mon, __intl }; }
  259. template<typename _CharT, typename _Traits, typename _MoneyT>
  260. basic_ostream<_CharT, _Traits>&
  261. operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
  262. {
  263. typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
  264. if (__cerb)
  265. {
  266. ios_base::iostate __err = ios_base::goodbit;
  267. __try
  268. {
  269. typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
  270. typedef money_put<_CharT, _Iter> _MoneyPut;
  271. const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
  272. if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
  273. __os.fill(), __f._M_mon).failed())
  274. __err |= ios_base::badbit;
  275. }
  276. __catch(__cxxabiv1::__forced_unwind&)
  277. {
  278. __os._M_setstate(ios_base::badbit);
  279. __throw_exception_again;
  280. }
  281. __catch(...)
  282. { __os._M_setstate(ios_base::badbit); }
  283. if (__err)
  284. __os.setstate(__err);
  285. }
  286. return __os;
  287. }
  288. template<typename _CharT>
  289. struct _Put_time
  290. {
  291. const std::tm* _M_tmb;
  292. const _CharT* _M_fmt;
  293. };
  294. /**
  295. * @brief Extended manipulator for formatting time.
  296. *
  297. * This manipulator uses time_put::put to format time.
  298. * [ext.manip]
  299. *
  300. * @param __tmb struct tm time data to format.
  301. * @param __fmt format string.
  302. */
  303. template<typename _CharT>
  304. inline _Put_time<_CharT>
  305. put_time(const std::tm* __tmb, const _CharT* __fmt)
  306. { return { __tmb, __fmt }; }
  307. template<typename _CharT, typename _Traits>
  308. basic_ostream<_CharT, _Traits>&
  309. operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
  310. {
  311. typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
  312. if (__cerb)
  313. {
  314. ios_base::iostate __err = ios_base::goodbit;
  315. __try
  316. {
  317. typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
  318. typedef time_put<_CharT, _Iter> _TimePut;
  319. const _CharT* const __fmt_end = __f._M_fmt +
  320. _Traits::length(__f._M_fmt);
  321. const _TimePut& __mp = use_facet<_TimePut>(__os.getloc());
  322. if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(),
  323. __f._M_tmb, __f._M_fmt, __fmt_end).failed())
  324. __err |= ios_base::badbit;
  325. }
  326. __catch(__cxxabiv1::__forced_unwind&)
  327. {
  328. __os._M_setstate(ios_base::badbit);
  329. __throw_exception_again;
  330. }
  331. __catch(...)
  332. { __os._M_setstate(ios_base::badbit); }
  333. if (__err)
  334. __os.setstate(__err);
  335. }
  336. return __os;
  337. }
  338. template<typename _CharT>
  339. struct _Get_time
  340. {
  341. std::tm* _M_tmb;
  342. const _CharT* _M_fmt;
  343. };
  344. /**
  345. * @brief Extended manipulator for extracting time.
  346. *
  347. * This manipulator uses time_get::get to extract time.
  348. * [ext.manip]
  349. *
  350. * @param __tmb struct to extract the time data to.
  351. * @param __fmt format string.
  352. */
  353. template<typename _CharT>
  354. inline _Get_time<_CharT>
  355. get_time(std::tm* __tmb, const _CharT* __fmt)
  356. { return { __tmb, __fmt }; }
  357. template<typename _CharT, typename _Traits>
  358. basic_istream<_CharT, _Traits>&
  359. operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
  360. {
  361. typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
  362. if (__cerb)
  363. {
  364. ios_base::iostate __err = ios_base::goodbit;
  365. __try
  366. {
  367. typedef istreambuf_iterator<_CharT, _Traits> _Iter;
  368. typedef time_get<_CharT, _Iter> _TimeGet;
  369. const _CharT* const __fmt_end = __f._M_fmt +
  370. _Traits::length(__f._M_fmt);
  371. const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc());
  372. __mg.get(_Iter(__is.rdbuf()), _Iter(), __is,
  373. __err, __f._M_tmb, __f._M_fmt, __fmt_end);
  374. }
  375. __catch(__cxxabiv1::__forced_unwind&)
  376. {
  377. __is._M_setstate(ios_base::badbit);
  378. __throw_exception_again;
  379. }
  380. __catch(...)
  381. { __is._M_setstate(ios_base::badbit); }
  382. if (__err)
  383. __is.setstate(__err);
  384. }
  385. return __is;
  386. }
  387. #if __cplusplus >= 201402L
  388. #define __cpp_lib_quoted_string_io 201304
  389. /**
  390. * @brief Manipulator for quoted strings.
  391. * @param __string String to quote.
  392. * @param __delim Character to quote string with.
  393. * @param __escape Escape character to escape itself or quote character.
  394. */
  395. template<typename _CharT>
  396. inline auto
  397. quoted(const _CharT* __string,
  398. _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
  399. {
  400. return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
  401. __escape);
  402. }
  403. template<typename _CharT, typename _Traits, typename _Alloc>
  404. inline auto
  405. quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
  406. _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
  407. {
  408. return __detail::_Quoted_string<
  409. const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
  410. __string, __delim, __escape);
  411. }
  412. template<typename _CharT, typename _Traits, typename _Alloc>
  413. inline auto
  414. quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
  415. _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
  416. {
  417. return __detail::_Quoted_string<
  418. basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
  419. __string, __delim, __escape);
  420. }
  421. #if __cplusplus >= 201703L
  422. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  423. // 2785. quoted should work with basic_string_view
  424. template<typename _CharT, typename _Traits>
  425. inline auto
  426. quoted(basic_string_view<_CharT, _Traits> __sv,
  427. _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
  428. {
  429. return __detail::_Quoted_string<
  430. basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
  431. }
  432. #endif // C++17
  433. #endif // C++14
  434. #endif // __cplusplus >= 201103L
  435. // Inhibit implicit instantiations for required instantiations,
  436. // which are defined via explicit instantiations elsewhere.
  437. // NB: This syntax is a GNU extension.
  438. #if _GLIBCXX_EXTERN_TEMPLATE
  439. extern template ostream& operator<<(ostream&, _Setfill<char>);
  440. extern template ostream& operator<<(ostream&, _Setiosflags);
  441. extern template ostream& operator<<(ostream&, _Resetiosflags);
  442. extern template ostream& operator<<(ostream&, _Setbase);
  443. extern template ostream& operator<<(ostream&, _Setprecision);
  444. extern template ostream& operator<<(ostream&, _Setw);
  445. extern template istream& operator>>(istream&, _Setfill<char>);
  446. extern template istream& operator>>(istream&, _Setiosflags);
  447. extern template istream& operator>>(istream&, _Resetiosflags);
  448. extern template istream& operator>>(istream&, _Setbase);
  449. extern template istream& operator>>(istream&, _Setprecision);
  450. extern template istream& operator>>(istream&, _Setw);
  451. #ifdef _GLIBCXX_USE_WCHAR_T
  452. extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
  453. extern template wostream& operator<<(wostream&, _Setiosflags);
  454. extern template wostream& operator<<(wostream&, _Resetiosflags);
  455. extern template wostream& operator<<(wostream&, _Setbase);
  456. extern template wostream& operator<<(wostream&, _Setprecision);
  457. extern template wostream& operator<<(wostream&, _Setw);
  458. extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
  459. extern template wistream& operator>>(wistream&, _Setiosflags);
  460. extern template wistream& operator>>(wistream&, _Resetiosflags);
  461. extern template wistream& operator>>(wistream&, _Setbase);
  462. extern template wistream& operator>>(wistream&, _Setprecision);
  463. extern template wistream& operator>>(wistream&, _Setw);
  464. #endif
  465. #endif
  466. _GLIBCXX_END_NAMESPACE_VERSION
  467. } // namespace
  468. #endif /* _GLIBCXX_IOMANIP */