1 // Standard stream manipulators -*- C++ -*-
3 // Copyright (C) 1997-2024 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 #define __glibcxx_want_quoted_string_io
45 #include <bits/version.h>
47 #if __cplusplus >= 201103L
49 #if __cplusplus > 201103L
50 #include <bits/quoted_string.h>
54 namespace std _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 // [27.6.3] standard manipulators
61 struct _Resetiosflags { ios_base::fmtflags _M_mask; };
64 * @brief Manipulator for @c setf.
65 * @param __mask A format flags mask.
67 * Sent to a stream object, this manipulator resets the specified flags,
68 * via @e stream.setf(0,__mask).
71 resetiosflags(ios_base::fmtflags __mask)
72 { return { __mask }; }
74 template<typename _CharT, typename _Traits>
75 inline basic_istream<_CharT, _Traits>&
76 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
78 __is.setf(ios_base::fmtflags(0), __f._M_mask);
82 template<typename _CharT, typename _Traits>
83 inline basic_ostream<_CharT, _Traits>&
84 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
86 __os.setf(ios_base::fmtflags(0), __f._M_mask);
91 struct _Setiosflags { ios_base::fmtflags _M_mask; };
94 * @brief Manipulator for @c setf.
95 * @param __mask A format flags mask.
97 * Sent to a stream object, this manipulator sets the format flags
101 setiosflags(ios_base::fmtflags __mask)
102 { return { __mask }; }
104 template<typename _CharT, typename _Traits>
105 inline basic_istream<_CharT, _Traits>&
106 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
108 __is.setf(__f._M_mask);
112 template<typename _CharT, typename _Traits>
113 inline basic_ostream<_CharT, _Traits>&
114 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
116 __os.setf(__f._M_mask);
121 struct _Setbase { int _M_base; };
124 * @brief Manipulator for @c setf.
125 * @param __base A numeric base.
127 * Sent to a stream object, this manipulator changes the
128 * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
129 * is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
133 { return { __base }; }
135 template<typename _CharT, typename _Traits>
136 inline basic_istream<_CharT, _Traits>&
137 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
139 __is.setf(__f._M_base == 8 ? ios_base::oct :
140 __f._M_base == 10 ? ios_base::dec :
141 __f._M_base == 16 ? ios_base::hex :
142 ios_base::fmtflags(0), ios_base::basefield);
146 template<typename _CharT, typename _Traits>
147 inline basic_ostream<_CharT, _Traits>&
148 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
150 __os.setf(__f._M_base == 8 ? ios_base::oct :
151 __f._M_base == 10 ? ios_base::dec :
152 __f._M_base == 16 ? ios_base::hex :
153 ios_base::fmtflags(0), ios_base::basefield);
158 template<typename _CharT>
159 struct _Setfill { _CharT _M_c; };
162 * @brief Manipulator for @c fill.
163 * @param __c The new fill character.
165 * Sent to a stream object, this manipulator calls @c fill(__c) for that
168 template<typename _CharT>
169 inline _Setfill<_CharT>
173 template<typename _CharT, typename _Traits>
174 __attribute__((__deprecated__("'std::setfill' should only be used with "
176 inline basic_istream<_CharT, _Traits>&
177 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
183 template<typename _CharT, typename _Traits>
184 inline basic_ostream<_CharT, _Traits>&
185 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
192 struct _Setprecision { int _M_n; };
195 * @brief Manipulator for @c precision.
196 * @param __n The new precision.
198 * Sent to a stream object, this manipulator calls @c precision(__n) for
202 setprecision(int __n)
205 template<typename _CharT, typename _Traits>
206 inline basic_istream<_CharT, _Traits>&
207 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
209 __is.precision(__f._M_n);
213 template<typename _CharT, typename _Traits>
214 inline basic_ostream<_CharT, _Traits>&
215 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
217 __os.precision(__f._M_n);
222 struct _Setw { int _M_n; };
225 * @brief Manipulator for @c width.
226 * @param __n The new width.
228 * Sent to a stream object, this manipulator calls @c width(__n) for
235 template<typename _CharT, typename _Traits>
236 inline basic_istream<_CharT, _Traits>&
237 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
239 __is.width(__f._M_n);
243 template<typename _CharT, typename _Traits>
244 inline basic_ostream<_CharT, _Traits>&
245 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
247 __os.width(__f._M_n);
251 #if __cplusplus >= 201103L
253 template<typename _MoneyT>
254 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
257 * @brief Extended manipulator for extracting money.
258 * @param __mon Either long double or a specialization of @c basic_string.
259 * @param __intl A bool indicating whether international format
262 * Sent to a stream object, this manipulator extracts @a __mon.
264 template<typename _MoneyT>
265 inline _Get_money<_MoneyT>
266 get_money(_MoneyT& __mon, bool __intl = false)
267 { return { __mon, __intl }; }
269 template<typename _CharT, typename _Traits, typename _MoneyT>
270 basic_istream<_CharT, _Traits>&
271 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
273 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
276 ios_base::iostate __err = ios_base::goodbit;
279 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
280 typedef money_get<_CharT, _Iter> _MoneyGet;
282 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
283 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
284 __is, __err, __f._M_mon);
286 __catch(__cxxabiv1::__forced_unwind&)
288 __is._M_setstate(ios_base::badbit);
289 __throw_exception_again;
292 { __is._M_setstate(ios_base::badbit); }
294 __is.setstate(__err);
300 template<typename _MoneyT>
301 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
304 * @brief Extended manipulator for inserting money.
305 * @param __mon Either long double or a specialization of @c basic_string.
306 * @param __intl A bool indicating whether international format
309 * Sent to a stream object, this manipulator inserts @a __mon.
311 template<typename _MoneyT>
312 inline _Put_money<_MoneyT>
313 put_money(const _MoneyT& __mon, bool __intl = false)
314 { return { __mon, __intl }; }
316 template<typename _CharT, typename _Traits, typename _MoneyT>
317 basic_ostream<_CharT, _Traits>&
318 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
320 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
323 ios_base::iostate __err = ios_base::goodbit;
326 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
327 typedef money_put<_CharT, _Iter> _MoneyPut;
329 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
330 if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
331 __os.fill(), __f._M_mon).failed())
332 __err |= ios_base::badbit;
334 __catch(__cxxabiv1::__forced_unwind&)
336 __os._M_setstate(ios_base::badbit);
337 __throw_exception_again;
340 { __os._M_setstate(ios_base::badbit); }
342 __os.setstate(__err);
347 template<typename _CharT>
350 const std::tm* _M_tmb;
351 const _CharT* _M_fmt;
355 * @brief Extended manipulator for formatting time.
357 * This manipulator uses time_put::put to format time.
360 * @param __tmb struct tm time data to format.
361 * @param __fmt format string.
363 template<typename _CharT>
364 inline _Put_time<_CharT>
365 put_time(const std::tm* __tmb, const _CharT* __fmt)
366 { return { __tmb, __fmt }; }
368 template<typename _CharT, typename _Traits>
369 basic_ostream<_CharT, _Traits>&
370 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
372 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
375 ios_base::iostate __err = ios_base::goodbit;
378 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
379 typedef time_put<_CharT, _Iter> _TimePut;
381 const _CharT* const __fmt_end = __f._M_fmt +
382 _Traits::length(__f._M_fmt);
384 const _TimePut& __mp = use_facet<_TimePut>(__os.getloc());
385 if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(),
386 __f._M_tmb, __f._M_fmt, __fmt_end).failed())
387 __err |= ios_base::badbit;
389 __catch(__cxxabiv1::__forced_unwind&)
391 __os._M_setstate(ios_base::badbit);
392 __throw_exception_again;
395 { __os._M_setstate(ios_base::badbit); }
397 __os.setstate(__err);
402 template<typename _CharT>
406 const _CharT* _M_fmt;
410 * @brief Extended manipulator for extracting time.
412 * This manipulator uses time_get::get to extract time.
415 * @param __tmb struct to extract the time data to.
416 * @param __fmt format string.
418 template<typename _CharT>
419 inline _Get_time<_CharT>
420 get_time(std::tm* __tmb, const _CharT* __fmt)
421 { return { __tmb, __fmt }; }
423 template<typename _CharT, typename _Traits>
424 basic_istream<_CharT, _Traits>&
425 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
427 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
430 ios_base::iostate __err = ios_base::goodbit;
433 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
434 typedef time_get<_CharT, _Iter> _TimeGet;
436 const _CharT* const __fmt_end = __f._M_fmt +
437 _Traits::length(__f._M_fmt);
439 const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc());
440 __mg.get(_Iter(__is.rdbuf()), _Iter(), __is,
441 __err, __f._M_tmb, __f._M_fmt, __fmt_end);
443 __catch(__cxxabiv1::__forced_unwind&)
445 __is._M_setstate(ios_base::badbit);
446 __throw_exception_again;
449 { __is._M_setstate(ios_base::badbit); }
451 __is.setstate(__err);
456 #ifdef __cpp_lib_quoted_string_io // C++ >= 14 && HOSTED
459 * @brief Manipulator for quoted strings.
460 * @param __string String to quote.
461 * @param __delim Character to quote string with.
462 * @param __escape Escape character to escape itself or quote character.
465 template<typename _CharT>
467 quoted(const _CharT* __string,
468 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
470 return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
474 template<typename _CharT, typename _Traits, typename _Alloc>
476 quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
477 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
479 return __detail::_Quoted_string<
480 const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
481 __string, __delim, __escape);
484 template<typename _CharT, typename _Traits, typename _Alloc>
486 quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
487 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
489 return __detail::_Quoted_string<
490 basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
491 __string, __delim, __escape);
494 #if __cplusplus >= 201703L
495 // _GLIBCXX_RESOLVE_LIB_DEFECTS
496 // 2785. quoted should work with basic_string_view
497 template<typename _CharT, typename _Traits>
499 quoted(basic_string_view<_CharT, _Traits> __sv,
500 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
502 return __detail::_Quoted_string<
503 basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
506 #endif // __cpp_lib_quoted_string_io
508 #endif // __cplusplus >= 201103L
510 // Inhibit implicit instantiations for required instantiations,
511 // which are defined via explicit instantiations elsewhere.
512 // NB: This syntax is a GNU extension.
513 #if _GLIBCXX_EXTERN_TEMPLATE
514 extern template ostream& operator<<(ostream&, _Setfill<char>);
515 extern template ostream& operator<<(ostream&, _Setiosflags);
516 extern template ostream& operator<<(ostream&, _Resetiosflags);
517 extern template ostream& operator<<(ostream&, _Setbase);
518 extern template ostream& operator<<(ostream&, _Setprecision);
519 extern template ostream& operator<<(ostream&, _Setw);
520 extern template istream& operator>>(istream&, _Setfill<char>);
521 extern template istream& operator>>(istream&, _Setiosflags);
522 extern template istream& operator>>(istream&, _Resetiosflags);
523 extern template istream& operator>>(istream&, _Setbase);
524 extern template istream& operator>>(istream&, _Setprecision);
525 extern template istream& operator>>(istream&, _Setw);
527 #ifdef _GLIBCXX_USE_WCHAR_T
528 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
529 extern template wostream& operator<<(wostream&, _Setiosflags);
530 extern template wostream& operator<<(wostream&, _Resetiosflags);
531 extern template wostream& operator<<(wostream&, _Setbase);
532 extern template wostream& operator<<(wostream&, _Setprecision);
533 extern template wostream& operator<<(wostream&, _Setw);
534 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
535 extern template wistream& operator>>(wistream&, _Setiosflags);
536 extern template wistream& operator>>(wistream&, _Resetiosflags);
537 extern template wistream& operator>>(wistream&, _Setbase);
538 extern template wistream& operator>>(wistream&, _Setprecision);
539 extern template wistream& operator>>(wistream&, _Setw);
543 _GLIBCXX_END_NAMESPACE_VERSION
546 #endif /* _GLIBCXX_IOMANIP */