|
libstdc++
|
00001 // Standard stream manipulators -*- C++ -*- 00002 00003 // Copyright (C) 1997-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/iomanip 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 // 00030 // ISO C++ 14882: 27.6.3 Standard manipulators 00031 // 00032 00033 #ifndef _GLIBCXX_IOMANIP 00034 #define _GLIBCXX_IOMANIP 1 00035 00036 #pragma GCC system_header 00037 00038 #include <bits/c++config.h> 00039 #include <iosfwd> 00040 #include <bits/ios_base.h> 00041 00042 #if __cplusplus >= 201103L 00043 #include <locale> 00044 #if __cplusplus > 201103L 00045 #include <sstream> // used in quoted. 00046 #endif 00047 #endif 00048 00049 namespace std _GLIBCXX_VISIBILITY(default) 00050 { 00051 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00052 00053 // [27.6.3] standard manipulators 00054 // Also see DR 183. 00055 00056 struct _Resetiosflags { ios_base::fmtflags _M_mask; }; 00057 00058 /** 00059 * @brief Manipulator for @c setf. 00060 * @param __mask A format flags mask. 00061 * 00062 * Sent to a stream object, this manipulator resets the specified flags, 00063 * via @e stream.setf(0,__mask). 00064 */ 00065 inline _Resetiosflags 00066 resetiosflags(ios_base::fmtflags __mask) 00067 { return { __mask }; } 00068 00069 template<typename _CharT, typename _Traits> 00070 inline basic_istream<_CharT, _Traits>& 00071 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f) 00072 { 00073 __is.setf(ios_base::fmtflags(0), __f._M_mask); 00074 return __is; 00075 } 00076 00077 template<typename _CharT, typename _Traits> 00078 inline basic_ostream<_CharT, _Traits>& 00079 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f) 00080 { 00081 __os.setf(ios_base::fmtflags(0), __f._M_mask); 00082 return __os; 00083 } 00084 00085 00086 struct _Setiosflags { ios_base::fmtflags _M_mask; }; 00087 00088 /** 00089 * @brief Manipulator for @c setf. 00090 * @param __mask A format flags mask. 00091 * 00092 * Sent to a stream object, this manipulator sets the format flags 00093 * to @a __mask. 00094 */ 00095 inline _Setiosflags 00096 setiosflags(ios_base::fmtflags __mask) 00097 { return { __mask }; } 00098 00099 template<typename _CharT, typename _Traits> 00100 inline basic_istream<_CharT, _Traits>& 00101 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f) 00102 { 00103 __is.setf(__f._M_mask); 00104 return __is; 00105 } 00106 00107 template<typename _CharT, typename _Traits> 00108 inline basic_ostream<_CharT, _Traits>& 00109 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f) 00110 { 00111 __os.setf(__f._M_mask); 00112 return __os; 00113 } 00114 00115 00116 struct _Setbase { int _M_base; }; 00117 00118 /** 00119 * @brief Manipulator for @c setf. 00120 * @param __base A numeric base. 00121 * 00122 * Sent to a stream object, this manipulator changes the 00123 * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base 00124 * is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value. 00125 */ 00126 inline _Setbase 00127 setbase(int __base) 00128 { return { __base }; } 00129 00130 template<typename _CharT, typename _Traits> 00131 inline basic_istream<_CharT, _Traits>& 00132 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f) 00133 { 00134 __is.setf(__f._M_base == 8 ? ios_base::oct : 00135 __f._M_base == 10 ? ios_base::dec : 00136 __f._M_base == 16 ? ios_base::hex : 00137 ios_base::fmtflags(0), ios_base::basefield); 00138 return __is; 00139 } 00140 00141 template<typename _CharT, typename _Traits> 00142 inline basic_ostream<_CharT, _Traits>& 00143 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f) 00144 { 00145 __os.setf(__f._M_base == 8 ? ios_base::oct : 00146 __f._M_base == 10 ? ios_base::dec : 00147 __f._M_base == 16 ? ios_base::hex : 00148 ios_base::fmtflags(0), ios_base::basefield); 00149 return __os; 00150 } 00151 00152 00153 template<typename _CharT> 00154 struct _Setfill { _CharT _M_c; }; 00155 00156 /** 00157 * @brief Manipulator for @c fill. 00158 * @param __c The new fill character. 00159 * 00160 * Sent to a stream object, this manipulator calls @c fill(__c) for that 00161 * object. 00162 */ 00163 template<typename _CharT> 00164 inline _Setfill<_CharT> 00165 setfill(_CharT __c) 00166 { return { __c }; } 00167 00168 template<typename _CharT, typename _Traits> 00169 inline basic_istream<_CharT, _Traits>& 00170 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f) 00171 { 00172 __is.fill(__f._M_c); 00173 return __is; 00174 } 00175 00176 template<typename _CharT, typename _Traits> 00177 inline basic_ostream<_CharT, _Traits>& 00178 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f) 00179 { 00180 __os.fill(__f._M_c); 00181 return __os; 00182 } 00183 00184 00185 struct _Setprecision { int _M_n; }; 00186 00187 /** 00188 * @brief Manipulator for @c precision. 00189 * @param __n The new precision. 00190 * 00191 * Sent to a stream object, this manipulator calls @c precision(__n) for 00192 * that object. 00193 */ 00194 inline _Setprecision 00195 setprecision(int __n) 00196 { return { __n }; } 00197 00198 template<typename _CharT, typename _Traits> 00199 inline basic_istream<_CharT, _Traits>& 00200 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f) 00201 { 00202 __is.precision(__f._M_n); 00203 return __is; 00204 } 00205 00206 template<typename _CharT, typename _Traits> 00207 inline basic_ostream<_CharT, _Traits>& 00208 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f) 00209 { 00210 __os.precision(__f._M_n); 00211 return __os; 00212 } 00213 00214 00215 struct _Setw { int _M_n; }; 00216 00217 /** 00218 * @brief Manipulator for @c width. 00219 * @param __n The new width. 00220 * 00221 * Sent to a stream object, this manipulator calls @c width(__n) for 00222 * that object. 00223 */ 00224 inline _Setw 00225 setw(int __n) 00226 { return { __n }; } 00227 00228 template<typename _CharT, typename _Traits> 00229 inline basic_istream<_CharT, _Traits>& 00230 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f) 00231 { 00232 __is.width(__f._M_n); 00233 return __is; 00234 } 00235 00236 template<typename _CharT, typename _Traits> 00237 inline basic_ostream<_CharT, _Traits>& 00238 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f) 00239 { 00240 __os.width(__f._M_n); 00241 return __os; 00242 } 00243 00244 #if __cplusplus >= 201103L 00245 00246 template<typename _MoneyT> 00247 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; }; 00248 00249 /** 00250 * @brief Extended manipulator for extracting money. 00251 * @param __mon Either long double or a specialization of @c basic_string. 00252 * @param __intl A bool indicating whether international format 00253 * is to be used. 00254 * 00255 * Sent to a stream object, this manipulator extracts @a __mon. 00256 */ 00257 template<typename _MoneyT> 00258 inline _Get_money<_MoneyT> 00259 get_money(_MoneyT& __mon, bool __intl = false) 00260 { return { __mon, __intl }; } 00261 00262 template<typename _CharT, typename _Traits, typename _MoneyT> 00263 basic_istream<_CharT, _Traits>& 00264 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f) 00265 { 00266 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false); 00267 if (__cerb) 00268 { 00269 ios_base::iostate __err = ios_base::goodbit; 00270 __try 00271 { 00272 typedef istreambuf_iterator<_CharT, _Traits> _Iter; 00273 typedef money_get<_CharT, _Iter> _MoneyGet; 00274 00275 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc()); 00276 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl, 00277 __is, __err, __f._M_mon); 00278 } 00279 __catch(__cxxabiv1::__forced_unwind&) 00280 { 00281 __is._M_setstate(ios_base::badbit); 00282 __throw_exception_again; 00283 } 00284 __catch(...) 00285 { __is._M_setstate(ios_base::badbit); } 00286 if (__err) 00287 __is.setstate(__err); 00288 } 00289 return __is; 00290 } 00291 00292 00293 template<typename _MoneyT> 00294 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; }; 00295 00296 /** 00297 * @brief Extended manipulator for inserting money. 00298 * @param __mon Either long double or a specialization of @c basic_string. 00299 * @param __intl A bool indicating whether international format 00300 * is to be used. 00301 * 00302 * Sent to a stream object, this manipulator inserts @a __mon. 00303 */ 00304 template<typename _MoneyT> 00305 inline _Put_money<_MoneyT> 00306 put_money(const _MoneyT& __mon, bool __intl = false) 00307 { return { __mon, __intl }; } 00308 00309 template<typename _CharT, typename _Traits, typename _MoneyT> 00310 basic_ostream<_CharT, _Traits>& 00311 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f) 00312 { 00313 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os); 00314 if (__cerb) 00315 { 00316 ios_base::iostate __err = ios_base::goodbit; 00317 __try 00318 { 00319 typedef ostreambuf_iterator<_CharT, _Traits> _Iter; 00320 typedef money_put<_CharT, _Iter> _MoneyPut; 00321 00322 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc()); 00323 if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os, 00324 __os.fill(), __f._M_mon).failed()) 00325 __err |= ios_base::badbit; 00326 } 00327 __catch(__cxxabiv1::__forced_unwind&) 00328 { 00329 __os._M_setstate(ios_base::badbit); 00330 __throw_exception_again; 00331 } 00332 __catch(...) 00333 { __os._M_setstate(ios_base::badbit); } 00334 if (__err) 00335 __os.setstate(__err); 00336 } 00337 return __os; 00338 } 00339 00340 template<typename _CharT> 00341 struct _Put_time 00342 { 00343 const std::tm* _M_tmb; 00344 const _CharT* _M_fmt; 00345 }; 00346 00347 /** 00348 * @brief Extended manipulator for formatting time. 00349 * 00350 * This manipulator uses time_put::put to format time. 00351 * [ext.manip] 00352 * 00353 * @param __tmb struct tm time data to format. 00354 * @param __fmt format string. 00355 */ 00356 template<typename _CharT> 00357 inline _Put_time<_CharT> 00358 put_time(const std::tm* __tmb, const _CharT* __fmt) 00359 { return { __tmb, __fmt }; } 00360 00361 template<typename _CharT, typename _Traits> 00362 basic_ostream<_CharT, _Traits>& 00363 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f) 00364 { 00365 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os); 00366 if (__cerb) 00367 { 00368 ios_base::iostate __err = ios_base::goodbit; 00369 __try 00370 { 00371 typedef ostreambuf_iterator<_CharT, _Traits> _Iter; 00372 typedef time_put<_CharT, _Iter> _TimePut; 00373 00374 const _CharT* const __fmt_end = __f._M_fmt + 00375 _Traits::length(__f._M_fmt); 00376 00377 const _TimePut& __mp = use_facet<_TimePut>(__os.getloc()); 00378 if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(), 00379 __f._M_tmb, __f._M_fmt, __fmt_end).failed()) 00380 __err |= ios_base::badbit; 00381 } 00382 __catch(__cxxabiv1::__forced_unwind&) 00383 { 00384 __os._M_setstate(ios_base::badbit); 00385 __throw_exception_again; 00386 } 00387 __catch(...) 00388 { __os._M_setstate(ios_base::badbit); } 00389 if (__err) 00390 __os.setstate(__err); 00391 } 00392 return __os; 00393 } 00394 00395 template<typename _CharT> 00396 struct _Get_time 00397 { 00398 std::tm* _M_tmb; 00399 const _CharT* _M_fmt; 00400 }; 00401 00402 /** 00403 * @brief Extended manipulator for extracting time. 00404 * 00405 * This manipulator uses time_get::get to extract time. 00406 * [ext.manip] 00407 * 00408 * @param __tmb struct to extract the time data to. 00409 * @param __fmt format string. 00410 */ 00411 template<typename _CharT> 00412 inline _Get_time<_CharT> 00413 get_time(std::tm* __tmb, const _CharT* __fmt) 00414 { return { __tmb, __fmt }; } 00415 00416 template<typename _CharT, typename _Traits> 00417 basic_istream<_CharT, _Traits>& 00418 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f) 00419 { 00420 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false); 00421 if (__cerb) 00422 { 00423 ios_base::iostate __err = ios_base::goodbit; 00424 __try 00425 { 00426 typedef istreambuf_iterator<_CharT, _Traits> _Iter; 00427 typedef time_get<_CharT, _Iter> _TimeGet; 00428 00429 const _CharT* const __fmt_end = __f._M_fmt + 00430 _Traits::length(__f._M_fmt); 00431 00432 const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc()); 00433 __mg.get(_Iter(__is.rdbuf()), _Iter(), __is, 00434 __err, __f._M_tmb, __f._M_fmt, __fmt_end); 00435 } 00436 __catch(__cxxabiv1::__forced_unwind&) 00437 { 00438 __is._M_setstate(ios_base::badbit); 00439 __throw_exception_again; 00440 } 00441 __catch(...) 00442 { __is._M_setstate(ios_base::badbit); } 00443 if (__err) 00444 __is.setstate(__err); 00445 } 00446 return __is; 00447 } 00448 00449 #if __cplusplus > 201103L 00450 00451 #define __cpp_lib_quoted_string_io 201304 00452 00453 _GLIBCXX_END_NAMESPACE_VERSION 00454 namespace __detail { 00455 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00456 00457 /** 00458 * @brief Struct for delimited strings. 00459 */ 00460 template<typename _String, typename _CharT> 00461 struct _Quoted_string 00462 { 00463 static_assert(is_reference<_String>::value 00464 || is_pointer<_String>::value, 00465 "String type must be pointer or reference"); 00466 00467 _Quoted_string(_String __str, _CharT __del, _CharT __esc) 00468 : _M_string(__str), _M_delim{__del}, _M_escape{__esc} 00469 { } 00470 00471 _Quoted_string& 00472 operator=(_Quoted_string&) = delete; 00473 00474 _String _M_string; 00475 _CharT _M_delim; 00476 _CharT _M_escape; 00477 }; 00478 00479 /** 00480 * @brief Inserter for quoted strings. 00481 * 00482 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00483 * DR 2344 quoted()'s interaction with padding is unclear 00484 */ 00485 template<typename _CharT, typename _Traits> 00486 auto& 00487 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 00488 const _Quoted_string<const _CharT*, _CharT>& __str) 00489 { 00490 std::basic_ostringstream<_CharT, _Traits> __ostr; 00491 __ostr << __str._M_delim; 00492 for (const _CharT* __c = __str._M_string; *__c; ++__c) 00493 { 00494 if (*__c == __str._M_delim || *__c == __str._M_escape) 00495 __ostr << __str._M_escape; 00496 __ostr << *__c; 00497 } 00498 __ostr << __str._M_delim; 00499 00500 return __os << __ostr.str(); 00501 } 00502 00503 /** 00504 * @brief Inserter for quoted strings. 00505 * 00506 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00507 * DR 2344 quoted()'s interaction with padding is unclear 00508 */ 00509 template<typename _CharT, typename _Traits, typename _String> 00510 auto& 00511 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 00512 const _Quoted_string<_String, _CharT>& __str) 00513 { 00514 std::basic_ostringstream<_CharT, _Traits> __ostr; 00515 __ostr << __str._M_delim; 00516 for (auto& __c : __str._M_string) 00517 { 00518 if (__c == __str._M_delim || __c == __str._M_escape) 00519 __ostr << __str._M_escape; 00520 __ostr << __c; 00521 } 00522 __ostr << __str._M_delim; 00523 00524 return __os << __ostr.str(); 00525 } 00526 00527 /** 00528 * @brief Extractor for delimited strings. 00529 * The left and right delimiters can be different. 00530 */ 00531 template<typename _CharT, typename _Traits, typename _Alloc> 00532 auto& 00533 operator>>(std::basic_istream<_CharT, _Traits>& __is, 00534 const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&, 00535 _CharT>& __str) 00536 { 00537 _CharT __c; 00538 __is >> __c; 00539 if (!__is.good()) 00540 return __is; 00541 if (__c != __str._M_delim) 00542 { 00543 __is.unget(); 00544 __is >> __str._M_string; 00545 return __is; 00546 } 00547 __str._M_string.clear(); 00548 std::ios_base::fmtflags __flags 00549 = __is.flags(__is.flags() & ~std::ios_base::skipws); 00550 do 00551 { 00552 __is >> __c; 00553 if (!__is.good()) 00554 break; 00555 if (__c == __str._M_escape) 00556 { 00557 __is >> __c; 00558 if (!__is.good()) 00559 break; 00560 } 00561 else if (__c == __str._M_delim) 00562 break; 00563 __str._M_string += __c; 00564 } 00565 while (true); 00566 __is.setf(__flags); 00567 00568 return __is; 00569 } 00570 _GLIBCXX_END_NAMESPACE_VERSION 00571 } // namespace __detail 00572 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00573 00574 /** 00575 * @brief Manipulator for quoted strings. 00576 * @param __string String to quote. 00577 * @param __delim Character to quote string with. 00578 * @param __escape Escape character to escape itself or quote character. 00579 */ 00580 template<typename _CharT> 00581 inline auto 00582 quoted(const _CharT* __string, 00583 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) 00584 { 00585 return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim, 00586 __escape); 00587 } 00588 00589 template<typename _CharT, typename _Traits, typename _Alloc> 00590 inline auto 00591 quoted(const basic_string<_CharT, _Traits, _Alloc>& __string, 00592 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) 00593 { 00594 return __detail::_Quoted_string< 00595 const basic_string<_CharT, _Traits, _Alloc>&, _CharT>( 00596 __string, __delim, __escape); 00597 } 00598 00599 template<typename _CharT, typename _Traits, typename _Alloc> 00600 inline auto 00601 quoted(basic_string<_CharT, _Traits, _Alloc>& __string, 00602 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) 00603 { 00604 return __detail::_Quoted_string< 00605 basic_string<_CharT, _Traits, _Alloc>&, _CharT>( 00606 __string, __delim, __escape); 00607 } 00608 00609 #endif // __cplusplus > 201103L 00610 00611 #endif // __cplusplus >= 201103L 00612 00613 // Inhibit implicit instantiations for required instantiations, 00614 // which are defined via explicit instantiations elsewhere. 00615 // NB: This syntax is a GNU extension. 00616 #if _GLIBCXX_EXTERN_TEMPLATE 00617 extern template ostream& operator<<(ostream&, _Setfill<char>); 00618 extern template ostream& operator<<(ostream&, _Setiosflags); 00619 extern template ostream& operator<<(ostream&, _Resetiosflags); 00620 extern template ostream& operator<<(ostream&, _Setbase); 00621 extern template ostream& operator<<(ostream&, _Setprecision); 00622 extern template ostream& operator<<(ostream&, _Setw); 00623 extern template istream& operator>>(istream&, _Setfill<char>); 00624 extern template istream& operator>>(istream&, _Setiosflags); 00625 extern template istream& operator>>(istream&, _Resetiosflags); 00626 extern template istream& operator>>(istream&, _Setbase); 00627 extern template istream& operator>>(istream&, _Setprecision); 00628 extern template istream& operator>>(istream&, _Setw); 00629 00630 #ifdef _GLIBCXX_USE_WCHAR_T 00631 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>); 00632 extern template wostream& operator<<(wostream&, _Setiosflags); 00633 extern template wostream& operator<<(wostream&, _Resetiosflags); 00634 extern template wostream& operator<<(wostream&, _Setbase); 00635 extern template wostream& operator<<(wostream&, _Setprecision); 00636 extern template wostream& operator<<(wostream&, _Setw); 00637 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>); 00638 extern template wistream& operator>>(wistream&, _Setiosflags); 00639 extern template wistream& operator>>(wistream&, _Resetiosflags); 00640 extern template wistream& operator>>(wistream&, _Setbase); 00641 extern template wistream& operator>>(wistream&, _Setprecision); 00642 extern template wistream& operator>>(wistream&, _Setw); 00643 #endif 00644 #endif 00645 00646 _GLIBCXX_END_NAMESPACE_VERSION 00647 } // namespace 00648 00649 #endif /* _GLIBCXX_IOMANIP */