This repository has been archived by the owner on May 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathiomanip
547 lines (477 loc) · 15.7 KB
/
iomanip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// iomanip standard header
#pragma once
#ifndef _IOMANIP_
#define _IOMANIP_
#ifndef RC_INVOKED
#include <istream>
#include <type_traits>
#include <xlocmon>
#include <xloctime>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma push_macro("new")
#undef new
_STD_BEGIN
// TEMPLATE STRUCT _Fillobj
template<class _Elem>
struct _Fillobj
{ // store fill character
_Fillobj(_Elem _Ch)
: _Fill(_Ch)
{ // construct from fill character
}
_Elem _Fill; // the fill character
};
// TEMPLATE FUNCTION setfill
template<class _Elem> inline
_Fillobj<_Elem> setfill(_Elem _Ch)
{ // return a _Fillobj manipulator
return (_Fillobj<_Elem>(_Ch));
}
template<class _Elem,
class _Traits,
class _Elem2> inline
basic_istream<_Elem, _Traits>&
operator>>(basic_istream<_Elem, _Traits>& _Istr,
const _Fillobj<_Elem2>& _Manip)
{ // set fill character in input stream
static_assert(is_same<_Elem, _Elem2>::value,
"wrong character type for setfill");
_Istr.fill(_Manip._Fill);
return (_Istr);
}
template<class _Elem,
class _Traits,
class _Elem2> inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Fillobj<_Elem2>& _Manip)
{ // set fill character in output stream
static_assert(is_same<_Elem, _Elem2>::value,
"wrong character type for setfill");
_Ostr.fill(_Manip._Fill);
return (_Ostr);
}
// TEMPLATE STRUCT _Monobj
template<class _Money>
struct _Monobj
{ // store reference to monetary amount
_Monobj(_Money& _Val_arg, bool _Intl_arg)
: _Val(_Val_arg), _Intl(_Intl_arg)
{ // construct from monetary amount reference and int'l flag
}
_Money& _Val; // the monetary amount reference
bool _Intl; // international flag
private:
_Monobj& operator=(const _Monobj&);
};
// TEMPLATE FUNCTION get_money
template<class _Money> inline
_Monobj<_Money> get_money(_Money& _Val_arg,
bool _Intl_arg = false)
{ // return a _Monobj manipulator
return (_Monobj<_Money>(_Val_arg, _Intl_arg));
}
template<class _Elem,
class _Traits,
class _Money> inline
basic_istream<_Elem, _Traits>&
operator>>(basic_istream<_Elem, _Traits>& _Istr,
const _Monobj<_Money>& _Manip)
{ // get monetary amount from input stream
typedef basic_istream<_Elem, _Traits> _Myis;
typedef istreambuf_iterator<_Elem, _Traits> _Iter;
typedef money_get<_Elem, _Iter> _Mymget;
ios_base::iostate _State = ios_base::goodbit;
const typename _Myis::sentry _Ok(_Istr);
if (_Ok)
{ // state okay, extract monetary amount
const _Mymget& _Mget_fac = _USE(_Istr.getloc(), _Mymget);
_TRY_IO_BEGIN
_Mget_fac.get(_Iter(_Istr.rdbuf()), _Iter(0), _Manip._Intl,
_Istr, _State, _Manip._Val);
_CATCH_IO_(_Istr)
}
_Istr.setstate(_State);
return (_Istr);
}
// TEMPLATE FUNCTION put_money
template<class _Money> inline
_Monobj<const _Money>
put_money(const _Money& _Val_arg,
bool _Intl_arg = false)
{ // return a _Monobj manipulator
return (_Monobj<const _Money>(_Val_arg, _Intl_arg));
}
template<class _Elem,
class _Traits,
class _Money> inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Monobj<_Money>& _Manip)
{ // put monetary amount to output stream
typedef basic_ostream<_Elem, _Traits> _Myos;
typedef ostreambuf_iterator<_Elem, _Traits> _Iter;
typedef money_put<_Elem, _Iter> _Mymput;
ios_base::iostate _State = ios_base::goodbit;
const typename _Myos::sentry _Ok(_Ostr);
if (_Ok)
{ // state okay, insert monetary amount
const _Mymput& _Mput_fac = _USE(_Ostr.getloc(), _Mymput);
_TRY_IO_BEGIN
if (_Mput_fac.put(_Iter(_Ostr.rdbuf()), _Manip._Intl,
_Ostr, _Ostr.fill(), _Manip._Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
// TEMPLATE STRUCT _Timeobj
template<class _Elem,
class _Ptr>
struct _Timeobj
{ // store reference to tm object and format
_Timeobj(_Ptr _Tptr_arg, const _Elem *_Fmt_arg)
: _Tptr(_Tptr_arg), _Fmtfirst(_Fmt_arg)
{ // construct from tm pointer and format pointer
for (_Fmtlast = _Fmtfirst; *_Fmtlast != 0; ++_Fmtlast)
; // find end of format string
}
_Ptr _Tptr; // the tm struct pointer
const _Elem *_Fmtfirst; // format string start
const _Elem *_Fmtlast; // format string end
};
// TEMPLATE FUNCTION get_time
template<class _Elem> inline
_Timeobj<_Elem, struct tm *>
get_time(struct tm *_Tptr_arg, const _Elem *_Fmt_arg)
{ // return a _Timeobj manipulator
return (_Timeobj<_Elem, struct tm *>(_Tptr_arg, _Fmt_arg));
}
template<class _Elem,
class _Traits,
class _Elem2> inline
basic_istream<_Elem, _Traits>&
operator>>(basic_istream<_Elem, _Traits>& _Istr,
const _Timeobj<_Elem2, struct tm *>& _Manip)
{ // get time information from input stream
typedef basic_istream<_Elem, _Traits> _Myis;
typedef istreambuf_iterator<_Elem, _Traits> _Iter;
typedef time_get<_Elem2, _Iter> _Mytget;
static_assert(is_same<_Elem, _Elem2>::value,
"wrong character type for get_time");
ios_base::iostate _State = ios_base::goodbit;
const typename _Myis::sentry _Ok(_Istr);
if (_Ok)
{ // state okay, extract time amounts
const _Mytget& _Tget_fac = _USE(_Istr.getloc(), _Mytget);
_TRY_IO_BEGIN
_Tget_fac.get(_Iter(_Istr.rdbuf()), _Iter(0), _Istr, _State,
_Manip._Tptr, _Manip._Fmtfirst, _Manip._Fmtlast);
_CATCH_IO_(_Istr)
}
_Istr.setstate(_State);
return (_Istr);
}
// TEMPLATE FUNCTION put_time
template<class _Elem> inline
_Timeobj<_Elem, const struct tm *>
put_time(const struct tm *_Tptr_arg, const _Elem *_Fmt_arg)
{ // return a _Timeobj manipulator
return (_Timeobj<_Elem, const struct tm *>(_Tptr_arg, _Fmt_arg));
}
template<class _Elem,
class _Traits,
class _Elem2> inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Timeobj<_Elem2, const struct tm *>& _Manip)
{ // put time information to output stream
typedef basic_ostream<_Elem, _Traits> _Myos;
typedef ostreambuf_iterator<_Elem, _Traits> _Iter;
typedef time_put<_Elem2, _Iter> _Mytput;
static_assert(is_same<_Elem, _Elem2>::value,
"wrong character type for put_time");
ios_base::iostate _State = ios_base::goodbit;
const typename _Myos::sentry _Ok(_Ostr);
if (_Ok)
{ // state okay, insert monetary amount
const _Mytput& _Tput_fac = _USE(_Ostr.getloc(), _Mytput);
_TRY_IO_BEGIN
if (_Tput_fac.put(_Iter(_Ostr.rdbuf()), _Ostr, _Ostr.fill(),
_Manip._Tptr, _Manip._Fmtfirst, _Manip._Fmtlast).failed())
_State |= ios_base::badbit;
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
// TEMPLATE STRUCT _Quote_in
template<class _Elem,
class _Traits,
class _Alloc>
struct _Quote_in
{ // store reference to string
typedef basic_string<_Elem, _Traits, _Alloc> _Mystr;
_Quote_in(_Mystr& _Str_obj,
_Elem _Delim_obj, _Elem _Escape_obj)
: _Str(_Str_obj),
_Delim(_Delim_obj), _Escape(_Escape_obj)
{ // construct from string, delimiter, and escape
}
_Mystr& _Str; // reference to string
_Elem _Delim; // delimiter element
_Elem _Escape; // escape element
_Quote_in& operator=(const _Quote_in&) = delete;
};
// TEMPLATE STRUCT _Quote_out
template<class _Elem,
class _Traits,
class _Sizet>
struct _Quote_out
{ // store pointer/length for string
_Quote_out(const _Elem *_Ptr_obj, _Sizet _Size_obj,
_Elem _Delim_obj, _Elem _Escape_obj)
: _Ptr(_Ptr_obj), _Size(_Size_obj),
_Delim(_Delim_obj), _Escape(_Escape_obj)
{ // construct from string, delimiter, and escape
}
const _Elem *_Ptr; // pointer to string
_Sizet _Size; // length of string
_Elem _Delim; // delimiter element
_Elem _Escape; // escape element
_Quote_out& operator=(const _Quote_out&) = delete;
};
// TEMPLATE FUNCTION quoted
template<class _Elem> inline
_Quote_out<_Elem, void, size_t>
quoted(const _Elem *_Ptr,
_Elem _Delim = _Elem('"'),
_Elem _Escape = _Elem('\\'))
{ // return a _Quote_out manipulator for pointer/length
size_t _Size = 0;
for (; _Ptr[_Size] != _Elem(0); ++_Size)
;
return (_Quote_out<_Elem, void, size_t>(_Ptr, _Size, _Delim, _Escape));
}
template<class _Elem,
class _Traits,
class _Alloc> inline
_Quote_out<_Elem, _Traits,
typename basic_string<_Elem, _Traits, _Alloc>::size_type>
quoted(const basic_string<_Elem, _Traits, _Alloc>& _Str,
_Elem _Delim = _Elem('"'),
_Elem _Escape = _Elem('\\'))
{ // return a _Quote_out manipulator for string
typedef _Quote_out<_Elem, _Traits,
typename basic_string<_Elem, _Traits, _Alloc>::size_type> _Qobj;
return (_Qobj(_Str.c_str(), _Str.size(), _Delim, _Escape));
}
template<class _Elem,
class _Traits,
class _Alloc> inline
_Quote_in<_Elem, _Traits, _Alloc>
quoted(basic_string<_Elem, _Traits, _Alloc>& _Str,
_Elem _Delim = _Elem('"'),
_Elem _Escape = _Elem('\\'))
{ // return a _Quote_in manipulator
return (_Quote_in<_Elem, _Traits, _Alloc>(_Str, _Delim, _Escape));
}
// quoted INSERTER
template<class _Elem,
class _Traits,
class _QuTraits,
class _Sizet> inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Quote_out<_Elem, _QuTraits, _Sizet>& _Manip)
{ // put quoted string to output stream
static_assert(is_void<_QuTraits>::value
|| is_same<_QuTraits, _Traits>::value,
"quoted() traits must match basic_ostream traits");
typedef basic_ostream<_Elem, _Traits> _Myos;
const _Elem *const _Last = _Manip._Ptr + _Manip._Size;
_Sizet _Size = _Manip._Size + 2; // allow for delimiters
for (const _Elem *_Ptr = _Manip._Ptr; _Ptr != _Last; ++_Ptr)
if (_Traits::eq(*_Ptr, _Manip._Delim)
|| _Traits::eq(*_Ptr, _Manip._Escape))
++_Size; // count escapes
ios_base::iostate _State = ios_base::goodbit;
_Sizet _Pad = _Ostr.width() <= 0 || (_Sizet)_Ostr.width() <= _Size
? 0 : (_Sizet)_Ostr.width() - _Size;
const typename _Myos::sentry _Ok(_Ostr);
if (!_Ok)
_State |= ios_base::badbit;
else
{ // state okay, insert characters
_TRY_IO_BEGIN
if ((_Ostr.flags() & ios_base::adjustfield) != ios_base::left)
for (; 0 < _Pad; --_Pad) // pad on left
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ostr.fill())))
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
if (_State == ios_base::goodbit
&& _Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Manip._Delim))) // put delimiter
_State |= ios_base::badbit;
for (const _Elem *_Ptr = _Manip._Ptr; _Ptr != _Last; ++_Ptr)
{ // put (possibly escaped) element
if ((_Traits::eq(*_Ptr, _Manip._Delim)
|| _Traits::eq(*_Ptr, _Manip._Escape))
&& _State == ios_base::goodbit
&& _Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Manip._Escape))) // put escape
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
if (_State == ios_base::goodbit
&& _Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(*_Ptr))) // put element
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
}
if (_State == ios_base::goodbit
&& _Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Manip._Delim))) // put delimiter
_State |= ios_base::badbit;
if (_State == ios_base::goodbit)
for (; 0 < _Pad; --_Pad) // pad on right
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ostr.fill())))
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
_Ostr.width(0);
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
template<class _Elem,
class _Traits,
class _Alloc> inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Quote_in<_Elem, _Traits, _Alloc>& _Manip)
{ // put quoted string to output stream
typedef basic_string<_Elem, _Traits, _Alloc> _Mystr;
const _Mystr& _Ref = _Manip._Str;
return (_Ostr << _STD quoted(_Ref, _Manip._Delim, _Manip._Escape));
}
// quoted EXTRACTOR
template<class _Elem,
class _Traits,
class _Alloc> inline
basic_istream<_Elem, _Traits>& operator>>(
basic_istream<_Elem, _Traits>& _Istr,
const _Quote_in<_Elem, _Traits, _Alloc>& _Manip)
{ // get quoted string from input stream
typedef basic_istream<_Elem, _Traits> _Myis;
typedef basic_string<_Elem, _Traits, _Alloc> _Mystr;
typedef typename _Mystr::size_type _Sizet;
ios_base::iostate _State = ios_base::goodbit;
bool _Changed = false;
const typename _Myis::sentry _Ok(_Istr);
while (_Ok)
{ // state okay, extract characters
_TRY_IO_BEGIN
_Sizet _Size = 0 < _Istr.width()
&& (_Sizet)_Istr.width() < _Manip._Str.max_size()
? (_Sizet)_Istr.width() : _Manip._Str.max_size();
typename _Traits::int_type _Meta = _Istr.rdbuf()->sgetc();
typename _Traits::int_type _Delim =
_Traits::to_int_type(_Manip._Delim);
typename _Traits::int_type _Escape =
_Traits::to_int_type(_Manip._Escape);
if (!_Traits::eq_int_type(_Meta, _Delim)) // no leading delimiter
return (_Istr >> _Manip._Str);
_Manip._Str.clear();
_Meta = _Istr.rdbuf()->snextc(); // discard leading delimiter
for (; !_Traits::eq_int_type(_Meta, _Delim) && 0 < _Size;
_Meta = _Istr.rdbuf()->snextc(), --_Size)
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else if (!_Traits::eq_int_type(_Meta, _Escape))
_Manip._Str.append(1,
_Traits::to_char_type(_Meta)); // add element
else if (_Traits::eq_int_type(_Traits::eof(),
_Meta = _Istr.rdbuf()->snextc()))
{ // escaped end of file, quit
_State |= ios_base::eofbit;
break;
}
else
_Manip._Str.append(1,
_Traits::to_char_type(_Meta)); // add escaped element
if (!_Traits::eq_int_type(_Meta, _Delim))
_State |= ios_base::failbit; // no trailing delimiter, fail
else
{ // discard trailing delimiter
_Changed = true;
_Istr.rdbuf()->snextc();
}
_CATCH_IO_(_Istr)
break;
}
_Istr.width(0);
if (!_Changed)
_State |= ios_base::failbit;
_Istr.setstate(_State);
return (_Istr);
}
// TEMPLATE STRUCT _Smanip
template<class _Arg>
struct _Smanip
{ // store function pointer and argument value
_Smanip(void (__cdecl *_Left)(ios_base&, _Arg), _Arg _Val)
: _Pfun(_Left), _Manarg(_Val)
{ // construct from function pointer and argument value
}
void (__cdecl *_Pfun)(ios_base&, _Arg); // the function pointer
_Arg _Manarg; // the argument value
};
template<class _Elem,
class _Traits,
class _Arg> inline
basic_istream<_Elem, _Traits>& operator>>(
basic_istream<_Elem, _Traits>& _Istr, const _Smanip<_Arg>& _Manip)
{ // extract by calling function with input stream and argument
(*_Manip._Pfun)(_Istr, _Manip._Manarg);
return (_Istr);
}
template<class _Elem,
class _Traits,
class _Arg> inline
basic_ostream<_Elem, _Traits>& operator<<(
basic_ostream<_Elem, _Traits>& _Ostr, const _Smanip<_Arg>& _Manip)
{ // insert by calling function with output stream and argument
(*_Manip._Pfun)(_Ostr, _Manip._Manarg);
return (_Ostr);
}
// INSTANTIATIONS
_MRTIMP2 _Smanip<ios_base::fmtflags> __cdecl resetiosflags(ios_base::fmtflags);
_MRTIMP2 _Smanip<ios_base::fmtflags> __cdecl setiosflags(ios_base::fmtflags);
_MRTIMP2 _Smanip<int> __cdecl setbase(int);
_MRTIMP2 _Smanip<streamsize> __cdecl setprecision(streamsize);
_MRTIMP2 _Smanip<streamsize> __cdecl setw(streamsize);
_STD_END
#pragma pop_macro("new")
#pragma warning(pop)
#pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _IOMANIP_ */
/*
* Copyright (c) by P.J. Plauger. All rights reserved.
* Consult your license regarding permissions and restrictions.
V6.50:0009 */