• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <__string>
48#include <istream>
49#include <version>
50
51#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
52#pragma GCC system_header
53#endif
54
55_LIBCPP_BEGIN_NAMESPACE_STD
56
57// resetiosflags
58
59class __iom_t1
60{
61    ios_base::fmtflags __mask_;
62public:
63    _LIBCPP_INLINE_VISIBILITY
64    explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
65
66    template <class _CharT, class _Traits>
67    friend
68    _LIBCPP_INLINE_VISIBILITY
69    basic_istream<_CharT, _Traits>&
70    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
71    {
72        __is.unsetf(__x.__mask_);
73        return __is;
74    }
75
76    template <class _CharT, class _Traits>
77    friend
78    _LIBCPP_INLINE_VISIBILITY
79    basic_ostream<_CharT, _Traits>&
80    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
81    {
82        __os.unsetf(__x.__mask_);
83        return __os;
84    }
85};
86
87inline _LIBCPP_INLINE_VISIBILITY
88__iom_t1
89resetiosflags(ios_base::fmtflags __mask)
90{
91    return __iom_t1(__mask);
92}
93
94// setiosflags
95
96class __iom_t2
97{
98    ios_base::fmtflags __mask_;
99public:
100    _LIBCPP_INLINE_VISIBILITY
101    explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
102
103    template <class _CharT, class _Traits>
104    friend
105    _LIBCPP_INLINE_VISIBILITY
106    basic_istream<_CharT, _Traits>&
107    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
108    {
109        __is.setf(__x.__mask_);
110        return __is;
111    }
112
113    template <class _CharT, class _Traits>
114    friend
115    _LIBCPP_INLINE_VISIBILITY
116    basic_ostream<_CharT, _Traits>&
117    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
118    {
119        __os.setf(__x.__mask_);
120        return __os;
121    }
122};
123
124inline _LIBCPP_INLINE_VISIBILITY
125__iom_t2
126setiosflags(ios_base::fmtflags __mask)
127{
128    return __iom_t2(__mask);
129}
130
131// setbase
132
133class __iom_t3
134{
135    int __base_;
136public:
137    _LIBCPP_INLINE_VISIBILITY
138    explicit __iom_t3(int __b) : __base_(__b) {}
139
140    template <class _CharT, class _Traits>
141    friend
142    _LIBCPP_INLINE_VISIBILITY
143    basic_istream<_CharT, _Traits>&
144    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
145    {
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);
150        return __is;
151    }
152
153    template <class _CharT, class _Traits>
154    friend
155    _LIBCPP_INLINE_VISIBILITY
156    basic_ostream<_CharT, _Traits>&
157    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
158    {
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);
163        return __os;
164    }
165};
166
167inline _LIBCPP_INLINE_VISIBILITY
168__iom_t3
169setbase(int __base)
170{
171    return __iom_t3(__base);
172}
173
174// setfill
175
176template<class _CharT>
177class __iom_t4
178{
179    _CharT __fill_;
180public:
181    _LIBCPP_INLINE_VISIBILITY
182    explicit __iom_t4(_CharT __c) : __fill_(__c) {}
183
184    template <class _Traits>
185    friend
186    _LIBCPP_INLINE_VISIBILITY
187    basic_ostream<_CharT, _Traits>&
188    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
189    {
190        __os.fill(__x.__fill_);
191        return __os;
192    }
193};
194
195template<class _CharT>
196inline _LIBCPP_INLINE_VISIBILITY
197__iom_t4<_CharT>
198setfill(_CharT __c)
199{
200    return __iom_t4<_CharT>(__c);
201}
202
203// setprecision
204
205class __iom_t5
206{
207    int __n_;
208public:
209    _LIBCPP_INLINE_VISIBILITY
210    explicit __iom_t5(int __n) : __n_(__n) {}
211
212    template <class _CharT, class _Traits>
213    friend
214    _LIBCPP_INLINE_VISIBILITY
215    basic_istream<_CharT, _Traits>&
216    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
217    {
218        __is.precision(__x.__n_);
219        return __is;
220    }
221
222    template <class _CharT, class _Traits>
223    friend
224    _LIBCPP_INLINE_VISIBILITY
225    basic_ostream<_CharT, _Traits>&
226    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
227    {
228        __os.precision(__x.__n_);
229        return __os;
230    }
231};
232
233inline _LIBCPP_INLINE_VISIBILITY
234__iom_t5
235setprecision(int __n)
236{
237    return __iom_t5(__n);
238}
239
240// setw
241
242class __iom_t6
243{
244    int __n_;
245public:
246    _LIBCPP_INLINE_VISIBILITY
247    explicit __iom_t6(int __n) : __n_(__n) {}
248
249    template <class _CharT, class _Traits>
250    friend
251    _LIBCPP_INLINE_VISIBILITY
252    basic_istream<_CharT, _Traits>&
253    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
254    {
255        __is.width(__x.__n_);
256        return __is;
257    }
258
259    template <class _CharT, class _Traits>
260    friend
261    _LIBCPP_INLINE_VISIBILITY
262    basic_ostream<_CharT, _Traits>&
263    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
264    {
265        __os.width(__x.__n_);
266        return __os;
267    }
268};
269
270inline _LIBCPP_INLINE_VISIBILITY
271__iom_t6
272setw(int __n)
273{
274    return __iom_t6(__n);
275}
276
277// get_money
278
279template <class _MoneyT> class __iom_t7;
280
281template <class _CharT, class _Traits, class _MoneyT>
282basic_istream<_CharT, _Traits>&
283operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
284
285template <class _MoneyT>
286class __iom_t7
287{
288    _MoneyT& __mon_;
289    bool __intl_;
290public:
291    _LIBCPP_INLINE_VISIBILITY
292    __iom_t7(_MoneyT& __mon, bool __intl)
293        : __mon_(__mon), __intl_(__intl) {}
294
295    template <class _CharT, class _Traits, class _Mp>
296    friend
297    basic_istream<_CharT, _Traits>&
298    operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
299};
300
301template <class _CharT, class _Traits, class _MoneyT>
302basic_istream<_CharT, _Traits>&
303operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
304{
305#ifndef _LIBCPP_NO_EXCEPTIONS
306    try
307    {
308#endif  // _LIBCPP_NO_EXCEPTIONS
309        typename basic_istream<_CharT, _Traits>::sentry __s(__is);
310        if (__s)
311        {
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);
318        }
319#ifndef _LIBCPP_NO_EXCEPTIONS
320    }
321    catch (...)
322    {
323        __is.__set_badbit_and_consider_rethrow();
324    }
325#endif  // _LIBCPP_NO_EXCEPTIONS
326    return __is;
327}
328
329template <class _MoneyT>
330inline _LIBCPP_INLINE_VISIBILITY
331__iom_t7<_MoneyT>
332get_money(_MoneyT& __mon, bool __intl = false)
333{
334    return __iom_t7<_MoneyT>(__mon, __intl);
335}
336
337// put_money
338
339template <class _MoneyT> class __iom_t8;
340
341template <class _CharT, class _Traits, class _MoneyT>
342basic_ostream<_CharT, _Traits>&
343operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
344
345template <class _MoneyT>
346class __iom_t8
347{
348    const _MoneyT& __mon_;
349    bool __intl_;
350public:
351    _LIBCPP_INLINE_VISIBILITY
352    __iom_t8(const _MoneyT& __mon, bool __intl)
353        : __mon_(__mon), __intl_(__intl) {}
354
355    template <class _CharT, class _Traits, class _Mp>
356    friend
357    basic_ostream<_CharT, _Traits>&
358    operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
359};
360
361template <class _CharT, class _Traits, class _MoneyT>
362basic_ostream<_CharT, _Traits>&
363operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
364{
365#ifndef _LIBCPP_NO_EXCEPTIONS
366    try
367    {
368#endif  // _LIBCPP_NO_EXCEPTIONS
369        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
370        if (__s)
371        {
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);
377        }
378#ifndef _LIBCPP_NO_EXCEPTIONS
379    }
380    catch (...)
381    {
382        __os.__set_badbit_and_consider_rethrow();
383    }
384#endif  // _LIBCPP_NO_EXCEPTIONS
385    return __os;
386}
387
388template <class _MoneyT>
389inline _LIBCPP_INLINE_VISIBILITY
390__iom_t8<_MoneyT>
391put_money(const _MoneyT& __mon, bool __intl = false)
392{
393    return __iom_t8<_MoneyT>(__mon, __intl);
394}
395
396// get_time
397
398template <class _CharT> class __iom_t9;
399
400template <class _CharT, class _Traits>
401basic_istream<_CharT, _Traits>&
402operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
403
404template <class _CharT>
405class __iom_t9
406{
407    tm* __tm_;
408    const _CharT* __fmt_;
409public:
410    _LIBCPP_INLINE_VISIBILITY
411    __iom_t9(tm* __tm, const _CharT* __fmt)
412        : __tm_(__tm), __fmt_(__fmt) {}
413
414    template <class _Cp, class _Traits>
415    friend
416    basic_istream<_Cp, _Traits>&
417    operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
418};
419
420template <class _CharT, class _Traits>
421basic_istream<_CharT, _Traits>&
422operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
423{
424#ifndef _LIBCPP_NO_EXCEPTIONS
425    try
426    {
427#endif  // _LIBCPP_NO_EXCEPTIONS
428        typename basic_istream<_CharT, _Traits>::sentry __s(__is);
429        if (__s)
430        {
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);
438        }
439#ifndef _LIBCPP_NO_EXCEPTIONS
440    }
441    catch (...)
442    {
443        __is.__set_badbit_and_consider_rethrow();
444    }
445#endif  // _LIBCPP_NO_EXCEPTIONS
446    return __is;
447}
448
449template <class _CharT>
450inline _LIBCPP_INLINE_VISIBILITY
451__iom_t9<_CharT>
452get_time(tm* __tm, const _CharT* __fmt)
453{
454    return __iom_t9<_CharT>(__tm, __fmt);
455}
456
457// put_time
458
459template <class _CharT> class __iom_t10;
460
461template <class _CharT, class _Traits>
462basic_ostream<_CharT, _Traits>&
463operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
464
465template <class _CharT>
466class __iom_t10
467{
468    const tm* __tm_;
469    const _CharT* __fmt_;
470public:
471    _LIBCPP_INLINE_VISIBILITY
472    __iom_t10(const tm* __tm, const _CharT* __fmt)
473        : __tm_(__tm), __fmt_(__fmt) {}
474
475    template <class _Cp, class _Traits>
476    friend
477    basic_ostream<_Cp, _Traits>&
478    operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
479};
480
481template <class _CharT, class _Traits>
482basic_ostream<_CharT, _Traits>&
483operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
484{
485#ifndef _LIBCPP_NO_EXCEPTIONS
486    try
487    {
488#endif  // _LIBCPP_NO_EXCEPTIONS
489        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
490        if (__s)
491        {
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);
498        }
499#ifndef _LIBCPP_NO_EXCEPTIONS
500    }
501    catch (...)
502    {
503        __os.__set_badbit_and_consider_rethrow();
504    }
505#endif  // _LIBCPP_NO_EXCEPTIONS
506    return __os;
507}
508
509template <class _CharT>
510inline _LIBCPP_INLINE_VISIBILITY
511__iom_t10<_CharT>
512put_time(const tm* __tm, const _CharT* __fmt)
513{
514    return __iom_t10<_CharT>(__tm, __fmt);
515}
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 _Traits, class _Iter>
573basic_ostream<_CharT, _Traits>& operator<<(
574         basic_ostream<_CharT, _Traits>& __os,
575         const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
576{
577    return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
578}
579
580template <class _CharT, class _Traits, class _Allocator>
581struct __quoted_proxy
582{
583    basic_string<_CharT, _Traits, _Allocator> &__string;
584    _CharT  __delim;
585    _CharT  __escape;
586
587    __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
588    : __string(__s), __delim(__d), __escape(__e) {}
589};
590
591template <class _CharT, class _Traits, class _Allocator>
592_LIBCPP_INLINE_VISIBILITY
593basic_ostream<_CharT, _Traits>& operator<<(
594        basic_ostream<_CharT, _Traits>& __os,
595        const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
596{
597    return __quoted_output (__os, __proxy.__string.cbegin (), __proxy.__string.cend (), __proxy.__delim, __proxy.__escape);
598}
599
600//  extractor for non-const basic_string& proxies
601template <class _CharT, class _Traits, class _Allocator>
602_LIBCPP_INLINE_VISIBILITY
603basic_istream<_CharT, _Traits>& operator>>(
604        basic_istream<_CharT, _Traits>& __is,
605        const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
606{
607    return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
608}
609
610
611template <class _CharT>
612_LIBCPP_INLINE_VISIBILITY
613__quoted_output_proxy<_CharT, const _CharT *>
614quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
615{
616    const _CharT *__end = __s;
617    while ( *__end ) ++__end;
618    return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
619}
620
621
622template <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('\\'))
626{
627    return __quoted_output_proxy<_CharT,
628            typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
629                    ( __s.cbegin(), __s.cend (), __delim, __escape );
630}
631
632template <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('\\'))
636{
637    return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
638}
639
640
641#if _LIBCPP_STD_VER > 11
642
643template <class _CharT, class _Traits, class _Allocator>
644_LIBCPP_INLINE_VISIBILITY
645__quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
646quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
647{
648    return __quoted(__s, __delim, __escape);
649}
650
651template <class _CharT, class _Traits, class _Allocator>
652_LIBCPP_INLINE_VISIBILITY
653__quoted_proxy<_CharT, _Traits, _Allocator>
654quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
655{
656    return __quoted(__s, __delim, __escape);
657}
658
659template <class _CharT, class _Traits>
660__quoted_output_proxy<_CharT, const _CharT *, _Traits>
661quoted (basic_string_view <_CharT, _Traits> __sv,
662             _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
663{
664    return __quoted_output_proxy<_CharT, const _CharT *, _Traits>
665         ( __sv.data(), __sv.data() + __sv.size(), __delim, __escape );
666}
667#endif
668
669_LIBCPP_END_NAMESPACE_STD
670
671#endif  // _LIBCPP_IOMANIP
672