|
libstdc++
|
00001 // String based streams -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 00004 // 2006, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 3, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 /** @file include/sstream 00027 * This is a Standard C++ Library header. 00028 */ 00029 00030 // 00031 // ISO C++ 14882: 27.7 String-based streams 00032 // 00033 00034 #ifndef _GLIBCXX_SSTREAM 00035 #define _GLIBCXX_SSTREAM 1 00036 00037 #pragma GCC system_header 00038 00039 #include <istream> 00040 #include <ostream> 00041 00042 namespace std _GLIBCXX_VISIBILITY(default) 00043 { 00044 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00045 00046 // [27.7.1] template class basic_stringbuf 00047 /** 00048 * @brief The actual work of input and output (for std::string). 00049 * @ingroup io 00050 * 00051 * @tparam _CharT Type of character stream. 00052 * @tparam _Traits Traits for character type, defaults to 00053 * char_traits<_CharT>. 00054 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 00055 * 00056 * This class associates either or both of its input and output sequences 00057 * with a sequence of characters, which can be initialized from, or made 00058 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.) 00059 * 00060 * For this class, open modes (of type @c ios_base::openmode) have 00061 * @c in set if the input sequence can be read, and @c out set if the 00062 * output sequence can be written. 00063 */ 00064 template<typename _CharT, typename _Traits, typename _Alloc> 00065 class basic_stringbuf : public basic_streambuf<_CharT, _Traits> 00066 { 00067 public: 00068 // Types: 00069 typedef _CharT char_type; 00070 typedef _Traits traits_type; 00071 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00072 // 251. basic_stringbuf missing allocator_type 00073 typedef _Alloc allocator_type; 00074 typedef typename traits_type::int_type int_type; 00075 typedef typename traits_type::pos_type pos_type; 00076 typedef typename traits_type::off_type off_type; 00077 00078 typedef basic_streambuf<char_type, traits_type> __streambuf_type; 00079 typedef basic_string<char_type, _Traits, _Alloc> __string_type; 00080 typedef typename __string_type::size_type __size_type; 00081 00082 protected: 00083 /// Place to stash in || out || in | out settings for current stringbuf. 00084 ios_base::openmode _M_mode; 00085 00086 // Data Members: 00087 __string_type _M_string; 00088 00089 public: 00090 // Constructors: 00091 /** 00092 * @brief Starts with an empty string buffer. 00093 * @param __mode Whether the buffer can read, or write, or both. 00094 * 00095 * The default constructor initializes the parent class using its 00096 * own default ctor. 00097 */ 00098 explicit 00099 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out) 00100 : __streambuf_type(), _M_mode(__mode), _M_string() 00101 { } 00102 00103 /** 00104 * @brief Starts with an existing string buffer. 00105 * @param __str A string to copy as a starting buffer. 00106 * @param __mode Whether the buffer can read, or write, or both. 00107 * 00108 * This constructor initializes the parent class using its 00109 * own default ctor. 00110 */ 00111 explicit 00112 basic_stringbuf(const __string_type& __str, 00113 ios_base::openmode __mode = ios_base::in | ios_base::out) 00114 : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size()) 00115 { _M_stringbuf_init(__mode); } 00116 00117 // Get and set: 00118 /** 00119 * @brief Copying out the string buffer. 00120 * @return A copy of one of the underlying sequences. 00121 * 00122 * <em>If the buffer is only created in input mode, the underlying 00123 * character sequence is equal to the input sequence; otherwise, it 00124 * is equal to the output sequence.</em> [27.7.1.2]/1 00125 */ 00126 __string_type 00127 str() const 00128 { 00129 __string_type __ret; 00130 if (this->pptr()) 00131 { 00132 // The current egptr() may not be the actual string end. 00133 if (this->pptr() > this->egptr()) 00134 __ret = __string_type(this->pbase(), this->pptr()); 00135 else 00136 __ret = __string_type(this->pbase(), this->egptr()); 00137 } 00138 else 00139 __ret = _M_string; 00140 return __ret; 00141 } 00142 00143 /** 00144 * @brief Setting a new buffer. 00145 * @param __s The string to use as a new sequence. 00146 * 00147 * Deallocates any previous stored sequence, then copies @a s to 00148 * use as a new one. 00149 */ 00150 void 00151 str(const __string_type& __s) 00152 { 00153 // Cannot use _M_string = __s, since v3 strings are COW. 00154 _M_string.assign(__s.data(), __s.size()); 00155 _M_stringbuf_init(_M_mode); 00156 } 00157 00158 protected: 00159 // Common initialization code goes here. 00160 void 00161 _M_stringbuf_init(ios_base::openmode __mode) 00162 { 00163 _M_mode = __mode; 00164 __size_type __len = 0; 00165 if (_M_mode & (ios_base::ate | ios_base::app)) 00166 __len = _M_string.size(); 00167 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len); 00168 } 00169 00170 virtual streamsize 00171 showmanyc() 00172 { 00173 streamsize __ret = -1; 00174 if (_M_mode & ios_base::in) 00175 { 00176 _M_update_egptr(); 00177 __ret = this->egptr() - this->gptr(); 00178 } 00179 return __ret; 00180 } 00181 00182 virtual int_type 00183 underflow(); 00184 00185 virtual int_type 00186 pbackfail(int_type __c = traits_type::eof()); 00187 00188 virtual int_type 00189 overflow(int_type __c = traits_type::eof()); 00190 00191 /** 00192 * @brief Manipulates the buffer. 00193 * @param __s Pointer to a buffer area. 00194 * @param __n Size of @a __s. 00195 * @return @c this 00196 * 00197 * If no buffer has already been created, and both @a __s and @a __n are 00198 * non-zero, then @c __s is used as a buffer; see 00199 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html 00200 * for more. 00201 */ 00202 virtual __streambuf_type* 00203 setbuf(char_type* __s, streamsize __n) 00204 { 00205 if (__s && __n >= 0) 00206 { 00207 // This is implementation-defined behavior, and assumes 00208 // that an external char_type array of length __n exists 00209 // and has been pre-allocated. If this is not the case, 00210 // things will quickly blow up. 00211 00212 // Step 1: Destroy the current internal array. 00213 _M_string.clear(); 00214 00215 // Step 2: Use the external array. 00216 _M_sync(__s, __n, 0); 00217 } 00218 return this; 00219 } 00220 00221 virtual pos_type 00222 seekoff(off_type __off, ios_base::seekdir __way, 00223 ios_base::openmode __mode = ios_base::in | ios_base::out); 00224 00225 virtual pos_type 00226 seekpos(pos_type __sp, 00227 ios_base::openmode __mode = ios_base::in | ios_base::out); 00228 00229 // Internal function for correctly updating the internal buffer 00230 // for a particular _M_string, due to initialization or re-sizing 00231 // of an existing _M_string. 00232 void 00233 _M_sync(char_type* __base, __size_type __i, __size_type __o); 00234 00235 // Internal function for correctly updating egptr() to the actual 00236 // string end. 00237 void 00238 _M_update_egptr() 00239 { 00240 const bool __testin = _M_mode & ios_base::in; 00241 if (this->pptr() && this->pptr() > this->egptr()) 00242 { 00243 if (__testin) 00244 this->setg(this->eback(), this->gptr(), this->pptr()); 00245 else 00246 this->setg(this->pptr(), this->pptr(), this->pptr()); 00247 } 00248 } 00249 00250 // Works around the issue with pbump, part of the protected 00251 // interface of basic_streambuf, taking just an int. 00252 void 00253 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off); 00254 }; 00255 00256 00257 // [27.7.2] Template class basic_istringstream 00258 /** 00259 * @brief Controlling input for std::string. 00260 * @ingroup io 00261 * 00262 * @tparam _CharT Type of character stream. 00263 * @tparam _Traits Traits for character type, defaults to 00264 * char_traits<_CharT>. 00265 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 00266 * 00267 * This class supports reading from objects of type std::basic_string, 00268 * using the inherited functions from std::basic_istream. To control 00269 * the associated sequence, an instance of std::basic_stringbuf is used, 00270 * which this page refers to as @c sb. 00271 */ 00272 template<typename _CharT, typename _Traits, typename _Alloc> 00273 class basic_istringstream : public basic_istream<_CharT, _Traits> 00274 { 00275 public: 00276 // Types: 00277 typedef _CharT char_type; 00278 typedef _Traits traits_type; 00279 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00280 // 251. basic_stringbuf missing allocator_type 00281 typedef _Alloc allocator_type; 00282 typedef typename traits_type::int_type int_type; 00283 typedef typename traits_type::pos_type pos_type; 00284 typedef typename traits_type::off_type off_type; 00285 00286 // Non-standard types: 00287 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 00288 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 00289 typedef basic_istream<char_type, traits_type> __istream_type; 00290 00291 private: 00292 __stringbuf_type _M_stringbuf; 00293 00294 public: 00295 // Constructors: 00296 /** 00297 * @brief Default constructor starts with an empty string buffer. 00298 * @param __mode Whether the buffer can read, or write, or both. 00299 * 00300 * @c ios_base::in is automatically included in @a __mode. 00301 * 00302 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base 00303 * class initializer. Does not allocate any buffer. 00304 * 00305 * That's a lie. We initialize the base class with NULL, because the 00306 * string class does its own memory management. 00307 */ 00308 explicit 00309 basic_istringstream(ios_base::openmode __mode = ios_base::in) 00310 : __istream_type(), _M_stringbuf(__mode | ios_base::in) 00311 { this->init(&_M_stringbuf); } 00312 00313 /** 00314 * @brief Starts with an existing string buffer. 00315 * @param __str A string to copy as a starting buffer. 00316 * @param __mode Whether the buffer can read, or write, or both. 00317 * 00318 * @c ios_base::in is automatically included in @a mode. 00319 * 00320 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb 00321 * to the base class initializer. 00322 * 00323 * That's a lie. We initialize the base class with NULL, because the 00324 * string class does its own memory management. 00325 */ 00326 explicit 00327 basic_istringstream(const __string_type& __str, 00328 ios_base::openmode __mode = ios_base::in) 00329 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in) 00330 { this->init(&_M_stringbuf); } 00331 00332 /** 00333 * @brief The destructor does nothing. 00334 * 00335 * The buffer is deallocated by the stringbuf object, not the 00336 * formatting stream. 00337 */ 00338 ~basic_istringstream() 00339 { } 00340 00341 // Members: 00342 /** 00343 * @brief Accessing the underlying buffer. 00344 * @return The current basic_stringbuf buffer. 00345 * 00346 * This hides both signatures of std::basic_ios::rdbuf(). 00347 */ 00348 __stringbuf_type* 00349 rdbuf() const 00350 { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 00351 00352 /** 00353 * @brief Copying out the string buffer. 00354 * @return @c rdbuf()->str() 00355 */ 00356 __string_type 00357 str() const 00358 { return _M_stringbuf.str(); } 00359 00360 /** 00361 * @brief Setting a new buffer. 00362 * @param __s The string to use as a new sequence. 00363 * 00364 * Calls @c rdbuf()->str(s). 00365 */ 00366 void 00367 str(const __string_type& __s) 00368 { _M_stringbuf.str(__s); } 00369 }; 00370 00371 00372 // [27.7.3] Template class basic_ostringstream 00373 /** 00374 * @brief Controlling output for std::string. 00375 * @ingroup io 00376 * 00377 * @tparam _CharT Type of character stream. 00378 * @tparam _Traits Traits for character type, defaults to 00379 * char_traits<_CharT>. 00380 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 00381 * 00382 * This class supports writing to objects of type std::basic_string, 00383 * using the inherited functions from std::basic_ostream. To control 00384 * the associated sequence, an instance of std::basic_stringbuf is used, 00385 * which this page refers to as @c sb. 00386 */ 00387 template <typename _CharT, typename _Traits, typename _Alloc> 00388 class basic_ostringstream : public basic_ostream<_CharT, _Traits> 00389 { 00390 public: 00391 // Types: 00392 typedef _CharT char_type; 00393 typedef _Traits traits_type; 00394 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00395 // 251. basic_stringbuf missing allocator_type 00396 typedef _Alloc allocator_type; 00397 typedef typename traits_type::int_type int_type; 00398 typedef typename traits_type::pos_type pos_type; 00399 typedef typename traits_type::off_type off_type; 00400 00401 // Non-standard types: 00402 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 00403 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 00404 typedef basic_ostream<char_type, traits_type> __ostream_type; 00405 00406 private: 00407 __stringbuf_type _M_stringbuf; 00408 00409 public: 00410 // Constructors/destructor: 00411 /** 00412 * @brief Default constructor starts with an empty string buffer. 00413 * @param __mode Whether the buffer can read, or write, or both. 00414 * 00415 * @c ios_base::out is automatically included in @a mode. 00416 * 00417 * Initializes @c sb using @c mode|out, and passes @c &sb to the base 00418 * class initializer. Does not allocate any buffer. 00419 * 00420 * That's a lie. We initialize the base class with NULL, because the 00421 * string class does its own memory management. 00422 */ 00423 explicit 00424 basic_ostringstream(ios_base::openmode __mode = ios_base::out) 00425 : __ostream_type(), _M_stringbuf(__mode | ios_base::out) 00426 { this->init(&_M_stringbuf); } 00427 00428 /** 00429 * @brief Starts with an existing string buffer. 00430 * @param __str A string to copy as a starting buffer. 00431 * @param __mode Whether the buffer can read, or write, or both. 00432 * 00433 * @c ios_base::out is automatically included in @a mode. 00434 * 00435 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb 00436 * to the base class initializer. 00437 * 00438 * That's a lie. We initialize the base class with NULL, because the 00439 * string class does its own memory management. 00440 */ 00441 explicit 00442 basic_ostringstream(const __string_type& __str, 00443 ios_base::openmode __mode = ios_base::out) 00444 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out) 00445 { this->init(&_M_stringbuf); } 00446 00447 /** 00448 * @brief The destructor does nothing. 00449 * 00450 * The buffer is deallocated by the stringbuf object, not the 00451 * formatting stream. 00452 */ 00453 ~basic_ostringstream() 00454 { } 00455 00456 // Members: 00457 /** 00458 * @brief Accessing the underlying buffer. 00459 * @return The current basic_stringbuf buffer. 00460 * 00461 * This hides both signatures of std::basic_ios::rdbuf(). 00462 */ 00463 __stringbuf_type* 00464 rdbuf() const 00465 { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 00466 00467 /** 00468 * @brief Copying out the string buffer. 00469 * @return @c rdbuf()->str() 00470 */ 00471 __string_type 00472 str() const 00473 { return _M_stringbuf.str(); } 00474 00475 /** 00476 * @brief Setting a new buffer. 00477 * @param __s The string to use as a new sequence. 00478 * 00479 * Calls @c rdbuf()->str(s). 00480 */ 00481 void 00482 str(const __string_type& __s) 00483 { _M_stringbuf.str(__s); } 00484 }; 00485 00486 00487 // [27.7.4] Template class basic_stringstream 00488 /** 00489 * @brief Controlling input and output for std::string. 00490 * @ingroup io 00491 * 00492 * @tparam _CharT Type of character stream. 00493 * @tparam _Traits Traits for character type, defaults to 00494 * char_traits<_CharT>. 00495 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 00496 * 00497 * This class supports reading from and writing to objects of type 00498 * std::basic_string, using the inherited functions from 00499 * std::basic_iostream. To control the associated sequence, an instance 00500 * of std::basic_stringbuf is used, which this page refers to as @c sb. 00501 */ 00502 template <typename _CharT, typename _Traits, typename _Alloc> 00503 class basic_stringstream : public basic_iostream<_CharT, _Traits> 00504 { 00505 public: 00506 // Types: 00507 typedef _CharT char_type; 00508 typedef _Traits traits_type; 00509 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00510 // 251. basic_stringbuf missing allocator_type 00511 typedef _Alloc allocator_type; 00512 typedef typename traits_type::int_type int_type; 00513 typedef typename traits_type::pos_type pos_type; 00514 typedef typename traits_type::off_type off_type; 00515 00516 // Non-standard Types: 00517 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 00518 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 00519 typedef basic_iostream<char_type, traits_type> __iostream_type; 00520 00521 private: 00522 __stringbuf_type _M_stringbuf; 00523 00524 public: 00525 // Constructors/destructors 00526 /** 00527 * @brief Default constructor starts with an empty string buffer. 00528 * @param __m Whether the buffer can read, or write, or both. 00529 * 00530 * Initializes @c sb using the mode from @c __m, and passes @c 00531 * &sb to the base class initializer. Does not allocate any 00532 * buffer. 00533 * 00534 * That's a lie. We initialize the base class with NULL, because the 00535 * string class does its own memory management. 00536 */ 00537 explicit 00538 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in) 00539 : __iostream_type(), _M_stringbuf(__m) 00540 { this->init(&_M_stringbuf); } 00541 00542 /** 00543 * @brief Starts with an existing string buffer. 00544 * @param __str A string to copy as a starting buffer. 00545 * @param __m Whether the buffer can read, or write, or both. 00546 * 00547 * Initializes @c sb using @a __str and @c __m, and passes @c &sb 00548 * to the base class initializer. 00549 * 00550 * That's a lie. We initialize the base class with NULL, because the 00551 * string class does its own memory management. 00552 */ 00553 explicit 00554 basic_stringstream(const __string_type& __str, 00555 ios_base::openmode __m = ios_base::out | ios_base::in) 00556 : __iostream_type(), _M_stringbuf(__str, __m) 00557 { this->init(&_M_stringbuf); } 00558 00559 /** 00560 * @brief The destructor does nothing. 00561 * 00562 * The buffer is deallocated by the stringbuf object, not the 00563 * formatting stream. 00564 */ 00565 ~basic_stringstream() 00566 { } 00567 00568 // Members: 00569 /** 00570 * @brief Accessing the underlying buffer. 00571 * @return The current basic_stringbuf buffer. 00572 * 00573 * This hides both signatures of std::basic_ios::rdbuf(). 00574 */ 00575 __stringbuf_type* 00576 rdbuf() const 00577 { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 00578 00579 /** 00580 * @brief Copying out the string buffer. 00581 * @return @c rdbuf()->str() 00582 */ 00583 __string_type 00584 str() const 00585 { return _M_stringbuf.str(); } 00586 00587 /** 00588 * @brief Setting a new buffer. 00589 * @param __s The string to use as a new sequence. 00590 * 00591 * Calls @c rdbuf()->str(s). 00592 */ 00593 void 00594 str(const __string_type& __s) 00595 { _M_stringbuf.str(__s); } 00596 }; 00597 00598 _GLIBCXX_END_NAMESPACE_VERSION 00599 } // namespace 00600 00601 #include <bits/sstream.tcc> 00602 00603 #endif /* _GLIBCXX_SSTREAM */