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_SSTREAM
00041 #define _GLIBCXX_SSTREAM 1
00042
00043 #pragma GCC system_header
00044
00045 #include <istream>
00046 #include <ostream>
00047
00048 namespace std
00049 {
00050
00062 template<typename _CharT, typename _Traits, typename _Alloc>
00063 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
00064 {
00065 public:
00066
00067 typedef _CharT char_type;
00068 typedef _Traits traits_type;
00069
00070
00071 typedef _Alloc allocator_type;
00072 typedef typename traits_type::int_type int_type;
00073 typedef typename traits_type::pos_type pos_type;
00074 typedef typename traits_type::off_type off_type;
00075
00077
00082 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
00083 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
00084 typedef typename __string_type::size_type __size_type;
00086
00087 protected:
00093 ios_base::openmode _M_mode;
00094
00095
00101 __string_type _M_string;
00102
00103 public:
00104
00112 explicit
00113 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
00114 : __streambuf_type(), _M_mode(__mode), _M_string()
00115 { }
00116
00125 explicit
00126 basic_stringbuf(const __string_type& __str,
00127 ios_base::openmode __mode = ios_base::in | ios_base::out)
00128 : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
00129 { _M_stringbuf_init(__mode); }
00130
00131
00140 __string_type
00141 str() const
00142 {
00143 if (this->pptr())
00144 {
00145
00146 if (this->pptr() > this->egptr())
00147 return __string_type(this->pbase(), this->pptr());
00148 else
00149 return __string_type(this->pbase(), this->egptr());
00150 }
00151 else
00152 return _M_string;
00153 }
00154
00162 void
00163 str(const __string_type& __s)
00164 {
00165
00166 _M_string.assign(__s.data(), __s.size());
00167 _M_stringbuf_init(this->_M_mode);
00168 }
00169
00170 protected:
00171
00177 void
00178 _M_stringbuf_init(ios_base::openmode __mode)
00179 {
00180 this->_M_mode = __mode;
00181
00182 __size_type __len = 0;
00183 if (this->_M_mode & (ios_base::ate | ios_base::app))
00184 __len = _M_string.size();
00185 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
00186 }
00187
00188 virtual streamsize
00189 showmanyc()
00190 {
00191 streamsize __ret = -1;
00192 if (_M_mode & ios_base::in)
00193 {
00194 _M_update_egptr();
00195 __ret = this->egptr() - this->gptr();
00196 }
00197 return __ret;
00198 }
00199
00200
00201 virtual int_type
00202 underflow();
00203
00204
00205 virtual int_type
00206 pbackfail(int_type __c = traits_type::eof());
00207
00208
00209 virtual int_type
00210 overflow(int_type __c = traits_type::eof());
00211
00223 virtual __streambuf_type*
00224 setbuf(char_type* __s, streamsize __n)
00225 {
00226 if (__s && __n >= 0)
00227 {
00228
00229
00230
00231
00232
00233
00234 _M_string = __string_type(__s, __n);
00235
00236
00237 _M_sync(__s, 0, 0);
00238 }
00239 return this;
00240 }
00241
00242
00243 virtual pos_type
00244 seekoff(off_type __off, ios_base::seekdir __way,
00245 ios_base::openmode __mode = ios_base::in | ios_base::out);
00246
00247
00248 virtual pos_type
00249 seekpos(pos_type __sp,
00250 ios_base::openmode __mode = ios_base::in | ios_base::out);
00251
00252
00253
00254
00255
00256
00257
00263 void
00264 _M_sync(char_type* __base, __size_type __i, __size_type __o)
00265 {
00266 const bool __testin = this->_M_mode & ios_base::in;
00267 const bool __testout = this->_M_mode & ios_base::out;
00268 const __size_type __len = _M_string.size();
00269
00270 if (__testin)
00271 this->setg(__base, __base + __i, __base + __len);
00272 if (__testout)
00273 {
00274 this->setp(__base, __base + _M_string.capacity());
00275 this->pbump(__o);
00276
00277
00278
00279
00280 if (!__testin)
00281 this->setg(__base + __len, __base + __len, __base + __len);
00282 }
00283 }
00284
00285
00286
00287 void
00288 _M_update_egptr()
00289 {
00290 const bool __testin = this->_M_mode & ios_base::in;
00291
00292 if (this->pptr() && this->pptr() > this->egptr())
00293 if (__testin)
00294 this->setg(this->eback(), this->gptr(), this->pptr());
00295 else
00296 this->setg(this->pptr(), this->pptr(), this->pptr());
00297 }
00298 };
00299
00300
00301
00310 template<typename _CharT, typename _Traits, typename _Alloc>
00311 class basic_istringstream : public basic_istream<_CharT, _Traits>
00312 {
00313 public:
00314
00315 typedef _CharT char_type;
00316 typedef _Traits traits_type;
00317
00318
00319 typedef _Alloc allocator_type;
00320 typedef typename traits_type::int_type int_type;
00321 typedef typename traits_type::pos_type pos_type;
00322 typedef typename traits_type::off_type off_type;
00323
00324
00325 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00326 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
00327 typedef basic_istream<char_type, traits_type> __istream_type;
00328
00329 private:
00335 __stringbuf_type _M_stringbuf;
00336
00337 public:
00338
00353 explicit
00354 basic_istringstream(ios_base::openmode __mode = ios_base::in)
00355 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
00356 { this->init(&_M_stringbuf); }
00357
00373 explicit
00374 basic_istringstream(const __string_type& __str,
00375 ios_base::openmode __mode = ios_base::in)
00376 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
00377 { this->init(&_M_stringbuf); }
00378
00385 ~basic_istringstream()
00386 { }
00387
00388
00395 __stringbuf_type*
00396 rdbuf() const
00397 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00398
00403 __string_type
00404 str() const
00405 { return _M_stringbuf.str(); }
00406
00413 void
00414 str(const __string_type& __s)
00415 { _M_stringbuf.str(__s); }
00416 };
00417
00418
00419
00428 template <typename _CharT, typename _Traits, typename _Alloc>
00429 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
00430 {
00431 public:
00432
00433 typedef _CharT char_type;
00434 typedef _Traits traits_type;
00435
00436
00437 typedef _Alloc allocator_type;
00438 typedef typename traits_type::int_type int_type;
00439 typedef typename traits_type::pos_type pos_type;
00440 typedef typename traits_type::off_type off_type;
00441
00442
00443 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00444 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
00445 typedef basic_ostream<char_type, traits_type> __ostream_type;
00446
00447 private:
00453 __stringbuf_type _M_stringbuf;
00454
00455 public:
00456
00471 explicit
00472 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
00473 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
00474 { this->init(&_M_stringbuf); }
00475
00491 explicit
00492 basic_ostringstream(const __string_type& __str,
00493 ios_base::openmode __mode = ios_base::out)
00494 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
00495 { this->init(&_M_stringbuf); }
00496
00503 ~basic_ostringstream()
00504 { }
00505
00506
00513 __stringbuf_type*
00514 rdbuf() const
00515 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00516
00521 __string_type
00522 str() const
00523 { return _M_stringbuf.str(); }
00524
00531 void
00532 str(const __string_type& __s)
00533 { _M_stringbuf.str(__s); }
00534 };
00535
00536
00537
00546 template <typename _CharT, typename _Traits, typename _Alloc>
00547 class basic_stringstream : public basic_iostream<_CharT, _Traits>
00548 {
00549 public:
00550
00551 typedef _CharT char_type;
00552 typedef _Traits traits_type;
00553
00554
00555 typedef _Alloc allocator_type;
00556 typedef typename traits_type::int_type int_type;
00557 typedef typename traits_type::pos_type pos_type;
00558 typedef typename traits_type::off_type off_type;
00559
00560
00561 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00562 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
00563 typedef basic_iostream<char_type, traits_type> __iostream_type;
00564
00565 private:
00571 __stringbuf_type _M_stringbuf;
00572
00573 public:
00574
00587 explicit
00588 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
00589 : __iostream_type(), _M_stringbuf(__m)
00590 { this->init(&_M_stringbuf); }
00591
00605 explicit
00606 basic_stringstream(const __string_type& __str,
00607 ios_base::openmode __m = ios_base::out | ios_base::in)
00608 : __iostream_type(), _M_stringbuf(__str, __m)
00609 { this->init(&_M_stringbuf); }
00610
00617 ~basic_stringstream()
00618 { }
00619
00620
00627 __stringbuf_type*
00628 rdbuf() const
00629 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00630
00635 __string_type
00636 str() const
00637 { return _M_stringbuf.str(); }
00638
00645 void
00646 str(const __string_type& __s)
00647 { _M_stringbuf.str(__s); }
00648 };
00649 }
00650
00651 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00652 # include <bits/sstream.tcc>
00653 #endif
00654
00655 #endif