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
00039 #ifndef _GLIBCXX_FSTREAM
00040 #define _GLIBCXX_FSTREAM 1
00041
00042 #pragma GCC system_header
00043
00044 #include <istream>
00045 #include <ostream>
00046 #include <locale>
00047 #include <cstdio>
00048 #include <bits/basic_file.h>
00049 #include <bits/gthr.h>
00050
00051 namespace std
00052 {
00053
00062
00063
00064
00065
00066
00067 template<typename _CharT, typename _Traits>
00068 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
00069 {
00070 public:
00071
00072 typedef _CharT char_type;
00073 typedef _Traits traits_type;
00074 typedef typename traits_type::int_type int_type;
00075 typedef typename traits_type::pos_type pos_type;
00076 typedef typename traits_type::off_type off_type;
00077
00078 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
00079 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00080 typedef __basic_file<char> __file_type;
00081 typedef typename traits_type::state_type __state_type;
00082 typedef codecvt<char_type, char, __state_type> __codecvt_type;
00083
00084 friend class ios_base;
00085
00086 protected:
00087
00088
00089 __c_lock _M_lock;
00090
00091
00092 __file_type _M_file;
00093
00099 ios_base::openmode _M_mode;
00100
00101
00102 __state_type _M_state_beg;
00103
00104
00105
00106
00107 __state_type _M_state_cur;
00108
00109
00110
00111 __state_type _M_state_last;
00112
00118 char_type* _M_buf;
00119
00127 size_t _M_buf_size;
00128
00129
00130 bool _M_buf_allocated;
00131
00141 bool _M_reading;
00142 bool _M_writing;
00143
00145
00152 char_type _M_pback;
00153 char_type* _M_pback_cur_save;
00154 char_type* _M_pback_end_save;
00155 bool _M_pback_init;
00157
00158
00159 const __codecvt_type* _M_codecvt;
00160
00168 char* _M_ext_buf;
00169
00175 streamsize _M_ext_buf_size;
00176
00184 const char* _M_ext_next;
00185 char* _M_ext_end;
00186
00194 void
00195 _M_create_pback()
00196 {
00197 if (!_M_pback_init)
00198 {
00199 _M_pback_cur_save = this->gptr();
00200 _M_pback_end_save = this->egptr();
00201 this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
00202 _M_pback_init = true;
00203 }
00204 }
00205
00213 void
00214 _M_destroy_pback() throw()
00215 {
00216 if (_M_pback_init)
00217 {
00218
00219 _M_pback_cur_save += this->gptr() != this->eback();
00220 this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
00221 _M_pback_init = false;
00222 }
00223 }
00224
00225 public:
00226
00233 basic_filebuf();
00234
00238 virtual
00239 ~basic_filebuf()
00240 { this->close(); }
00241
00242
00246 bool
00247 is_open() const throw()
00248 { return _M_file.is_open(); }
00249
00263 __filebuf_type*
00264 open(const char* __s, ios_base::openmode __mode);
00265
00277 __filebuf_type*
00278 close() throw();
00279
00280 protected:
00281 void
00282 _M_allocate_internal_buffer();
00283
00284 void
00285 _M_destroy_internal_buffer() throw();
00286
00287
00288 virtual streamsize
00289 showmanyc();
00290
00291
00292
00293
00294
00295
00296 virtual int_type
00297 underflow();
00298
00299 virtual int_type
00300 pbackfail(int_type __c = _Traits::eof());
00301
00302
00303
00304
00305
00306
00307
00308
00309 virtual int_type
00310 overflow(int_type __c = _Traits::eof());
00311
00312
00313
00314 bool
00315 _M_convert_to_external(char_type*, streamsize);
00316
00329 virtual __streambuf_type*
00330 setbuf(char_type* __s, streamsize __n);
00331
00332 virtual pos_type
00333 seekoff(off_type __off, ios_base::seekdir __way,
00334 ios_base::openmode __mode = ios_base::in | ios_base::out);
00335
00336 virtual pos_type
00337 seekpos(pos_type __pos,
00338 ios_base::openmode __mode = ios_base::in | ios_base::out);
00339
00340
00341 pos_type
00342 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
00343
00344 virtual int
00345 sync();
00346
00347 virtual void
00348 imbue(const locale& __loc);
00349
00350 virtual streamsize
00351 xsgetn(char_type* __s, streamsize __n);
00352
00353 virtual streamsize
00354 xsputn(const char_type* __s, streamsize __n);
00355
00356
00357 bool
00358 _M_terminate_output();
00359
00374 void
00375 _M_set_buffer(streamsize __off)
00376 {
00377 const bool __testin = _M_mode & ios_base::in;
00378 const bool __testout = _M_mode & ios_base::out;
00379
00380 if (__testin && __off > 0)
00381 this->setg(_M_buf, _M_buf, _M_buf + __off);
00382 else
00383 this->setg(_M_buf, _M_buf, _M_buf);
00384
00385 if (__testout && __off == 0 && _M_buf_size > 1 )
00386 this->setp(_M_buf, _M_buf + _M_buf_size - 1);
00387 else
00388 this->setp(NULL, NULL);
00389 }
00390 };
00391
00392
00401 template<typename _CharT, typename _Traits>
00402 class basic_ifstream : public basic_istream<_CharT, _Traits>
00403 {
00404 public:
00405
00406 typedef _CharT char_type;
00407 typedef _Traits traits_type;
00408 typedef typename traits_type::int_type int_type;
00409 typedef typename traits_type::pos_type pos_type;
00410 typedef typename traits_type::off_type off_type;
00411
00412
00413 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00414 typedef basic_istream<char_type, traits_type> __istream_type;
00415
00416 private:
00417 __filebuf_type _M_filebuf;
00418
00419 public:
00420
00428 basic_ifstream() : __istream_type(), _M_filebuf()
00429 { this->init(&_M_filebuf); }
00430
00441 explicit
00442 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
00443 : __istream_type(), _M_filebuf()
00444 {
00445 this->init(&_M_filebuf);
00446 this->open(__s, __mode);
00447 }
00448
00455 ~basic_ifstream()
00456 { }
00457
00458
00465 __filebuf_type*
00466 rdbuf() const
00467 { return const_cast<__filebuf_type*>(&_M_filebuf); }
00468
00473 bool
00474 is_open()
00475 { return _M_filebuf.is_open(); }
00476
00477
00478
00479 bool
00480 is_open() const
00481 { return _M_filebuf.is_open(); }
00482
00494 void
00495 open(const char* __s, ios_base::openmode __mode = ios_base::in)
00496 {
00497 if (!_M_filebuf.open(__s, __mode | ios_base::in))
00498 this->setstate(ios_base::failbit);
00499 else
00500
00501
00502 this->clear();
00503 }
00504
00511 void
00512 close()
00513 {
00514 if (!_M_filebuf.close())
00515 this->setstate(ios_base::failbit);
00516 }
00517 };
00518
00519
00520
00529 template<typename _CharT, typename _Traits>
00530 class basic_ofstream : public basic_ostream<_CharT,_Traits>
00531 {
00532 public:
00533
00534 typedef _CharT char_type;
00535 typedef _Traits traits_type;
00536 typedef typename traits_type::int_type int_type;
00537 typedef typename traits_type::pos_type pos_type;
00538 typedef typename traits_type::off_type off_type;
00539
00540
00541 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00542 typedef basic_ostream<char_type, traits_type> __ostream_type;
00543
00544 private:
00545 __filebuf_type _M_filebuf;
00546
00547 public:
00548
00556 basic_ofstream(): __ostream_type(), _M_filebuf()
00557 { this->init(&_M_filebuf); }
00558
00570 explicit
00571 basic_ofstream(const char* __s,
00572 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
00573 : __ostream_type(), _M_filebuf()
00574 {
00575 this->init(&_M_filebuf);
00576 this->open(__s, __mode);
00577 }
00578
00585 ~basic_ofstream()
00586 { }
00587
00588
00595 __filebuf_type*
00596 rdbuf() const
00597 { return const_cast<__filebuf_type*>(&_M_filebuf); }
00598
00603 bool
00604 is_open()
00605 { return _M_filebuf.is_open(); }
00606
00607
00608
00609 bool
00610 is_open() const
00611 { return _M_filebuf.is_open(); }
00612
00624 void
00625 open(const char* __s,
00626 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
00627 {
00628 if (!_M_filebuf.open(__s, __mode | ios_base::out))
00629 this->setstate(ios_base::failbit);
00630 else
00631
00632
00633 this->clear();
00634 }
00635
00642 void
00643 close()
00644 {
00645 if (!_M_filebuf.close())
00646 this->setstate(ios_base::failbit);
00647 }
00648 };
00649
00650
00651
00660 template<typename _CharT, typename _Traits>
00661 class basic_fstream : public basic_iostream<_CharT, _Traits>
00662 {
00663 public:
00664
00665 typedef _CharT char_type;
00666 typedef _Traits traits_type;
00667 typedef typename traits_type::int_type int_type;
00668 typedef typename traits_type::pos_type pos_type;
00669 typedef typename traits_type::off_type off_type;
00670
00671
00672 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00673 typedef basic_ios<char_type, traits_type> __ios_type;
00674 typedef basic_iostream<char_type, traits_type> __iostream_type;
00675
00676 private:
00677 __filebuf_type _M_filebuf;
00678
00679 public:
00680
00688 basic_fstream()
00689 : __iostream_type(), _M_filebuf()
00690 { this->init(&_M_filebuf); }
00691
00700 explicit
00701 basic_fstream(const char* __s,
00702 ios_base::openmode __mode = ios_base::in | ios_base::out)
00703 : __iostream_type(NULL), _M_filebuf()
00704 {
00705 this->init(&_M_filebuf);
00706 this->open(__s, __mode);
00707 }
00708
00715 ~basic_fstream()
00716 { }
00717
00718
00725 __filebuf_type*
00726 rdbuf() const
00727 { return const_cast<__filebuf_type*>(&_M_filebuf); }
00728
00733 bool
00734 is_open()
00735 { return _M_filebuf.is_open(); }
00736
00737
00738
00739 bool
00740 is_open() const
00741 { return _M_filebuf.is_open(); }
00742
00754 void
00755 open(const char* __s,
00756 ios_base::openmode __mode = ios_base::in | ios_base::out)
00757 {
00758 if (!_M_filebuf.open(__s, __mode))
00759 this->setstate(ios_base::failbit);
00760 else
00761
00762
00763 this->clear();
00764 }
00765
00772 void
00773 close()
00774 {
00775 if (!_M_filebuf.close())
00776 this->setstate(ios_base::failbit);
00777 }
00778 };
00779 }
00780
00781 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00782 # include <bits/fstream.tcc>
00783 #endif
00784
00785 #endif