/home/ntakagi/work/STLport-5.1.5/stlport/stl/_sstream.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 
00019 
00020 // This header defines classes basic_stringbuf, basic_istringstream,
00021 // basic_ostringstream, and basic_stringstream.  These classes
00022 // represent streamsbufs and streams whose sources or destinations are
00023 // C++ strings.
00024 
00025 #ifndef _STLP_INTERNAL_SSTREAM
00026 #define _STLP_INTERNAL_SSTREAM
00027 
00028 #ifndef _STLP_INTERNAL_STREAMBUF
00029 #  include <stl/_streambuf.h>
00030 #endif
00031 
00032 #ifndef _STLP_INTERNAL_ISTREAM
00033 #  include <stl/_istream.h> // Includes <ostream>, <ios>, <iosfwd>
00034 #endif
00035 
00036 #ifndef _STLP_INTERNAL_STRING_H
00037 #  include <stl/_string.h>
00038 #endif
00039 
00040 _STLP_BEGIN_NAMESPACE
00041 
00042 //----------------------------------------------------------------------
00043 // This version of basic_stringbuf relies on the internal details of
00044 // basic_string.  It relies on the fact that, in this implementation,
00045 // basic_string's iterators are pointers.  It also assumes (as allowed
00046 // by the standard) that _CharT is a POD type.
00047 
00048 // We have a very small buffer for the put area, just so that we don't
00049 // have to use append() for every sputc.  Conceptually, the buffer
00050 // immediately follows the end of the underlying string.  We use this
00051 // buffer when appending to write-only streambufs, but we don't use it
00052 // for read-write streambufs.
00053 
00054 template <class _CharT, class _Traits, class _Alloc>
00055 class basic_stringbuf : public basic_streambuf<_CharT, _Traits> {
00056 public:                         // Typedefs.
00057   typedef _CharT                     char_type;
00058   typedef typename _Traits::int_type int_type;
00059   typedef typename _Traits::pos_type pos_type;
00060   typedef typename _Traits::off_type off_type;
00061   typedef _Traits                    traits_type;
00062 
00063   typedef basic_streambuf<_CharT, _Traits>          _Base;
00064   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Self;
00065   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
00066 
00067 public:                         // Constructors, destructor.
00068   explicit basic_stringbuf(ios_base::openmode __mode
00069                                       = ios_base::in | ios_base::out);
00070   explicit basic_stringbuf(const _String& __s, ios_base::openmode __mode
00071                                       = ios_base::in | ios_base::out);
00072   virtual ~basic_stringbuf();
00073 
00074 public:                         // Get or set the string.
00075   _String str() const { _M_append_buffer(); return _M_str; }
00076   void str(const _String& __s);
00077 
00078 protected:                      // Overridden virtual member functions.
00079   virtual int_type underflow();
00080   virtual int_type uflow();
00081   virtual int_type pbackfail(int_type __c);
00082   virtual int_type overflow(int_type __c);
00083   int_type pbackfail() {return pbackfail(_Traits::eof());}
00084   int_type overflow() {return overflow(_Traits::eof());}
00085 
00086   virtual streamsize xsputn(const char_type* __s, streamsize __n);
00087   virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
00088 
00089   virtual _Base* setbuf(_CharT* __buf, streamsize __n);
00090   virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
00091                            ios_base::openmode __mode
00092                                       = ios_base::in | ios_base::out);
00093   virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode
00094                                       = ios_base::in | ios_base::out);
00095 
00096 private:                        // Helper functions.
00097   // Append the internal buffer to the string if necessary.
00098   void _M_append_buffer() const;
00099   void _M_set_ptrs();
00100 
00101 private:
00102   ios_base::openmode _M_mode;
00103   mutable basic_string<_CharT, _Traits, _Alloc> _M_str;
00104 
00105   enum _JustName { _S_BufSiz = 8 };
00106   _CharT _M_Buf[ 8 /* _S_BufSiz */];
00107 };
00108 
00109 #if defined (_STLP_USE_TEMPLATE_EXPORT)
00110 _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<char, char_traits<char>, allocator<char> >;
00111 #  if !defined (_STLP_NO_WCHAR_T)
00112 _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
00113 #  endif
00114 #endif /* _STLP_USE_TEMPLATE_EXPORT */
00115 
00116 //----------------------------------------------------------------------
00117 // Class basic_istringstream, an input stream that uses a stringbuf.
00118 
00119 template <class _CharT, class _Traits, class _Alloc>
00120 class basic_istringstream : public basic_istream<_CharT, _Traits> {
00121 public:                         // Typedefs
00122   typedef typename _Traits::char_type   char_type;
00123   typedef typename _Traits::int_type    int_type;
00124   typedef typename _Traits::pos_type    pos_type;
00125   typedef typename _Traits::off_type    off_type;
00126   typedef _Traits traits_type;
00127 
00128   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
00129   typedef basic_istream<_CharT, _Traits>            _Base;
00130   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
00131   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
00132 
00133 public:                         // Constructors, destructor.
00134   basic_istringstream(ios_base::openmode __mode = ios_base::in);
00135   basic_istringstream(const _String& __str,
00136                       ios_base::openmode __mode = ios_base::in);
00137   ~basic_istringstream();
00138 
00139 public:                         // Member functions
00140 
00141   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
00142     { return __CONST_CAST(_Buf*,&_M_buf); }
00143 
00144   _String str() const { return _M_buf.str(); }
00145   void str(const _String& __s) { _M_buf.str(__s); }
00146 
00147 private:
00148   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
00149 
00150 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
00151   typedef basic_istringstream<_CharT, _Traits> _Self;
00152   //explicitely defined as private to avoid warnings:
00153   basic_istringstream(_Self const&);
00154   _Self& operator = (_Self const&);
00155 #endif
00156 };
00157 
00158 
00159 //----------------------------------------------------------------------
00160 // Class basic_ostringstream, an output stream that uses a stringbuf.
00161 
00162 template <class _CharT, class _Traits, class _Alloc>
00163 class basic_ostringstream : public basic_ostream<_CharT, _Traits> {
00164 public:                         // Typedefs
00165   typedef typename _Traits::char_type   char_type;
00166   typedef typename _Traits::int_type    int_type;
00167   typedef typename _Traits::pos_type    pos_type;
00168   typedef typename _Traits::off_type    off_type;
00169   typedef _Traits traits_type;
00170 
00171   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
00172   typedef basic_ostream<_CharT, _Traits>            _Base;
00173   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
00174   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
00175 
00176 public:                         // Constructors, destructor.
00177   basic_ostringstream(ios_base::openmode __mode = ios_base::out);
00178   basic_ostringstream(const _String& __str,
00179                       ios_base::openmode __mode = ios_base::out);
00180   ~basic_ostringstream();
00181 
00182 public:                         // Member functions.
00183 
00184   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
00185     { return __CONST_CAST(_Buf*,&_M_buf); }
00186 
00187   _String str() const { return _M_buf.str(); }
00188     void str(const _String& __s) { _M_buf.str(__s); } // dwa 02/07/00 - BUG STOMPER DAVE
00189 
00190 
00191 private:
00192   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
00193 
00194 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
00195   typedef basic_ostringstream<_CharT, _Traits> _Self;
00196   //explicitely defined as private to avoid warnings:
00197   basic_ostringstream(_Self const&);
00198   _Self& operator = (_Self const&);
00199 #endif
00200 };
00201 
00202 
00203 //----------------------------------------------------------------------
00204 // Class basic_stringstream, a bidirectional stream that uses a stringbuf.
00205 
00206 template <class _CharT, class _Traits, class _Alloc>
00207 class basic_stringstream : public basic_iostream<_CharT, _Traits> {
00208 public:                         // Typedefs
00209   typedef typename _Traits::char_type char_type;
00210   typedef typename _Traits::int_type  int_type;
00211   typedef typename _Traits::pos_type  pos_type;
00212   typedef typename _Traits::off_type  off_type;
00213   typedef _Traits  traits_type;
00214 
00215   typedef basic_ios<_CharT, _Traits>                 _Basic_ios;
00216   typedef basic_iostream<_CharT, _Traits>            _Base;
00217   typedef basic_string<_CharT, _Traits, _Alloc>      _String;
00218   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
00219 
00220   typedef ios_base::openmode openmode;
00221 
00222 public:                         // Constructors, destructor.
00223   basic_stringstream(openmode __mod = ios_base::in | ios_base::out);
00224   basic_stringstream(const _String& __str,
00225                      openmode __mod = ios_base::in | ios_base::out);
00226   ~basic_stringstream();
00227 
00228 public:                         // Member functions.
00229 
00230   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
00231     { return __CONST_CAST(_Buf*,&_M_buf); }
00232 
00233   _String str() const { return _M_buf.str(); }
00234     void str(const _String& __s) { _M_buf.str(__s); }
00235 
00236 private:
00237   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
00238 
00239 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
00240   typedef basic_stringstream<_CharT, _Traits> _Self;
00241   //explicitely defined as private to avoid warnings:
00242   basic_stringstream(_Self const&);
00243   _Self& operator = (_Self const&);
00244 #endif
00245 };
00246 
00247 
00248 #if defined (_STLP_USE_TEMPLATE_EXPORT)
00249 _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<char, char_traits<char>, allocator<char> >;
00250 _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<char, char_traits<char>, allocator<char> >;
00251 _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<char, char_traits<char>, allocator<char> >;
00252 #  if !defined (_STLP_NO_WCHAR_T)
00253 _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
00254 _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
00255 _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
00256 #  endif
00257 #endif /* _STLP_USE_TEMPLATE_EXPORT */
00258 
00259 _STLP_END_NAMESPACE
00260 
00261 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
00262 #  include <stl/_sstream.c>
00263 #endif
00264 
00265 #endif /* _STLP_INTERNAL_SSTREAM */
00266 
00267 // Local Variables:
00268 // mode:C++
00269 // End:



Generated on Mon Mar 10 15:32:36 2008 by  doxygen 1.5.1