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 #ifndef _CPP_FSTREAM
00035 #define _CPP_FSTREAM 1
00036
00037 #pragma GCC system_header
00038
00039 #include <bits/std_istream.h>
00040 #include <bits/std_ostream.h>
00041 #include <bits/basic_file.h>
00042 #include <bits/std_locale.h>
00043 #include <bits/c++threads.h>
00044
00045 namespace std
00046 {
00047 template<typename _CharT, typename _Traits>
00048 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
00049 {
00050 public:
00051
00052 typedef _CharT char_type;
00053 typedef _Traits traits_type;
00054 typedef typename traits_type::int_type int_type;
00055 typedef typename traits_type::pos_type pos_type;
00056 typedef typename traits_type::off_type off_type;
00057
00058
00059 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
00060 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00061 typedef __basic_file<char_type> __file_type;
00062 typedef typename traits_type::state_type __state_type;
00063 typedef codecvt<char_type, char, __state_type> __codecvt_type;
00064 typedef typename __codecvt_type::result __res_type;
00065 typedef ctype<char_type> __ctype_type;
00066
00067 friend class ios_base;
00068
00069 private:
00070
00071
00072 __file_type* _M_file;
00073
00074
00075 __state_type _M_state_cur;
00076 __state_type _M_state_beg;
00077
00078
00079 __c_lock _M_lock;
00080
00081
00082 bool _M_last_overflowed;
00083
00084 public:
00085
00086 basic_filebuf();
00087
00088
00089 basic_filebuf(__c_file_type* __f, bool __s, ios_base::openmode __mode);
00090
00091 virtual
00092 ~basic_filebuf()
00093 {
00094 this->close();
00095 _M_last_overflowed = false;
00096 }
00097
00098
00099 bool
00100 is_open(void) const { return _M_file ? _M_file->is_open() : false; }
00101
00102 __filebuf_type*
00103 open(const char* __s, ios_base::openmode __mode);
00104
00105 __filebuf_type*
00106 close(void);
00107
00108 protected:
00109
00110 void
00111 _M_allocate_buffers();
00112
00113
00114 void
00115 _M_filebuf_init();
00116
00117
00118 virtual streamsize
00119 showmanyc(void);
00120
00121
00122
00123
00124
00125 virtual int_type
00126 underflow(void);
00127
00128 virtual int_type
00129 pbackfail(int_type __c = _Traits::eof());
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 virtual int_type
00140 overflow(int_type __c = _Traits::eof());
00141
00142
00143
00144
00145
00146
00147
00148
00149 int_type
00150 _M_really_overflow(int_type __c = _Traits::eof());
00151
00152 virtual __streambuf_type*
00153 setbuf(char_type* __s, streamsize __n)
00154 {
00155 if (!this->is_open() && __s == 0 && __n == 0)
00156 {
00157 _M_buf_size = 0;
00158 _M_buf_size_opt = 0;
00159 }
00160 _M_last_overflowed = false;
00161 return this;
00162 }
00163
00164 virtual pos_type
00165 seekoff(off_type __off, ios_base::seekdir __way,
00166 ios_base::openmode __mode = ios_base::in | ios_base::out);
00167
00168 virtual pos_type
00169 seekpos(pos_type __pos,
00170 ios_base::openmode __mode = ios_base::in | ios_base::out);
00171
00172 virtual int
00173 sync(void)
00174 {
00175 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
00176 if (__testput)
00177 {
00178
00179
00180 _M_file->sync();
00181
00182
00183
00184
00185
00186 streamoff __cur = _M_file->seekoff(0, ios_base::cur);
00187 off_type __off = _M_out_cur - _M_out_beg;
00188 _M_really_overflow();
00189 _M_file->seekpos(__cur + __off);
00190 }
00191 _M_last_overflowed = false;
00192 return 0;
00193 }
00194
00195 virtual void
00196 imbue(const locale& __loc);
00197
00198 virtual streamsize
00199 xsgetn(char_type* __s, streamsize __n)
00200 {
00201 streamsize __ret = 0;
00202
00203 if (_M_pback_init)
00204 {
00205 while (__ret < __n && _M_in_cur < _M_in_end)
00206 {
00207 *__s = *_M_in_cur;
00208 ++__ret;
00209 ++__s;
00210 ++_M_in_cur;
00211 }
00212 _M_pback_destroy();
00213 }
00214 if (__ret < __n)
00215 __ret += __streambuf_type::xsgetn(__s, __n - __ret);
00216 return __ret;
00217 }
00218
00219 virtual streamsize
00220 xsputn(const char_type* __s, streamsize __n)
00221 {
00222 _M_pback_destroy();
00223 return __streambuf_type::xsputn(__s, __n);
00224 }
00225
00226 void
00227 _M_output_unshift();
00228 };
00229
00230
00231
00232 template<typename _CharT, typename _Traits>
00233 class basic_ifstream : public basic_istream<_CharT, _Traits>
00234 {
00235 public:
00236
00237 typedef _CharT char_type;
00238 typedef _Traits traits_type;
00239 typedef typename traits_type::int_type int_type;
00240 typedef typename traits_type::pos_type pos_type;
00241 typedef typename traits_type::off_type off_type;
00242
00243
00244 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00245 typedef basic_istream<char_type, traits_type> __istream_type;
00246
00247
00248 basic_ifstream()
00249 : __istream_type(new __filebuf_type())
00250 { }
00251
00252 explicit
00253 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
00254 : __istream_type(new __filebuf_type())
00255 { this->open(__s, __mode); }
00256
00257 ~basic_ifstream()
00258 {
00259 delete _M_streambuf;
00260 _M_streambuf = NULL;
00261 }
00262
00263
00264 __filebuf_type*
00265 rdbuf() const
00266 { return static_cast<__filebuf_type*>(_M_streambuf); }
00267
00268 bool
00269 is_open(void) { return rdbuf()->is_open(); }
00270
00271 void
00272 open(const char* __s, ios_base::openmode __mode = ios_base::in)
00273 {
00274 if (rdbuf()->open(__s, __mode | ios_base::in) == NULL)
00275 this->setstate(ios_base::failbit);
00276 }
00277
00278 void
00279 close(void)
00280 {
00281 if (!rdbuf()->close())
00282 this->setstate(ios_base::failbit);
00283 }
00284 };
00285
00286
00287
00288 template<typename _CharT, typename _Traits>
00289 class basic_ofstream : public basic_ostream<_CharT,_Traits>
00290 {
00291 public:
00292
00293 typedef _CharT char_type;
00294 typedef _Traits traits_type;
00295 typedef typename traits_type::int_type int_type;
00296 typedef typename traits_type::pos_type pos_type;
00297 typedef typename traits_type::off_type off_type;
00298
00299
00300 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00301 typedef basic_ostream<char_type, traits_type> __ostream_type;
00302
00303
00304 basic_ofstream()
00305 : __ostream_type(new __filebuf_type())
00306 { }
00307
00308 explicit
00309 basic_ofstream(const char* __s,
00310 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
00311 : __ostream_type(new __filebuf_type())
00312 { this->open(__s, __mode); }
00313
00314 ~basic_ofstream()
00315 {
00316 delete _M_streambuf;
00317 _M_streambuf = NULL;
00318 }
00319
00320
00321 __filebuf_type*
00322 rdbuf(void) const
00323 { return static_cast<__filebuf_type*>(_M_streambuf); }
00324
00325 bool
00326 is_open(void) { return rdbuf()->is_open(); }
00327
00328 void
00329 open(const char* __s,
00330 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
00331 {
00332 if (!rdbuf()->open(__s, __mode | ios_base::out))
00333 this->setstate(ios_base::failbit);
00334 }
00335
00336 void
00337 close(void)
00338 {
00339 if (!rdbuf()->close())
00340 setstate(ios_base::failbit);
00341 }
00342 };
00343
00344
00345
00346 template<typename _CharT, typename _Traits>
00347 class basic_fstream : public basic_iostream<_CharT, _Traits>
00348 {
00349 public:
00350
00351 typedef _CharT char_type;
00352 typedef _Traits traits_type;
00353 typedef typename traits_type::int_type int_type;
00354 typedef typename traits_type::pos_type pos_type;
00355 typedef typename traits_type::off_type off_type;
00356
00357
00358 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
00359 typedef basic_ios<char_type, traits_type> __ios_type;
00360 typedef basic_iostream<char_type, traits_type> __iostream_type;
00361
00362
00363 basic_fstream()
00364 : __iostream_type(new __filebuf_type())
00365 { }
00366
00367 explicit
00368 basic_fstream(const char* __s,
00369 ios_base::openmode __mode = ios_base::in | ios_base::out)
00370 : __iostream_type(new __filebuf_type())
00371 { this->open(__s, __mode); }
00372
00373 ~basic_fstream()
00374 {
00375 delete _M_streambuf;
00376 _M_streambuf = NULL;
00377 }
00378
00379
00380 __filebuf_type*
00381 rdbuf(void) const
00382 { return static_cast<__filebuf_type*>(_M_streambuf); }
00383
00384 bool
00385 is_open(void) { return rdbuf()->is_open(); }
00386
00387 void
00388 open(const char* __s,
00389 ios_base::openmode __mode = ios_base::in | ios_base::out)
00390 {
00391 if (!rdbuf()->open(__s, __mode))
00392 setstate (ios_base::failbit);
00393 }
00394
00395 void
00396 close(void)
00397 {
00398 if (!rdbuf()->close())
00399 setstate (ios_base::failbit);
00400 }
00401 };
00402 }
00403
00404
00405 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
00406 # define export
00407 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
00408 # include <bits/fstream.tcc>
00409 #endif
00410 #endif
00411
00412 #endif
00413