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