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