sstream.tcc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // String based streams -*- C++ -*-
  2. // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. // 2006, 2007, 2009
  4. // Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License and
  19. // a copy of the GCC Runtime Library Exception along with this program;
  20. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. // <http://www.gnu.org/licenses/>.
  22. /** @file sstream.tcc
  23. * This is an internal header file, included by other library headers.
  24. * You should not attempt to use it directly.
  25. */
  26. //
  27. // ISO C++ 14882: 27.7 String-based streams
  28. //
  29. #ifndef _SSTREAM_TCC
  30. #define _SSTREAM_TCC 1
  31. #pragma GCC system_header
  32. _GLIBCXX_BEGIN_NAMESPACE(std)
  33. template <class _CharT, class _Traits, class _Alloc>
  34. typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
  35. basic_stringbuf<_CharT, _Traits, _Alloc>::
  36. pbackfail(int_type __c)
  37. {
  38. int_type __ret = traits_type::eof();
  39. if (this->eback() < this->gptr())
  40. {
  41. // Try to put back __c into input sequence in one of three ways.
  42. // Order these tests done in is unspecified by the standard.
  43. const bool __testeof = traits_type::eq_int_type(__c, __ret);
  44. if (!__testeof)
  45. {
  46. const bool __testeq = traits_type::eq(traits_type::
  47. to_char_type(__c),
  48. this->gptr()[-1]);
  49. const bool __testout = this->_M_mode & ios_base::out;
  50. if (__testeq || __testout)
  51. {
  52. this->gbump(-1);
  53. if (!__testeq)
  54. *this->gptr() = traits_type::to_char_type(__c);
  55. __ret = __c;
  56. }
  57. }
  58. else
  59. {
  60. this->gbump(-1);
  61. __ret = traits_type::not_eof(__c);
  62. }
  63. }
  64. return __ret;
  65. }
  66. template <class _CharT, class _Traits, class _Alloc>
  67. typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
  68. basic_stringbuf<_CharT, _Traits, _Alloc>::
  69. overflow(int_type __c)
  70. {
  71. const bool __testout = this->_M_mode & ios_base::out;
  72. if (__builtin_expect(!__testout, false))
  73. return traits_type::eof();
  74. const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
  75. if (__builtin_expect(__testeof, false))
  76. return traits_type::not_eof(__c);
  77. const __size_type __capacity = _M_string.capacity();
  78. const __size_type __max_size = _M_string.max_size();
  79. const bool __testput = this->pptr() < this->epptr();
  80. if (__builtin_expect(!__testput && __capacity == __max_size, false))
  81. return traits_type::eof();
  82. // Try to append __c into output sequence in one of two ways.
  83. // Order these tests done in is unspecified by the standard.
  84. const char_type __conv = traits_type::to_char_type(__c);
  85. if (!__testput)
  86. {
  87. // NB: Start ostringstream buffers at 512 chars. This is an
  88. // experimental value (pronounced "arbitrary" in some of the
  89. // hipper English-speaking countries), and can be changed to
  90. // suit particular needs.
  91. //
  92. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  93. // 169. Bad efficiency of overflow() mandated
  94. // 432. stringbuf::overflow() makes only one write position
  95. // available
  96. const __size_type __opt_len = std::max(__size_type(2 * __capacity),
  97. __size_type(512));
  98. const __size_type __len = std::min(__opt_len, __max_size);
  99. __string_type __tmp;
  100. __tmp.reserve(__len);
  101. if (this->pbase())
  102. __tmp.assign(this->pbase(), this->epptr() - this->pbase());
  103. __tmp.push_back(__conv);
  104. _M_string.swap(__tmp);
  105. _M_sync(const_cast<char_type*>(_M_string.data()),
  106. this->gptr() - this->eback(), this->pptr() - this->pbase());
  107. }
  108. else
  109. *this->pptr() = __conv;
  110. this->pbump(1);
  111. return __c;
  112. }
  113. template <class _CharT, class _Traits, class _Alloc>
  114. typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
  115. basic_stringbuf<_CharT, _Traits, _Alloc>::
  116. underflow()
  117. {
  118. int_type __ret = traits_type::eof();
  119. const bool __testin = this->_M_mode & ios_base::in;
  120. if (__testin)
  121. {
  122. // Update egptr() to match the actual string end.
  123. _M_update_egptr();
  124. if (this->gptr() < this->egptr())
  125. __ret = traits_type::to_int_type(*this->gptr());
  126. }
  127. return __ret;
  128. }
  129. template <class _CharT, class _Traits, class _Alloc>
  130. typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
  131. basic_stringbuf<_CharT, _Traits, _Alloc>::
  132. seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
  133. {
  134. pos_type __ret = pos_type(off_type(-1));
  135. bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
  136. bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
  137. const bool __testboth = __testin && __testout && __way != ios_base::cur;
  138. __testin &= !(__mode & ios_base::out);
  139. __testout &= !(__mode & ios_base::in);
  140. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  141. // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
  142. const char_type* __beg = __testin ? this->eback() : this->pbase();
  143. if ((__beg || !__off) && (__testin || __testout || __testboth))
  144. {
  145. _M_update_egptr();
  146. off_type __newoffi = __off;
  147. off_type __newoffo = __newoffi;
  148. if (__way == ios_base::cur)
  149. {
  150. __newoffi += this->gptr() - __beg;
  151. __newoffo += this->pptr() - __beg;
  152. }
  153. else if (__way == ios_base::end)
  154. __newoffo = __newoffi += this->egptr() - __beg;
  155. if ((__testin || __testboth)
  156. && __newoffi >= 0
  157. && this->egptr() - __beg >= __newoffi)
  158. {
  159. this->gbump((__beg + __newoffi) - this->gptr());
  160. __ret = pos_type(__newoffi);
  161. }
  162. if ((__testout || __testboth)
  163. && __newoffo >= 0
  164. && this->egptr() - __beg >= __newoffo)
  165. {
  166. this->pbump((__beg + __newoffo) - this->pptr());
  167. __ret = pos_type(__newoffo);
  168. }
  169. }
  170. return __ret;
  171. }
  172. template <class _CharT, class _Traits, class _Alloc>
  173. typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
  174. basic_stringbuf<_CharT, _Traits, _Alloc>::
  175. seekpos(pos_type __sp, ios_base::openmode __mode)
  176. {
  177. pos_type __ret = pos_type(off_type(-1));
  178. const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
  179. const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
  180. const char_type* __beg = __testin ? this->eback() : this->pbase();
  181. if ((__beg || !off_type(__sp)) && (__testin || __testout))
  182. {
  183. _M_update_egptr();
  184. const off_type __pos(__sp);
  185. const bool __testpos = (0 <= __pos
  186. && __pos <= this->egptr() - __beg);
  187. if (__testpos)
  188. {
  189. if (__testin)
  190. this->gbump((__beg + __pos) - this->gptr());
  191. if (__testout)
  192. this->pbump((__beg + __pos) - this->pptr());
  193. __ret = __sp;
  194. }
  195. }
  196. return __ret;
  197. }
  198. template <class _CharT, class _Traits, class _Alloc>
  199. void
  200. basic_stringbuf<_CharT, _Traits, _Alloc>::
  201. _M_sync(char_type* __base, __size_type __i, __size_type __o)
  202. {
  203. const bool __testin = _M_mode & ios_base::in;
  204. const bool __testout = _M_mode & ios_base::out;
  205. char_type* __endg = __base + _M_string.size();
  206. char_type* __endp = __base + _M_string.capacity();
  207. if (__base != _M_string.data())
  208. {
  209. // setbuf: __i == size of buffer area (_M_string.size() == 0).
  210. __endg += __i;
  211. __i = 0;
  212. __endp = __endg;
  213. }
  214. if (__testin)
  215. this->setg(__base, __base + __i, __endg);
  216. if (__testout)
  217. {
  218. this->setp(__base, __endp);
  219. this->pbump(__o);
  220. // egptr() always tracks the string end. When !__testin,
  221. // for the correct functioning of the streambuf inlines
  222. // the other get area pointers are identical.
  223. if (!__testin)
  224. this->setg(__endg, __endg, __endg);
  225. }
  226. }
  227. // Inhibit implicit instantiations for required instantiations,
  228. // which are defined via explicit instantiations elsewhere.
  229. // NB: This syntax is a GNU extension.
  230. #if _GLIBCXX_EXTERN_TEMPLATE
  231. extern template class basic_stringbuf<char>;
  232. extern template class basic_istringstream<char>;
  233. extern template class basic_ostringstream<char>;
  234. extern template class basic_stringstream<char>;
  235. #ifdef _GLIBCXX_USE_WCHAR_T
  236. extern template class basic_stringbuf<wchar_t>;
  237. extern template class basic_istringstream<wchar_t>;
  238. extern template class basic_ostringstream<wchar_t>;
  239. extern template class basic_stringstream<wchar_t>;
  240. #endif
  241. #endif
  242. _GLIBCXX_END_NAMESPACE
  243. #endif