2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_IOMANIP
11 #define _LIBCPP_IOMANIP
18 // types T1, T2, ... are unspecified implementation types
19 T1 resetiosflags(ios_base::fmtflags mask);
20 T2 setiosflags (ios_base::fmtflags mask);
22 template<charT> T4 setfill(charT c);
23 T5 setprecision(int n);
25 template <class moneyT> T7 get_money(moneyT& mon, bool intl = false);
26 template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
27 template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
28 template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
30 template <class charT>
31 T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
33 template <class charT, class traits, class Allocator>
34 T12 quoted(const basic_string<charT, traits, Allocator>& s,
35 charT delim=charT('"'), charT escape=charT('\\')); // C++14
37 template <class charT, class traits, class Allocator>
38 T13 quoted(basic_string<charT, traits, Allocator>& s,
39 charT delim=charT('"'), charT escape=charT('\\')); // C++14
45 #include <__cxx03/__config>
46 #include <__cxx03/istream>
47 #include <__cxx03/version>
49 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
50 # pragma GCC system_header
53 _LIBCPP_BEGIN_NAMESPACE_STD
58 ios_base::fmtflags __mask_;
61 _LIBCPP_HIDE_FROM_ABI explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
63 template <class _CharT, class _Traits>
64 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
65 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x) {
66 __is.unsetf(__x.__mask_);
70 template <class _CharT, class _Traits>
71 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
72 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x) {
73 __os.unsetf(__x.__mask_);
78 inline _LIBCPP_HIDE_FROM_ABI __iom_t1 resetiosflags(ios_base::fmtflags __mask) { return __iom_t1(__mask); }
83 ios_base::fmtflags __mask_;
86 _LIBCPP_HIDE_FROM_ABI explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
88 template <class _CharT, class _Traits>
89 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
90 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x) {
91 __is.setf(__x.__mask_);
95 template <class _CharT, class _Traits>
96 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
97 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x) {
98 __os.setf(__x.__mask_);
103 inline _LIBCPP_HIDE_FROM_ABI __iom_t2 setiosflags(ios_base::fmtflags __mask) { return __iom_t2(__mask); }
111 _LIBCPP_HIDE_FROM_ABI explicit __iom_t3(int __b) : __base_(__b) {}
113 template <class _CharT, class _Traits>
114 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
115 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x) {
116 __is.setf(__x.__base_ == 8 ? ios_base::oct
117 : __x.__base_ == 10 ? ios_base::dec
118 : __x.__base_ == 16 ? ios_base::hex
119 : ios_base::fmtflags(0),
120 ios_base::basefield);
124 template <class _CharT, class _Traits>
125 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
126 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x) {
127 __os.setf(__x.__base_ == 8 ? ios_base::oct
128 : __x.__base_ == 10 ? ios_base::dec
129 : __x.__base_ == 16 ? ios_base::hex
130 : ios_base::fmtflags(0),
131 ios_base::basefield);
136 inline _LIBCPP_HIDE_FROM_ABI __iom_t3 setbase(int __base) { return __iom_t3(__base); }
140 template <class _CharT>
145 _LIBCPP_HIDE_FROM_ABI explicit __iom_t4(_CharT __c) : __fill_(__c) {}
147 template <class _Traits>
148 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
149 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x) {
150 __os.fill(__x.__fill_);
155 template <class _CharT>
156 inline _LIBCPP_HIDE_FROM_ABI __iom_t4<_CharT> setfill(_CharT __c) {
157 return __iom_t4<_CharT>(__c);
166 _LIBCPP_HIDE_FROM_ABI explicit __iom_t5(int __n) : __n_(__n) {}
168 template <class _CharT, class _Traits>
169 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
170 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x) {
171 __is.precision(__x.__n_);
175 template <class _CharT, class _Traits>
176 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
177 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x) {
178 __os.precision(__x.__n_);
183 inline _LIBCPP_HIDE_FROM_ABI __iom_t5 setprecision(int __n) { return __iom_t5(__n); }
191 _LIBCPP_HIDE_FROM_ABI explicit __iom_t6(int __n) : __n_(__n) {}
193 template <class _CharT, class _Traits>
194 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
195 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x) {
196 __is.width(__x.__n_);
200 template <class _CharT, class _Traits>
201 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
202 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x) {
203 __os.width(__x.__n_);
208 inline _LIBCPP_HIDE_FROM_ABI __iom_t6 setw(int __n) { return __iom_t6(__n); }
212 template <class _MoneyT>
215 template <class _CharT, class _Traits, class _MoneyT>
216 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
217 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
219 template <class _MoneyT>
225 _LIBCPP_HIDE_FROM_ABI __iom_t7(_MoneyT& __mon, bool __intl) : __mon_(__mon), __intl_(__intl) {}
227 template <class _CharT, class _Traits, class _Mp>
228 friend basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
231 template <class _CharT, class _Traits, class _MoneyT>
232 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
233 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x) {
234 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
236 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
237 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
239 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
240 typedef money_get<_CharT, _Ip> _Fp;
241 ios_base::iostate __err = ios_base::goodbit;
242 const _Fp& __mf = std::use_facet<_Fp>(__is.getloc());
243 __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_);
244 __is.setstate(__err);
246 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
248 __is.__set_badbit_and_consider_rethrow();
250 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
254 template <class _MoneyT>
255 inline _LIBCPP_HIDE_FROM_ABI __iom_t7<_MoneyT> get_money(_MoneyT& __mon, bool __intl = false) {
256 return __iom_t7<_MoneyT>(__mon, __intl);
261 template <class _MoneyT>
264 template <class _CharT, class _Traits, class _MoneyT>
265 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
266 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
268 template <class _MoneyT>
270 const _MoneyT& __mon_;
274 _LIBCPP_HIDE_FROM_ABI __iom_t8(const _MoneyT& __mon, bool __intl) : __mon_(__mon), __intl_(__intl) {}
276 template <class _CharT, class _Traits, class _Mp>
277 friend basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
280 template <class _CharT, class _Traits, class _MoneyT>
281 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
282 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x) {
283 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
285 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
286 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
288 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
289 typedef money_put<_CharT, _Op> _Fp;
290 const _Fp& __mf = std::use_facet<_Fp>(__os.getloc());
291 if (__mf.put(_Op(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
292 __os.setstate(ios_base::badbit);
294 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
296 __os.__set_badbit_and_consider_rethrow();
298 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
302 template <class _MoneyT>
303 inline _LIBCPP_HIDE_FROM_ABI __iom_t8<_MoneyT> put_money(const _MoneyT& __mon, bool __intl = false) {
304 return __iom_t8<_MoneyT>(__mon, __intl);
309 template <class _CharT>
312 template <class _CharT, class _Traits>
313 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
314 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
316 template <class _CharT>
319 const _CharT* __fmt_;
322 _LIBCPP_HIDE_FROM_ABI __iom_t9(tm* __tm, const _CharT* __fmt) : __tm_(__tm), __fmt_(__fmt) {}
324 template <class _Cp, class _Traits>
325 friend basic_istream<_Cp, _Traits>& operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
328 template <class _CharT, class _Traits>
329 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
330 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x) {
331 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
333 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
334 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
336 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
337 typedef time_get<_CharT, _Ip> _Fp;
338 ios_base::iostate __err = ios_base::goodbit;
339 const _Fp& __tf = std::use_facet<_Fp>(__is.getloc());
340 __tf.get(_Ip(__is), _Ip(), __is, __err, __x.__tm_, __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
341 __is.setstate(__err);
343 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
345 __is.__set_badbit_and_consider_rethrow();
347 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
351 template <class _CharT>
352 inline _LIBCPP_HIDE_FROM_ABI __iom_t9<_CharT> get_time(tm* __tm, const _CharT* __fmt) {
353 return __iom_t9<_CharT>(__tm, __fmt);
358 template <class _CharT>
361 template <class _CharT, class _Traits>
362 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
363 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
365 template <class _CharT>
368 const _CharT* __fmt_;
371 _LIBCPP_HIDE_FROM_ABI __iom_t10(const tm* __tm, const _CharT* __fmt) : __tm_(__tm), __fmt_(__fmt) {}
373 template <class _Cp, class _Traits>
374 friend basic_ostream<_Cp, _Traits>& operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
377 template <class _CharT, class _Traits>
378 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
379 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x) {
380 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
382 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
383 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
385 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
386 typedef time_put<_CharT, _Op> _Fp;
387 const _Fp& __tf = std::use_facet<_Fp>(__os.getloc());
388 if (__tf.put(_Op(__os), __os, __os.fill(), __x.__tm_, __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_))
390 __os.setstate(ios_base::badbit);
392 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
394 __os.__set_badbit_and_consider_rethrow();
396 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
400 template <class _CharT>
401 inline _LIBCPP_HIDE_FROM_ABI __iom_t10<_CharT> put_time(const tm* __tm, const _CharT* __fmt) {
402 return __iom_t10<_CharT>(__tm, __fmt);
405 template <class _CharT, class _Traits>
406 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& __quoted_output(
407 basic_ostream<_CharT, _Traits>& __os,
408 const _CharT* __first,
409 const _CharT* __last,
412 basic_string<_CharT, _Traits> __str;
413 __str.push_back(__delim);
414 for (; __first != __last; ++__first) {
415 if (_Traits::eq(*__first, __escape) || _Traits::eq(*__first, __delim))
416 __str.push_back(__escape);
417 __str.push_back(*__first);
419 __str.push_back(__delim);
420 return std::__put_character_sequence(__os, __str.data(), __str.size());
423 template <class _CharT, class _Traits, class _String>
424 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
425 __quoted_input(basic_istream<_CharT, _Traits>& __is, _String& __string, _CharT __delim, _CharT __escape) {
432 if (!_Traits::eq(__c, __delim)) {
433 // no delimiter, read the whole string
439 __save_flags<_CharT, _Traits> __sf(__is);
445 if (_Traits::eq(__c, __escape)) {
449 } else if (_Traits::eq(__c, __delim))
451 __string.push_back(__c);
456 template <class _CharT, class _Traits>
457 struct _LIBCPP_HIDDEN __quoted_output_proxy {
458 const _CharT* __first_;
459 const _CharT* __last_;
463 _LIBCPP_HIDE_FROM_ABI explicit __quoted_output_proxy(const _CharT* __f, const _CharT* __l, _CharT __d, _CharT __e)
464 : __first_(__f), __last_(__l), __delim_(__d), __escape_(__e) {}
466 template <class _T2, __enable_if_t<_IsSame<_Traits, void>::value || _IsSame<_Traits, _T2>::value, int> = 0>
467 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _T2>&
468 operator<<(basic_ostream<_CharT, _T2>& __os, const __quoted_output_proxy& __p) {
469 return std::__quoted_output(__os, __p.__first_, __p.__last_, __p.__delim_, __p.__escape_);
473 template <class _CharT, class _Traits, class _Allocator>
474 struct _LIBCPP_HIDDEN __quoted_proxy {
475 basic_string<_CharT, _Traits, _Allocator>& __string_;
479 _LIBCPP_HIDE_FROM_ABI explicit __quoted_proxy(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __d, _CharT __e)
480 : __string_(__s), __delim_(__d), __escape_(__e) {}
482 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
483 operator<<(basic_ostream<_CharT, _Traits>& __os, const __quoted_proxy& __p) {
484 return std::__quoted_output(
485 __os, __p.__string_.data(), __p.__string_.data() + __p.__string_.size(), __p.__delim_, __p.__escape_);
488 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
489 operator>>(basic_istream<_CharT, _Traits>& __is, const __quoted_proxy& __p) {
490 return std::__quoted_input(__is, __p.__string_, __p.__delim_, __p.__escape_);
494 template <class _CharT, class _Traits, class _Allocator>
495 _LIBCPP_HIDE_FROM_ABI __quoted_output_proxy<_CharT, _Traits>
496 __quoted(const basic_string<_CharT, _Traits, _Allocator>& __s,
497 _CharT __delim = _CharT('"'),
498 _CharT __escape = _CharT('\\')) {
499 return __quoted_output_proxy<_CharT, _Traits>(__s.data(), __s.data() + __s.size(), __delim, __escape);
502 template <class _CharT, class _Traits, class _Allocator>
503 _LIBCPP_HIDE_FROM_ABI __quoted_proxy<_CharT, _Traits, _Allocator>
504 __quoted(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) {
505 return __quoted_proxy<_CharT, _Traits, _Allocator>(__s, __delim, __escape);
508 #if _LIBCPP_STD_VER >= 14
510 template <class _CharT>
511 _LIBCPP_HIDE_FROM_ABI auto quoted(const _CharT* __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) {
512 const _CharT* __end = __s;
515 return __quoted_output_proxy<_CharT, void>(__s, __end, __delim, __escape);
518 template <class _CharT, class _Traits, class _Allocator>
519 _LIBCPP_HIDE_FROM_ABI auto
520 quoted(const basic_string<_CharT, _Traits, _Allocator>& __s,
521 _CharT __delim = _CharT('"'),
522 _CharT __escape = _CharT('\\')) {
523 return __quoted_output_proxy<_CharT, _Traits>(__s.data(), __s.data() + __s.size(), __delim, __escape);
526 template <class _CharT, class _Traits, class _Allocator>
527 _LIBCPP_HIDE_FROM_ABI auto
528 quoted(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) {
529 return __quoted_proxy<_CharT, _Traits, _Allocator>(__s, __delim, __escape);
532 template <class _CharT, class _Traits>
533 _LIBCPP_HIDE_FROM_ABI auto
534 quoted(basic_string_view<_CharT, _Traits> __sv, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) {
535 return __quoted_output_proxy<_CharT, _Traits>(__sv.data(), __sv.data() + __sv.size(), __delim, __escape);
538 #endif // _LIBCPP_STD_VER >= 14
540 _LIBCPP_END_NAMESPACE_STD
542 #endif // _LIBCPP_IOMANIP