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 __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 533template <class _CharT, class _Traits, class _String> 534basic_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 571template <class _CharT, class _Iter, class _Traits=char_traits<_CharT>> 572struct __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 584template <class _CharT, class _Traits, class _Iter> 585basic_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 592template <class _CharT, class _Traits, class _Allocator> 593struct __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 603template <class _CharT, class _Traits, class _Allocator> 604_LIBCPP_INLINE_VISIBILITY 605basic_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 613template <class _CharT, class _Traits, class _Allocator> 614_LIBCPP_INLINE_VISIBILITY 615basic_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 623template <class _CharT> 624_LIBCPP_INLINE_VISIBILITY 625__quoted_output_proxy<_CharT, const _CharT *> 626quoted ( 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 633template <class _CharT, class _Traits, class _Allocator> 634_LIBCPP_INLINE_VISIBILITY 635__quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator> 636quoted ( 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 643template <class _CharT, class _Traits, class _Allocator> 644__quoted_proxy<_CharT, _Traits, _Allocator> 645quoted ( 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 654