_sstream.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. // This header defines classes basic_stringbuf, basic_istringstream,
  19. // basic_ostringstream, and basic_stringstream. These classes
  20. // represent streamsbufs and streams whose sources or destinations are
  21. // C++ strings.
  22. #ifndef _STLP_INTERNAL_SSTREAM
  23. #define _STLP_INTERNAL_SSTREAM
  24. #ifndef _STLP_INTERNAL_STREAMBUF
  25. # include <stl/_streambuf.h>
  26. #endif
  27. #ifndef _STLP_INTERNAL_ISTREAM
  28. # include <stl/_istream.h> // Includes <ostream>, <ios>, <iosfwd>
  29. #endif
  30. #ifndef _STLP_INTERNAL_STRING_H
  31. # include <stl/_string.h>
  32. #endif
  33. _STLP_BEGIN_NAMESPACE
  34. //----------------------------------------------------------------------
  35. // This version of basic_stringbuf relies on the internal details of
  36. // basic_string. It relies on the fact that, in this implementation,
  37. // basic_string's iterators are pointers. It also assumes (as allowed
  38. // by the standard) that _CharT is a POD type.
  39. // We have a very small buffer for the put area, just so that we don't
  40. // have to use append() for every sputc. Conceptually, the buffer
  41. // immediately follows the end of the underlying string. We use this
  42. // buffer when appending to write-only streambufs, but we don't use it
  43. // for read-write streambufs.
  44. template <class _CharT, class _Traits, class _Alloc>
  45. class basic_stringbuf : public basic_streambuf<_CharT, _Traits> {
  46. public: // Typedefs.
  47. typedef _CharT char_type;
  48. typedef typename _Traits::int_type int_type;
  49. typedef typename _Traits::pos_type pos_type;
  50. typedef typename _Traits::off_type off_type;
  51. typedef _Traits traits_type;
  52. typedef basic_streambuf<_CharT, _Traits> _Base;
  53. typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Self;
  54. typedef basic_string<_CharT, _Traits, _Alloc> _String;
  55. public: // Constructors, destructor.
  56. explicit basic_stringbuf(ios_base::openmode __mode
  57. = ios_base::in | ios_base::out);
  58. explicit basic_stringbuf(const _String& __s, ios_base::openmode __mode
  59. = ios_base::in | ios_base::out);
  60. virtual ~basic_stringbuf();
  61. public: // Get or set the string.
  62. _String str() const { return _M_str; }
  63. void str(const _String& __s);
  64. protected: // Overridden virtual member functions.
  65. virtual int_type underflow();
  66. virtual int_type uflow();
  67. virtual int_type pbackfail(int_type __c);
  68. virtual int_type overflow(int_type __c);
  69. int_type pbackfail() {return pbackfail(_Traits::eof());}
  70. int_type overflow() {return overflow(_Traits::eof());}
  71. virtual streamsize xsputn(const char_type* __s, streamsize __n);
  72. virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
  73. virtual _Base* setbuf(_CharT* __buf, streamsize __n);
  74. virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
  75. ios_base::openmode __mode
  76. = ios_base::in | ios_base::out);
  77. virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode
  78. = ios_base::in | ios_base::out);
  79. private: // Helper functions.
  80. void _M_set_ptrs();
  81. static _CharT* _S_start(const _String& __str) { return __CONST_CAST(_CharT*, __str.data()); }
  82. static _CharT* _S_finish(const _String& __str) { return __CONST_CAST(_CharT*, __str.data()) + __str.size(); }
  83. private:
  84. ios_base::openmode _M_mode;
  85. _String _M_str;
  86. };
  87. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  88. _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<char, char_traits<char>, allocator<char> >;
  89. # if !defined (_STLP_NO_WCHAR_T)
  90. _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
  91. # endif
  92. #endif /* _STLP_USE_TEMPLATE_EXPORT */
  93. //----------------------------------------------------------------------
  94. // Class basic_istringstream, an input stream that uses a stringbuf.
  95. template <class _CharT, class _Traits, class _Alloc>
  96. class basic_istringstream : public basic_istream<_CharT, _Traits> {
  97. public: // Typedefs
  98. typedef typename _Traits::char_type char_type;
  99. typedef typename _Traits::int_type int_type;
  100. typedef typename _Traits::pos_type pos_type;
  101. typedef typename _Traits::off_type off_type;
  102. typedef _Traits traits_type;
  103. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  104. typedef basic_istream<_CharT, _Traits> _Base;
  105. typedef basic_string<_CharT, _Traits, _Alloc> _String;
  106. typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Buf;
  107. public: // Constructors, destructor.
  108. basic_istringstream(ios_base::openmode __mode = ios_base::in);
  109. basic_istringstream(const _String& __str,
  110. ios_base::openmode __mode = ios_base::in);
  111. ~basic_istringstream();
  112. public: // Member functions
  113. basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
  114. { return __CONST_CAST(_Buf*,&_M_buf); }
  115. _String str() const { return _M_buf.str(); }
  116. void str(const _String& __s) { _M_buf.str(__s); }
  117. private:
  118. basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
  119. #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
  120. typedef basic_istringstream<_CharT, _Traits> _Self;
  121. //explicitely defined as private to avoid warnings:
  122. basic_istringstream(_Self const&);
  123. _Self& operator = (_Self const&);
  124. #endif
  125. };
  126. //----------------------------------------------------------------------
  127. // Class basic_ostringstream, an output stream that uses a stringbuf.
  128. template <class _CharT, class _Traits, class _Alloc>
  129. class basic_ostringstream : public basic_ostream<_CharT, _Traits> {
  130. public: // Typedefs
  131. typedef typename _Traits::char_type char_type;
  132. typedef typename _Traits::int_type int_type;
  133. typedef typename _Traits::pos_type pos_type;
  134. typedef typename _Traits::off_type off_type;
  135. typedef _Traits traits_type;
  136. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  137. typedef basic_ostream<_CharT, _Traits> _Base;
  138. typedef basic_string<_CharT, _Traits, _Alloc> _String;
  139. typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Buf;
  140. public: // Constructors, destructor.
  141. basic_ostringstream(ios_base::openmode __mode = ios_base::out);
  142. basic_ostringstream(const _String& __str,
  143. ios_base::openmode __mode = ios_base::out);
  144. ~basic_ostringstream();
  145. public: // Member functions.
  146. basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
  147. { return __CONST_CAST(_Buf*,&_M_buf); }
  148. _String str() const { return _M_buf.str(); }
  149. void str(const _String& __s) { _M_buf.str(__s); } // dwa 02/07/00 - BUG STOMPER DAVE
  150. private:
  151. basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
  152. #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
  153. typedef basic_ostringstream<_CharT, _Traits> _Self;
  154. //explicitely defined as private to avoid warnings:
  155. basic_ostringstream(_Self const&);
  156. _Self& operator = (_Self const&);
  157. #endif
  158. };
  159. //----------------------------------------------------------------------
  160. // Class basic_stringstream, a bidirectional stream that uses a stringbuf.
  161. template <class _CharT, class _Traits, class _Alloc>
  162. class basic_stringstream : public basic_iostream<_CharT, _Traits> {
  163. public: // Typedefs
  164. typedef typename _Traits::char_type char_type;
  165. typedef typename _Traits::int_type int_type;
  166. typedef typename _Traits::pos_type pos_type;
  167. typedef typename _Traits::off_type off_type;
  168. typedef _Traits traits_type;
  169. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  170. typedef basic_iostream<_CharT, _Traits> _Base;
  171. typedef basic_string<_CharT, _Traits, _Alloc> _String;
  172. typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Buf;
  173. typedef ios_base::openmode openmode;
  174. public: // Constructors, destructor.
  175. basic_stringstream(openmode __mod = ios_base::in | ios_base::out);
  176. basic_stringstream(const _String& __str,
  177. openmode __mod = ios_base::in | ios_base::out);
  178. ~basic_stringstream();
  179. public: // Member functions.
  180. basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
  181. { return __CONST_CAST(_Buf*,&_M_buf); }
  182. _String str() const { return _M_buf.str(); }
  183. void str(const _String& __s) { _M_buf.str(__s); }
  184. private:
  185. basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
  186. #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
  187. typedef basic_stringstream<_CharT, _Traits> _Self;
  188. //explicitely defined as private to avoid warnings:
  189. basic_stringstream(_Self const&);
  190. _Self& operator = (_Self const&);
  191. #endif
  192. };
  193. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  194. _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<char, char_traits<char>, allocator<char> >;
  195. _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<char, char_traits<char>, allocator<char> >;
  196. _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<char, char_traits<char>, allocator<char> >;
  197. # if !defined (_STLP_NO_WCHAR_T)
  198. _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
  199. _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
  200. _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
  201. # endif
  202. #endif /* _STLP_USE_TEMPLATE_EXPORT */
  203. _STLP_END_NAMESPACE
  204. #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  205. # include <stl/_sstream.c>
  206. #endif
  207. #endif /* _STLP_INTERNAL_SSTREAM */
  208. // Local Variables:
  209. // mode:C++
  210. // End: