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
49 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
50 #pragma GCC system_header
53 _LIBCPP_BEGIN_NAMESPACE_STD
59 ios_base::fmtflags __mask_;
61 _LIBCPP_INLINE_VISIBILITY
62 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
64 template <class _CharT, class _Traits>
66 _LIBCPP_INLINE_VISIBILITY
67 basic_istream<_CharT, _Traits>&
68 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
70 __is.unsetf(__x.__mask_);
74 template <class _CharT, class _Traits>
76 _LIBCPP_INLINE_VISIBILITY
77 basic_ostream<_CharT, _Traits>&
78 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
80 __os.unsetf(__x.__mask_);
85 inline _LIBCPP_INLINE_VISIBILITY
87 resetiosflags(ios_base::fmtflags __mask)
89 return __iom_t1(__mask);
96 ios_base::fmtflags __mask_;
98 _LIBCPP_INLINE_VISIBILITY
99 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
101 template <class _CharT, class _Traits>
103 _LIBCPP_INLINE_VISIBILITY
104 basic_istream<_CharT, _Traits>&
105 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
107 __is.setf(__x.__mask_);
111 template <class _CharT, class _Traits>
113 _LIBCPP_INLINE_VISIBILITY
114 basic_ostream<_CharT, _Traits>&
115 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
117 __os.setf(__x.__mask_);
122 inline _LIBCPP_INLINE_VISIBILITY
124 setiosflags(ios_base::fmtflags __mask)
126 return __iom_t2(__mask);
135 _LIBCPP_INLINE_VISIBILITY
136 explicit __iom_t3(int __b) : __base_(__b) {}
138 template <class _CharT, class _Traits>
140 _LIBCPP_INLINE_VISIBILITY
141 basic_istream<_CharT, _Traits>&
142 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
144 __is.setf(__x.__base_ == 8 ? ios_base::oct :
145 __x.__base_ == 10 ? ios_base::dec :
146 __x.__base_ == 16 ? ios_base::hex :
147 ios_base::fmtflags(0), ios_base::basefield);
151 template <class _CharT, class _Traits>
153 _LIBCPP_INLINE_VISIBILITY
154 basic_ostream<_CharT, _Traits>&
155 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
157 __os.setf(__x.__base_ == 8 ? ios_base::oct :
158 __x.__base_ == 10 ? ios_base::dec :
159 __x.__base_ == 16 ? ios_base::hex :
160 ios_base::fmtflags(0), ios_base::basefield);
165 inline _LIBCPP_INLINE_VISIBILITY
169 return __iom_t3(__base);
174 template<class _CharT>
179 _LIBCPP_INLINE_VISIBILITY
180 explicit __iom_t4(_CharT __c) : __fill_(__c) {}
182 template <class _Traits>
184 _LIBCPP_INLINE_VISIBILITY
185 basic_ostream<_CharT, _Traits>&
186 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
188 __os.fill(__x.__fill_);
193 template<class _CharT>
194 inline _LIBCPP_INLINE_VISIBILITY
198 return __iom_t4<_CharT>(__c);
207 _LIBCPP_INLINE_VISIBILITY
208 explicit __iom_t5(int __n) : __n_(__n) {}
210 template <class _CharT, class _Traits>
212 _LIBCPP_INLINE_VISIBILITY
213 basic_istream<_CharT, _Traits>&
214 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
216 __is.precision(__x.__n_);
220 template <class _CharT, class _Traits>
222 _LIBCPP_INLINE_VISIBILITY
223 basic_ostream<_CharT, _Traits>&
224 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
226 __os.precision(__x.__n_);
231 inline _LIBCPP_INLINE_VISIBILITY
233 setprecision(int __n)
235 return __iom_t5(__n);
244 _LIBCPP_INLINE_VISIBILITY
245 explicit __iom_t6(int __n) : __n_(__n) {}
247 template <class _CharT, class _Traits>
249 _LIBCPP_INLINE_VISIBILITY
250 basic_istream<_CharT, _Traits>&
251 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
253 __is.width(__x.__n_);
257 template <class _CharT, class _Traits>
259 _LIBCPP_INLINE_VISIBILITY
260 basic_ostream<_CharT, _Traits>&
261 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
263 __os.width(__x.__n_);
268 inline _LIBCPP_INLINE_VISIBILITY
272 return __iom_t6(__n);
277 template <class _MoneyT> class __iom_t7;
279 template <class _CharT, class _Traits, class _MoneyT>
280 basic_istream<_CharT, _Traits>&
281 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
283 template <class _MoneyT>
289 _LIBCPP_INLINE_VISIBILITY
290 __iom_t7(_MoneyT& __mon, bool __intl)
291 : __mon_(__mon), __intl_(__intl) {}
293 template <class _CharT, class _Traits, class _Mp>
295 basic_istream<_CharT, _Traits>&
296 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
299 template <class _CharT, class _Traits, class _MoneyT>
300 basic_istream<_CharT, _Traits>&
301 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
303 #ifndef _LIBCPP_NO_EXCEPTIONS
306 #endif // _LIBCPP_NO_EXCEPTIONS
307 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
310 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
311 typedef money_get<_CharT, _Ip> _Fp;
312 ios_base::iostate __err = ios_base::goodbit;
313 const _Fp& __mf = use_facet<_Fp>(__is.getloc());
314 __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_);
315 __is.setstate(__err);
317 #ifndef _LIBCPP_NO_EXCEPTIONS
321 __is.__set_badbit_and_consider_rethrow();
323 #endif // _LIBCPP_NO_EXCEPTIONS
327 template <class _MoneyT>
328 inline _LIBCPP_INLINE_VISIBILITY
330 get_money(_MoneyT& __mon, bool __intl = false)
332 return __iom_t7<_MoneyT>(__mon, __intl);
337 template <class _MoneyT> class __iom_t8;
339 template <class _CharT, class _Traits, class _MoneyT>
340 basic_ostream<_CharT, _Traits>&
341 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
343 template <class _MoneyT>
346 const _MoneyT& __mon_;
349 _LIBCPP_INLINE_VISIBILITY
350 __iom_t8(const _MoneyT& __mon, bool __intl)
351 : __mon_(__mon), __intl_(__intl) {}
353 template <class _CharT, class _Traits, class _Mp>
355 basic_ostream<_CharT, _Traits>&
356 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
359 template <class _CharT, class _Traits, class _MoneyT>
360 basic_ostream<_CharT, _Traits>&
361 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
363 #ifndef _LIBCPP_NO_EXCEPTIONS
366 #endif // _LIBCPP_NO_EXCEPTIONS
367 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
370 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
371 typedef money_put<_CharT, _Op> _Fp;
372 const _Fp& __mf = use_facet<_Fp>(__os.getloc());
373 if (__mf.put(_Op(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
374 __os.setstate(ios_base::badbit);
376 #ifndef _LIBCPP_NO_EXCEPTIONS
380 __os.__set_badbit_and_consider_rethrow();
382 #endif // _LIBCPP_NO_EXCEPTIONS
386 template <class _MoneyT>
387 inline _LIBCPP_INLINE_VISIBILITY
389 put_money(const _MoneyT& __mon, bool __intl = false)
391 return __iom_t8<_MoneyT>(__mon, __intl);
396 template <class _CharT> class __iom_t9;
398 template <class _CharT, class _Traits>
399 basic_istream<_CharT, _Traits>&
400 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
402 template <class _CharT>
406 const _CharT* __fmt_;
408 _LIBCPP_INLINE_VISIBILITY
409 __iom_t9(tm* __tm, const _CharT* __fmt)
410 : __tm_(__tm), __fmt_(__fmt) {}
412 template <class _Cp, class _Traits>
414 basic_istream<_Cp, _Traits>&
415 operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
418 template <class _CharT, class _Traits>
419 basic_istream<_CharT, _Traits>&
420 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
422 #ifndef _LIBCPP_NO_EXCEPTIONS
425 #endif // _LIBCPP_NO_EXCEPTIONS
426 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
429 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
430 typedef time_get<_CharT, _Ip> _Fp;
431 ios_base::iostate __err = ios_base::goodbit;
432 const _Fp& __tf = use_facet<_Fp>(__is.getloc());
433 __tf.get(_Ip(__is), _Ip(), __is, __err, __x.__tm_,
434 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
435 __is.setstate(__err);
437 #ifndef _LIBCPP_NO_EXCEPTIONS
441 __is.__set_badbit_and_consider_rethrow();
443 #endif // _LIBCPP_NO_EXCEPTIONS
447 template <class _CharT>
448 inline _LIBCPP_INLINE_VISIBILITY
450 get_time(tm* __tm, const _CharT* __fmt)
452 return __iom_t9<_CharT>(__tm, __fmt);
457 template <class _CharT> class __iom_t10;
459 template <class _CharT, class _Traits>
460 basic_ostream<_CharT, _Traits>&
461 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
463 template <class _CharT>
467 const _CharT* __fmt_;
469 _LIBCPP_INLINE_VISIBILITY
470 __iom_t10(const tm* __tm, const _CharT* __fmt)
471 : __tm_(__tm), __fmt_(__fmt) {}
473 template <class _Cp, class _Traits>
475 basic_ostream<_Cp, _Traits>&
476 operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
479 template <class _CharT, class _Traits>
480 basic_ostream<_CharT, _Traits>&
481 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
483 #ifndef _LIBCPP_NO_EXCEPTIONS
486 #endif // _LIBCPP_NO_EXCEPTIONS
487 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
490 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
491 typedef time_put<_CharT, _Op> _Fp;
492 const _Fp& __tf = use_facet<_Fp>(__os.getloc());
493 if (__tf.put(_Op(__os), __os, __os.fill(), __x.__tm_,
494 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed())
495 __os.setstate(ios_base::badbit);
497 #ifndef _LIBCPP_NO_EXCEPTIONS
501 __os.__set_badbit_and_consider_rethrow();
503 #endif // _LIBCPP_NO_EXCEPTIONS
507 template <class _CharT>
508 inline _LIBCPP_INLINE_VISIBILITY
510 put_time(const tm* __tm, const _CharT* __fmt)
512 return __iom_t10<_CharT>(__tm, __fmt);
515 #if _LIBCPP_STD_VER > 11
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 _Iter, class _Traits=char_traits<_CharT>>
573 struct __quoted_output_proxy
580 __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
581 : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
582 // This would be a nice place for a string_ref
585 template <class _CharT, class _Traits, class _Iter>
586 basic_ostream<_CharT, _Traits>& operator<<(
587 basic_ostream<_CharT, _Traits>& __os,
588 const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
590 return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
593 template <class _CharT, class _Traits, class _Allocator>
594 struct __quoted_proxy
596 basic_string<_CharT, _Traits, _Allocator> &__string;
600 __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
601 : __string(__s), __delim(__d), __escape(__e) {}
604 template <class _CharT, class _Traits, class _Allocator>
605 _LIBCPP_INLINE_VISIBILITY
606 basic_ostream<_CharT, _Traits>& operator<<(
607 basic_ostream<_CharT, _Traits>& __os,
608 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
610 return __quoted_output (__os, __proxy.__string.cbegin (), __proxy.__string.cend (), __proxy.__delim, __proxy.__escape);
613 // extractor for non-const basic_string& proxies
614 template <class _CharT, class _Traits, class _Allocator>
615 _LIBCPP_INLINE_VISIBILITY
616 basic_istream<_CharT, _Traits>& operator>>(
617 basic_istream<_CharT, _Traits>& __is,
618 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
620 return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
624 template <class _CharT>
625 _LIBCPP_INLINE_VISIBILITY
626 __quoted_output_proxy<_CharT, const _CharT *>
627 quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
629 const _CharT *__end = __s;
630 while ( *__end ) ++__end;
631 return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
634 template <class _CharT, class _Traits, class _Allocator>
635 _LIBCPP_INLINE_VISIBILITY
636 __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
637 quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
639 return __quoted_output_proxy<_CharT,
640 typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
641 ( __s.cbegin(), __s.cend (), __delim, __escape );
644 template <class _CharT, class _Traits, class _Allocator>
645 __quoted_proxy<_CharT, _Traits, _Allocator>
646 quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
648 return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
652 _LIBCPP_END_NAMESPACE_STD
654 #endif // _LIBCPP_IOMANIP