]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/iomanip
gcn/mkoffload.cc: Re-add fprintf for #include of stdlib.h/stdbool.h
[thirdparty/gcc.git] / libstdc++-v3 / include / std / iomanip
1 // Standard stream manipulators -*- C++ -*-
2
3 // Copyright (C) 1997-2024 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 /** @file include/iomanip
26 * This is a Standard C++ Library header.
27 */
28
29 //
30 // ISO C++ 14882: 27.6.3 Standard manipulators
31 //
32
33 #ifndef _GLIBCXX_IOMANIP
34 #define _GLIBCXX_IOMANIP 1
35
36 #pragma GCC system_header
37
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wc++11-extensions"
40
41 #include <bits/requires_hosted.h> // iostreams
42
43 #include <bits/c++config.h>
44 #include <iosfwd>
45 #include <bits/ios_base.h>
46
47 #define __glibcxx_want_quoted_string_io
48 #include <bits/version.h>
49
50 #if __cplusplus >= 201103L
51 #include <locale>
52 #if __cplusplus > 201103L
53 #include <bits/quoted_string.h>
54 #endif
55 #endif
56
57 namespace std _GLIBCXX_VISIBILITY(default)
58 {
59 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60
61 // [27.6.3] standard manipulators
62 // Also see DR 183.
63
64 struct _Resetiosflags { ios_base::fmtflags _M_mask; };
65
66 /**
67 * @brief Manipulator for @c setf.
68 * @param __mask A format flags mask.
69 *
70 * Sent to a stream object, this manipulator resets the specified flags,
71 * via @e stream.setf(0,__mask).
72 */
73 inline _Resetiosflags
74 resetiosflags(ios_base::fmtflags __mask)
75 { return { __mask }; }
76
77 template<typename _CharT, typename _Traits>
78 inline basic_istream<_CharT, _Traits>&
79 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
80 {
81 __is.setf(ios_base::fmtflags(0), __f._M_mask);
82 return __is;
83 }
84
85 template<typename _CharT, typename _Traits>
86 inline basic_ostream<_CharT, _Traits>&
87 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
88 {
89 __os.setf(ios_base::fmtflags(0), __f._M_mask);
90 return __os;
91 }
92
93
94 struct _Setiosflags { ios_base::fmtflags _M_mask; };
95
96 /**
97 * @brief Manipulator for @c setf.
98 * @param __mask A format flags mask.
99 *
100 * Sent to a stream object, this manipulator sets the format flags
101 * to @a __mask.
102 */
103 inline _Setiosflags
104 setiosflags(ios_base::fmtflags __mask)
105 { return { __mask }; }
106
107 template<typename _CharT, typename _Traits>
108 inline basic_istream<_CharT, _Traits>&
109 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
110 {
111 __is.setf(__f._M_mask);
112 return __is;
113 }
114
115 template<typename _CharT, typename _Traits>
116 inline basic_ostream<_CharT, _Traits>&
117 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
118 {
119 __os.setf(__f._M_mask);
120 return __os;
121 }
122
123
124 struct _Setbase { int _M_base; };
125
126 /**
127 * @brief Manipulator for @c setf.
128 * @param __base A numeric base.
129 *
130 * Sent to a stream object, this manipulator changes the
131 * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
132 * is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
133 */
134 inline _Setbase
135 setbase(int __base)
136 { return { __base }; }
137
138 template<typename _CharT, typename _Traits>
139 inline basic_istream<_CharT, _Traits>&
140 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
141 {
142 __is.setf(__f._M_base == 8 ? ios_base::oct :
143 __f._M_base == 10 ? ios_base::dec :
144 __f._M_base == 16 ? ios_base::hex :
145 ios_base::fmtflags(0), ios_base::basefield);
146 return __is;
147 }
148
149 template<typename _CharT, typename _Traits>
150 inline basic_ostream<_CharT, _Traits>&
151 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
152 {
153 __os.setf(__f._M_base == 8 ? ios_base::oct :
154 __f._M_base == 10 ? ios_base::dec :
155 __f._M_base == 16 ? ios_base::hex :
156 ios_base::fmtflags(0), ios_base::basefield);
157 return __os;
158 }
159
160
161 template<typename _CharT>
162 struct _Setfill { _CharT _M_c; };
163
164 /**
165 * @brief Manipulator for @c fill.
166 * @param __c The new fill character.
167 *
168 * Sent to a stream object, this manipulator calls @c fill(__c) for that
169 * object.
170 */
171 template<typename _CharT>
172 inline _Setfill<_CharT>
173 setfill(_CharT __c)
174 { return { __c }; }
175
176 template<typename _CharT, typename _Traits>
177 __attribute__((__deprecated__("'std::setfill' should only be used with "
178 "output streams")))
179 inline basic_istream<_CharT, _Traits>&
180 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
181 {
182 __is.fill(__f._M_c);
183 return __is;
184 }
185
186 template<typename _CharT, typename _Traits>
187 inline basic_ostream<_CharT, _Traits>&
188 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
189 {
190 __os.fill(__f._M_c);
191 return __os;
192 }
193
194
195 struct _Setprecision { int _M_n; };
196
197 /**
198 * @brief Manipulator for @c precision.
199 * @param __n The new precision.
200 *
201 * Sent to a stream object, this manipulator calls @c precision(__n) for
202 * that object.
203 */
204 inline _Setprecision
205 setprecision(int __n)
206 { return { __n }; }
207
208 template<typename _CharT, typename _Traits>
209 inline basic_istream<_CharT, _Traits>&
210 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
211 {
212 __is.precision(__f._M_n);
213 return __is;
214 }
215
216 template<typename _CharT, typename _Traits>
217 inline basic_ostream<_CharT, _Traits>&
218 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
219 {
220 __os.precision(__f._M_n);
221 return __os;
222 }
223
224
225 struct _Setw { int _M_n; };
226
227 /**
228 * @brief Manipulator for @c width.
229 * @param __n The new width.
230 *
231 * Sent to a stream object, this manipulator calls @c width(__n) for
232 * that object.
233 */
234 inline _Setw
235 setw(int __n)
236 { return { __n }; }
237
238 template<typename _CharT, typename _Traits>
239 inline basic_istream<_CharT, _Traits>&
240 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
241 {
242 __is.width(__f._M_n);
243 return __is;
244 }
245
246 template<typename _CharT, typename _Traits>
247 inline basic_ostream<_CharT, _Traits>&
248 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
249 {
250 __os.width(__f._M_n);
251 return __os;
252 }
253
254 #if __cplusplus >= 201103L
255
256 template<typename _MoneyT>
257 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
258
259 /**
260 * @brief Extended manipulator for extracting money.
261 * @param __mon Either long double or a specialization of @c basic_string.
262 * @param __intl A bool indicating whether international format
263 * is to be used.
264 *
265 * Sent to a stream object, this manipulator extracts @a __mon.
266 */
267 template<typename _MoneyT>
268 inline _Get_money<_MoneyT>
269 get_money(_MoneyT& __mon, bool __intl = false)
270 { return { __mon, __intl }; }
271
272 template<typename _CharT, typename _Traits, typename _MoneyT>
273 basic_istream<_CharT, _Traits>&
274 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
275 {
276 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
277 if (__cerb)
278 {
279 ios_base::iostate __err = ios_base::goodbit;
280 __try
281 {
282 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
283 typedef money_get<_CharT, _Iter> _MoneyGet;
284
285 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
286 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
287 __is, __err, __f._M_mon);
288 }
289 __catch(__cxxabiv1::__forced_unwind&)
290 {
291 __is._M_setstate(ios_base::badbit);
292 __throw_exception_again;
293 }
294 __catch(...)
295 { __is._M_setstate(ios_base::badbit); }
296 if (__err)
297 __is.setstate(__err);
298 }
299 return __is;
300 }
301
302
303 template<typename _MoneyT>
304 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
305
306 /**
307 * @brief Extended manipulator for inserting money.
308 * @param __mon Either long double or a specialization of @c basic_string.
309 * @param __intl A bool indicating whether international format
310 * is to be used.
311 *
312 * Sent to a stream object, this manipulator inserts @a __mon.
313 */
314 template<typename _MoneyT>
315 inline _Put_money<_MoneyT>
316 put_money(const _MoneyT& __mon, bool __intl = false)
317 { return { __mon, __intl }; }
318
319 template<typename _CharT, typename _Traits, typename _MoneyT>
320 basic_ostream<_CharT, _Traits>&
321 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
322 {
323 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
324 if (__cerb)
325 {
326 ios_base::iostate __err = ios_base::goodbit;
327 __try
328 {
329 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
330 typedef money_put<_CharT, _Iter> _MoneyPut;
331
332 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
333 if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
334 __os.fill(), __f._M_mon).failed())
335 __err |= ios_base::badbit;
336 }
337 __catch(__cxxabiv1::__forced_unwind&)
338 {
339 __os._M_setstate(ios_base::badbit);
340 __throw_exception_again;
341 }
342 __catch(...)
343 { __os._M_setstate(ios_base::badbit); }
344 if (__err)
345 __os.setstate(__err);
346 }
347 return __os;
348 }
349
350 template<typename _CharT>
351 struct _Put_time
352 {
353 const std::tm* _M_tmb;
354 const _CharT* _M_fmt;
355 };
356
357 /**
358 * @brief Extended manipulator for formatting time.
359 *
360 * This manipulator uses time_put::put to format time.
361 * [ext.manip]
362 *
363 * @param __tmb struct tm time data to format.
364 * @param __fmt format string.
365 */
366 template<typename _CharT>
367 inline _Put_time<_CharT>
368 put_time(const std::tm* __tmb, const _CharT* __fmt)
369 { return { __tmb, __fmt }; }
370
371 template<typename _CharT, typename _Traits>
372 basic_ostream<_CharT, _Traits>&
373 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
374 {
375 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
376 if (__cerb)
377 {
378 ios_base::iostate __err = ios_base::goodbit;
379 __try
380 {
381 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
382 typedef time_put<_CharT, _Iter> _TimePut;
383
384 const _CharT* const __fmt_end = __f._M_fmt +
385 _Traits::length(__f._M_fmt);
386
387 const _TimePut& __mp = use_facet<_TimePut>(__os.getloc());
388 if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(),
389 __f._M_tmb, __f._M_fmt, __fmt_end).failed())
390 __err |= ios_base::badbit;
391 }
392 __catch(__cxxabiv1::__forced_unwind&)
393 {
394 __os._M_setstate(ios_base::badbit);
395 __throw_exception_again;
396 }
397 __catch(...)
398 { __os._M_setstate(ios_base::badbit); }
399 if (__err)
400 __os.setstate(__err);
401 }
402 return __os;
403 }
404
405 template<typename _CharT>
406 struct _Get_time
407 {
408 std::tm* _M_tmb;
409 const _CharT* _M_fmt;
410 };
411
412 /**
413 * @brief Extended manipulator for extracting time.
414 *
415 * This manipulator uses time_get::get to extract time.
416 * [ext.manip]
417 *
418 * @param __tmb struct to extract the time data to.
419 * @param __fmt format string.
420 */
421 template<typename _CharT>
422 inline _Get_time<_CharT>
423 get_time(std::tm* __tmb, const _CharT* __fmt)
424 { return { __tmb, __fmt }; }
425
426 template<typename _CharT, typename _Traits>
427 basic_istream<_CharT, _Traits>&
428 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
429 {
430 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
431 if (__cerb)
432 {
433 ios_base::iostate __err = ios_base::goodbit;
434 __try
435 {
436 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
437 typedef time_get<_CharT, _Iter> _TimeGet;
438
439 const _CharT* const __fmt_end = __f._M_fmt +
440 _Traits::length(__f._M_fmt);
441
442 const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc());
443 __mg.get(_Iter(__is.rdbuf()), _Iter(), __is,
444 __err, __f._M_tmb, __f._M_fmt, __fmt_end);
445 }
446 __catch(__cxxabiv1::__forced_unwind&)
447 {
448 __is._M_setstate(ios_base::badbit);
449 __throw_exception_again;
450 }
451 __catch(...)
452 { __is._M_setstate(ios_base::badbit); }
453 if (__err)
454 __is.setstate(__err);
455 }
456 return __is;
457 }
458
459 #ifdef __cpp_lib_quoted_string_io // C++ >= 14 && HOSTED
460
461 /**
462 * @brief Manipulator for quoted strings.
463 * @param __string String to quote.
464 * @param __delim Character to quote string with.
465 * @param __escape Escape character to escape itself or quote character.
466 * @since C++14
467 */
468 template<typename _CharT>
469 inline auto
470 quoted(const _CharT* __string,
471 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
472 {
473 return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
474 __escape);
475 }
476
477 template<typename _CharT, typename _Traits, typename _Alloc>
478 inline auto
479 quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
480 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
481 {
482 return __detail::_Quoted_string<
483 const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
484 __string, __delim, __escape);
485 }
486
487 template<typename _CharT, typename _Traits, typename _Alloc>
488 inline auto
489 quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
490 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
491 {
492 return __detail::_Quoted_string<
493 basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
494 __string, __delim, __escape);
495 }
496
497 #if __cplusplus >= 201703L
498 // _GLIBCXX_RESOLVE_LIB_DEFECTS
499 // 2785. quoted should work with basic_string_view
500 template<typename _CharT, typename _Traits>
501 inline auto
502 quoted(basic_string_view<_CharT, _Traits> __sv,
503 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
504 {
505 return __detail::_Quoted_string<
506 basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
507 }
508 #endif // C++17
509 #endif // __cpp_lib_quoted_string_io
510
511 #endif // __cplusplus >= 201103L
512
513 // Inhibit implicit instantiations for required instantiations,
514 // which are defined via explicit instantiations elsewhere.
515 // NB: This syntax is a GNU extension.
516 #if _GLIBCXX_EXTERN_TEMPLATE
517 extern template ostream& operator<<(ostream&, _Setfill<char>);
518 extern template ostream& operator<<(ostream&, _Setiosflags);
519 extern template ostream& operator<<(ostream&, _Resetiosflags);
520 extern template ostream& operator<<(ostream&, _Setbase);
521 extern template ostream& operator<<(ostream&, _Setprecision);
522 extern template ostream& operator<<(ostream&, _Setw);
523 extern template istream& operator>>(istream&, _Setfill<char>);
524 extern template istream& operator>>(istream&, _Setiosflags);
525 extern template istream& operator>>(istream&, _Resetiosflags);
526 extern template istream& operator>>(istream&, _Setbase);
527 extern template istream& operator>>(istream&, _Setprecision);
528 extern template istream& operator>>(istream&, _Setw);
529
530 #ifdef _GLIBCXX_USE_WCHAR_T
531 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
532 extern template wostream& operator<<(wostream&, _Setiosflags);
533 extern template wostream& operator<<(wostream&, _Resetiosflags);
534 extern template wostream& operator<<(wostream&, _Setbase);
535 extern template wostream& operator<<(wostream&, _Setprecision);
536 extern template wostream& operator<<(wostream&, _Setw);
537 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
538 extern template wistream& operator>>(wistream&, _Setiosflags);
539 extern template wistream& operator>>(wistream&, _Resetiosflags);
540 extern template wistream& operator>>(wistream&, _Setbase);
541 extern template wistream& operator>>(wistream&, _Setprecision);
542 extern template wistream& operator>>(wistream&, _Setw);
543 #endif
544 #endif
545
546 _GLIBCXX_END_NAMESPACE_VERSION
547 } // namespace
548
549 #pragma GCC diagnostic pop
550 #endif /* _GLIBCXX_IOMANIP */