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_STREAMBUF
00035 #define _CPP_STREAMBUF 1
00036
00037 #pragma GCC system_header
00038
00039 #include <bits/c++config.h>
00040 #include <bits/std_iosfwd.h>
00041 #include <bits/std_cstdio.h>
00042 #include <bits/localefwd.h>
00043 #include <bits/ios_base.h>
00044
00045 namespace std
00046 {
00047 template<typename _CharT, typename _Traits>
00048 streamsize
00049 __copy_streambufs(basic_ios<_CharT, _Traits>& _ios,
00050 basic_streambuf<_CharT, _Traits>* __sbin,
00051 basic_streambuf<_CharT, _Traits>* __sbout);
00052
00053
00054 template<typename _CharT, typename _Traits>
00055 class basic_streambuf
00056 {
00057 public:
00058
00059 typedef _CharT char_type;
00060 typedef _Traits traits_type;
00061 typedef typename traits_type::int_type int_type;
00062 typedef typename traits_type::pos_type pos_type;
00063 typedef typename traits_type::off_type off_type;
00064
00065
00066 typedef ctype<char_type> __ctype_type;
00067 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
00068
00069 friend class basic_ios<char_type, traits_type>;
00070 friend class basic_istream<char_type, traits_type>;
00071 friend class basic_ostream<char_type, traits_type>;
00072 friend class istreambuf_iterator<char_type, traits_type>;
00073 friend class ostreambuf_iterator<char_type, traits_type>;
00074
00075 friend streamsize
00076 __copy_streambufs<>(basic_ios<char_type, traits_type>& __ios,
00077 __streambuf_type* __sbin,__streambuf_type* __sbout);
00078
00079 protected:
00080
00081
00082
00083
00084
00085
00086 char_type* _M_buf;
00087
00088
00089 int_type _M_buf_size;
00090
00091
00092 int_type _M_buf_size_opt;
00093
00094
00095
00096 bool _M_buf_unified;
00097
00098
00099
00100
00101
00102
00103 char_type* _M_in_beg;
00104 char_type* _M_in_cur;
00105 char_type* _M_in_end;
00106 char_type* _M_out_beg;
00107 char_type* _M_out_cur;
00108 char_type* _M_out_end;
00109
00110
00111 ios_base::openmode _M_mode;
00112
00113
00114 locale _M_buf_locale;
00115
00116
00117 bool _M_buf_locale_init;
00118
00119
00120
00121
00122
00123
00124 int_type _M_pback_size;
00125 char_type* _M_pback;
00126 char_type* _M_pback_cur_save;
00127 char_type* _M_pback_end_save;
00128 bool _M_pback_init;
00129
00130
00131
00132
00133 void
00134 _M_pback_create()
00135 {
00136 if (!_M_pback_init)
00137 {
00138 int_type __dist = _M_in_end - _M_in_cur;
00139 int_type __len = min(_M_pback_size, __dist);
00140 traits_type::copy(_M_pback, _M_in_cur, __len);
00141 _M_pback_cur_save = _M_in_cur;
00142 _M_pback_end_save = _M_in_end;
00143 this->setg(_M_pback, _M_pback, _M_pback + __len);
00144 _M_pback_init = true;
00145 }
00146 }
00147
00148
00149
00150
00151 void
00152 _M_pback_destroy()
00153 {
00154 if (_M_pback_init)
00155 {
00156
00157 int_type __off_cur = _M_in_cur - _M_pback;
00158
00159
00160 int_type __off_end = 0;
00161 int_type __pback_len = _M_in_end - _M_pback;
00162 int_type __save_len = _M_pback_end_save - _M_buf;
00163 if (__pback_len > __save_len)
00164 __off_end = __pback_len - __save_len;
00165
00166 this->setg(_M_buf, _M_pback_cur_save + __off_cur,
00167 _M_pback_end_save + __off_end);
00168 _M_pback_cur_save = NULL;
00169 _M_pback_end_save = NULL;
00170 _M_pback_init = false;
00171 }
00172 }
00173
00174
00175
00176 void
00177 _M_in_cur_move(off_type __n)
00178 {
00179 bool __testout = _M_out_cur;
00180 _M_in_cur += __n;
00181 if (__testout && _M_buf_unified)
00182 _M_out_cur += __n;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 void
00194 _M_out_cur_move(off_type __n)
00195 {
00196 bool __testin = _M_in_cur;
00197
00198 _M_out_cur += __n;
00199 if (__testin && _M_buf_unified)
00200 _M_in_cur += __n;
00201 if (_M_out_cur > _M_out_end)
00202 {
00203 _M_out_end = _M_out_cur;
00204
00205 if (__testin)
00206 _M_in_end += __n;
00207 }
00208 }
00209
00210
00211
00212
00213 off_type
00214 _M_out_buf_size()
00215 {
00216 off_type __ret = 0;
00217 if (_M_out_cur)
00218 {
00219
00220 if (_M_out_beg == _M_buf)
00221 __ret = _M_out_beg + _M_buf_size - _M_out_cur;
00222
00223 else
00224 __ret = _M_out_end - _M_out_cur;
00225 }
00226 return __ret;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236 void
00237 _M_set_indeterminate(void)
00238 {
00239 if (_M_mode & ios_base::in)
00240 this->setg(_M_buf, _M_buf, _M_buf);
00241 if (_M_mode & ios_base::out)
00242 this->setp(_M_buf, _M_buf);
00243 }
00244
00245 void
00246 _M_set_determinate(off_type __off)
00247 {
00248 bool __testin = _M_mode & ios_base::in;
00249 bool __testout = _M_mode & ios_base::out;
00250 if (__testin)
00251 this->setg(_M_buf, _M_buf, _M_buf + __off);
00252 if (__testout)
00253 this->setp(_M_buf, _M_buf + __off);
00254
00255 }
00256
00257 bool
00258 _M_is_indeterminate(void)
00259 {
00260 bool __ret = false;
00261 if (_M_mode & ios_base::in)
00262 __ret = _M_in_beg == _M_in_cur && _M_in_cur == _M_in_end;
00263 if (_M_mode & ios_base::out)
00264 __ret = _M_out_beg == _M_out_cur && _M_out_cur == _M_out_end;
00265 return __ret;
00266 }
00267
00268 public:
00269 virtual
00270 ~basic_streambuf()
00271 {
00272 _M_buf_unified = false;
00273 _M_buf_size = 0;
00274 _M_buf_size_opt = 0;
00275 _M_mode = ios_base::openmode(0);
00276 _M_buf_locale_init = false;
00277
00278 }
00279
00280
00281 locale
00282 pubimbue(const locale &__loc)
00283 {
00284 locale __tmp(this->getloc());
00285 this->imbue(__loc);
00286 return __tmp;
00287 }
00288
00289 locale
00290 getloc() const
00291 {
00292 if (_M_buf_locale_init)
00293 return _M_buf_locale;
00294 else
00295 return locale();
00296 }
00297
00298
00299 __streambuf_type*
00300 pubsetbuf(char_type* __s, streamsize __n)
00301 { return this->setbuf(__s, __n); }
00302
00303 pos_type
00304 pubseekoff(off_type __off, ios_base::seekdir __way,
00305 ios_base::openmode __mode = ios_base::in | ios_base::out)
00306 { return this->seekoff(__off, __way, __mode); }
00307
00308 pos_type
00309 pubseekpos(pos_type __sp,
00310 ios_base::openmode __mode = ios_base::in | ios_base::out)
00311 { return this->seekpos(__sp, __mode); }
00312
00313 int
00314 pubsync() { return this->sync(); }
00315
00316
00317
00318 streamsize
00319 in_avail()
00320 {
00321 streamsize __ret;
00322 if (_M_in_cur && _M_in_cur < _M_in_end)
00323 {
00324 if (_M_pback_init)
00325 {
00326 int_type __save_len = _M_pback_end_save - _M_pback_cur_save;
00327 int_type __pback_len = _M_in_cur - _M_pback;
00328 __ret = __save_len - __pback_len;
00329 }
00330 else
00331 __ret = this->egptr() - this->gptr();
00332 }
00333 else
00334 __ret = this->showmanyc();
00335 return __ret;
00336 }
00337
00338 int_type
00339 snextc()
00340 {
00341 int_type __eof = traits_type::eof();
00342 return (this->sbumpc() == __eof ? __eof : this->sgetc());
00343 }
00344
00345 int_type
00346 sbumpc();
00347
00348 int_type
00349 sgetc()
00350 {
00351 int_type __ret;
00352 if (_M_in_cur && _M_in_cur < _M_in_end)
00353 __ret = traits_type::to_int_type(*gptr());
00354 else
00355 __ret = this->underflow();
00356 return __ret;
00357 }
00358
00359 streamsize
00360 sgetn(char_type* __s, streamsize __n)
00361 { return this->xsgetn(__s, __n); }
00362
00363
00364 int_type
00365 sputbackc(char_type __c);
00366
00367 int_type
00368 sungetc();
00369
00370
00371 int_type
00372 sputc(char_type __c);
00373
00374 streamsize
00375 sputn(const char_type* __s, streamsize __n)
00376 { return this->xsputn(__s, __n); }
00377
00378 protected:
00379 basic_streambuf()
00380 : _M_buf(NULL), _M_buf_size(0),
00381 _M_buf_size_opt(static_cast<int_type>(BUFSIZ)), _M_buf_unified(false),
00382 _M_in_beg(0), _M_in_cur(0), _M_in_end(0), _M_out_beg(0), _M_out_cur(0),
00383 _M_out_end(0), _M_mode(ios_base::openmode(0)), _M_buf_locale(locale()),
00384 _M_buf_locale_init(false), _M_pback_size(1), _M_pback(NULL),
00385 _M_pback_cur_save(NULL), _M_pback_end_save(NULL), _M_pback_init(false)
00386 { }
00387
00388
00389 char_type*
00390 eback() const { return _M_in_beg; }
00391
00392 char_type*
00393 gptr() const { return _M_in_cur; }
00394
00395 char_type*
00396 egptr() const { return _M_in_end; }
00397
00398 void
00399 gbump(int __n) { _M_in_cur += __n; }
00400
00401 void
00402 setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
00403 {
00404 _M_in_beg = __gbeg;
00405 _M_in_cur = __gnext;
00406 _M_in_end = __gend;
00407 if (!(_M_mode & ios_base::in) && __gbeg && __gnext && __gend)
00408 _M_mode = _M_mode | ios_base::in;
00409 }
00410
00411
00412 char_type*
00413 pbase() const { return _M_out_beg; }
00414
00415 char_type*
00416 pptr() const { return _M_out_cur; }
00417
00418 char_type*
00419 epptr() const { return _M_out_end; }
00420
00421 void
00422 pbump(int __n) { _M_out_cur += __n; }
00423
00424 void
00425 setp(char_type* __pbeg, char_type* __pend)
00426 {
00427 _M_out_beg = _M_out_cur = __pbeg;
00428 _M_out_end = __pend;
00429 if (!(_M_mode & ios_base::out) && __pbeg && __pend)
00430 _M_mode = _M_mode | ios_base::out;
00431 }
00432
00433
00434
00435 virtual void
00436 imbue(const locale& __loc)
00437 {
00438 _M_buf_locale_init = true;
00439 if (_M_buf_locale != __loc)
00440 _M_buf_locale = __loc;
00441 }
00442
00443
00444 virtual basic_streambuf<char_type,_Traits>*
00445 setbuf(char_type*, streamsize)
00446 { return this; }
00447
00448 virtual pos_type
00449 seekoff(off_type, ios_base::seekdir,
00450 ios_base::openmode = ios_base::in | ios_base::out)
00451 { return pos_type(off_type(-1)); }
00452
00453 virtual pos_type
00454 seekpos(pos_type,
00455 ios_base::openmode = ios_base::in | ios_base::out)
00456 { return pos_type(off_type(-1)); }
00457
00458 virtual int
00459 sync() { return 0; }
00460
00461
00462 virtual streamsize
00463 showmanyc() { return 0; }
00464
00465 virtual streamsize
00466 xsgetn(char_type* __s, streamsize __n);
00467
00468 virtual int_type
00469 underflow()
00470 { return traits_type::eof(); }
00471
00472 virtual int_type
00473 uflow()
00474 {
00475 int_type __ret = traits_type::eof();
00476 bool __testeof = this->underflow() == __ret;
00477 bool __testpending = _M_in_cur && _M_in_cur < _M_in_end;
00478 if (!__testeof && __testpending)
00479 {
00480 __ret = traits_type::to_int_type(*_M_in_cur);
00481 ++_M_in_cur;
00482 if (_M_buf_unified && _M_mode & ios_base::out)
00483 ++_M_out_cur;
00484 }
00485 return __ret;
00486 }
00487
00488
00489 virtual int_type
00490 pbackfail(int_type = traits_type::eof())
00491 { return traits_type::eof(); }
00492
00493
00494 virtual streamsize
00495 xsputn(const char_type* __s, streamsize __n);
00496
00497 virtual int_type
00498 overflow(int_type = traits_type::eof())
00499 { return traits_type::eof(); }
00500
00501 #ifdef _GLIBCPP_DEPRECATED
00502 public:
00503 void
00504 stossc()
00505 {
00506 if (_M_in_cur < _M_in_end)
00507 ++_M_in_cur;
00508 else
00509 this->uflow();
00510 }
00511 #endif
00512
00513 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00514 private:
00515 basic_streambuf(const __streambuf_type&);
00516
00517 __streambuf_type&
00518 operator=(const __streambuf_type&);
00519 #endif
00520 };
00521
00522 }
00523
00524 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
00525 # define export
00526 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
00527 #include <bits/streambuf.tcc>
00528 #endif
00529 #endif
00530
00531 #endif
00532