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 <__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 59 class __iom_t1 60 { 61 ios_base::fmtflags __mask_; 62 public: 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 87 inline _LIBCPP_INLINE_VISIBILITY 88 __iom_t1 89 resetiosflags(ios_base::fmtflags __mask) 90 { 91 return __iom_t1(__mask); 92 } 93 94 // setiosflags 95 96 class __iom_t2 97 { 98 ios_base::fmtflags __mask_; 99 public: 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 124 inline _LIBCPP_INLINE_VISIBILITY 125 __iom_t2 126 setiosflags(ios_base::fmtflags __mask) 127 { 128 return __iom_t2(__mask); 129 } 130 131 // setbase 132 133 class __iom_t3 134 { 135 int __base_; 136 public: 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 167 inline _LIBCPP_INLINE_VISIBILITY 168 __iom_t3 169 setbase(int __base) 170 { 171 return __iom_t3(__base); 172 } 173 174 // setfill 175 176 template<class _CharT> 177 class __iom_t4 178 { 179 _CharT __fill_; 180 public: 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 195 template<class _CharT> 196 inline _LIBCPP_INLINE_VISIBILITY 197 __iom_t4<_CharT> 198 setfill(_CharT __c) 199 { 200 return __iom_t4<_CharT>(__c); 201 } 202 203 // setprecision 204 205 class __iom_t5 206 { 207 int __n_; 208 public: 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 233 inline _LIBCPP_INLINE_VISIBILITY 234 __iom_t5 235 setprecision(int __n) 236 { 237 return __iom_t5(__n); 238 } 239 240 // setw 241 242 class __iom_t6 243 { 244 int __n_; 245 public: 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 270 inline _LIBCPP_INLINE_VISIBILITY 271 __iom_t6 272 setw(int __n) 273 { 274 return __iom_t6(__n); 275 } 276 277 // get_money 278 279 template <class _MoneyT> class __iom_t7; 280 281 template <class _CharT, class _Traits, class _MoneyT> 282 basic_istream<_CharT, _Traits>& 283 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x); 284 285 template <class _MoneyT> 286 class __iom_t7 287 { 288 _MoneyT& __mon_; 289 bool __intl_; 290 public: 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 301 template <class _CharT, class _Traits, class _MoneyT> 302 basic_istream<_CharT, _Traits>& 303 operator>>(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 329 template <class _MoneyT> 330 inline _LIBCPP_INLINE_VISIBILITY 331 __iom_t7<_MoneyT> 332 get_money(_MoneyT& __mon, bool __intl = false) 333 { 334 return __iom_t7<_MoneyT>(__mon, __intl); 335 } 336 337 // put_money 338 339 template <class _MoneyT> class __iom_t8; 340 341 template <class _CharT, class _Traits, class _MoneyT> 342 basic_ostream<_CharT, _Traits>& 343 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x); 344 345 template <class _MoneyT> 346 class __iom_t8 347 { 348 const _MoneyT& __mon_; 349 bool __intl_; 350 public: 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 361 template <class _CharT, class _Traits, class _MoneyT> 362 basic_ostream<_CharT, _Traits>& 363 operator<<(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 388 template <class _MoneyT> 389 inline _LIBCPP_INLINE_VISIBILITY 390 __iom_t8<_MoneyT> 391 put_money(const _MoneyT& __mon, bool __intl = false) 392 { 393 return __iom_t8<_MoneyT>(__mon, __intl); 394 } 395 396 // get_time 397 398 template <class _CharT> class __iom_t9; 399 400 template <class _CharT, class _Traits> 401 basic_istream<_CharT, _Traits>& 402 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x); 403 404 template <class _CharT> 405 class __iom_t9 406 { 407 tm* __tm_; 408 const _CharT* __fmt_; 409 public: 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 420 template <class _CharT, class _Traits> 421 basic_istream<_CharT, _Traits>& 422 operator>>(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 449 template <class _CharT> 450 inline _LIBCPP_INLINE_VISIBILITY 451 __iom_t9<_CharT> 452 get_time(tm* __tm, const _CharT* __fmt) 453 { 454 return __iom_t9<_CharT>(__tm, __fmt); 455 } 456 457 // put_time 458 459 template <class _CharT> class __iom_t10; 460 461 template <class _CharT, class _Traits> 462 basic_ostream<_CharT, _Traits>& 463 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x); 464 465 template <class _CharT> 466 class __iom_t10 467 { 468 const tm* __tm_; 469 const _CharT* __fmt_; 470 public: 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 481 template <class _CharT, class _Traits> 482 basic_ostream<_CharT, _Traits>& 483 operator<<(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 509 template <class _CharT> 510 inline _LIBCPP_INLINE_VISIBILITY 511 __iom_t10<_CharT> 512 put_time(const tm* __tm, const _CharT* __fmt) 513 { 514 return __iom_t10<_CharT>(__tm, __fmt); 515 } 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 _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 534 template <class _CharT, class _Traits, class _String> 535 basic_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 572 template <class _CharT, class _Traits, class _Iter> 573 basic_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 580 template <class _CharT, class _Traits, class _Allocator> 581 struct __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 591 template <class _CharT, class _Traits, class _Allocator> 592 _LIBCPP_INLINE_VISIBILITY 593 basic_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 601 template <class _CharT, class _Traits, class _Allocator> 602 _LIBCPP_INLINE_VISIBILITY 603 basic_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 611 template <class _CharT> 612 _LIBCPP_INLINE_VISIBILITY 613 __quoted_output_proxy<_CharT, const _CharT *> 614 quoted ( 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 622 template <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 632 template <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 643 template <class _CharT, class _Traits, class _Allocator> 644 _LIBCPP_INLINE_VISIBILITY 645 __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator> 646 quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\')) 647 { 648 return __quoted(__s, __delim, __escape); 649 } 650 651 template <class _CharT, class _Traits, class _Allocator> 652 _LIBCPP_INLINE_VISIBILITY 653 __quoted_proxy<_CharT, _Traits, _Allocator> 654 quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\')) 655 { 656 return __quoted(__s, __delim, __escape); 657 } 658 659 template <class _CharT, class _Traits> 660 __quoted_output_proxy<_CharT, const _CharT *, _Traits> 661 quoted (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