1// -*- C++ -*-
2//===--------------------------- iomanip ----------------------------------===//
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
11#ifndef _LIBCPP_IOMANIP
12#define _LIBCPP_IOMANIP
13
14/*
15    iomanip synopsis
16
17namespace std {
18
19// types T1, T2, ... are unspecified implementation types
20T1 resetiosflags(ios_base::fmtflags mask);
21T2 setiosflags (ios_base::fmtflags mask);
22T3 setbase(int base);
23template<charT> T4 setfill(charT c);
24T5 setprecision(int n);
25T6 setw(int n);
26template <class moneyT> T7 get_money(moneyT& mon, bool intl = false);
27template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
28template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
29template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
30
31template <class charT>
32  T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
33
34template <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
37
38template <class charT, class traits, class Allocator>
39  T13 quoted(basic_string<charT, traits, Allocator>& s,
40             charT delim=charT('"'), charT escape=charT('\\')); // C++14
41
42}  // std
43
44*/
45
46#include <__config>
47#include <istream>
48
49#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
50#pragma GCC system_header
51#endif
52
53_LIBCPP_BEGIN_NAMESPACE_STD
54
55// resetiosflags
56
57class __iom_t1
58{
59    ios_base::fmtflags __mask_;
60public:
61    _LIBCPP_INLINE_VISIBILITY
62    explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
63
64    template <class _CharT, class _Traits>
65    friend
66    _LIBCPP_INLINE_VISIBILITY
67    basic_istream<_CharT, _Traits>&
68    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
69    {
70        __is.unsetf(__x.__mask_);
71        return __is;
72    }
73
74    template <class _CharT, class _Traits>
75    friend
76    _LIBCPP_INLINE_VISIBILITY
77    basic_ostream<_CharT, _Traits>&
78    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
79    {
80        __os.unsetf(__x.__mask_);
81        return __os;
82    }
83};
84
85inline _LIBCPP_INLINE_VISIBILITY
86__iom_t1
87resetiosflags(ios_base::fmtflags __mask)
88{
89    return __iom_t1(__mask);
90}
91
92// setiosflags
93
94class __iom_t2
95{
96    ios_base::fmtflags __mask_;
97public:
98    _LIBCPP_INLINE_VISIBILITY
99    explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
100
101    template <class _CharT, class _Traits>
102    friend
103    _LIBCPP_INLINE_VISIBILITY
104    basic_istream<_CharT, _Traits>&
105    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
106    {
107        __is.setf(__x.__mask_);
108        return __is;
109    }
110
111    template <class _CharT, class _Traits>
112    friend
113    _LIBCPP_INLINE_VISIBILITY
114    basic_ostream<_CharT, _Traits>&
115    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
116    {
117        __os.setf(__x.__mask_);
118        return __os;
119    }
120};
121
122inline _LIBCPP_INLINE_VISIBILITY
123__iom_t2
124setiosflags(ios_base::fmtflags __mask)
125{
126    return __iom_t2(__mask);
127}
128
129// setbase
130
131class __iom_t3
132{
133    int __base_;
134public:
135    _LIBCPP_INLINE_VISIBILITY
136    explicit __iom_t3(int __b) : __base_(__b) {}
137
138    template <class _CharT, class _Traits>
139    friend
140    _LIBCPP_INLINE_VISIBILITY
141    basic_istream<_CharT, _Traits>&
142    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
143    {
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);
148        return __is;
149    }
150
151    template <class _CharT, class _Traits>
152    friend
153    _LIBCPP_INLINE_VISIBILITY
154    basic_ostream<_CharT, _Traits>&
155    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
156    {
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);
161        return __os;
162    }
163};
164
165inline _LIBCPP_INLINE_VISIBILITY
166__iom_t3
167setbase(int __base)
168{
169    return __iom_t3(__base);
170}
171
172// setfill
173
174template<class _CharT>
175class __iom_t4
176{
177    _CharT __fill_;
178public:
179    _LIBCPP_INLINE_VISIBILITY
180    explicit __iom_t4(_CharT __c) : __fill_(__c) {}
181
182    template <class _Traits>
183    friend
184    _LIBCPP_INLINE_VISIBILITY
185    basic_ostream<_CharT, _Traits>&
186    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
187    {
188        __os.fill(__x.__fill_);
189        return __os;
190    }
191};
192
193template<class _CharT>
194inline _LIBCPP_INLINE_VISIBILITY
195__iom_t4<_CharT>
196setfill(_CharT __c)
197{
198    return __iom_t4<_CharT>(__c);
199}
200
201// setprecision
202
203class __iom_t5
204{
205    int __n_;
206public:
207    _LIBCPP_INLINE_VISIBILITY
208    explicit __iom_t5(int __n) : __n_(__n) {}
209
210    template <class _CharT, class _Traits>
211    friend
212    _LIBCPP_INLINE_VISIBILITY
213    basic_istream<_CharT, _Traits>&
214    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
215    {
216        __is.precision(__x.__n_);
217        return __is;
218    }
219
220    template <class _CharT, class _Traits>
221    friend
222    _LIBCPP_INLINE_VISIBILITY
223    basic_ostream<_CharT, _Traits>&
224    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
225    {
226        __os.precision(__x.__n_);
227        return __os;
228    }
229};
230
231inline _LIBCPP_INLINE_VISIBILITY
232__iom_t5
233setprecision(int __n)
234{
235    return __iom_t5(__n);
236}
237
238// setw
239
240class __iom_t6
241{
242    int __n_;
243public:
244    _LIBCPP_INLINE_VISIBILITY
245    explicit __iom_t6(int __n) : __n_(__n) {}
246
247    template <class _CharT, class _Traits>
248    friend
249    _LIBCPP_INLINE_VISIBILITY
250    basic_istream<_CharT, _Traits>&
251    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
252    {
253        __is.width(__x.__n_);
254        return __is;
255    }
256
257    template <class _CharT, class _Traits>
258    friend
259    _LIBCPP_INLINE_VISIBILITY
260    basic_ostream<_CharT, _Traits>&
261    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
262    {
263        __os.width(__x.__n_);
264        return __os;
265    }
266};
267
268inline _LIBCPP_INLINE_VISIBILITY
269__iom_t6
270setw(int __n)
271{
272    return __iom_t6(__n);
273}
274
275// get_money
276
277template <class _MoneyT> class __iom_t7;
278
279template <class _CharT, class _Traits, class _MoneyT>
280basic_istream<_CharT, _Traits>&
281operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
282
283template <class _MoneyT>
284class __iom_t7
285{
286    _MoneyT& __mon_;
287    bool __intl_;
288public:
289    _LIBCPP_INLINE_VISIBILITY
290    __iom_t7(_MoneyT& __mon, bool __intl)
291        : __mon_(__mon), __intl_(__intl) {}
292
293    template <class _CharT, class _Traits, class _Mp>
294    friend
295    basic_istream<_CharT, _Traits>&
296    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
297};
298
299template <class _CharT, class _Traits, class _MoneyT>
300basic_istream<_CharT, _Traits>&
301operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
302{
303#ifndef _LIBCPP_NO_EXCEPTIONS
304    try
305    {
306#endif  // _LIBCPP_NO_EXCEPTIONS
307        typename basic_istream<_CharT, _Traits>::sentry __s(__is);
308        if (__s)
309        {
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);
316        }
317#ifndef _LIBCPP_NO_EXCEPTIONS
318    }
319    catch (...)
320    {
321        __is.__set_badbit_and_consider_rethrow();
322    }
323#endif  // _LIBCPP_NO_EXCEPTIONS
324    return __is;
325}
326
327template <class _MoneyT>
328inline _LIBCPP_INLINE_VISIBILITY
329__iom_t7<_MoneyT>
330get_money(_MoneyT& __mon, bool __intl = false)
331{
332    return __iom_t7<_MoneyT>(__mon, __intl);
333}
334
335// put_money
336
337template <class _MoneyT> class __iom_t8;
338
339template <class _CharT, class _Traits, class _MoneyT>
340basic_ostream<_CharT, _Traits>&
341operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
342
343template <class _MoneyT>
344class __iom_t8
345{
346    const _MoneyT& __mon_;
347    bool __intl_;
348public:
349    _LIBCPP_INLINE_VISIBILITY
350    __iom_t8(const _MoneyT& __mon, bool __intl)
351        : __mon_(__mon), __intl_(__intl) {}
352
353    template <class _CharT, class _Traits, class _Mp>
354    friend
355    basic_ostream<_CharT, _Traits>&
356    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
357};
358
359template <class _CharT, class _Traits, class _MoneyT>
360basic_ostream<_CharT, _Traits>&
361operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
362{
363#ifndef _LIBCPP_NO_EXCEPTIONS
364    try
365    {
366#endif  // _LIBCPP_NO_EXCEPTIONS
367        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
368        if (__s)
369        {
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);
375        }
376#ifndef _LIBCPP_NO_EXCEPTIONS
377    }
378    catch (...)
379    {
380        __os.__set_badbit_and_consider_rethrow();
381    }
382#endif  // _LIBCPP_NO_EXCEPTIONS
383    return __os;
384}
385
386template <class _MoneyT>
387inline _LIBCPP_INLINE_VISIBILITY
388__iom_t8<_MoneyT>
389put_money(const _MoneyT& __mon, bool __intl = false)
390{
391    return __iom_t8<_MoneyT>(__mon, __intl);
392}
393
394// get_time
395
396template <class _CharT> class __iom_t9;
397
398template <class _CharT, class _Traits>
399basic_istream<_CharT, _Traits>&
400operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
401
402template <class _CharT>
403class __iom_t9
404{
405    tm* __tm_;
406    const _CharT* __fmt_;
407public:
408    _LIBCPP_INLINE_VISIBILITY
409    __iom_t9(tm* __tm, const _CharT* __fmt)
410        : __tm_(__tm), __fmt_(__fmt) {}
411
412    template <class _Cp, class _Traits>
413    friend
414    basic_istream<_Cp, _Traits>&
415    operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
416};
417
418template <class _CharT, class _Traits>
419basic_istream<_CharT, _Traits>&
420operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
421{
422#ifndef _LIBCPP_NO_EXCEPTIONS
423    try
424    {
425#endif  // _LIBCPP_NO_EXCEPTIONS
426        typename basic_istream<_CharT, _Traits>::sentry __s(__is);
427        if (__s)
428        {
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);
436        }
437#ifndef _LIBCPP_NO_EXCEPTIONS
438    }
439    catch (...)
440    {
441        __is.__set_badbit_and_consider_rethrow();
442    }
443#endif  // _LIBCPP_NO_EXCEPTIONS
444    return __is;
445}
446
447template <class _CharT>
448inline _LIBCPP_INLINE_VISIBILITY
449__iom_t9<_CharT>
450get_time(tm* __tm, const _CharT* __fmt)
451{
452    return __iom_t9<_CharT>(__tm, __fmt);
453}
454
455// put_time
456
457template <class _CharT> class __iom_t10;
458
459template <class _CharT, class _Traits>
460basic_ostream<_CharT, _Traits>&
461operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
462
463template <class _CharT>
464class __iom_t10
465{
466    const tm* __tm_;
467    const _CharT* __fmt_;
468public:
469    _LIBCPP_INLINE_VISIBILITY
470    __iom_t10(const tm* __tm, const _CharT* __fmt)
471        : __tm_(__tm), __fmt_(__fmt) {}
472
473    template <class _Cp, class _Traits>
474    friend
475    basic_ostream<_Cp, _Traits>&
476    operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
477};
478
479template <class _CharT, class _Traits>
480basic_ostream<_CharT, _Traits>&
481operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
482{
483#ifndef _LIBCPP_NO_EXCEPTIONS
484    try
485    {
486#endif  // _LIBCPP_NO_EXCEPTIONS
487        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
488        if (__s)
489        {
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);
496        }
497#ifndef _LIBCPP_NO_EXCEPTIONS
498    }
499    catch (...)
500    {
501        __os.__set_badbit_and_consider_rethrow();
502    }
503#endif  // _LIBCPP_NO_EXCEPTIONS
504    return __os;
505}
506
507template <class _CharT>
508inline _LIBCPP_INLINE_VISIBILITY
509__iom_t10<_CharT>
510put_time(const tm* __tm, const _CharT* __fmt)
511{
512    return __iom_t10<_CharT>(__tm, __fmt);
513}
514
515#if _LIBCPP_STD_VER > 11
516
517template <class _CharT, class _Traits, class _ForwardIterator>
518std::basic_ostream<_CharT, _Traits> &
519__quoted_output ( basic_ostream<_CharT, _Traits> &__os,
520        _ForwardIterator __first, _ForwardIterator __last, _CharT __delim, _CharT __escape )
521{
522    _VSTD::basic_string<_CharT, _Traits> __str;
523    __str.push_back(__delim);
524    for ( ; __first != __last; ++ __first )
525    {
526        if (_Traits::eq (*__first, __escape) || _Traits::eq (*__first, __delim))
527            __str.push_back(__escape);
528        __str.push_back(*__first);
529    }
530    __str.push_back(__delim);
531    return __put_character_sequence(__os, __str.data(), __str.size());
532}
533
534template <class _CharT, class _Traits, class _String>
535basic_istream<_CharT, _Traits> &
536__quoted_input ( basic_istream<_CharT, _Traits> &__is, _String & __string, _CharT __delim, _CharT __escape )
537{
538    __string.clear ();
539    _CharT __c;
540    __is >> __c;
541    if ( __is.fail ())
542        return __is;
543
544    if (!_Traits::eq (__c, __delim))    // no delimiter, read the whole string
545    {
546        __is.unget ();
547        __is >> __string;
548        return __is;
549    }
550
551    __save_flags<_CharT, _Traits> sf(__is);
552    noskipws (__is);
553    while (true)
554        {
555        __is >> __c;
556        if ( __is.fail ())
557            break;
558        if (_Traits::eq (__c, __escape))
559        {
560            __is >> __c;
561            if ( __is.fail ())
562                break;
563        }
564        else if (_Traits::eq (__c, __delim))
565            break;
566        __string.push_back ( __c );
567        }
568    return __is;
569}
570
571
572template <class _CharT, class _Iter, class _Traits=char_traits<_CharT>>
573struct __quoted_output_proxy
574{
575    _Iter  __first;
576    _Iter  __last;
577    _CharT  __delim;
578    _CharT  __escape;
579
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
583};
584
585template <class _CharT, class _Traits, class _Iter>
586basic_ostream<_CharT, _Traits>& operator<<(
587         basic_ostream<_CharT, _Traits>& __os,
588         const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
589{
590    return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
591}
592
593template <class _CharT, class _Traits, class _Allocator>
594struct __quoted_proxy
595{
596    basic_string<_CharT, _Traits, _Allocator> &__string;
597    _CharT  __delim;
598    _CharT  __escape;
599
600    __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
601    : __string(__s), __delim(__d), __escape(__e) {}
602};
603
604template <class _CharT, class _Traits, class _Allocator>
605_LIBCPP_INLINE_VISIBILITY
606basic_ostream<_CharT, _Traits>& operator<<(
607        basic_ostream<_CharT, _Traits>& __os,
608        const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
609{
610    return __quoted_output (__os, __proxy.__string.cbegin (), __proxy.__string.cend (), __proxy.__delim, __proxy.__escape);
611}
612
613//  extractor for non-const basic_string& proxies
614template <class _CharT, class _Traits, class _Allocator>
615_LIBCPP_INLINE_VISIBILITY
616basic_istream<_CharT, _Traits>& operator>>(
617        basic_istream<_CharT, _Traits>& __is,
618        const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
619{
620    return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
621}
622
623
624template <class _CharT>
625_LIBCPP_INLINE_VISIBILITY
626__quoted_output_proxy<_CharT, const _CharT *>
627quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
628{
629    const _CharT *__end = __s;
630    while ( *__end ) ++__end;
631    return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
632}
633
634template <class _CharT, class _Traits, class _Allocator>
635_LIBCPP_INLINE_VISIBILITY
636__quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
637quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
638{
639    return __quoted_output_proxy<_CharT,
640            typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
641                    ( __s.cbegin(), __s.cend (), __delim, __escape );
642}
643
644template <class _CharT, class _Traits, class _Allocator>
645__quoted_proxy<_CharT, _Traits, _Allocator>
646quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
647{
648    return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
649}
650#endif
651
652_LIBCPP_END_NAMESPACE_STD
653
654#endif  // _LIBCPP_IOMANIP
655