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
00040 #ifndef _GLIBCXX_FSTREAM
00041 #define _GLIBCXX_FSTREAM 1
00042
00043 #pragma GCC system_header
00044
00045 #include <istream>
00046 #include <ostream>
00047 #include <locale>
00048 #include <cstdio>
00049 #include <bits/basic_file.h>
00050 #include <bits/gthr.h>
00051
00052 namespace std
00053 {
00054
00063
00064
00065
00066
00067
00068 template<typename _CharT, typename _Traits>
00069 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
00070 {
00071 public:
00072
00073 typedef _CharT char_type;
00074 typedef _Traits traits_type;
00075 typedef typename traits_type::int_type int_type;
00076 typedef typename traits_type::pos_type pos_type;
00077 typedef typename traits_type::off_type off_type;
00078
00080
00085 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
00086 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00087 typedef __basic_file<char> __file_type;
00088 typedef typename traits_type::state_type __state_type;
00089 typedef codecvt<char_type, char, __state_type> __codecvt_type;
00091
00092 friend class ios_base;
00093
00094 protected:
00095
00096
00102 __c_lock _M_lock;
00103
00104
00110 __file_type _M_file;
00111
00117 ios_base::openmode _M_mode;
00118
00119
00125 __state_type _M_state_beg;
00126
00127
00128
00129
00135 __state_type _M_state_cur;
00136
00137
00138
00144 __state_type _M_state_last;
00145
00151 char_type* _M_buf;
00152
00160 size_t _M_buf_size;
00161
00162
00168 bool _M_buf_allocated;
00169
00179 bool _M_reading;
00180 bool _M_writing;
00181
00183
00190 char_type _M_pback;
00191 char_type* _M_pback_cur_save;
00192 char_type* _M_pback_end_save;
00193 bool _M_pback_init;
00195
00196
00197 const __codecvt_type* _M_codecvt;
00198
00206 char* _M_ext_buf;
00207
00213 streamsize _M_ext_buf_size;
00214
00222 const char* _M_ext_next;
00223 char* _M_ext_end;
00224
00232 void
00233 _M_create_pback()
00234 {
00235 if (!_M_pback_init)
00236 {
00237 _M_pback_cur_save = this->gptr();
00238 _M_pback_end_save = this->egptr();
00239 this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
00240 _M_pback_init = true;
00241 }
00242 }
00243
00251 void
00252 _M_destroy_pback() throw()
00253 {
00254 if (_M_pback_init)
00255 {
00256
00257 _M_pback_cur_save += this->gptr() != this->eback();
00258 this->setg(this->_M_buf, _M_pback_cur_save, _M_pback_end_save);
00259 _M_pback_init = false;
00260 }
00261 }
00262
00263 public:
00264
00271 basic_filebuf();
00272
00276 virtual
00277 ~basic_filebuf()
00278 { this->close(); }
00279
00280
00284 bool
00285 is_open() const throw() { return _M_file.is_open(); }
00286
00300 __filebuf_type*
00301 open(const char* __s, ios_base::openmode __mode);
00302
00314 __filebuf_type*
00315 close() throw();
00316
00317 protected:
00323 void
00324 _M_allocate_internal_buffer();
00325
00331 void
00332 _M_destroy_internal_buffer() throw();
00333
00334
00335
00336 virtual streamsize
00337 showmanyc();
00338
00339
00340
00341
00342
00343
00344
00345 virtual int_type
00346 underflow();
00347
00348
00349 virtual int_type
00350 pbackfail(int_type __c = _Traits::eof());
00351
00352
00353
00354
00355
00356
00357
00358
00364 virtual int_type
00365 overflow(int_type __c = _Traits::eof());
00366
00367
00368
00374 bool
00375 _M_convert_to_external(char_type*, streamsize);
00376
00389 virtual __streambuf_type*
00390 setbuf(char_type* __s, streamsize __n);
00391
00392
00393 virtual pos_type
00394 seekoff(off_type __off, ios_base::seekdir __way,
00395 ios_base::openmode __mode = ios_base::in | ios_base::out);
00396
00397
00398 virtual pos_type
00399 seekpos(pos_type __pos,
00400 ios_base::openmode __mode = ios_base::in | ios_base::out);
00401
00402
00408 pos_type
00409 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
00410
00411
00412 virtual int
00413 sync();
00414
00415
00416 virtual void
00417 imbue(const locale& __loc);
00418
00419
00420 virtual streamsize
00421 xsgetn(char_type* __s, streamsize __n);
00422
00423
00424 virtual streamsize
00425 xsputn(const char_type* __s, streamsize __n);
00426
00427
00433 bool
00434 _M_terminate_output();
00435
00450 void
00451 _M_set_buffer(streamsize __off)
00452 {
00453 const bool __testin = this->_M_mode & ios_base::in;
00454 const bool __testout = this->_M_mode & ios_base::out;
00455
00456 if (__testin && __off > 0)
00457 this->setg(this->_M_buf, this->_M_buf, this->_M_buf + __off);
00458 else
00459 this->setg(this->_M_buf, this->_M_buf, this->_M_buf);
00460
00461 if (__testout && __off == 0 && this->_M_buf_size > 1 )
00462 this->setp(this->_M_buf, this->_M_buf + this->_M_buf_size - 1);
00463 else
00464 this->setp(NULL, NULL);
00465 }
00466 };
00467
00468
00477 template<typename _CharT, typename _Traits>
00478 class basic_ifstream : public basic_istream<_CharT, _Traits>
00479 {
00480 public:
00481
00482 typedef _CharT char_type;
00483 typedef _Traits traits_type;
00484 typedef typename traits_type::int_type int_type;
00485 typedef typename traits_type::pos_type pos_type;
00486 typedef typename traits_type::off_type off_type;
00487
00488
00489 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00490 typedef basic_istream<char_type, traits_type> __istream_type;
00491
00492 private:
00498 __filebuf_type _M_filebuf;
00499
00500 public:
00501
00509 basic_ifstream() : __istream_type(), _M_filebuf()
00510 { this->init(&_M_filebuf); }
00511
00522 explicit
00523 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
00524 : __istream_type(), _M_filebuf()
00525 {
00526 this->init(&_M_filebuf);
00527 this->open(__s, __mode);
00528 }
00529
00536 ~basic_ifstream()
00537 { }
00538
00539
00546 __filebuf_type*
00547 rdbuf() const
00548 { return const_cast<__filebuf_type*>(&_M_filebuf); }
00549
00554 bool
00555 is_open() { return _M_filebuf.is_open(); }
00556
00568 void
00569 open(const char* __s, ios_base::openmode __mode = ios_base::in)
00570 {
00571 if (!_M_filebuf.open(__s, __mode | ios_base::in))
00572 this->setstate(ios_base::failbit);
00573 }
00574
00581 void
00582 close()
00583 {
00584 if (!_M_filebuf.close())
00585 this->setstate(ios_base::failbit);
00586 }
00587 };
00588
00589
00590
00599 template<typename _CharT, typename _Traits>
00600 class basic_ofstream : public basic_ostream<_CharT,_Traits>
00601 {
00602 public:
00603
00604 typedef _CharT char_type;
00605 typedef _Traits traits_type;
00606 typedef typename traits_type::int_type int_type;
00607 typedef typename traits_type::pos_type pos_type;
00608 typedef typename traits_type::off_type off_type;
00609
00610
00611 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00612 typedef basic_ostream<char_type, traits_type> __ostream_type;
00613
00614 private:
00620 __filebuf_type _M_filebuf;
00621
00622 public:
00623
00631 basic_ofstream(): __ostream_type(), _M_filebuf()
00632 { this->init(&_M_filebuf); }
00633
00645 explicit
00646 basic_ofstream(const char* __s,
00647 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
00648 : __ostream_type(), _M_filebuf()
00649 {
00650 this->init(&_M_filebuf);
00651 this->open(__s, __mode);
00652 }
00653
00660 ~basic_ofstream()
00661 { }
00662
00663
00670 __filebuf_type*
00671 rdbuf() const
00672 { return const_cast<__filebuf_type*>(&_M_filebuf); }
00673
00678 bool
00679 is_open() { return _M_filebuf.is_open(); }
00680
00692 void
00693 open(const char* __s,
00694 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
00695 {
00696 if (!_M_filebuf.open(__s, __mode | ios_base::out))
00697 this->setstate(ios_base::failbit);
00698 }
00699
00706 void
00707 close()
00708 {
00709 if (!_M_filebuf.close())
00710 this->setstate(ios_base::failbit);
00711 }
00712 };
00713
00714
00715
00724 template<typename _CharT, typename _Traits>
00725 class basic_fstream : public basic_iostream<_CharT, _Traits>
00726 {
00727 public:
00728
00729 typedef _CharT char_type;
00730 typedef _Traits traits_type;
00731 typedef typename traits_type::int_type int_type;
00732 typedef typename traits_type::pos_type pos_type;
00733 typedef typename traits_type::off_type off_type;
00734
00735
00736 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00737 typedef basic_ios<char_type, traits_type> __ios_type;
00738 typedef basic_iostream<char_type, traits_type> __iostream_type;
00739
00740 private:
00746 __filebuf_type _M_filebuf;
00747
00748 public:
00749
00757 basic_fstream()
00758 : __iostream_type(), _M_filebuf()
00759 { this->init(&_M_filebuf); }
00760
00769 explicit
00770 basic_fstream(const char* __s,
00771 ios_base::openmode __mode = ios_base::in | ios_base::out)
00772 : __iostream_type(NULL), _M_filebuf()
00773 {
00774 this->init(&_M_filebuf);
00775 this->open(__s, __mode);
00776 }
00777
00784 ~basic_fstream()
00785 { }
00786
00787
00794 __filebuf_type*
00795 rdbuf() const
00796 { return const_cast<__filebuf_type*>(&_M_filebuf); }
00797
00802 bool
00803 is_open() { return _M_filebuf.is_open(); }
00804
00816 void
00817 open(const char* __s,
00818 ios_base::openmode __mode = ios_base::in | ios_base::out)
00819 {
00820 if (!_M_filebuf.open(__s, __mode))
00821 this->setstate(ios_base::failbit);
00822 }
00823
00830 void
00831 close()
00832 {
00833 if (!_M_filebuf.close())
00834 this->setstate(ios_base::failbit);
00835 }
00836 };
00837 }
00838
00839 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00840 # include <bits/fstream.tcc>
00841 #endif
00842
00843 #endif