2 //===--------------------------- iomanip ----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_IOMANIP
12 #define _LIBCPP_IOMANIP
19 // types T1, T2, ... are unspecified implementation types
20 T1 resetiosflags(ios_base::fmtflags mask);
21 T2 setiosflags (ios_base::fmtflags mask);
23 template<charT> T4 setfill(charT c);
24 T5 setprecision(int n);
26 template <class moneyT> T7 get_money(moneyT& mon, bool intl = false);
27 template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
28 template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
29 template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
31 template <class charT>
32 T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
34 template <class charT, class traits, class Allocator>
35 T12 quoted(const basic_string<charT, traits, Allocator>& s,
36 charT delim=charT('"'), charT escape=charT('\\')); // C++14
38 template <class charT, class traits, class Allocator>
39 T13 quoted(basic_string<charT, traits, Allocator>& s,
40 charT delim=charT('"'), charT escape=charT('\\')); // C++14
51 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
52 #pragma GCC system_header
55 _LIBCPP_BEGIN_NAMESPACE_STD
61 ios_base::fmtflags __mask_;
63 _LIBCPP_INLINE_VISIBILITY
64 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
66 template <class _CharT, class _Traits>
68 _LIBCPP_INLINE_VISIBILITY
69 basic_istream<_CharT, _Traits>&
70 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
72 __is.unsetf(__x.__mask_);
76 template <class _CharT, class _Traits>
78 _LIBCPP_INLINE_VISIBILITY
79 basic_ostream<_CharT, _Traits>&
80 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
82 __os.unsetf(__x.__mask_);
87 inline _LIBCPP_INLINE_VISIBILITY
89 resetiosflags(ios_base::fmtflags __mask)
91 return __iom_t1(__mask);
98 ios_base::fmtflags __mask_;
100 _LIBCPP_INLINE_VISIBILITY
101 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
103 template <class _CharT, class _Traits>
105 _LIBCPP_INLINE_VISIBILITY
106 basic_istream<_CharT, _Traits>&
107 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
109 __is.setf(__x.__mask_);
113 template <class _CharT, class _Traits>
115 _LIBCPP_INLINE_VISIBILITY
116 basic_ostream<_CharT, _Traits>&
117 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
119 __os.setf(__x.__mask_);
124 inline _LIBCPP_INLINE_VISIBILITY
126 setiosflags(ios_base::fmtflags __mask)
128 return __iom_t2(__mask);
137 _LIBCPP_INLINE_VISIBILITY
138 explicit __iom_t3(int __b) : __base_(__b) {}
140 template <class _CharT, class _Traits>
142 _LIBCPP_INLINE_VISIBILITY
143 basic_istream<_CharT, _Traits>&
144 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
146 __is.setf(__x.__base_ == 8 ? ios_base::oct :
147 __x.__base_ == 10 ? ios_base::dec :
148 __x.__base_ == 16 ? ios_base::hex :
149 ios_base::fmtflags(0), ios_base::basefield);
153 template <class _CharT, class _Traits>
155 _LIBCPP_INLINE_VISIBILITY
156 basic_ostream<_CharT, _Traits>&
157 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
159 __os.setf(__x.__base_ == 8 ? ios_base::oct :
160 __x.__base_ == 10 ? ios_base::dec :
161 __x.__base_ == 16 ? ios_base::hex :
162 ios_base::fmtflags(0), ios_base::basefield);
167 inline _LIBCPP_INLINE_VISIBILITY
171 return __iom_t3(__base);
176 template<class _CharT>
181 _LIBCPP_INLINE_VISIBILITY
182 explicit __iom_t4(_CharT __c) : __fill_(__c) {}
184 template <class _Traits>
186 _LIBCPP_INLINE_VISIBILITY
187 basic_ostream<_CharT, _Traits>&
188 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
190 __os.fill(__x.__fill_);
195 template<class _CharT>
196 inline _LIBCPP_INLINE_VISIBILITY
200 return __iom_t4<_CharT>(__c);
209 _LIBCPP_INLINE_VISIBILITY
210 explicit __iom_t5(int __n) : __n_(__n) {}
212 template <class _CharT, class _Traits>
214 _LIBCPP_INLINE_VISIBILITY
215 basic_istream<_CharT, _Traits>&
216 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
218 __is.precision(__x.__n_);
222 template <class _CharT, class _Traits>
224 _LIBCPP_INLINE_VISIBILITY
225 basic_ostream<_CharT, _Traits>&
226 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
228 __os.precision(__x.__n_);
233 inline _LIBCPP_INLINE_VISIBILITY
235 setprecision(int __n)
237 return __iom_t5(__n);
246 _LIBCPP_INLINE_VISIBILITY
247 explicit __iom_t6(int __n) : __n_(__n) {}
249 template <class _CharT, class _Traits>
251 _LIBCPP_INLINE_VISIBILITY
252 basic_istream<_CharT, _Traits>&
253 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
255 __is.width(__x.__n_);
259 template <class _CharT, class _Traits>
261 _LIBCPP_INLINE_VISIBILITY
262 basic_ostream<_CharT, _Traits>&
263 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
265 __os.width(__x.__n_);
270 inline _LIBCPP_INLINE_VISIBILITY
274 return __iom_t6(__n);
279 template <class _MoneyT> class __iom_t7;
281 template <class _CharT, class _Traits, class _MoneyT>
282 basic_istream<_CharT, _Traits>&
283 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
285 template <class _MoneyT>
291 _LIBCPP_INLINE_VISIBILITY
292 __iom_t7(_MoneyT& __mon, bool __intl)
293 : __mon_(__mon), __intl_(__intl) {}
295 template <class _CharT, class _Traits, class _Mp>
297 basic_istream<_CharT, _Traits>&
298 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
301 template <class _CharT, class _Traits, class _MoneyT>
302 basic_istream<_CharT, _Traits>&
303 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
305 #ifndef _LIBCPP_NO_EXCEPTIONS
308 #endif // _LIBCPP_NO_EXCEPTIONS
309 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
312 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
313 typedef money_get<_CharT, _Ip> _Fp;
314 ios_base::iostate __err = ios_base::goodbit;
315 const _Fp& __mf = use_facet<_Fp>(__is.getloc());
316 __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_);
317 __is.setstate(__err);
319 #ifndef _LIBCPP_NO_EXCEPTIONS
323 __is.__set_badbit_and_consider_rethrow();
325 #endif // _LIBCPP_NO_EXCEPTIONS
329 template <class _MoneyT>
330 inline _LIBCPP_INLINE_VISIBILITY
332 get_money(_MoneyT& __mon, bool __intl = false)
334 return __iom_t7<_MoneyT>(__mon, __intl);
339 template <class _MoneyT> class __iom_t8;
341 template <class _CharT, class _Traits, class _MoneyT>
342 basic_ostream<_CharT, _Traits>&
343 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
345 template <class _MoneyT>
348 const _MoneyT& __mon_;
351 _LIBCPP_INLINE_VISIBILITY
352 __iom_t8(const _MoneyT& __mon, bool __intl)
353 : __mon_(__mon), __intl_(__intl) {}
355 template <class _CharT, class _Traits, class _Mp>
357 basic_ostream<_CharT, _Traits>&
358 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
361 template <class _CharT, class _Traits, class _MoneyT>
362 basic_ostream<_CharT, _Traits>&
363 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
365 #ifndef _LIBCPP_NO_EXCEPTIONS
368 #endif // _LIBCPP_NO_EXCEPTIONS
369 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
372 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
373 typedef money_put<_CharT, _Op> _Fp;
374 const _Fp& __mf = use_facet<_Fp>(__os.getloc());
375 if (__mf.put(_Op(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
376 __os.setstate(ios_base::badbit);
378 #ifndef _LIBCPP_NO_EXCEPTIONS
382 __os.__set_badbit_and_consider_rethrow();
384 #endif // _LIBCPP_NO_EXCEPTIONS
388 template <class _MoneyT>
389 inline _LIBCPP_INLINE_VISIBILITY
391 put_money(const _MoneyT& __mon, bool __intl = false)
393 return __iom_t8<_MoneyT>(__mon, __intl);
398 template <class _CharT> class __iom_t9;
400 template <class _CharT, class _Traits>
401 basic_istream<_CharT, _Traits>&
402 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
404 template <class _CharT>
408 const _CharT* __fmt_;
410 _LIBCPP_INLINE_VISIBILITY
411 __iom_t9(tm* __tm, const _CharT* __fmt)
412 : __tm_(__tm), __fmt_(__fmt) {}
414 template <class _Cp, class _Traits>
416 basic_istream<_Cp, _Traits>&
417 operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
420 template <class _CharT, class _Traits>
421 basic_istream<_CharT, _Traits>&
422 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
424 #ifndef _LIBCPP_NO_EXCEPTIONS
427 #endif // _LIBCPP_NO_EXCEPTIONS
428 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
431 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
432 typedef time_get<_CharT, _Ip> _Fp;
433 ios_base::iostate __err = ios_base::goodbit;
434 const _Fp& __tf = use_facet<_Fp>(__is.getloc());
435 __tf.get(_Ip(__is), _Ip(), __is, __err, __x.__tm_,
436 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
437 __is.setstate(__err);
439 #ifndef _LIBCPP_NO_EXCEPTIONS
443 __is.__set_badbit_and_consider_rethrow();
445 #endif // _LIBCPP_NO_EXCEPTIONS
449 template <class _CharT>
450 inline _LIBCPP_INLINE_VISIBILITY
452 get_time(tm* __tm, const _CharT* __fmt)
454 return __iom_t9<_CharT>(__tm, __fmt);
459 template <class _CharT> class __iom_t10;
461 template <class _CharT, class _Traits>
462 basic_ostream<_CharT, _Traits>&
463 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
465 template <class _CharT>
469 const _CharT* __fmt_;
471 _LIBCPP_INLINE_VISIBILITY
472 __iom_t10(const tm* __tm, const _CharT* __fmt)
473 : __tm_(__tm), __fmt_(__fmt) {}
475 template <class _Cp, class _Traits>
477 basic_ostream<_Cp, _Traits>&
478 operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
481 template <class _CharT, class _Traits>
482 basic_ostream<_CharT, _Traits>&
483 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
485 #ifndef _LIBCPP_NO_EXCEPTIONS
488 #endif // _LIBCPP_NO_EXCEPTIONS
489 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
492 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
493 typedef time_put<_CharT, _Op> _Fp;
494 const _Fp& __tf = use_facet<_Fp>(__os.getloc());
495 if (__tf.put(_Op(__os), __os, __os.fill(), __x.__tm_,
496 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed())
497 __os.setstate(ios_base::badbit);
499 #ifndef _LIBCPP_NO_EXCEPTIONS
503 __os.__set_badbit_and_consider_rethrow();
505 #endif // _LIBCPP_NO_EXCEPTIONS
509 template <class _CharT>
510 inline _LIBCPP_INLINE_VISIBILITY
512 put_time(const tm* __tm, const _CharT* __fmt)
514 return __iom_t10<_CharT>(__tm, __fmt);
517 template <class _CharT, class _Traits, class _ForwardIterator>
518 std::basic_ostream<_CharT, _Traits> &
519 __quoted_output ( basic_ostream<_CharT, _Traits> &__os,
520 _ForwardIterator __first, _ForwardIterator __last, _CharT __delim, _CharT __escape )
522 _VSTD::basic_string<_CharT, _Traits> __str;
523 __str.push_back(__delim);
524 for ( ; __first != __last; ++ __first )
526 if (_Traits::eq (*__first, __escape) || _Traits::eq (*__first, __delim))
527 __str.push_back(__escape);
528 __str.push_back(*__first);
530 __str.push_back(__delim);
531 return __put_character_sequence(__os, __str.data(), __str.size());
534 template <class _CharT, class _Traits, class _String>
535 basic_istream<_CharT, _Traits> &
536 __quoted_input ( basic_istream<_CharT, _Traits> &__is, _String & __string, _CharT __delim, _CharT __escape )
544 if (!_Traits::eq (__c, __delim)) // no delimiter, read the whole string
551 __save_flags<_CharT, _Traits> sf(__is);
558 if (_Traits::eq (__c, __escape))
564 else if (_Traits::eq (__c, __delim))
566 __string.push_back ( __c );
572 template <class _CharT, class _Traits, class _Iter>
573 basic_ostream<_CharT, _Traits>& operator<<(
574 basic_ostream<_CharT, _Traits>& __os,
575 const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
577 return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
580 template <class _CharT, class _Traits, class _Allocator>
581 struct __quoted_proxy
583 basic_string<_CharT, _Traits, _Allocator> &__string;
587 __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
588 : __string(__s), __delim(__d), __escape(__e) {}
591 template <class _CharT, class _Traits, class _Allocator>
592 _LIBCPP_INLINE_VISIBILITY
593 basic_ostream<_CharT, _Traits>& operator<<(
594 basic_ostream<_CharT, _Traits>& __os,
595 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
597 return __quoted_output (__os, __proxy.__string.cbegin (), __proxy.__string.cend (), __proxy.__delim, __proxy.__escape);
600 // extractor for non-const basic_string& proxies
601 template <class _CharT, class _Traits, class _Allocator>
602 _LIBCPP_INLINE_VISIBILITY
603 basic_istream<_CharT, _Traits>& operator>>(
604 basic_istream<_CharT, _Traits>& __is,
605 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
607 return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
611 template <class _CharT>
612 _LIBCPP_INLINE_VISIBILITY
613 __quoted_output_proxy<_CharT, const _CharT *>
614 quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
616 const _CharT *__end = __s;
617 while ( *__end ) ++__end;
618 return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
622 template <class _CharT, class _Traits, class _Allocator>
623 _LIBCPP_INLINE_VISIBILITY
624 __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
625 __quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
627 return __quoted_output_proxy<_CharT,
628 typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
629 ( __s.cbegin(), __s.cend (), __delim, __escape );
632 template <class _CharT, class _Traits, class _Allocator>
633 _LIBCPP_INLINE_VISIBILITY
634 __quoted_proxy<_CharT, _Traits, _Allocator>
635 __quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
637 return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
641 #if _LIBCPP_STD_VER > 11
643 template <class _CharT, class _Traits, class _Allocator>
644 _LIBCPP_INLINE_VISIBILITY
645 __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
646 quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
648 return __quoted(__s, __delim, __escape);
651 template <class _CharT, class _Traits, class _Allocator>
652 _LIBCPP_INLINE_VISIBILITY
653 __quoted_proxy<_CharT, _Traits, _Allocator>
654 quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
656 return __quoted(__s, __delim, __escape);
659 template <class _CharT, class _Traits>
660 __quoted_output_proxy<_CharT, const _CharT *, _Traits>
661 quoted (basic_string_view <_CharT, _Traits> __sv,
662 _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
664 return __quoted_output_proxy<_CharT, const _CharT *, _Traits>
665 ( __sv.data(), __sv.data() + __sv.size(), __delim, __escape );
669 _LIBCPP_END_NAMESPACE_STD
671 #endif // _LIBCPP_IOMANIP