2 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
4 * Silicon Graphics Computer Systems, Inc.
9 * This material is provided "as is", with absolutely no warranty expressed
10 * or implied. Any use is at your own risk.
12 * Permission to use or copy this software for any purpose is hereby granted
13 * without fee, provided the above notices are retained on all copies.
14 * Permission to modify the code and to distribute modified code is granted,
15 * provided the above notices are retained, and a notice that the code was
16 * modified is included with the above copyright notice.
21 // This header defines classes basic_stringbuf, basic_istringstream,
22 // basic_ostringstream, and basic_stringstream. These classes
23 // represent streamsbufs and streams whose sources or destinations are
26 #ifndef _STLP_SSTREAM_H
27 #define _STLP_SSTREAM_H
29 #ifndef _STLP_INTERNAL_STREAMBUF
30 # include <stl/_streambuf.h>
33 #ifndef _STLP_INTERNAL_ISTREAM_H
34 # include <stl/_istream.h> // Includes <ostream>, <ios>, <iosfwd>
37 #ifndef _STLP_STRING_H
38 # include <stl/_string.h>
43 //----------------------------------------------------------------------
44 // This version of basic_stringbuf relies on the internal details of
45 // basic_string. It relies on the fact that, in this implementation,
46 // basic_string's iterators are pointers. It also assumes (as allowed
47 // by the standard) that _CharT is a POD type.
49 // We have a very small buffer for the put area, just so that we don't
50 // have to use append() for every sputc. Conceptually, the buffer
51 // immediately follows the end of the underlying string. We use this
52 // buffer when appending to write-only streambufs, but we don't use it
53 // for read-write streambufs.
55 template <class _CharT, class _Traits, class _Alloc>
57 NONSHARABLE_CLASS ( basic_stringbuf ) : public basic_streambuf<_CharT, _Traits>
59 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
63 typedef _CharT char_type;
64 typedef typename _Traits::int_type int_type;
65 typedef typename _Traits::pos_type pos_type;
66 typedef typename _Traits::off_type off_type;
67 typedef _Traits traits_type;
69 typedef basic_streambuf<_CharT, _Traits> _Base;
70 typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Self;
71 typedef basic_string<_CharT, _Traits, _Alloc> _String;
73 public: // Constructors, destructor.
74 _STLP_DECLSPEC explicit basic_stringbuf(ios_base::openmode __mode
75 = ios_base::in | ios_base::out);
76 _STLP_DECLSPEC explicit basic_stringbuf(const _String& __s, ios_base::openmode __mode
77 = ios_base::in | ios_base::out);
78 _STLP_DECLSPEC virtual ~basic_stringbuf();
80 public: // Get or set the string.
82 if ( _M_mode & ios_base::out )
86 _STLP_DECLSPEC void str(const _String& __s);
88 protected: // Overridden virtual member functions.
89 virtual int_type underflow();
90 virtual int_type uflow();
91 virtual int_type pbackfail(int_type __c);
92 virtual int_type overflow(int_type __c);
93 int_type pbackfail() {return pbackfail(_Traits::eof());}
94 int_type overflow() {return overflow(_Traits::eof());}
96 virtual streamsize xsputn(const char_type* __s, streamsize __n);
97 virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
99 virtual _Base* setbuf(_CharT* __buf, streamsize __n);
100 virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
101 ios_base::openmode __mode
102 = ios_base::in | ios_base::out);
103 virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode
104 = ios_base::in | ios_base::out);
105 ios_base::openmode _M_mode;
107 private: // Helper functions.
108 // Append the internal buffer to the string if necessary.
109 void _M_append_buffer() const;
113 mutable basic_string<_CharT, _Traits, _Alloc> _M_str;
115 enum _JustName { _S_BufSiz = 8 };
116 _CharT _M_Buf[ 8 /* _S_BufSiz */];
119 # if defined (_STLP_USE_TEMPLATE_EXPORT)
120 _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<char, char_traits<char>, allocator<char> >;
121 # if !defined (_STLP_NO_WCHAR_T)
122 _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
124 # endif /* _STLP_USE_TEMPLATE_EXPORT */
126 //----------------------------------------------------------------------
127 // Class basic_istringstream, an input stream that uses a stringbuf.
129 template <class _CharT, class _Traits, class _Alloc>
131 NONSHARABLE_CLASS ( basic_istringstream ) : public basic_istream<_CharT, _Traits>
133 class basic_istringstream : public basic_istream<_CharT, _Traits>
137 typedef typename _Traits::char_type char_type;
138 typedef typename _Traits::int_type int_type;
139 typedef typename _Traits::pos_type pos_type;
140 typedef typename _Traits::off_type off_type;
141 typedef _Traits traits_type;
143 typedef basic_ios<_CharT, _Traits> _Basic_ios;
144 typedef basic_istream<_CharT, _Traits> _Base;
145 typedef basic_string<_CharT, _Traits, _Alloc> _String;
146 typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Buf;
148 public: // Constructors, destructor.
149 basic_istringstream(ios_base::openmode __mode = ios_base::in);
150 basic_istringstream(const _String& __str,
151 ios_base::openmode __mode = ios_base::in);
152 ~basic_istringstream();
154 public: // Member functions
156 basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
157 { return __CONST_CAST(_Buf*,&_M_buf); }
159 _String str() const { return _M_buf.str(); }
160 void str(const _String& __s) { _M_buf.str(__s); }
163 basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
167 //----------------------------------------------------------------------
168 // Class basic_ostringstream, an output stream that uses a stringbuf.
170 template <class _CharT, class _Traits, class _Alloc>
172 NONSHARABLE_CLASS ( basic_ostringstream ) : public basic_ostream<_CharT, _Traits>
174 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
178 typedef typename _Traits::char_type char_type;
179 typedef typename _Traits::int_type int_type;
180 typedef typename _Traits::pos_type pos_type;
181 typedef typename _Traits::off_type off_type;
182 typedef _Traits traits_type;
184 typedef basic_ios<_CharT, _Traits> _Basic_ios;
185 typedef basic_ostream<_CharT, _Traits> _Base;
186 typedef basic_string<_CharT, _Traits, _Alloc> _String;
187 typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Buf;
189 public: // Constructors, destructor.
190 basic_ostringstream(ios_base::openmode __mode = ios_base::out);
191 basic_ostringstream(const _String& __str,
192 ios_base::openmode __mode = ios_base::out);
193 ~basic_ostringstream();
195 public: // Member functions.
197 basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
198 { return __CONST_CAST(_Buf*,&_M_buf); }
200 _String str() const { return _M_buf.str(); }
201 void str(const _String& __s) { _M_buf.str(__s); } // dwa 02/07/00 - BUG STOMPER DAVE
205 basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
209 //----------------------------------------------------------------------
210 // Class basic_stringstream, a bidirectional stream that uses a stringbuf.
212 template <class _CharT, class _Traits, class _Alloc>
214 NONSHARABLE_CLASS ( basic_stringstream ) : public basic_iostream<_CharT, _Traits>
216 class basic_stringstream : public basic_iostream<_CharT, _Traits>
220 typedef typename _Traits::char_type char_type;
221 typedef typename _Traits::int_type int_type;
222 typedef typename _Traits::pos_type pos_type;
223 typedef typename _Traits::off_type off_type;
224 typedef _Traits traits_type;
226 typedef basic_ios<_CharT, _Traits> _Basic_ios;
227 typedef basic_iostream<_CharT, _Traits> _Base;
228 typedef basic_string<_CharT, _Traits, _Alloc> _String;
229 typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Buf;
231 typedef ios_base::openmode openmode;
233 public: // Constructors, destructor.
234 _STLP_DECLSPEC basic_stringstream(openmode __mod = ios_base::in | ios_base::out);
235 _STLP_DECLSPEC basic_stringstream(const _String& __str,
236 openmode __mod = ios_base::in | ios_base::out);
237 ~basic_stringstream();
239 public: // Member functions.
241 basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
242 { return __CONST_CAST(_Buf*,&_M_buf); }
244 _String str() const { return _M_buf.str(); }
245 void str(const _String& __s) { _M_buf.str(__s); }
248 basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
252 # if defined (_STLP_USE_TEMPLATE_EXPORT)
253 _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<char, char_traits<char>, allocator<char> >;
254 _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<char, char_traits<char>, allocator<char> >;
255 _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<char, char_traits<char>, allocator<char> >;
256 # if !defined (_STLP_NO_WCHAR_T)
257 _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
258 _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
259 _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
261 # endif /* _STLP_USE_TEMPLATE_EXPORT */
265 # if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
266 # include <stl/_sstream.c>
269 #endif /* _STLP_SSTREAM_H */