blob: cdb0d5f0dbf17fb9d493f335897025d80b91e098 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- iomanip ----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_IOMANIP
12#define _LIBCPP_IOMANIP
13
14/*
15 iomanip synopsis
16
17// types T1, T2, ... are unspecified implementation types
18T1 resetiosflags(ios_base::fmtflags mask);
19T2 setiosflags (ios_base::fmtflags mask);
20T3 setbase(int base);
21template<charT> T4 setfill(charT c);
22T5 setprecision(int n);
23T6 setw(int n);
24template <class moneyT> T7 get_money(moneyT& mon, bool intl = false);
25template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
26template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
27template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
28
Marshall Clowf38a2982013-09-05 04:48:45 +000029template <class charT>
30 T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
31
32template <class charT, class traits, class Allocator>
33 T12 quoted(const basic_string<charT, traits, Allocator>& s,
34 charT delim=charT('"'), charT escape=charT('\\')); // C++14
35
36template <class charT, class traits, class Allocator>
37 T13 quoted(basic_string<charT, traits, Allocator>& s,
38 charT delim=charT('"'), charT escape=charT('\\')); // C++14
39
Howard Hinnantc51e1022010-05-11 19:42:16 +000040} // std
41
42*/
43
44#include <__config>
45#include <istream>
46
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000047#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000048#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000049#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000050
51_LIBCPP_BEGIN_NAMESPACE_STD
52
53// resetiosflags
54
55class __iom_t1
56{
57 ios_base::fmtflags __mask_;
58public:
Howard Hinnant64da2602010-09-22 15:29:08 +000059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000060 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
61
62 template <class _CharT, class _Traits>
63 friend
Howard Hinnant64da2602010-09-22 15:29:08 +000064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000065 basic_istream<_CharT, _Traits>&
66 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
67 {
68 __is.unsetf(__x.__mask_);
69 return __is;
70 }
71
72 template <class _CharT, class _Traits>
73 friend
Howard Hinnant64da2602010-09-22 15:29:08 +000074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000075 basic_ostream<_CharT, _Traits>&
76 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
77 {
78 __os.unsetf(__x.__mask_);
79 return __os;
80 }
81};
82
83inline _LIBCPP_INLINE_VISIBILITY
84__iom_t1
85resetiosflags(ios_base::fmtflags __mask)
86{
87 return __iom_t1(__mask);
88}
89
90// setiosflags
91
92class __iom_t2
93{
94 ios_base::fmtflags __mask_;
95public:
Howard Hinnant64da2602010-09-22 15:29:08 +000096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000097 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
98
99 template <class _CharT, class _Traits>
100 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102 basic_istream<_CharT, _Traits>&
103 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
104 {
105 __is.setf(__x.__mask_);
106 return __is;
107 }
108
109 template <class _CharT, class _Traits>
110 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112 basic_ostream<_CharT, _Traits>&
113 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
114 {
115 __os.setf(__x.__mask_);
116 return __os;
117 }
118};
119
120inline _LIBCPP_INLINE_VISIBILITY
121__iom_t2
122setiosflags(ios_base::fmtflags __mask)
123{
124 return __iom_t2(__mask);
125}
126
127// setbase
128
129class __iom_t3
130{
131 int __base_;
132public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134 explicit __iom_t3(int __b) : __base_(__b) {}
135
136 template <class _CharT, class _Traits>
137 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139 basic_istream<_CharT, _Traits>&
140 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
141 {
142 __is.setf(__x.__base_ == 8 ? ios_base::oct :
143 __x.__base_ == 10 ? ios_base::dec :
144 __x.__base_ == 16 ? ios_base::hex :
145 ios_base::fmtflags(0), ios_base::basefield);
146 return __is;
147 }
148
149 template <class _CharT, class _Traits>
150 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 basic_ostream<_CharT, _Traits>&
153 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
154 {
155 __os.setf(__x.__base_ == 8 ? ios_base::oct :
156 __x.__base_ == 10 ? ios_base::dec :
157 __x.__base_ == 16 ? ios_base::hex :
158 ios_base::fmtflags(0), ios_base::basefield);
159 return __os;
160 }
161};
162
163inline _LIBCPP_INLINE_VISIBILITY
164__iom_t3
165setbase(int __base)
166{
167 return __iom_t3(__base);
168}
169
170// setfill
171
172template<class _CharT>
173class __iom_t4
174{
175 _CharT __fill_;
176public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178 explicit __iom_t4(_CharT __c) : __fill_(__c) {}
179
180 template <class _Traits>
181 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183 basic_ostream<_CharT, _Traits>&
184 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
185 {
186 __os.fill(__x.__fill_);
187 return __os;
188 }
189};
190
191template<class _CharT>
192inline _LIBCPP_INLINE_VISIBILITY
193__iom_t4<_CharT>
194setfill(_CharT __c)
195{
196 return __iom_t4<_CharT>(__c);
197}
198
199// setprecision
200
201class __iom_t5
202{
203 int __n_;
204public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 explicit __iom_t5(int __n) : __n_(__n) {}
207
208 template <class _CharT, class _Traits>
209 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211 basic_istream<_CharT, _Traits>&
212 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
213 {
214 __is.precision(__x.__n_);
215 return __is;
216 }
217
218 template <class _CharT, class _Traits>
219 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 basic_ostream<_CharT, _Traits>&
222 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
223 {
224 __os.precision(__x.__n_);
225 return __os;
226 }
227};
228
229inline _LIBCPP_INLINE_VISIBILITY
230__iom_t5
231setprecision(int __n)
232{
233 return __iom_t5(__n);
234}
235
236// setw
237
238class __iom_t6
239{
240 int __n_;
241public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243 explicit __iom_t6(int __n) : __n_(__n) {}
244
245 template <class _CharT, class _Traits>
246 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248 basic_istream<_CharT, _Traits>&
249 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
250 {
251 __is.width(__x.__n_);
252 return __is;
253 }
254
255 template <class _CharT, class _Traits>
256 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258 basic_ostream<_CharT, _Traits>&
259 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
260 {
261 __os.width(__x.__n_);
262 return __os;
263 }
264};
265
266inline _LIBCPP_INLINE_VISIBILITY
267__iom_t6
268setw(int __n)
269{
270 return __iom_t6(__n);
271}
272
273// get_money
274
275template <class _MoneyT> class __iom_t7;
276
277template <class _CharT, class _Traits, class _MoneyT>
278basic_istream<_CharT, _Traits>&
279operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
280
281template <class _MoneyT>
282class __iom_t7
283{
284 _MoneyT& __mon_;
285 bool __intl_;
286public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288 __iom_t7(_MoneyT& __mon, bool __intl)
289 : __mon_(__mon), __intl_(__intl) {}
290
Howard Hinnantc834c512011-11-29 18:15:50 +0000291 template <class _CharT, class _Traits, class _Mp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292 friend
293 basic_istream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +0000294 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295};
296
297template <class _CharT, class _Traits, class _MoneyT>
298basic_istream<_CharT, _Traits>&
299operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
300{
301#ifndef _LIBCPP_NO_EXCEPTIONS
302 try
303 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000304#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
306 if (__s)
307 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000308 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
309 typedef money_get<_CharT, _Ip> _Fp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310 ios_base::iostate __err = ios_base::goodbit;
Howard Hinnantc834c512011-11-29 18:15:50 +0000311 const _Fp& __mf = use_facet<_Fp>(__is.getloc());
312 __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313 __is.setstate(__err);
314 }
315#ifndef _LIBCPP_NO_EXCEPTIONS
316 }
317 catch (...)
318 {
319 __is.__set_badbit_and_consider_rethrow();
320 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000321#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322 return __is;
323}
324
325template <class _MoneyT>
326inline _LIBCPP_INLINE_VISIBILITY
327__iom_t7<_MoneyT>
328get_money(_MoneyT& __mon, bool __intl = false)
329{
330 return __iom_t7<_MoneyT>(__mon, __intl);
331}
332
333// put_money
334
335template <class _MoneyT> class __iom_t8;
336
337template <class _CharT, class _Traits, class _MoneyT>
338basic_ostream<_CharT, _Traits>&
339operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
340
341template <class _MoneyT>
342class __iom_t8
343{
344 const _MoneyT& __mon_;
345 bool __intl_;
346public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348 __iom_t8(const _MoneyT& __mon, bool __intl)
349 : __mon_(__mon), __intl_(__intl) {}
350
Howard Hinnantc834c512011-11-29 18:15:50 +0000351 template <class _CharT, class _Traits, class _Mp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352 friend
353 basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +0000354 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355};
356
357template <class _CharT, class _Traits, class _MoneyT>
358basic_ostream<_CharT, _Traits>&
359operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
360{
361#ifndef _LIBCPP_NO_EXCEPTIONS
362 try
363 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000364#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
366 if (__s)
367 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000368 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
369 typedef money_put<_CharT, _Op> _Fp;
370 const _Fp& __mf = use_facet<_Fp>(__os.getloc());
371 if (__mf.put(_Op(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 __os.setstate(ios_base::badbit);
373 }
374#ifndef _LIBCPP_NO_EXCEPTIONS
375 }
376 catch (...)
377 {
378 __os.__set_badbit_and_consider_rethrow();
379 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381 return __os;
382}
383
384template <class _MoneyT>
385inline _LIBCPP_INLINE_VISIBILITY
386__iom_t8<_MoneyT>
387put_money(const _MoneyT& __mon, bool __intl = false)
388{
389 return __iom_t8<_MoneyT>(__mon, __intl);
390}
391
392// get_time
393
394template <class _CharT> class __iom_t9;
395
396template <class _CharT, class _Traits>
397basic_istream<_CharT, _Traits>&
398operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
399
400template <class _CharT>
401class __iom_t9
402{
403 tm* __tm_;
404 const _CharT* __fmt_;
405public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 __iom_t9(tm* __tm, const _CharT* __fmt)
408 : __tm_(__tm), __fmt_(__fmt) {}
409
Howard Hinnantc834c512011-11-29 18:15:50 +0000410 template <class _Cp, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 friend
Howard Hinnantc834c512011-11-29 18:15:50 +0000412 basic_istream<_Cp, _Traits>&
413 operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414};
415
416template <class _CharT, class _Traits>
417basic_istream<_CharT, _Traits>&
418operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
419{
420#ifndef _LIBCPP_NO_EXCEPTIONS
421 try
422 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000423#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
425 if (__s)
426 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000427 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
428 typedef time_get<_CharT, _Ip> _Fp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429 ios_base::iostate __err = ios_base::goodbit;
Howard Hinnantc834c512011-11-29 18:15:50 +0000430 const _Fp& __tf = use_facet<_Fp>(__is.getloc());
431 __tf.get(_Ip(__is), _Ip(), __is, __err, __x.__tm_,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
433 __is.setstate(__err);
434 }
435#ifndef _LIBCPP_NO_EXCEPTIONS
436 }
437 catch (...)
438 {
439 __is.__set_badbit_and_consider_rethrow();
440 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000441#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442 return __is;
443}
444
445template <class _CharT>
446inline _LIBCPP_INLINE_VISIBILITY
447__iom_t9<_CharT>
448get_time(tm* __tm, const _CharT* __fmt)
449{
450 return __iom_t9<_CharT>(__tm, __fmt);
451}
452
453// put_time
454
455template <class _CharT> class __iom_t10;
456
457template <class _CharT, class _Traits>
458basic_ostream<_CharT, _Traits>&
459operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
460
461template <class _CharT>
462class __iom_t10
463{
464 const tm* __tm_;
465 const _CharT* __fmt_;
466public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468 __iom_t10(const tm* __tm, const _CharT* __fmt)
469 : __tm_(__tm), __fmt_(__fmt) {}
470
Howard Hinnantc834c512011-11-29 18:15:50 +0000471 template <class _Cp, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000472 friend
Howard Hinnantc834c512011-11-29 18:15:50 +0000473 basic_ostream<_Cp, _Traits>&
474 operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475};
476
477template <class _CharT, class _Traits>
478basic_ostream<_CharT, _Traits>&
479operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
480{
481#ifndef _LIBCPP_NO_EXCEPTIONS
482 try
483 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000484#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000485 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
486 if (__s)
487 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000488 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
489 typedef time_put<_CharT, _Op> _Fp;
490 const _Fp& __tf = use_facet<_Fp>(__os.getloc());
491 if (__tf.put(_Op(__os), __os, __os.fill(), __x.__tm_,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed())
493 __os.setstate(ios_base::badbit);
494 }
495#ifndef _LIBCPP_NO_EXCEPTIONS
496 }
497 catch (...)
498 {
499 __os.__set_badbit_and_consider_rethrow();
500 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000501#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502 return __os;
503}
504
505template <class _CharT>
506inline _LIBCPP_INLINE_VISIBILITY
507__iom_t10<_CharT>
508put_time(const tm* __tm, const _CharT* __fmt)
509{
510 return __iom_t10<_CharT>(__tm, __fmt);
511}
512
Marshall Clowf38a2982013-09-05 04:48:45 +0000513#if _LIBCPP_STD_VER > 11
514
515template <class _CharT, class _Traits, class _ForwardIterator>
516std::basic_ostream<_CharT, _Traits> &
517__quoted_output ( basic_ostream<_CharT, _Traits> &__os,
518 _ForwardIterator __first, _ForwardIterator __last, _CharT __delim, _CharT __escape )
519{
520 __os << __delim;
521 for ( ; __first != __last; ++ __first )
522 {
523 if (_Traits::eq (*__first, __escape) || _Traits::eq (*__first, __delim))
524 __os << __escape;
525 __os << *__first;
526 }
527 __os << __delim;
528 return __os;
529}
530
531template <class _CharT, class _Traits, class _String>
532basic_istream<_CharT, _Traits> &
533__quoted_input ( basic_istream<_CharT, _Traits> &__is, _String & __string, _CharT __delim, _CharT __escape )
534{
535 __string.clear ();
536 _CharT __c;
537 __is >> __c;
538 if ( __is.fail ())
539 return __is;
540
541 if (!_Traits::eq (__c, __delim)) // no delimiter, read the whole string
542 {
543 __is.unget ();
544 __is >> __string;
545 return __is;
546 }
547
548 __save_flags<_CharT, _Traits> sf(__is);
549 noskipws (__is);
550 while (true)
551 {
552 __is >> __c;
553 if ( __is.fail ())
554 break;
555 if (_Traits::eq (__c, __escape))
556 {
557 __is >> __c;
558 if ( __is.fail ())
559 break;
560 }
561 else if (_Traits::eq (__c, __delim))
562 break;
563 __string.push_back ( __c );
564 }
565 return __is;
566}
567
568
569template <class _CharT, class _Iter, class _Traits=char_traits<_CharT>>
570struct __quoted_output_proxy
571{
572 _Iter __first;
573 _Iter __last;
574 _CharT __delim;
575 _CharT __escape;
576
577 __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
578 : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
579 // This would be a nice place for a string_ref
580};
581
582template <class _CharT, class _Traits, class _Iter>
583basic_ostream<_CharT, _Traits>& operator<<(
584 basic_ostream<_CharT, _Traits>& __os,
585 const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
586{
587 return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
588}
589
590template <class _CharT, class _Traits, class _Allocator>
591struct __quoted_proxy
592{
593 basic_string<_CharT, _Traits, _Allocator> &__string;
594 _CharT __delim;
595 _CharT __escape;
596
597 __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
598 : __string(__s), __delim(__d), __escape(__e) {}
599};
600
601template <class _CharT, class _Traits, class _Allocator>
602_LIBCPP_INLINE_VISIBILITY
603basic_ostream<_CharT, _Traits>& operator<<(
604 basic_ostream<_CharT, _Traits>& __os,
605 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
606{
607 return __quoted_output (__os, __proxy.string.cbegin (), __proxy.string.cend (), __proxy.__delim, __proxy.__escape);
608}
609
610// extractor for non-const basic_string& proxies
611template <class _CharT, class _Traits, class _Allocator>
612_LIBCPP_INLINE_VISIBILITY
613basic_istream<_CharT, _Traits>& operator>>(
614 basic_istream<_CharT, _Traits>& __is,
615 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
616{
617 return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
618}
619
620
621template <class _CharT>
622_LIBCPP_INLINE_VISIBILITY
623__quoted_output_proxy<_CharT, const _CharT *>
624quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
625{
626 const _CharT *__end = __s;
627 while ( *__end ) ++__end;
628 return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
629}
630
631template <class _CharT, class _Traits, class _Allocator>
632_LIBCPP_INLINE_VISIBILITY
633__quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
634quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
635{
636 return __quoted_output_proxy<_CharT,
637 typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
638 ( __s.cbegin(), __s.cend (), __delim, __escape );
639}
640
641template <class _CharT, class _Traits, class _Allocator>
642__quoted_proxy<_CharT, _Traits, _Allocator>
643quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
644{
645 return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
646}
647#endif
648
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649_LIBCPP_END_NAMESPACE_STD
650
651#endif // _LIBCPP_IOMANIP