1// Standard stream manipulators -*- C++ -*-
3// Copyright (C) 1997-2022 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/c++config.h>
40#include <bits/ios_base.h>
42#if __cplusplus >= 201103L
44#if __cplusplus > 201103L
45#include <bits/quoted_string.h>
49namespace std _GLIBCXX_VISIBILITY(default)
51_GLIBCXX_BEGIN_NAMESPACE_VERSION
53 // [27.6.3] standard manipulators
56 struct _Resetiosflags { ios_base::fmtflags _M_mask; };
59 * @brief Manipulator for @c setf.
60 * @param __mask A format flags mask.
62 * Sent to a stream object, this manipulator resets the specified flags,
63 * via @e stream.setf(0,__mask).
66 resetiosflags(ios_base::fmtflags __mask)
67 { return { __mask }; }
69 template<typename _CharT, typename _Traits>
70 inline basic_istream<_CharT, _Traits>&
71 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
73 __is.setf(ios_base::fmtflags(0), __f._M_mask);
77 template<typename _CharT, typename _Traits>
78 inline basic_ostream<_CharT, _Traits>&
79 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
81 __os.setf(ios_base::fmtflags(0), __f._M_mask);
86 struct _Setiosflags { ios_base::fmtflags _M_mask; };
89 * @brief Manipulator for @c setf.
90 * @param __mask A format flags mask.
92 * Sent to a stream object, this manipulator sets the format flags
96 setiosflags(ios_base::fmtflags __mask)
97 { return { __mask }; }
99 template<typename _CharT, typename _Traits>
100 inline basic_istream<_CharT, _Traits>&
101 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
103 __is.setf(__f._M_mask);
107 template<typename _CharT, typename _Traits>
108 inline basic_ostream<_CharT, _Traits>&
109 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
111 __os.setf(__f._M_mask);
116 struct _Setbase { int _M_base; };
119 * @brief Manipulator for @c setf.
120 * @param __base A numeric base.
122 * Sent to a stream object, this manipulator changes the
123 * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
124 * is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
128 { return { __base }; }
130 template<typename _CharT, typename _Traits>
131 inline basic_istream<_CharT, _Traits>&
132 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
134 __is.setf(__f._M_base == 8 ? ios_base::oct :
135 __f._M_base == 10 ? ios_base::dec :
136 __f._M_base == 16 ? ios_base::hex :
137 ios_base::fmtflags(0), ios_base::basefield);
141 template<typename _CharT, typename _Traits>
142 inline basic_ostream<_CharT, _Traits>&
143 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
145 __os.setf(__f._M_base == 8 ? ios_base::oct :
146 __f._M_base == 10 ? ios_base::dec :
147 __f._M_base == 16 ? ios_base::hex :
148 ios_base::fmtflags(0), ios_base::basefield);
153 template<typename _CharT>
154 struct _Setfill { _CharT _M_c; };
157 * @brief Manipulator for @c fill.
158 * @param __c The new fill character.
160 * Sent to a stream object, this manipulator calls @c fill(__c) for that
163 template<typename _CharT>
164 inline _Setfill<_CharT>
168 template<typename _CharT, typename _Traits>
169 inline basic_istream<_CharT, _Traits>&
170 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
176 template<typename _CharT, typename _Traits>
177 inline basic_ostream<_CharT, _Traits>&
178 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
185 struct _Setprecision { int _M_n; };
188 * @brief Manipulator for @c precision.
189 * @param __n The new precision.
191 * Sent to a stream object, this manipulator calls @c precision(__n) for
195 setprecision(int __n)
198 template<typename _CharT, typename _Traits>
199 inline basic_istream<_CharT, _Traits>&
200 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
202 __is.precision(__f._M_n);
206 template<typename _CharT, typename _Traits>
207 inline basic_ostream<_CharT, _Traits>&
208 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
210 __os.precision(__f._M_n);
215 struct _Setw { int _M_n; };
218 * @brief Manipulator for @c width.
219 * @param __n The new width.
221 * Sent to a stream object, this manipulator calls @c width(__n) for
228 template<typename _CharT, typename _Traits>
229 inline basic_istream<_CharT, _Traits>&
230 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
232 __is.width(__f._M_n);
236 template<typename _CharT, typename _Traits>
237 inline basic_ostream<_CharT, _Traits>&
238 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
240 __os.width(__f._M_n);
244#if __cplusplus >= 201103L
246 template<typename _MoneyT>
247 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
250 * @brief Extended manipulator for extracting money.
251 * @param __mon Either long double or a specialization of @c basic_string.
252 * @param __intl A bool indicating whether international format
255 * Sent to a stream object, this manipulator extracts @a __mon.
257 template<typename _MoneyT>
258 inline _Get_money<_MoneyT>
259 get_money(_MoneyT& __mon, bool __intl = false)
260 { return { __mon, __intl }; }
262 template<typename _CharT, typename _Traits, typename _MoneyT>
263 basic_istream<_CharT, _Traits>&
264 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
266 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
269 ios_base::iostate __err = ios_base::goodbit;
272 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
273 typedef money_get<_CharT, _Iter> _MoneyGet;
275 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
276 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
277 __is, __err, __f._M_mon);
279 __catch(__cxxabiv1::__forced_unwind&)
281 __is._M_setstate(ios_base::badbit);
282 __throw_exception_again;
285 { __is._M_setstate(ios_base::badbit); }
287 __is.setstate(__err);
293 template<typename _MoneyT>
294 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
297 * @brief Extended manipulator for inserting money.
298 * @param __mon Either long double or a specialization of @c basic_string.
299 * @param __intl A bool indicating whether international format
302 * Sent to a stream object, this manipulator inserts @a __mon.
304 template<typename _MoneyT>
305 inline _Put_money<_MoneyT>
306 put_money(const _MoneyT& __mon, bool __intl = false)
307 { return { __mon, __intl }; }
309 template<typename _CharT, typename _Traits, typename _MoneyT>
310 basic_ostream<_CharT, _Traits>&
311 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
313 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
316 ios_base::iostate __err = ios_base::goodbit;
319 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
320 typedef money_put<_CharT, _Iter> _MoneyPut;
322 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
323 if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
324 __os.fill(), __f._M_mon).failed())
325 __err |= ios_base::badbit;
327 __catch(__cxxabiv1::__forced_unwind&)
329 __os._M_setstate(ios_base::badbit);
330 __throw_exception_again;
333 { __os._M_setstate(ios_base::badbit); }
335 __os.setstate(__err);
340 template<typename _CharT>
343 const std::tm* _M_tmb;
344 const _CharT* _M_fmt;
348 * @brief Extended manipulator for formatting time.
350 * This manipulator uses time_put::put to format time.
353 * @param __tmb struct tm time data to format.
354 * @param __fmt format string.
356 template<typename _CharT>
357 inline _Put_time<_CharT>
358 put_time(const std::tm* __tmb, const _CharT* __fmt)
359 { return { __tmb, __fmt }; }
361 template<typename _CharT, typename _Traits>
362 basic_ostream<_CharT, _Traits>&
363 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
365 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
368 ios_base::iostate __err = ios_base::goodbit;
371 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
372 typedef time_put<_CharT, _Iter> _TimePut;
374 const _CharT* const __fmt_end = __f._M_fmt +
375 _Traits::length(__f._M_fmt);
377 const _TimePut& __mp = use_facet<_TimePut>(__os.getloc());
378 if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(),
379 __f._M_tmb, __f._M_fmt, __fmt_end).failed())
380 __err |= ios_base::badbit;
382 __catch(__cxxabiv1::__forced_unwind&)
384 __os._M_setstate(ios_base::badbit);
385 __throw_exception_again;
388 { __os._M_setstate(ios_base::badbit); }
390 __os.setstate(__err);
395 template<typename _CharT>
399 const _CharT* _M_fmt;
403 * @brief Extended manipulator for extracting time.
405 * This manipulator uses time_get::get to extract time.
408 * @param __tmb struct to extract the time data to.
409 * @param __fmt format string.
411 template<typename _CharT>
412 inline _Get_time<_CharT>
413 get_time(std::tm* __tmb, const _CharT* __fmt)
414 { return { __tmb, __fmt }; }
416 template<typename _CharT, typename _Traits>
417 basic_istream<_CharT, _Traits>&
418 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
420 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
423 ios_base::iostate __err = ios_base::goodbit;
426 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
427 typedef time_get<_CharT, _Iter> _TimeGet;
429 const _CharT* const __fmt_end = __f._M_fmt +
430 _Traits::length(__f._M_fmt);
432 const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc());
433 __mg.get(_Iter(__is.rdbuf()), _Iter(), __is,
434 __err, __f._M_tmb, __f._M_fmt, __fmt_end);
436 __catch(__cxxabiv1::__forced_unwind&)
438 __is._M_setstate(ios_base::badbit);
439 __throw_exception_again;
442 { __is._M_setstate(ios_base::badbit); }
444 __is.setstate(__err);
449#if __cplusplus >= 201402L
451#define __cpp_lib_quoted_string_io 201304L
454 * @brief Manipulator for quoted strings.
455 * @param __string String to quote.
456 * @param __delim Character to quote string with.
457 * @param __escape Escape character to escape itself or quote character.
459 template<typename _CharT>
461 quoted(const _CharT* __string,
462 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
464 return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
468 template<typename _CharT, typename _Traits, typename _Alloc>
470 quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
471 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
473 return __detail::_Quoted_string<
474 const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
475 __string, __delim, __escape);
478 template<typename _CharT, typename _Traits, typename _Alloc>
480 quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
481 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
483 return __detail::_Quoted_string<
484 basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
485 __string, __delim, __escape);
488#if __cplusplus >= 201703L
489 // _GLIBCXX_RESOLVE_LIB_DEFECTS
490 // 2785. quoted should work with basic_string_view
491 template<typename _CharT, typename _Traits>
493 quoted(basic_string_view<_CharT, _Traits> __sv,
494 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
496 return __detail::_Quoted_string<
497 basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
502#endif // __cplusplus >= 201103L
504 // Inhibit implicit instantiations for required instantiations,
505 // which are defined via explicit instantiations elsewhere.
506 // NB: This syntax is a GNU extension.
507#if _GLIBCXX_EXTERN_TEMPLATE
508 extern template ostream& operator<<(ostream&, _Setfill<char>);
509 extern template ostream& operator<<(ostream&, _Setiosflags);
510 extern template ostream& operator<<(ostream&, _Resetiosflags);
511 extern template ostream& operator<<(ostream&, _Setbase);
512 extern template ostream& operator<<(ostream&, _Setprecision);
513 extern template ostream& operator<<(ostream&, _Setw);
514 extern template istream& operator>>(istream&, _Setfill<char>);
515 extern template istream& operator>>(istream&, _Setiosflags);
516 extern template istream& operator>>(istream&, _Resetiosflags);
517 extern template istream& operator>>(istream&, _Setbase);
518 extern template istream& operator>>(istream&, _Setprecision);
519 extern template istream& operator>>(istream&, _Setw);
521#ifdef _GLIBCXX_USE_WCHAR_T
522 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
523 extern template wostream& operator<<(wostream&, _Setiosflags);
524 extern template wostream& operator<<(wostream&, _Resetiosflags);
525 extern template wostream& operator<<(wostream&, _Setbase);
526 extern template wostream& operator<<(wostream&, _Setprecision);
527 extern template wostream& operator<<(wostream&, _Setw);
528 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
529 extern template wistream& operator>>(wistream&, _Setiosflags);
530 extern template wistream& operator>>(wistream&, _Resetiosflags);
531 extern template wistream& operator>>(wistream&, _Setbase);
532 extern template wistream& operator>>(wistream&, _Setprecision);
533 extern template wistream& operator>>(wistream&, _Setw);
537_GLIBCXX_END_NAMESPACE_VERSION
540#endif /* _GLIBCXX_IOMANIP */