00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
#ifndef _CPP_FSTREAM
00041
#define _CPP_FSTREAM 1
00042
00043
#pragma GCC system_header
00044
00045
#include <istream>
00046
#include <ostream>
00047
#include <locale>
00048
#include <bits/basic_file.h>
00049
#include <bits/gthr.h>
00050
00051
namespace std
00052 {
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
template<
typename _CharT,
typename _Traits>
00063 class basic_filebuf :
public basic_streambuf<_CharT, _Traits>
00064 {
00065
public:
00066
00067 typedef _CharT
char_type;
00068 typedef _Traits
traits_type;
00069 typedef typename traits_type::int_type
int_type;
00070 typedef typename traits_type::pos_type
pos_type;
00071 typedef typename traits_type::off_type
off_type;
00072
00073
00074
00075
00076
00077
00078
00079
typedef basic_streambuf<char_type, traits_type> __streambuf_type;
00080
typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00081
typedef __basic_file<char> __file_type;
00082
typedef typename traits_type::state_type __state_type;
00083
typedef codecvt<char_type, char, __state_type> __codecvt_type;
00084
typedef ctype<char_type> __ctype_type;
00085
00086
00087
friend class ios_base;
00088
00089
protected:
00090
00091
00092
00093
00094
00095
00096
00097 __c_lock _M_lock;
00098
00099
00100
00101
00102
00103
00104
00105 __file_type _M_file;
00106
00107
00108
00109
00110
00111
00112
00113 __state_type _M_state_cur;
00114 __state_type _M_state_beg;
00115
00116
00117
00118
00119
00120
00121
00122
bool _M_buf_allocated;
00123
00124
00125
bool _M_last_overflowed;
00126
00127
00128
00129
00130
00131
00132
00133
00134
char_type* _M_filepos;
00135
00136
public:
00137
00138
00139
00140
00141
00142
00143
00144
basic_filebuf();
00145
00146
00147
00148
00149
virtual
00150
~basic_filebuf()
00151 {
00152 this->
close();
00153 _M_last_overflowed =
false;
00154 }
00155
00156
00157
00158
00159
00160
bool
00161
is_open() const throw() {
return _M_file.is_open(); }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
__filebuf_type*
00177
open(
const char* __s,
ios_base::openmode __mode);
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
__filebuf_type*
00191
close() throw();
00192
00193 protected:
00194
00195
00196
00197
00198
00199
void
00200 _M_allocate_internal_buffer();
00201
00202
00203
00204
00205
00206
00207
void
00208 _M_destroy_internal_buffer() throw();
00209
00210
00211
00212 virtual streamsize
00213 showmanyc();
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
int_type
00232 _M_underflow_common(
bool __bump);
00233
00234
00235 virtual
int_type
00236 underflow();
00237
00238
00239 virtual
int_type
00240 uflow();
00241
00242
00243 virtual
int_type
00244 pbackfail(
int_type __c = _Traits::eof());
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 virtual
int_type
00257 overflow(
int_type __c = _Traits::eof());
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
int_type
00272 _M_really_overflow(
int_type __c = _Traits::eof());
00273
00274
00275
00276
00277
00278
00279
00280
00281
void
00282 _M_convert_to_external(
char_type*, streamsize, streamsize&, streamsize&);
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 virtual __streambuf_type*
00297 setbuf(char_type* __s, streamsize __n);
00298
00299
00300 virtual
pos_type
00301 seekoff(
off_type __off,
ios_base::seekdir __way,
00302
ios_base::openmode __mode =
ios_base::in |
ios_base::out);
00303
00304
00305 virtual
pos_type
00306 seekpos(
pos_type __pos,
00307
ios_base::openmode __mode =
ios_base::in |
ios_base::out);
00308
00309
00310 virtual
int
00311 sync()
00312 {
00313
int __ret = 0;
00314
bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
00315
00316
00317
00318
if (__testput)
00319 {
00320
00321
off_type __off = _M_out_cur - _M_out_end;
00322
00323
00324
if (traits_type::eq_int_type(_M_really_overflow(),
00325 traits_type::eof()))
00326 __ret = -1;
00327
else if (__off)
00328 _M_file.
seekoff(__off, ios_base::cur);
00329 }
00330
else
00331 _M_file.sync();
00332
00333 _M_last_overflowed =
false;
00334
return __ret;
00335 }
00336
00337
00338
virtual void
00339
imbue(
const locale& __loc);
00340
00341
00342
virtual streamsize
00343
xsgetn(char_type* __s, streamsize __n)
00344 {
00345 streamsize __ret = 0;
00346
00347
if (_M_pback_init)
00348 {
00349
while (__ret < __n && _M_in_cur < _M_in_end)
00350 {
00351 *__s = *_M_in_cur;
00352 ++__ret;
00353 ++__s;
00354 ++_M_in_cur;
00355 }
00356 _M_pback_destroy();
00357 }
00358
if (__ret < __n)
00359 __ret += __streambuf_type::xsgetn(__s, __n - __ret);
00360
return __ret;
00361 }
00362
00363
00364
virtual streamsize
00365
xsputn(
const char_type* __s, streamsize __n)
00366 {
00367 _M_pback_destroy();
00368
return __streambuf_type::xsputn(__s, __n);
00369 }
00370
00371
00372
00373
00374
00375
00376 void
00377 _M_output_unshift();
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
void
00392 _M_set_indeterminate(
void)
00393 {
00394
if (_M_mode & ios_base::in)
00395 this->
setg(_M_buf, _M_buf, _M_buf);
00396
if (_M_mode & ios_base::out)
00397 this->
setp(_M_buf, _M_buf);
00398 _M_filepos = _M_buf;
00399 }
00400
00401
00402
00403
00404
00405
00406
void
00407 _M_set_determinate(off_type __off)
00408 {
00409
bool __testin = _M_mode & ios_base::in;
00410
bool __testout = _M_mode & ios_base::out;
00411
if (__testin)
00412 this->
setg(_M_buf, _M_buf, _M_buf + __off);
00413
if (__testout)
00414 this->
setp(_M_buf, _M_buf + __off);
00415 _M_filepos = _M_buf + __off;
00416 }
00417
00418
00419
00420
00421
00422
00423
bool
00424 _M_is_indeterminate(
void)
00425 {
00426
bool __ret =
false;
00427
00428
if (_M_buf)
00429 {
00430
if (_M_mode & ios_base::in)
00431 __ret = _M_in_beg == _M_in_cur && _M_in_cur == _M_in_end;
00432
if (_M_mode & ios_base::out)
00433 __ret = _M_out_beg == _M_out_cur && _M_out_cur == _M_out_end;
00434 }
00435
return __ret;
00436 }
00437 };
00438
00439
00440
template<>
00441 basic_filebuf<char>::int_type
00442 basic_filebuf<char>::_M_underflow_common(
bool __bump);
00443
00444
#ifdef _GLIBCPP_USE_WCHAR_T
00445
template<>
00446 basic_filebuf<wchar_t>::int_type
00447 basic_filebuf<wchar_t>::_M_underflow_common(
bool __bump);
00448
#endif
00449
00450
00451
template <
typename _CharT,
typename _Traits>
00452
typename basic_filebuf<_CharT, _Traits>::int_type
00453 basic_filebuf<_CharT, _Traits>::underflow()
00454 {
return _M_underflow_common(
false); }
00455
00456
template <
typename _CharT,
typename _Traits>
00457
typename basic_filebuf<_CharT, _Traits>::int_type
00458 basic_filebuf<_CharT, _Traits>::uflow()
00459 {
return _M_underflow_common(
true); }
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
template<
typename _CharT,
typename _Traits>
00472
class basic_ifstream :
public basic_istream<_CharT, _Traits>
00473 {
00474
public:
00475
00476
typedef _CharT char_type;
00477
typedef _Traits
traits_type;
00478
typedef typename traits_type::int_type
int_type;
00479
typedef typename traits_type::pos_type
pos_type;
00480
typedef typename traits_type::off_type
off_type;
00481
00482
00483
typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00484
typedef basic_istream<char_type, traits_type> __istream_type;
00485
00486
private:
00487
00488
00489
00490
00491
00492 __filebuf_type _M_filebuf;
00493
00494 public:
00495
00496
00497
00498
00499
00500
00501
00502
00503
basic_ifstream()
00504 :
__istream_type(NULL), _M_filebuf()
00505 { this->
init(&_M_filebuf); }
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
explicit
00518
basic_ifstream(
const char* __s,
ios_base::openmode __mode = ios_base::in)
00519 :
__istream_type(NULL), _M_filebuf()
00520 {
00521 this->
init(&_M_filebuf);
00522 this->
open(__s, __mode);
00523 }
00524
00525
00526
00527
00528
00529
00530
00531
~basic_ifstream()
00532 { }
00533
00534
00535
00536
00537
00538
00539
00540
00541
__filebuf_type*
00542
rdbuf()
const
00543
{
return const_cast<__filebuf_type*>(&_M_filebuf); }
00544
00545
00546
00547
00548
00549
bool
00550
is_open() {
return _M_filebuf.is_open(); }
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
void
00564
open(
const char* __s,
ios_base::openmode __mode = ios_base::in)
00565 {
00566 if (!_M_filebuf.open(__s, __mode | ios_base::in))
00567 this->
setstate(ios_base::failbit);
00568 }
00569
00570
00571
00572
00573
00574
00575
00576
void
00577
close()
00578 {
00579
if (!_M_filebuf.close())
00580 this->
setstate(ios_base::failbit);
00581 }
00582 };
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
template<
typename _CharT,
typename _Traits>
00595
class basic_ofstream :
public basic_ostream<_CharT,_Traits>
00596 {
00597
public:
00598
00599
typedef _CharT char_type;
00600
typedef _Traits
traits_type;
00601
typedef typename traits_type::int_type
int_type;
00602
typedef typename traits_type::pos_type
pos_type;
00603
typedef typename traits_type::off_type
off_type;
00604
00605
00606
typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00607
typedef basic_ostream<char_type, traits_type> __ostream_type;
00608
00609
private:
00610
00611
00612
00613
00614
00615 __filebuf_type _M_filebuf;
00616
00617 public:
00618
00619
00620
00621
00622
00623
00624
00625
00626
basic_ofstream()
00627 :
__ostream_type(NULL), _M_filebuf()
00628 { this->
init(&_M_filebuf); }
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
explicit
00642
basic_ofstream(
const char* __s,
00643 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
00644 :
__ostream_type(NULL), _M_filebuf()
00645 {
00646 this->
init(&_M_filebuf);
00647 this->
open(__s, __mode);
00648 }
00649
00650
00651
00652
00653
00654
00655
00656
~basic_ofstream()
00657 { }
00658
00659
00660
00661
00662
00663
00664
00665
00666
__filebuf_type*
00667
rdbuf()
const
00668
{
return const_cast<__filebuf_type*>(&_M_filebuf); }
00669
00670
00671
00672
00673
00674
bool
00675
is_open() {
return _M_filebuf.is_open(); }
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
void
00689
open(
const char* __s,
00690
ios_base::openmode __mode = ios_base::out | ios_base::trunc)
00691 {
00692 if (!_M_filebuf.open(__s, __mode | ios_base::out))
00693 this->
setstate(ios_base::failbit);
00694 }
00695
00696
00697
00698
00699
00700
00701
00702
void
00703
close()
00704 {
00705
if (!_M_filebuf.close())
00706 this->
setstate(ios_base::failbit);
00707 }
00708 };
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720 template<
typename _CharT,
typename _Traits>
00721
class basic_fstream :
public basic_iostream<_CharT, _Traits>
00722 {
00723
public:
00724
00725
typedef _CharT char_type;
00726
typedef _Traits
traits_type;
00727
typedef typename traits_type::int_type
int_type;
00728
typedef typename traits_type::pos_type
pos_type;
00729
typedef typename traits_type::off_type
off_type;
00730
00731
00732
typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00733
typedef basic_ios<char_type, traits_type> __ios_type;
00734
typedef basic_iostream<char_type, traits_type> __iostream_type;
00735
00736
private:
00737
00738
00739
00740
00741
00742 __filebuf_type _M_filebuf;
00743
00744 public:
00745
00746
00747
00748
00749
00750
00751
00752
00753
basic_fstream()
00754 :
__iostream_type(NULL), _M_filebuf()
00755 { this->
init(&_M_filebuf); }
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
explicit
00766
basic_fstream(
const char* __s,
00767
ios_base::openmode __mode = ios_base::in | ios_base::out)
00768 : __iostream_type(NULL), _M_filebuf()
00769 {
00770 this->
init(&_M_filebuf);
00771 this->
open(__s, __mode);
00772 }
00773
00774
00775
00776
00777
00778
00779
00780
~basic_fstream()
00781 { }
00782
00783
00784
00785
00786
00787
00788
00789
00790
__filebuf_type*
00791
rdbuf()
const
00792
{
return const_cast<__filebuf_type*>(&_M_filebuf); }
00793
00794
00795
00796
00797
00798 bool
00799
is_open() {
return _M_filebuf.is_open(); }
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
void
00813
open(
const char* __s,
00814
ios_base::openmode __mode = ios_base::in | ios_base::out)
00815 {
00816
if (!_M_filebuf.open(__s, __mode))
00817 setstate(ios_base::failbit);
00818 }
00819
00820
00821
00822
00823
00824
00825
00826
void
00827
close()
00828 {
00829
if (!_M_filebuf.close())
00830
setstate(ios_base::failbit);
00831 }
00832 };
00833 }
00834
00835
#ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
00836
# define export
00837
#endif
00838
#ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
00839
# include <bits/fstream.tcc>
00840
#endif
00841
00842
#endif