sstream

Go to the documentation of this file.
00001 // String based streams -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2002 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 2, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 // 00031 // ISO C++ 14882: 27.7 String-based streams 00032 // 00033 00039 #ifndef _CPP_SSTREAM 00040 #define _CPP_SSTREAM 1 00041 00042 #pragma GCC system_header 00043 00044 #include <istream> 00045 #include <ostream> 00046 00047 namespace std 00048 { 00049 template<typename _CharT, typename _Traits, typename _Alloc> 00050 class basic_stringbuf : public basic_streambuf<_CharT, _Traits> 00051 { 00052 public: 00053 // Types: 00054 typedef _CharT char_type; 00055 typedef _Traits traits_type; 00056 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00057 // 251. basic_stringbuf missing allocator_type 00058 typedef _Alloc allocator_type; 00059 #endif 00060 typedef typename traits_type::int_type int_type; 00061 typedef typename traits_type::pos_type pos_type; 00062 typedef typename traits_type::off_type off_type; 00063 00064 // Non-standard Types: 00065 typedef basic_streambuf<char_type, traits_type> __streambuf_type; 00066 typedef basic_string<char_type, _Traits, _Alloc> __string_type; 00067 typedef typename __string_type::size_type __size_type; 00068 00069 protected: 00070 // Data Members: 00071 __string_type _M_string; 00072 00073 public: 00074 // Constructors: 00075 explicit 00076 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out) 00077 : __streambuf_type(), _M_string() 00078 { _M_stringbuf_init(__mode); } 00079 00080 explicit 00081 basic_stringbuf(const __string_type& __str, 00082 ios_base::openmode __mode = ios_base::in | ios_base::out) 00083 : __streambuf_type(), _M_string(__str.data(), __str.size()) 00084 { _M_stringbuf_init(__mode); } 00085 00086 // Get and set: 00087 __string_type 00088 str() const 00089 { 00090 if (_M_mode & ios_base::out) 00091 { 00092 // This is the deal: _M_string.size() is a value that 00093 // represents the size of the initial string that makes 00094 // _M_string, and may not be the correct size of the 00095 // current stringbuf internal buffer. 00096 __size_type __len = _M_string.size(); 00097 if (_M_out_cur > _M_out_beg) 00098 __len = max(__size_type(_M_out_end - _M_out_beg), __len); 00099 return __string_type(_M_out_beg, _M_out_beg + __len); 00100 } 00101 else 00102 return _M_string; 00103 } 00104 00105 void 00106 str(const __string_type& __s) 00107 { 00108 // Cannot use _M_string = __s, since v3 strings are COW. 00109 _M_string.assign(__s.data(), __s.size()); 00110 _M_stringbuf_init(_M_mode); 00111 } 00112 00113 protected: 00114 // Common initialization code for both ctors goes here. 00115 void 00116 _M_stringbuf_init(ios_base::openmode __mode) 00117 { 00118 // _M_buf_size is a convenient alias for "what the streambuf 00119 // thinks the allocated size of the string really is." This is 00120 // necessary as ostringstreams are implemented with the 00121 // streambufs having control of the allocation and 00122 // re-allocation of the internal string object, _M_string. 00123 _M_buf_size = _M_string.size(); 00124 00125 // NB: Start ostringstream buffers at 512 bytes. This is an 00126 // experimental value (pronounced "arbitrary" in some of the 00127 // hipper english-speaking countries), and can be changed to 00128 // suit particular needs. 00129 _M_buf_size_opt = 512; 00130 _M_mode = __mode; 00131 if (_M_mode & (ios_base::ate | ios_base::app)) 00132 _M_really_sync(0, _M_buf_size); 00133 else 00134 _M_really_sync(0, 0); 00135 } 00136 00137 // Overridden virtual functions: 00138 virtual int_type 00139 underflow() 00140 { 00141 if (_M_in_cur && _M_in_cur < _M_in_end) 00142 return traits_type::to_int_type(*gptr()); 00143 else 00144 return traits_type::eof(); 00145 } 00146 00147 virtual int_type 00148 pbackfail(int_type __c = traits_type::eof()); 00149 00150 virtual int_type 00151 overflow(int_type __c = traits_type::eof()); 00152 00153 virtual __streambuf_type* 00154 setbuf(char_type* __s, streamsize __n) 00155 { 00156 if (__s && __n) 00157 { 00158 _M_string = __string_type(__s, __n); 00159 _M_really_sync(0, 0); 00160 } 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 __sp, 00170 ios_base::openmode __mode = ios_base::in | ios_base::out); 00171 00172 // Internal function for correctly updating the internal buffer 00173 // for a particular _M_string, due to initialization or 00174 // re-sizing of an existing _M_string. 00175 // Assumes: contents of _M_string and internal buffer match exactly. 00176 // __i == _M_in_cur - _M_in_beg 00177 // __o == _M_out_cur - _M_out_beg 00178 virtual int 00179 _M_really_sync(__size_type __i, __size_type __o) 00180 { 00181 char_type* __base = const_cast<char_type*>(_M_string.data()); 00182 bool __testin = _M_mode & ios_base::in; 00183 bool __testout = _M_mode & ios_base::out; 00184 __size_type __len = _M_string.size(); 00185 00186 _M_buf = __base; 00187 if (__testin) 00188 this->setg(__base, __base + __i, __base + __len); 00189 if (__testout) 00190 { 00191 this->setp(__base, __base + __len); 00192 _M_out_cur += __o; 00193 } 00194 return 0; 00195 } 00196 }; 00197 00198 00199 // 27.7.2 Template class basic_istringstream 00200 template<typename _CharT, typename _Traits, typename _Alloc> 00201 class basic_istringstream : public basic_istream<_CharT, _Traits> 00202 { 00203 public: 00204 // Types: 00205 typedef _CharT char_type; 00206 typedef _Traits traits_type; 00207 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00208 // 251. basic_stringbuf missing allocator_type 00209 typedef _Alloc allocator_type; 00210 #endif 00211 typedef typename traits_type::int_type int_type; 00212 typedef typename traits_type::pos_type pos_type; 00213 typedef typename traits_type::off_type off_type; 00214 00215 // Non-standard types: 00216 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 00217 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 00218 typedef basic_istream<char_type, traits_type> __istream_type; 00219 00220 private: 00221 __stringbuf_type _M_stringbuf; 00222 00223 public: 00224 // Constructors: 00225 explicit 00226 basic_istringstream(ios_base::openmode __mode = ios_base::in) 00227 : __istream_type(NULL), _M_stringbuf(__mode | ios_base::in) 00228 { this->init(&_M_stringbuf); } 00229 00230 explicit 00231 basic_istringstream(const __string_type& __str, 00232 ios_base::openmode __mode = ios_base::in) 00233 : __istream_type(NULL), _M_stringbuf(__str, __mode | ios_base::in) 00234 { this->init(&_M_stringbuf); } 00235 00236 ~basic_istringstream() 00237 { } 00238 00239 // Members: 00240 __stringbuf_type* 00241 rdbuf() const 00242 { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 00243 00244 __string_type 00245 str() const 00246 { return _M_stringbuf.str(); } 00247 00248 void 00249 str(const __string_type& __s) 00250 { _M_stringbuf.str(__s); } 00251 }; 00252 00253 00254 // 27.7.3 Template class basic_ostringstream 00255 template <typename _CharT, typename _Traits, typename _Alloc> 00256 class basic_ostringstream : public basic_ostream<_CharT, _Traits> 00257 { 00258 public: 00259 // Types: 00260 typedef _CharT char_type; 00261 typedef _Traits traits_type; 00262 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00263 // 251. basic_stringbuf missing allocator_type 00264 typedef _Alloc allocator_type; 00265 #endif 00266 typedef typename traits_type::int_type int_type; 00267 typedef typename traits_type::pos_type pos_type; 00268 typedef typename traits_type::off_type off_type; 00269 00270 // Non-standard types: 00271 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 00272 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 00273 typedef basic_ostream<char_type, traits_type> __ostream_type; 00274 00275 private: 00276 __stringbuf_type _M_stringbuf; 00277 00278 public: 00279 // Constructors/destructor: 00280 explicit 00281 basic_ostringstream(ios_base::openmode __mode = ios_base::out) 00282 : __ostream_type(NULL), _M_stringbuf(__mode | ios_base::out) 00283 { this->init(&_M_stringbuf); } 00284 00285 explicit 00286 basic_ostringstream(const __string_type& __str, 00287 ios_base::openmode __mode = ios_base::out) 00288 : __ostream_type(NULL), _M_stringbuf(__str, __mode | ios_base::out) 00289 { this->init(&_M_stringbuf); } 00290 00291 ~basic_ostringstream() 00292 { } 00293 00294 // Members: 00295 __stringbuf_type* 00296 rdbuf() const 00297 { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 00298 00299 __string_type 00300 str() const 00301 { return _M_stringbuf.str(); } 00302 00303 void 00304 str(const __string_type& __s) 00305 { _M_stringbuf.str(__s); } 00306 }; 00307 00308 00309 // 27.7.4 Template class basic_stringstream 00310 template <typename _CharT, typename _Traits, typename _Alloc> 00311 class basic_stringstream : public basic_iostream<_CharT, _Traits> 00312 { 00313 public: 00314 // Types: 00315 typedef _CharT char_type; 00316 typedef _Traits traits_type; 00317 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00318 // 251. basic_stringbuf missing allocator_type 00319 typedef _Alloc allocator_type; 00320 #endif 00321 typedef typename traits_type::int_type int_type; 00322 typedef typename traits_type::pos_type pos_type; 00323 typedef typename traits_type::off_type off_type; 00324 00325 // Non-standard Types: 00326 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 00327 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 00328 typedef basic_iostream<char_type, traits_type> __iostream_type; 00329 00330 private: 00331 __stringbuf_type _M_stringbuf; 00332 00333 public: 00334 // Constructors/destructors 00335 explicit 00336 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in) 00337 : __iostream_type(NULL), _M_stringbuf(__m) 00338 { this->init(&_M_stringbuf); } 00339 00340 explicit 00341 basic_stringstream(const __string_type& __str, 00342 ios_base::openmode __m = ios_base::out | ios_base::in) 00343 : __iostream_type(NULL), _M_stringbuf(__str, __m) 00344 { this->init(&_M_stringbuf); } 00345 00346 ~basic_stringstream() 00347 { } 00348 00349 // Members: 00350 __stringbuf_type* 00351 rdbuf() const 00352 { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 00353 00354 __string_type 00355 str() const 00356 { return _M_stringbuf.str(); } 00357 00358 void 00359 str(const __string_type& __s) 00360 { _M_stringbuf.str(__s); } 00361 }; 00362 } // namespace std 00363 00364 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT 00365 # define export 00366 #endif 00367 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS 00368 # include <bits/sstream.tcc> 00369 #endif 00370 00371 #endif

Generated on Wed Sep 29 13:54:50 2004 for libstdc++-v3 Source by doxygen 1.3.7