1// Standard stream manipulators -*- C++ -*-
3// Copyright (C) 1997-2023 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/iomanip
26 * This is a Standard C++ Library header.
30// ISO C++ 14882: 27.6.3 Standard manipulators
33#ifndef _GLIBCXX_IOMANIP
34#define _GLIBCXX_IOMANIP 1
36#pragma GCC system_header
38#include <bits/requires_hosted.h> // iostreams
40#include <bits/c++config.h>
42#include <bits/ios_base.h>
44#if __cplusplus >= 201103L
46#if __cplusplus > 201103L
47#include <bits/quoted_string.h>
51namespace std _GLIBCXX_VISIBILITY(default)
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
55 // [27.6.3] standard manipulators
58 struct _Resetiosflags { ios_base::fmtflags _M_mask; };
61 * @brief Manipulator for @c setf.
62 * @param __mask A format flags mask.
64 * Sent to a stream object, this manipulator resets the specified flags,
65 * via @e stream.setf(0,__mask).
68 resetiosflags(ios_base::fmtflags __mask)
69 { return { __mask }; }
71 template<typename _CharT, typename _Traits>
72 inline basic_istream<_CharT, _Traits>&
73 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
75 __is.setf(ios_base::fmtflags(0), __f._M_mask);
79 template<typename _CharT, typename _Traits>
80 inline basic_ostream<_CharT, _Traits>&
81 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
83 __os.setf(ios_base::fmtflags(0), __f._M_mask);
88 struct _Setiosflags { ios_base::fmtflags _M_mask; };
91 * @brief Manipulator for @c setf.
92 * @param __mask A format flags mask.
94 * Sent to a stream object, this manipulator sets the format flags
98 setiosflags(ios_base::fmtflags __mask)
99 { return { __mask }; }
101 template<typename _CharT, typename _Traits>
102 inline basic_istream<_CharT, _Traits>&
103 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
105 __is.setf(__f._M_mask);
109 template<typename _CharT, typename _Traits>
110 inline basic_ostream<_CharT, _Traits>&
111 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
113 __os.setf(__f._M_mask);
118 struct _Setbase { int _M_base; };
121 * @brief Manipulator for @c setf.
122 * @param __base A numeric base.
124 * Sent to a stream object, this manipulator changes the
125 * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
126 * is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
130 { return { __base }; }
132 template<typename _CharT, typename _Traits>
133 inline basic_istream<_CharT, _Traits>&
134 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
136 __is.setf(__f._M_base == 8 ? ios_base::oct :
137 __f._M_base == 10 ? ios_base::dec :
138 __f._M_base == 16 ? ios_base::hex :
139 ios_base::fmtflags(0), ios_base::basefield);
143 template<typename _CharT, typename _Traits>
144 inline basic_ostream<_CharT, _Traits>&
145 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
147 __os.setf(__f._M_base == 8 ? ios_base::oct :
148 __f._M_base == 10 ? ios_base::dec :
149 __f._M_base == 16 ? ios_base::hex :
150 ios_base::fmtflags(0), ios_base::basefield);
155 template<typename _CharT>
156 struct _Setfill { _CharT _M_c; };
159 * @brief Manipulator for @c fill.
160 * @param __c The new fill character.
162 * Sent to a stream object, this manipulator calls @c fill(__c) for that
165 template<typename _CharT>
166 inline _Setfill<_CharT>
170 template<typename _CharT, typename _Traits>
171 inline basic_istream<_CharT, _Traits>&
172 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
178 template<typename _CharT, typename _Traits>
179 inline basic_ostream<_CharT, _Traits>&
180 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
187 struct _Setprecision { int _M_n; };
190 * @brief Manipulator for @c precision.
191 * @param __n The new precision.
193 * Sent to a stream object, this manipulator calls @c precision(__n) for
197 setprecision(int __n)
200 template<typename _CharT, typename _Traits>
201 inline basic_istream<_CharT, _Traits>&
202 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
204 __is.precision(__f._M_n);
208 template<typename _CharT, typename _Traits>
209 inline basic_ostream<_CharT, _Traits>&
210 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
212 __os.precision(__f._M_n);
217 struct _Setw { int _M_n; };
220 * @brief Manipulator for @c width.
221 * @param __n The new width.
223 * Sent to a stream object, this manipulator calls @c width(__n) for
230 template<typename _CharT, typename _Traits>
231 inline basic_istream<_CharT, _Traits>&
232 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
234 __is.width(__f._M_n);
238 template<typename _CharT, typename _Traits>
239 inline basic_ostream<_CharT, _Traits>&
240 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
242 __os.width(__f._M_n);
246#if __cplusplus >= 201103L
248 template<typename _MoneyT>
249 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
252 * @brief Extended manipulator for extracting money.
253 * @param __mon Either long double or a specialization of @c basic_string.
254 * @param __intl A bool indicating whether international format
257 * Sent to a stream object, this manipulator extracts @a __mon.
259 template<typename _MoneyT>
260 inline _Get_money<_MoneyT>
261 get_money(_MoneyT& __mon, bool __intl = false)
262 { return { __mon, __intl }; }
264 template<typename _CharT, typename _Traits, typename _MoneyT>
265 basic_istream<_CharT, _Traits>&
266 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
268 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
271 ios_base::iostate __err = ios_base::goodbit;
274 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
275 typedef money_get<_CharT, _Iter> _MoneyGet;
277 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
278 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
279 __is, __err, __f._M_mon);
281 __catch(__cxxabiv1::__forced_unwind&)
283 __is._M_setstate(ios_base::badbit);
284 __throw_exception_again;
287 { __is._M_setstate(ios_base::badbit); }
289 __is.setstate(__err);
295 template<typename _MoneyT>
296 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
299 * @brief Extended manipulator for inserting money.
300 * @param __mon Either long double or a specialization of @c basic_string.
301 * @param __intl A bool indicating whether international format
304 * Sent to a stream object, this manipulator inserts @a __mon.
306 template<typename _MoneyT>
307 inline _Put_money<_MoneyT>
308 put_money(const _MoneyT& __mon, bool __intl = false)
309 { return { __mon, __intl }; }
311 template<typename _CharT, typename _Traits, typename _MoneyT>
312 basic_ostream<_CharT, _Traits>&
313 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
315 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
318 ios_base::iostate __err = ios_base::goodbit;
321 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
322 typedef money_put<_CharT, _Iter> _MoneyPut;
324 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
325 if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
326 __os.fill(), __f._M_mon).failed())
327 __err |= ios_base::badbit;
329 __catch(__cxxabiv1::__forced_unwind&)
331 __os._M_setstate(ios_base::badbit);
332 __throw_exception_again;
335 { __os._M_setstate(ios_base::badbit); }
337 __os.setstate(__err);
342 template<typename _CharT>
345 const std::tm* _M_tmb;
346 const _CharT* _M_fmt;
350 * @brief Extended manipulator for formatting time.
352 * This manipulator uses time_put::put to format time.
355 * @param __tmb struct tm time data to format.
356 * @param __fmt format string.
358 template<typename _CharT>
359 inline _Put_time<_CharT>
360 put_time(const std::tm* __tmb, const _CharT* __fmt)
361 { return { __tmb, __fmt }; }
363 template<typename _CharT, typename _Traits>
364 basic_ostream<_CharT, _Traits>&
365 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
367 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
370 ios_base::iostate __err = ios_base::goodbit;
373 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
374 typedef time_put<_CharT, _Iter> _TimePut;
376 const _CharT* const __fmt_end = __f._M_fmt +
377 _Traits::length(__f._M_fmt);
379 const _TimePut& __mp = use_facet<_TimePut>(__os.getloc());
380 if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(),
381 __f._M_tmb, __f._M_fmt, __fmt_end).failed())
382 __err |= ios_base::badbit;
384 __catch(__cxxabiv1::__forced_unwind&)
386 __os._M_setstate(ios_base::badbit);
387 __throw_exception_again;
390 { __os._M_setstate(ios_base::badbit); }
392 __os.setstate(__err);
397 template<typename _CharT>
401 const _CharT* _M_fmt;
405 * @brief Extended manipulator for extracting time.
407 * This manipulator uses time_get::get to extract time.
410 * @param __tmb struct to extract the time data to.
411 * @param __fmt format string.
413 template<typename _CharT>
414 inline _Get_time<_CharT>
415 get_time(std::tm* __tmb, const _CharT* __fmt)
416 { return { __tmb, __fmt }; }
418 template<typename _CharT, typename _Traits>
419 basic_istream<_CharT, _Traits>&
420 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
422 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
425 ios_base::iostate __err = ios_base::goodbit;
428 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
429 typedef time_get<_CharT, _Iter> _TimeGet;
431 const _CharT* const __fmt_end = __f._M_fmt +
432 _Traits::length(__f._M_fmt);
434 const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc());
435 __mg.get(_Iter(__is.rdbuf()), _Iter(), __is,
436 __err, __f._M_tmb, __f._M_fmt, __fmt_end);
438 __catch(__cxxabiv1::__forced_unwind&)
440 __is._M_setstate(ios_base::badbit);
441 __throw_exception_again;
444 { __is._M_setstate(ios_base::badbit); }
446 __is.setstate(__err);
451#if __cplusplus >= 201402L
453#define __cpp_lib_quoted_string_io 201304L
456 * @brief Manipulator for quoted strings.
457 * @param __string String to quote.
458 * @param __delim Character to quote string with.
459 * @param __escape Escape character to escape itself or quote character.
461 template<typename _CharT>
463 quoted(const _CharT* __string,
464 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
466 return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
470 template<typename _CharT, typename _Traits, typename _Alloc>
472 quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
473 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
475 return __detail::_Quoted_string<
476 const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
477 __string, __delim, __escape);
480 template<typename _CharT, typename _Traits, typename _Alloc>
482 quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
483 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
485 return __detail::_Quoted_string<
486 basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
487 __string, __delim, __escape);
490#if __cplusplus >= 201703L
491 // _GLIBCXX_RESOLVE_LIB_DEFECTS
492 // 2785. quoted should work with basic_string_view
493 template<typename _CharT, typename _Traits>
495 quoted(basic_string_view<_CharT, _Traits> __sv,
496 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
498 return __detail::_Quoted_string<
499 basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
504#endif // __cplusplus >= 201103L
506 // Inhibit implicit instantiations for required instantiations,
507 // which are defined via explicit instantiations elsewhere.
508 // NB: This syntax is a GNU extension.
509#if _GLIBCXX_EXTERN_TEMPLATE
510 extern template ostream& operator<<(ostream&, _Setfill<char>);
511 extern template ostream& operator<<(ostream&, _Setiosflags);
512 extern template ostream& operator<<(ostream&, _Resetiosflags);
513 extern template ostream& operator<<(ostream&, _Setbase);
514 extern template ostream& operator<<(ostream&, _Setprecision);
515 extern template ostream& operator<<(ostream&, _Setw);
516 extern template istream& operator>>(istream&, _Setfill<char>);
517 extern template istream& operator>>(istream&, _Setiosflags);
518 extern template istream& operator>>(istream&, _Resetiosflags);
519 extern template istream& operator>>(istream&, _Setbase);
520 extern template istream& operator>>(istream&, _Setprecision);
521 extern template istream& operator>>(istream&, _Setw);
523#ifdef _GLIBCXX_USE_WCHAR_T
524 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
525 extern template wostream& operator<<(wostream&, _Setiosflags);
526 extern template wostream& operator<<(wostream&, _Resetiosflags);
527 extern template wostream& operator<<(wostream&, _Setbase);
528 extern template wostream& operator<<(wostream&, _Setprecision);
529 extern template wostream& operator<<(wostream&, _Setw);
530 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
531 extern template wistream& operator>>(wistream&, _Setiosflags);
532 extern template wistream& operator>>(wistream&, _Resetiosflags);
533 extern template wistream& operator>>(wistream&, _Setbase);
534 extern template wistream& operator>>(wistream&, _Setprecision);
535 extern template wistream& operator>>(wistream&, _Setw);
539_GLIBCXX_END_NAMESPACE_VERSION
542#endif /* _GLIBCXX_IOMANIP */