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_SSTREAM
00035 #define _CPP_SSTREAM 1
00036
00037 #pragma GCC system_header
00038
00039 #include <bits/std_istream.h>
00040 #include <bits/std_ostream.h>
00041
00042 namespace std
00043 {
00044 template<typename _CharT, typename _Traits, typename _Alloc>
00045 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
00046 {
00047 public:
00048
00049 typedef _CharT char_type;
00050 typedef _Traits traits_type;
00051 typedef typename traits_type::int_type int_type;
00052 typedef typename traits_type::pos_type pos_type;
00053 typedef typename traits_type::off_type off_type;
00054
00055
00056 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
00057 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
00058 typedef typename __string_type::size_type __size_type;
00059
00060 private:
00061
00062 __string_type _M_string;
00063
00064 public:
00065
00066 explicit
00067 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
00068 : __streambuf_type(), _M_string()
00069 { _M_stringbuf_init(__mode); }
00070
00071 explicit
00072 basic_stringbuf(const __string_type& __str,
00073 ios_base::openmode __mode = ios_base::in | ios_base::out)
00074 : __streambuf_type(), _M_string(__str)
00075 { _M_stringbuf_init(__mode); }
00076
00077
00078 __string_type
00079 str() const
00080 {
00081 if (_M_mode & ios_base::in && !(_M_mode & ios_base::out))
00082 return _M_string;
00083 else
00084 {
00085
00086
00087
00088
00089 __size_type __len = _M_string.size();
00090 if (_M_out_cur > _M_out_beg)
00091 __len = max(__size_type(_M_out_end - _M_out_beg), __len);
00092 return __string_type(_M_out_beg, _M_out_beg + __len);
00093 }
00094 }
00095
00096 void
00097 str(const __string_type& __s)
00098 {
00099 _M_string = __s;
00100 _M_stringbuf_init(_M_mode);
00101 }
00102
00103 protected:
00104
00105 void
00106 _M_stringbuf_init(ios_base::openmode __mode)
00107 {
00108
00109
00110
00111
00112
00113 _M_buf_size = _M_string.size();
00114
00115
00116
00117
00118
00119 _M_buf_size_opt = 512;
00120 _M_mode = __mode;
00121 if (_M_mode & ios_base::ate)
00122 _M_really_sync(0, _M_buf_size);
00123 else
00124 _M_really_sync(0, 0);
00125 }
00126
00127
00128 virtual int_type
00129 underflow()
00130 {
00131 if (_M_in_cur && _M_in_cur < _M_in_end)
00132 return traits_type::to_int_type(*gptr());
00133 else
00134 return traits_type::eof();
00135 }
00136
00137 virtual int_type
00138 pbackfail(int_type __c = traits_type::eof());
00139
00140 virtual int_type
00141 overflow(int_type __c = traits_type::eof());
00142
00143 virtual __streambuf_type*
00144 setbuf(char_type* __s, streamsize __n)
00145 {
00146 if (__n)
00147 {
00148 _M_string = __string_type(__s, __n);
00149 _M_really_sync(0, 0);
00150 }
00151 return this;
00152 }
00153
00154 virtual pos_type
00155 seekoff(off_type __off, ios_base::seekdir __way,
00156 ios_base::openmode __mode = ios_base::in | ios_base::out);
00157
00158 virtual pos_type
00159 seekpos(pos_type __sp,
00160 ios_base::openmode __mode = ios_base::in | ios_base::out);
00161
00162
00163
00164
00165
00166
00167
00168 virtual int
00169 _M_really_sync(__size_type __i, __size_type __o)
00170 {
00171 char_type* __base = const_cast<char_type*>(_M_string.data());
00172 bool __testin = _M_mode & ios_base::in;
00173 bool __testout = _M_mode & ios_base::out;
00174 __size_type __len = _M_string.size();
00175
00176 _M_buf = __base;
00177 if (__testin)
00178 this->setg(__base, __base + __i, __base + __len);
00179 if (__testout)
00180 {
00181 this->setp(__base, __base + __len);
00182 _M_out_cur += __o;
00183 }
00184 return 0;
00185 }
00186 };
00187
00188
00189
00190 template<typename _CharT, typename _Traits, typename _Alloc>
00191 class basic_istringstream : public basic_istream<_CharT, _Traits>
00192 {
00193 public:
00194
00195 typedef _CharT char_type;
00196 typedef _Traits traits_type;
00197 typedef typename traits_type::int_type int_type;
00198 typedef typename traits_type::pos_type pos_type;
00199 typedef typename traits_type::off_type off_type;
00200
00201
00202 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00203 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
00204 typedef basic_istream<char_type, traits_type> __istream_type;
00205
00206
00207 explicit
00208 basic_istringstream(ios_base::openmode __mode = ios_base::in)
00209 : __istream_type(new __stringbuf_type(__mode | ios_base::in))
00210 { }
00211
00212 explicit
00213 basic_istringstream(const __string_type& __str,
00214 ios_base::openmode __mode = ios_base::in)
00215 : __istream_type(new __stringbuf_type(__str, __mode | ios_base::in))
00216 { }
00217
00218 ~basic_istringstream()
00219 {
00220 delete _M_streambuf;
00221 _M_streambuf = NULL;
00222 }
00223
00224
00225 __stringbuf_type*
00226 rdbuf() const
00227 { return static_cast<__stringbuf_type*>(_M_streambuf); }
00228
00229 __string_type
00230 str() const
00231 { return this->rdbuf()->str(); }
00232
00233 void
00234 str(const __string_type& __s)
00235 { rdbuf()->str(__s); }
00236
00237 };
00238
00239
00240
00241 template <typename _CharT, typename _Traits, typename _Alloc>
00242 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
00243 {
00244 public:
00245
00246 typedef _CharT char_type;
00247 typedef _Traits traits_type;
00248 typedef typename traits_type::int_type int_type;
00249 typedef typename traits_type::pos_type pos_type;
00250 typedef typename traits_type::off_type off_type;
00251
00252
00253 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00254 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
00255 typedef basic_ostream<char_type, traits_type> __ostream_type;
00256
00257
00258 explicit
00259 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
00260 : __ostream_type(new __stringbuf_type(__mode | ios_base::out))
00261 { }
00262
00263 explicit
00264 basic_ostringstream(const __string_type __str,
00265 ios_base::openmode __mode = ios_base::out)
00266 : __ostream_type(new __stringbuf_type(__str, __mode | ios_base::out))
00267 { }
00268
00269 ~basic_ostringstream()
00270 {
00271 delete _M_streambuf;
00272 _M_streambuf = NULL;
00273 }
00274
00275
00276 __stringbuf_type*
00277 rdbuf() const
00278 { return static_cast<__stringbuf_type*>(_M_streambuf); }
00279
00280 __string_type
00281 str() const
00282 { return this->rdbuf()->str(); }
00283
00284 void
00285 str(const __string_type& __s)
00286 { rdbuf()->str(__s); }
00287
00288 };
00289
00290
00291
00292 template <typename _CharT, typename _Traits, typename _Alloc>
00293 class basic_stringstream : public basic_iostream<_CharT, _Traits>
00294 {
00295 public:
00296
00297 typedef _CharT char_type;
00298 typedef _Traits traits_type;
00299 typedef typename traits_type::int_type int_type;
00300 typedef typename traits_type::pos_type pos_type;
00301 typedef typename traits_type::off_type off_type;
00302
00303
00304 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00305 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
00306 typedef basic_iostream<char_type, traits_type> __iostream_type;
00307
00308
00309 explicit
00310 basic_stringstream(ios_base::openmode __mode =
00311 ios_base::out | ios_base::in)
00312 : __iostream_type(new __stringbuf_type(__mode))
00313 { }
00314
00315 explicit
00316 basic_stringstream(const __string_type& __str,
00317 ios_base::openmode __mode =
00318 ios_base::out | ios_base::in)
00319 : __iostream_type(new __stringbuf_type(__str, __mode))
00320 { }
00321
00322 ~basic_stringstream()
00323 {
00324 delete _M_streambuf;
00325 _M_streambuf = NULL;
00326 }
00327
00328
00329 __stringbuf_type*
00330 rdbuf() const
00331 { return static_cast<__stringbuf_type*>(_M_streambuf); }
00332
00333 __string_type
00334 str() const
00335 { return rdbuf()->str(); }
00336
00337 void
00338 str(const __string_type& __s)
00339 { rdbuf()->str(__s); }
00340 };
00341
00342 }
00343
00344
00345
00346 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
00347 # define export
00348 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
00349 # include <bits/sstream.tcc>
00350 #endif
00351 #endif
00352
00353 #endif
00354