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 pathistream
1158 lines (979 loc) · 30.6 KB
/
istream
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// istream standard header
#pragma once
#ifndef _ISTREAM_
#define _ISTREAM_
#ifndef RC_INVOKED
#include <ostream>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma push_macro("new")
#undef new
#pragma warning(disable: 4189)
_STD_BEGIN
#pragma vtordisp(push, 2) // compiler bug workaround
// TEMPLATE CLASS basic_istream
template<class _Elem,
class _Traits>
class basic_istream
: virtual public basic_ios<_Elem, _Traits>
{ // control extractions from a stream buffer
public:
typedef basic_istream<_Elem, _Traits> _Myt;
typedef basic_ios<_Elem, _Traits> _Myios;
typedef basic_streambuf<_Elem, _Traits> _Mysb;
typedef istreambuf_iterator<_Elem, _Traits> _Iter;
typedef ctype<_Elem> _Ctype;
typedef num_get<_Elem, _Iter> _Nget;
#if defined(__FORCE_INSTANCE)
explicit __CLR_OR_THIS_CALL basic_istream(_Mysb *_Strbuf,
bool _Isstd, bool _Noinit)
: _Chcount(0)
{ // construct from stream buffer pointer
if (!_Noinit)
_Myios::init(_Strbuf, _Isstd);
}
#endif /* defined(__FORCE_INSTANCE) */
explicit __CLR_OR_THIS_CALL basic_istream(_Mysb *_Strbuf,
bool _Isstd = false)
: _Chcount(0)
{ // construct from stream buffer pointer
_Myios::init(_Strbuf, _Isstd);
}
__CLR_OR_THIS_CALL basic_istream(_Uninitialized)
{ // construct uninitialized
this->_Addstd(this);
}
protected:
__CLR_OR_THIS_CALL basic_istream(_Myt&& _Right)
: _Chcount(_Right._Chcount)
{ // construct by moving _Right
_Myios::init();
_Myios::move(_STD move(_Right));
_Right._Chcount = 0;
}
_Myt& __CLR_OR_THIS_CALL operator=(_Myt&& _Right)
{ // move from _Right
this->swap(_Right);
return (*this);
}
void __CLR_OR_THIS_CALL swap(_Myt& _Right)
{ // swap with _Right
_Myios::swap(_Right);
_STD swap(_Chcount, _Right._Chcount);
}
public:
__CLR_OR_THIS_CALL basic_istream(const _Myt&) = delete;
_Myt& __CLR_OR_THIS_CALL operator=(const _Myt&) = delete;
virtual __CLR_OR_THIS_CALL ~basic_istream() _NOEXCEPT
{ // destroy the object
}
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
// TEMPLATE CLASS sentry
class _Sentry_base
{ // stores thread lock and reference to input stream
public:
__CLR_OR_THIS_CALL _Sentry_base(_Myt& _Istr)
: _Myistr(_Istr)
{ // lock the stream buffer, if there
if (_Myistr.rdbuf() != 0)
_Myistr.rdbuf()->_Lock();
}
__CLR_OR_THIS_CALL ~_Sentry_base() _NOEXCEPT
{ // destroy after unlocking
if (_Myistr.rdbuf() != 0)
_Myistr.rdbuf()->_Unlock();
}
_Myt& _Myistr; // the input stream, for _Unlock call at destruction
private:
_Sentry_base& operator=(const _Sentry_base&);
};
class sentry
: public _Sentry_base
{ // stores thread lock and result of _Ipfx call
public:
explicit __CLR_OR_THIS_CALL sentry(_Myt& _Istr, bool _Noskip = false)
: _Sentry_base(_Istr)
{ // construct locking and calling _Ipfx
_Ok = this->_Myistr._Ipfx(_Noskip);
}
explicit __CLR_OR_THIS_CALL operator bool() const
{ // test if _Ipfx succeeded
return (_Ok);
}
__CLR_OR_THIS_CALL sentry(const sentry&) = delete;
sentry& __CLR_OR_THIS_CALL operator=(const sentry&) = delete;
private:
bool _Ok; // true if _Ipfx succeeded at construction
};
bool __CLR_OR_THIS_CALL _Ipfx(bool _Noskip = false)
{ // test stream state and skip whitespace as needed
if (this->good())
{ // state okay, flush tied stream and skip whitespace
if (_Myios::tie() != 0)
_Myios::tie()->flush();
if (!_Noskip && this->flags() & ios_base::skipws)
{ // skip whitespace
const _Ctype& _Ctype_fac = _USE(this->getloc(), _Ctype);
_TRY_IO_BEGIN
int_type _Meta = _Myios::rdbuf()->sgetc();
for (; ; _Meta = _Myios::rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_Myios::setstate(ios_base::eofbit);
break;
}
else if (!_Ctype_fac.is(_Ctype::space,
_Traits::to_char_type(_Meta)))
break; // not whitespace, quit
_CATCH_IO_END
}
if (this->good())
return (true);
}
_Myios::setstate(ios_base::failbit);
return (false);
}
bool __CLR_OR_THIS_CALL ipfx(bool _Noskip = false)
{ // test stream state and skip whitespace as needed (retained)
return (_Ipfx(_Noskip));
}
void __CLR_OR_THIS_CALL isfx()
{ // perform any wrapup (retained)
}
#ifdef _M_CEE_PURE
_Myt& __CLR_OR_THIS_CALL operator>>(_Myt& (__clrcall *_Pfn)(_Myt&))
{ // call basic_istream manipulator
_DEBUG_POINTER(_Pfn);
return ((*_Pfn)(*this));
}
_Myt& __CLR_OR_THIS_CALL operator>>(_Myios& (__clrcall *_Pfn)(_Myios&))
{ // call basic_ios manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(_Myios *)this);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(ios_base& (__clrcall *_Pfn)(ios_base&))
{ // call ios_base manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(ios_base *)this);
return (*this);
}
#endif /* _M_CEE_PURE */
_Myt& __CLR_OR_THIS_CALL operator>>(_Myt& (__cdecl *_Pfn)(_Myt&))
{ // call basic_istream manipulator
_DEBUG_POINTER(_Pfn);
return ((*_Pfn)(*this));
}
_Myt& __CLR_OR_THIS_CALL operator>>(_Myios& (__cdecl *_Pfn)(_Myios&))
{ // call basic_ios manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(_Myios *)this);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(ios_base& (__cdecl *_Pfn)(ios_base&))
{ // call ios_base manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(ios_base *)this);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(bool& _Val)
{ // extract a boolean
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(short& _Val)
{ // extract a short
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
long _Tmp = 0;
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Tmp);
_CATCH_IO_END
if (_State & ios_base::failbit
|| _Tmp < SHRT_MIN || SHRT_MAX < _Tmp)
_State |= ios_base::failbit;
else
_Val = (short)_Tmp;
}
_Myios::setstate(_State);
return (*this);
}
/* NOTE:
If you are not using native wchar_t, the unsigned short extractor
is masked by an explicit specialization that treats an unsigned
short as a wide character.
To read or write unsigned shorts as integers with wchar_t streams,
make wchar_t a native type with the command line option /Zc:wchar_t.
*/
_Myt& __CLR_OR_THIS_CALL operator>>(unsigned short& _Val)
{ // extract an unsigned short
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(int& _Val)
{ // extract an int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
long _Tmp = 0;
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Tmp);
_CATCH_IO_END
if (_State & ios_base::failbit
|| _Tmp < INT_MIN || INT_MAX < _Tmp)
_State |= ios_base::failbit;
else
_Val = _Tmp;
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(unsigned int& _Val)
{ // extract an unsigned int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(long& _Val)
{ // extract a long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(unsigned long& _Val)
{ // extract an unsigned long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
#ifdef _LONGLONG
_Myt& __CLR_OR_THIS_CALL operator>>(_LONGLONG& _Val)
{ // extract a long long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(_ULONGLONG& _Val)
{ // extract an unsigned long long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
#endif /* _LONGLONG */
_Myt& __CLR_OR_THIS_CALL operator>>(float& _Val)
{ // extract a float
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(double& _Val)
{ // extract a double
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(long double& _Val)
{ // extract a long double
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(void *& _Val)
{ // extract a void pointer
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(this->getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(_Mysb *_Strbuf)
{ // extract until end-of-file into a stream buffer
ios_base::iostate _State = ios_base::goodbit;
bool _Copied = false;
const sentry _Ok(*this);
if (_Ok && _Strbuf != 0)
{ // state okay, extract characters
_TRY_IO_BEGIN
int_type _Meta = _Myios::rdbuf()->sgetc();
for (; ; _Meta = _Myios::rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else
{ // got a character, insert it into buffer
_TRY_BEGIN
if (_Traits::eq_int_type(_Traits::eof(),
_Strbuf->sputc(_Traits::to_char_type(_Meta))))
break;
_CATCH_ALL
break;
_CATCH_END
_Copied = true;
}
_CATCH_IO_END
}
_Myios::setstate(!_Copied ? _State | ios_base::failbit : _State);
return (*this);
}
int_type __CLR_OR_THIS_CALL get()
{ // extract a metacharacter
int_type _Meta = 0;
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
if (!_Ok)
_Meta = _Traits::eof(); // state not okay, return EOF
else
{ // state okay, extract a character
_TRY_IO_BEGIN
_Meta = _Myios::rdbuf()->sgetc();
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
_State |= ios_base::eofbit | ios_base::failbit; // end of file
else
{ // got a character, count it
_Myios::rdbuf()->sbumpc();
++_Chcount;
}
_CATCH_IO_END
}
_Myios::setstate(_State);
return (_Meta);
}
_Myt& __CLR_OR_THIS_CALL get(_Elem *_Str, streamsize _Count)
{ // get up to _Count characters into NTCS
return (get(_Str, _Count, _Myios::widen('\n')));
}
_Myt& __CLR_OR_THIS_CALL get(_Elem *_Str,
streamsize _Count, _Elem _Delim)
{ // get up to _Count characters into NTCS, stop before _Delim
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
if (_Ok && 0 < _Count)
{ // state okay, extract characters
_TRY_IO_BEGIN
int_type _Meta = _Myios::rdbuf()->sgetc();
for (; 0 < --_Count; _Meta = _Myios::rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else if (_Traits::to_char_type(_Meta) == _Delim)
break; // got a delimiter, quit
else
{ // got a character, add it to string
_DEBUG_POINTER(_Str);
*_Str++ = _Traits::to_char_type(_Meta);
++_Chcount;
}
_CATCH_IO_END
}
_Myios::setstate(_Chcount == 0
? _State | ios_base::failbit : _State);
*_Str = _Elem(); // add terminating null character
return (*this);
}
_Myt& __CLR_OR_THIS_CALL get(_Elem& _Ch)
{ // get a character
int_type _Meta = get();
if (!_Traits::eq_int_type(_Traits::eof(), _Meta))
_Ch = _Traits::to_char_type(_Meta);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL get(_Mysb& _Strbuf)
{ // extract up to newline and insert into stream buffer
return (get(_Strbuf, _Myios::widen('\n')));
}
_Myt& __CLR_OR_THIS_CALL get(_Mysb& _Strbuf, _Elem _Delim)
{ // extract up to delimiter and insert into stream buffer
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
if (_Ok)
{ // state okay, use facet to extract
_TRY_IO_BEGIN
int_type _Meta = _Myios::rdbuf()->sgetc();
for (; ; _Meta = _Myios::rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else
{ // got a character, insert it into stream buffer
_TRY_BEGIN
_Elem _Ch = _Traits::to_char_type(_Meta);
if (_Ch == _Delim
|| _Traits::eq_int_type(_Traits::eof(),
_Strbuf.sputc(_Ch)))
break;
_CATCH_ALL
break;
_CATCH_END
++_Chcount;
}
_CATCH_IO_END
}
if (_Chcount == 0)
_State |= ios_base::failbit;
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL getline(_Elem *_Str, streamsize _Count)
{ // get up to _Count characters into NTCS, discard newline
return (getline(_Str, _Count, _Myios::widen('\n')));
}
_Myt& __CLR_OR_THIS_CALL getline(_Elem *_Str,
streamsize _Count, _Elem _Delim)
{ // get up to _Count characters into NTCS, discard _Delim
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
if (_Ok && 0 < _Count)
{ // state okay, use facet to extract
int_type _Metadelim = _Traits::to_int_type(_Delim);
_TRY_IO_BEGIN
int_type _Meta = _Myios::rdbuf()->sgetc();
for (; ; _Meta = _Myios::rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else if (_Meta == _Metadelim)
{ // got a delimiter, discard it and quit
++_Chcount;
_Myios::rdbuf()->sbumpc();
break;
}
else if (--_Count <= 0)
{ // buffer full, quit
_State |= ios_base::failbit;
break;
}
else
{ // got a character, add it to string
_DEBUG_POINTER(_Str);
*_Str++ = _Traits::to_char_type(_Meta);
++_Chcount;
}
_CATCH_IO_END
}
*_Str = _Elem(); // add terminating null character
_Myios::setstate(_Chcount == 0 ? _State | ios_base::failbit : _State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL ignore(streamsize _Count = 1,
int_type _Metadelim = _Traits::eof())
{ // ignore up to _Count characters, discarding delimiter
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
if (_Ok && 0 < _Count)
{ // state okay, use facet to extract
_TRY_IO_BEGIN
for (; ; )
{ // get a metacharacter if more room in buffer
int_type _Meta;
if (_Count != (numeric_limits<streamsize>::max)()
&& --_Count < 0)
break; // buffer full, quit
else if (_Traits::eq_int_type(_Traits::eof(),
_Meta = _Myios::rdbuf()->sbumpc()))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else
{ // got a character, count it
++_Chcount;
if (_Meta == _Metadelim)
break; // got a delimiter, quit
}
}
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL read(_Elem *_Str, streamsize _Count)
{ // read up to _Count characters into buffer
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
if (_Ok && 0 < _Count)
{ // state okay, use facet to extract
_TRY_IO_BEGIN
_DEBUG_POINTER(_Str);
const streamsize _Num = _Myios::rdbuf()->sgetn(_Str, _Count);
_Chcount += _Num;
if (_Num != _Count)
_State |= ios_base::eofbit | ios_base::failbit; // short read
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
streamsize __CLR_OR_THIS_CALL readsome(_Elem *_Str,
streamsize _Count)
{ // read up to _Count characters into buffer, without blocking
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
streamsize _Num;
if (!_Ok)
_State |= ios_base::failbit; // no buffer, fail
else if ((_Num = _Myios::rdbuf()->in_avail()) < 0)
_State |= ios_base::eofbit; // no characters available
else if (0 < _Count && 0 < _Num)
{ // validate _Str and read
_DEBUG_POINTER(_Str);
read(_Str, _Num < _Count ? _Num : _Count); // read available
}
_Myios::setstate(_State);
return (gcount());
}
int_type __CLR_OR_THIS_CALL peek()
{ // return next character, unconsumed
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
int_type _Meta = 0;
const sentry _Ok(*this, true);
if (!_Ok)
_Meta = _Traits::eof(); // state not okay, return EOF
else
{ // state okay, read a character
_TRY_IO_BEGIN
if (_Traits::eq_int_type(_Traits::eof(),
_Meta = _Myios::rdbuf()->sgetc()))
_State |= ios_base::eofbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (_Meta);
}
_Myt& __CLR_OR_THIS_CALL putback(_Elem _Ch)
{ // put back a character
_Chcount = 0;
ios_base::iostate _State = ios_base::goodbit;
ios_base::iostate _Oldstate = _Myios::rdstate();
_Myios::clear(_Oldstate & ~ios_base::eofbit);
const sentry _Ok(*this, true);
if (_Ok)
{ // state okay, put character back
_TRY_IO_BEGIN
if (_Traits::eq_int_type(_Traits::eof(),
_Myios::rdbuf()->sputbackc(_Ch)))
_State |= ios_base::badbit | _Oldstate;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL unget()
{ // put back last read character
_Chcount = 0;
ios_base::iostate _State = ios_base::goodbit;
ios_base::iostate _Oldstate = _Myios::rdstate();
_Myios::clear(_Oldstate & ~ios_base::eofbit);
const sentry _Ok(*this, true);
if (_Ok)
{ // state okay, put character back
_TRY_IO_BEGIN
if (_Traits::eq_int_type(_Traits::eof(),
_Myios::rdbuf()->sungetc()))
_State |= ios_base::badbit | _Oldstate;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
streamsize __CLR_OR_THIS_CALL gcount() const
{ // get count from last extraction
return (_Chcount);
}
int __CLR_OR_THIS_CALL sync()
{ // synchronize with input source
const sentry _Ok(*this, true);
if (_Myios::rdbuf() == 0)
return (-1);
else if (_Myios::rdbuf()->pubsync() == -1)
{ // sync failed
_Myios::setstate(ios_base::badbit);
return (-1);
}
else
return (0);
}
_Myt& __CLR_OR_THIS_CALL seekg(pos_type _Pos)
{ // set input stream position to _Pos
ios_base::iostate _State = ios_base::goodbit;
ios_base::iostate _Oldstate = _Myios::rdstate();
_Myios::clear(_Oldstate & ~ios_base::eofbit);
const sentry _Ok(*this, true);
if (!this->fail()
&& (off_type)_Myios::rdbuf()->pubseekpos(_Pos,
ios_base::in) == _BADOFF)
_Myios::setstate(_State | ios_base::failbit);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL seekg(off_type _Off, ios_base::seekdir _Way)
{ // change input stream position by _Off, according to _Way
ios_base::iostate _State = ios_base::goodbit;
ios_base::iostate _Oldstate = _Myios::rdstate();
_Myios::clear(_Oldstate & ~ios_base::eofbit);
const sentry _Ok(*this, true);
if (!this->fail()
&& (off_type)_Myios::rdbuf()->pubseekoff(_Off, _Way,
ios_base::in) == _BADOFF)
_Myios::setstate(_State | ios_base::failbit);
return (*this);
}
pos_type __CLR_OR_THIS_CALL tellg()
{ // return input stream position
const sentry _Ok(*this, true);
if (!this->fail())
return (_Myios::rdbuf()->pubseekoff(0,
ios_base::cur, ios_base::in));
else
return (pos_type(_BADOFF));
}
private:
streamsize _Chcount; // the character count
};
// basic_istream TEMPLATE OPERATORS
#pragma vtordisp(pop) // compiler bug workaround
#ifndef _NATIVE_WCHAR_T_DEFINED
template<class _Elem,
class _Traits> inline
basic_istream<_Elem, _Traits>& operator>>(
basic_istream<_Elem, _Traits>& _Istr, _Elem& _Ch);
/* NOTE:
If you are not using native wchar_t, the following explicit
specialization will mask the member function (above) that treats
an unsigned short as an integer.
To read or write unsigned shorts as integers with wchar_t streams,
make wchar_t a native type with the command line option /Zc:wchar_t.
*/
template<> inline
basic_istream<unsigned short, char_traits<unsigned short> >&
__CLR_OR_THIS_CALL basic_istream<unsigned short,
char_traits<unsigned short> >::operator>>(unsigned short& _Ch)
{ // extract a character
return (_STD operator>>(*this, _Ch));
}
#endif /* _NATIVE_WCHAR_T_DEFINED */
#if defined(_DLL_CPPLIB) && !defined(_M_CEE_PURE)
#if !defined(_CRTBLD) || defined(__FORCE_INSTANCE)
template class _CRTIMP2_PURE basic_istream<char, char_traits<char> >;
template class _CRTIMP2_PURE basic_istream<wchar_t, char_traits<wchar_t> >;
#endif /* !defined(_CRTBLD) || defined(__FORCE_INSTANCE) */
#ifdef __FORCE_INSTANCE
template class _CRTIMP2_PURE basic_istream<unsigned short,
char_traits<unsigned short> >;
#endif /* __FORCE_INSTANCE */
#endif /* defined(_DLL_CPPLIB) etc. */
// TEMPLATE CLASS basic_iostream
template<class _Elem,
class _Traits>
class basic_iostream
: public basic_istream<_Elem, _Traits>,
public basic_ostream<_Elem, _Traits>
{ // control insertions and extractions from a stream buffer
public:
typedef basic_iostream<_Elem, _Traits> _Myt;
typedef basic_istream<_Elem, _Traits> _Myis;
typedef basic_ostream<_Elem, _Traits> _Myos;
typedef basic_ios<_Elem, _Traits> _Myios;
typedef _Elem char_type;
typedef _Traits traits_type;
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
explicit __CLR_OR_THIS_CALL basic_iostream(basic_streambuf<_Elem, _Traits> *_Strbuf)
: _Myis(_Strbuf, false),
_Myos(_Noinit, false)
{ // construct from stream buffer pointer
}
protected:
__CLR_OR_THIS_CALL basic_iostream(_Myt&& _Right)
: _Myis(_Right.rdbuf(), false),
_Myos(_Noinit, false)
{ // construct by moving _Right
_Myios::init();
_Myios::move(_STD forward<_Myt>(_Right));
}
_Myt& __CLR_OR_THIS_CALL operator=(_Myt&& _Right)
{ // move from _Right
this->swap(_Right);
return (*this);
}
void __CLR_OR_THIS_CALL swap(_Myt& _Right)
{ // swap with _Right
if (this != &_Right)
_Myios::swap(_Right);
}
public:
__CLR_OR_THIS_CALL basic_iostream(const _Myt&) = delete;
_Myt& __CLR_OR_THIS_CALL operator=(const _Myt&) = delete;
virtual __CLR_OR_THIS_CALL ~basic_iostream() _NOEXCEPT
{ // destroy the object
}
};
// basic_iostream TEMPLATE OPERATORS
#if defined(_DLL_CPPLIB) && !defined(_M_CEE_PURE)
#if !defined(_CRTBLD) || defined(__FORCE_INSTANCE)
template class _CRTIMP2_PURE basic_iostream<char, char_traits<char> >;
template class _CRTIMP2_PURE basic_iostream<wchar_t, char_traits<wchar_t> >;
#endif /* !defined(_CRTBLD) || defined(__FORCE_INSTANCE) */
#ifdef __FORCE_INSTANCE
template class _CRTIMP2_PURE basic_iostream<unsigned short,
char_traits<unsigned short> >;