]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/libc++/include/iomanip
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / contrib / libc++ / include / iomanip
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
17 namespace std {
18
19 // types T1, T2, ... are unspecified implementation types
20 T1 resetiosflags(ios_base::fmtflags mask);
21 T2 setiosflags (ios_base::fmtflags mask);
22 T3 setbase(int base);
23 template<charT> T4 setfill(charT c);
24 T5 setprecision(int n);
25 T6 setw(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);
30
31 template <class charT>
32   T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
33
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
37
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
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
57 class __iom_t1
58 {
59     ios_base::fmtflags __mask_;
60 public:
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
85 inline _LIBCPP_INLINE_VISIBILITY
86 __iom_t1
87 resetiosflags(ios_base::fmtflags __mask)
88 {
89     return __iom_t1(__mask);
90 }
91
92 // setiosflags
93
94 class __iom_t2
95 {
96     ios_base::fmtflags __mask_;
97 public:
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
122 inline _LIBCPP_INLINE_VISIBILITY
123 __iom_t2
124 setiosflags(ios_base::fmtflags __mask)
125 {
126     return __iom_t2(__mask);
127 }
128
129 // setbase
130
131 class __iom_t3
132 {
133     int __base_;
134 public:
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
165 inline _LIBCPP_INLINE_VISIBILITY
166 __iom_t3
167 setbase(int __base)
168 {
169     return __iom_t3(__base);
170 }
171
172 // setfill
173
174 template<class _CharT>
175 class __iom_t4
176 {
177     _CharT __fill_;
178 public:
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
193 template<class _CharT>
194 inline _LIBCPP_INLINE_VISIBILITY
195 __iom_t4<_CharT>
196 setfill(_CharT __c)
197 {
198     return __iom_t4<_CharT>(__c);
199 }
200
201 // setprecision
202
203 class __iom_t5
204 {
205     int __n_;
206 public:
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
231 inline _LIBCPP_INLINE_VISIBILITY
232 __iom_t5
233 setprecision(int __n)
234 {
235     return __iom_t5(__n);
236 }
237
238 // setw
239
240 class __iom_t6
241 {
242     int __n_;
243 public:
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
268 inline _LIBCPP_INLINE_VISIBILITY
269 __iom_t6
270 setw(int __n)
271 {
272     return __iom_t6(__n);
273 }
274
275 // get_money
276
277 template <class _MoneyT> class __iom_t7;
278
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);
282
283 template <class _MoneyT>
284 class __iom_t7
285 {
286     _MoneyT& __mon_;
287     bool __intl_;
288 public:
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
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)
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
327 template <class _MoneyT>
328 inline _LIBCPP_INLINE_VISIBILITY
329 __iom_t7<_MoneyT>
330 get_money(_MoneyT& __mon, bool __intl = false)
331 {
332     return __iom_t7<_MoneyT>(__mon, __intl);
333 }
334
335 // put_money
336
337 template <class _MoneyT> class __iom_t8;
338
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);
342
343 template <class _MoneyT>
344 class __iom_t8
345 {
346     const _MoneyT& __mon_;
347     bool __intl_;
348 public:
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
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)
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
386 template <class _MoneyT>
387 inline _LIBCPP_INLINE_VISIBILITY
388 __iom_t8<_MoneyT>
389 put_money(const _MoneyT& __mon, bool __intl = false)
390 {
391     return __iom_t8<_MoneyT>(__mon, __intl);
392 }
393
394 // get_time
395
396 template <class _CharT> class __iom_t9;
397
398 template <class _CharT, class _Traits>
399 basic_istream<_CharT, _Traits>&
400 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
401
402 template <class _CharT>
403 class __iom_t9
404 {
405     tm* __tm_;
406     const _CharT* __fmt_;
407 public:
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
418 template <class _CharT, class _Traits>
419 basic_istream<_CharT, _Traits>&
420 operator>>(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
447 template <class _CharT>
448 inline _LIBCPP_INLINE_VISIBILITY
449 __iom_t9<_CharT>
450 get_time(tm* __tm, const _CharT* __fmt)
451 {
452     return __iom_t9<_CharT>(__tm, __fmt);
453 }
454
455 // put_time
456
457 template <class _CharT> class __iom_t10;
458
459 template <class _CharT, class _Traits>
460 basic_ostream<_CharT, _Traits>&
461 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
462
463 template <class _CharT>
464 class __iom_t10
465 {
466     const tm* __tm_;
467     const _CharT* __fmt_;
468 public:
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
479 template <class _CharT, class _Traits>
480 basic_ostream<_CharT, _Traits>&
481 operator<<(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
507 template <class _CharT>
508 inline _LIBCPP_INLINE_VISIBILITY
509 __iom_t10<_CharT>
510 put_time(const tm* __tm, const _CharT* __fmt)
511 {
512     return __iom_t10<_CharT>(__tm, __fmt);
513 }
514
515 #if _LIBCPP_STD_VER > 11
516
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 )
521 {
522     __os << __delim;
523     for ( ; __first != __last; ++ __first )
524     {
525         if (_Traits::eq (*__first, __escape) || _Traits::eq (*__first, __delim))
526             __os << __escape;
527         __os << *__first;
528     }
529     __os << __delim;
530     return __os;
531 }
532
533 template <class _CharT, class _Traits, class _String>
534 basic_istream<_CharT, _Traits> &
535 __quoted_input ( basic_istream<_CharT, _Traits> &__is, _String & __string, _CharT __delim, _CharT __escape )
536 {
537     __string.clear ();
538     _CharT __c;
539     __is >> __c;
540     if ( __is.fail ())
541         return __is;
542
543     if (!_Traits::eq (__c, __delim))    // no delimiter, read the whole string
544     {
545         __is.unget ();
546         __is >> __string;
547         return __is;
548     }
549
550     __save_flags<_CharT, _Traits> sf(__is);
551     noskipws (__is);
552     while (true)
553         {
554         __is >> __c;
555         if ( __is.fail ())
556             break;
557         if (_Traits::eq (__c, __escape))
558         {
559             __is >> __c;
560             if ( __is.fail ())
561                 break;
562         }
563         else if (_Traits::eq (__c, __delim))
564             break;
565         __string.push_back ( __c );
566         }
567     return __is;
568 }
569
570
571 template <class _CharT, class _Iter, class _Traits=char_traits<_CharT>>
572 struct __quoted_output_proxy
573 {
574     _Iter  __first;
575     _Iter  __last;
576     _CharT  __delim;
577     _CharT  __escape;
578
579     __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
580     : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
581     //  This would be a nice place for a string_ref 
582 };
583
584 template <class _CharT, class _Traits, class _Iter>
585 basic_ostream<_CharT, _Traits>& operator<<(
586          basic_ostream<_CharT, _Traits>& __os, 
587          const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
588 {
589     return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
590 }
591
592 template <class _CharT, class _Traits, class _Allocator>
593 struct __quoted_proxy
594 {
595     basic_string<_CharT, _Traits, _Allocator> &__string;
596     _CharT  __delim;
597     _CharT  __escape;
598
599     __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
600     : __string(__s), __delim(__d), __escape(__e) {}
601 };
602
603 template <class _CharT, class _Traits, class _Allocator>
604 _LIBCPP_INLINE_VISIBILITY
605 basic_ostream<_CharT, _Traits>& operator<<(
606         basic_ostream<_CharT, _Traits>& __os, 
607         const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
608 {
609     return __quoted_output (__os, __proxy.__string.cbegin (), __proxy.__string.cend (), __proxy.__delim, __proxy.__escape);
610 }
611
612 //  extractor for non-const basic_string& proxies
613 template <class _CharT, class _Traits, class _Allocator>
614 _LIBCPP_INLINE_VISIBILITY
615 basic_istream<_CharT, _Traits>& operator>>(
616         basic_istream<_CharT, _Traits>& __is, 
617         const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
618 {
619     return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
620 }
621
622
623 template <class _CharT>
624 _LIBCPP_INLINE_VISIBILITY
625 __quoted_output_proxy<_CharT, const _CharT *>
626 quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
627 {
628     const _CharT *__end = __s;
629     while ( *__end ) ++__end;
630     return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
631 }
632
633 template <class _CharT, class _Traits, class _Allocator>
634 _LIBCPP_INLINE_VISIBILITY
635 __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
636 quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
637 {
638     return __quoted_output_proxy<_CharT, 
639             typename basic_string <_CharT, _Traits, _Allocator>::const_iterator> 
640                     ( __s.cbegin(), __s.cend (), __delim, __escape );
641 }
642
643 template <class _CharT, class _Traits, class _Allocator>
644 __quoted_proxy<_CharT, _Traits, _Allocator>
645 quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
646 {
647     return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
648 }
649 #endif
650
651 _LIBCPP_END_NAMESPACE_STD
652
653 #endif  // _LIBCPP_IOMANIP