libstdc++
iomanip
Go to the documentation of this file.
00001 // Standard stream manipulators -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2014 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 #if __cplusplus > 201103L
00341 
00342 #define __cpp_lib_quoted_string_io 201304
00343 
00344 _GLIBCXX_END_NAMESPACE_VERSION
00345   namespace __detail {
00346   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00347 
00348     /**
00349      * @brief Struct for delimited strings.
00350      */
00351     template<typename _String, typename _CharT>
00352       struct _Quoted_string
00353       {
00354     static_assert(is_reference<_String>::value
00355            || is_pointer<_String>::value,
00356               "String type must be pointer or reference");
00357 
00358     _Quoted_string(_String __str, _CharT __del, _CharT __esc)
00359     : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
00360     { }
00361 
00362     _Quoted_string&
00363     operator=(_Quoted_string&) = delete;
00364 
00365     _String _M_string;
00366     _CharT _M_delim;
00367     _CharT _M_escape;
00368       };
00369 
00370     /**
00371      * @brief Inserter for quoted strings.
00372      *
00373      *  _GLIBCXX_RESOLVE_LIB_DEFECTS
00374      *  DR 2344 quoted()'s interaction with padding is unclear
00375      */
00376     template<typename _CharT, typename _Traits>
00377       auto&
00378       operator<<(std::basic_ostream<_CharT, _Traits>& __os,
00379          const _Quoted_string<const _CharT*, _CharT>& __str)
00380       {
00381     std::basic_ostringstream<_CharT, _Traits> __ostr;
00382     __ostr << __str._M_delim;
00383     for (const _CharT* __c = __str._M_string; *__c; ++__c)
00384       {
00385         if (*__c == __str._M_delim || *__c == __str._M_escape)
00386           __ostr << __str._M_escape;
00387         __ostr << *__c;
00388       }
00389     __ostr << __str._M_delim;
00390 
00391     return __os << __ostr.str();
00392       }
00393 
00394     /**
00395      * @brief Inserter for quoted strings.
00396      *
00397      *  _GLIBCXX_RESOLVE_LIB_DEFECTS
00398      *  DR 2344 quoted()'s interaction with padding is unclear
00399      */
00400     template<typename _CharT, typename _Traits, typename _String>
00401       auto&
00402       operator<<(std::basic_ostream<_CharT, _Traits>& __os,
00403          const _Quoted_string<_String, _CharT>& __str)
00404       {
00405     std::basic_ostringstream<_CharT, _Traits> __ostr;
00406     __ostr << __str._M_delim;
00407     for (auto& __c : __str._M_string)
00408       {
00409         if (__c == __str._M_delim || __c == __str._M_escape)
00410           __ostr << __str._M_escape;
00411         __ostr << __c;
00412       }
00413     __ostr << __str._M_delim;
00414 
00415     return __os << __ostr.str();
00416       }
00417 
00418     /**
00419      * @brief Extractor for delimited strings.
00420      *        The left and right delimiters can be different.
00421      */
00422     template<typename _CharT, typename _Traits, typename _Alloc>
00423       auto&
00424       operator>>(std::basic_istream<_CharT, _Traits>& __is,
00425          const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&,
00426                       _CharT>& __str)
00427       {
00428     _CharT __c;
00429     __is >> __c;
00430     if (!__is.good())
00431       return __is;
00432     if (__c != __str._M_delim)
00433       {
00434         __is.unget();
00435         __is >> __str._M_string;
00436         return __is;
00437       }
00438     __str._M_string.clear();
00439     std::ios_base::fmtflags __flags
00440       = __is.flags(__is.flags() & ~std::ios_base::skipws);
00441     do
00442       {
00443         __is >> __c;
00444         if (!__is.good())
00445           break;
00446         if (__c == __str._M_escape)
00447           {
00448         __is >> __c;
00449         if (!__is.good())
00450           break;
00451           }
00452         else if (__c == __str._M_delim)
00453           break;
00454         __str._M_string += __c;
00455       }
00456     while (true);
00457     __is.setf(__flags);
00458 
00459     return __is;
00460       }
00461   _GLIBCXX_END_NAMESPACE_VERSION
00462   } // namespace __detail
00463 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00464 
00465   /**
00466    * @brief Manipulator for quoted strings.
00467    * @param __str    String to quote.
00468    * @param __delim  Character to quote string with.
00469    * @param __escape Escape character to escape itself or quote character.
00470    */
00471   template<typename _CharT>
00472     inline auto
00473     quoted(const _CharT* __string,
00474        _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00475     {
00476       return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
00477                                  __escape);
00478     }
00479 
00480   template<typename _CharT, typename _Traits, typename _Alloc>
00481     inline auto
00482     quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
00483        _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00484     {
00485       return __detail::_Quoted_string<
00486             const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
00487                 __string, __delim, __escape);
00488     }
00489 
00490   template<typename _CharT, typename _Traits, typename _Alloc>
00491     inline auto
00492     quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
00493        _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00494     {
00495       return __detail::_Quoted_string<
00496             basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
00497                 __string, __delim, __escape);
00498     }
00499 
00500 #endif // __cplusplus > 201103L
00501 
00502 #endif // __cplusplus >= 201103L
00503 
00504   // Inhibit implicit instantiations for required instantiations,
00505   // which are defined via explicit instantiations elsewhere.  
00506   // NB:  This syntax is a GNU extension.
00507 #if _GLIBCXX_EXTERN_TEMPLATE
00508   extern template ostream& operator<<(ostream&, _Setfill<char>);
00509   extern template ostream& operator<<(ostream&, _Setiosflags);
00510   extern template ostream& operator<<(ostream&, _Resetiosflags);
00511   extern template ostream& operator<<(ostream&, _Setbase);
00512   extern template ostream& operator<<(ostream&, _Setprecision);
00513   extern template ostream& operator<<(ostream&, _Setw);
00514   extern template istream& operator>>(istream&, _Setfill<char>);
00515   extern template istream& operator>>(istream&, _Setiosflags);
00516   extern template istream& operator>>(istream&, _Resetiosflags);
00517   extern template istream& operator>>(istream&, _Setbase);
00518   extern template istream& operator>>(istream&, _Setprecision);
00519   extern template istream& operator>>(istream&, _Setw);
00520 
00521 #ifdef _GLIBCXX_USE_WCHAR_T
00522   extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
00523   extern template wostream& operator<<(wostream&, _Setiosflags);
00524   extern template wostream& operator<<(wostream&, _Resetiosflags);
00525   extern template wostream& operator<<(wostream&, _Setbase);
00526   extern template wostream& operator<<(wostream&, _Setprecision);
00527   extern template wostream& operator<<(wostream&, _Setw);
00528   extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
00529   extern template wistream& operator>>(wistream&, _Setiosflags);
00530   extern template wistream& operator>>(wistream&, _Resetiosflags);
00531   extern template wistream& operator>>(wistream&, _Setbase);
00532   extern template wistream& operator>>(wistream&, _Setprecision);
00533   extern template wistream& operator>>(wistream&, _Setw);
00534 #endif
00535 #endif
00536 
00537 _GLIBCXX_END_NAMESPACE_VERSION
00538 } // namespace
00539 
00540 #endif /* _GLIBCXX_IOMANIP */