_sstream.h

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_SSTREAM_H
00026 #define _STLP_SSTREAM_H
00027 
00028 #ifndef _STLP_INTERNAL_STREAMBUF
00029 # include <stl/_streambuf.h>
00030 #endif
00031 
00032 #ifndef _STLP_INTERNAL_ISTREAM_H
00033 # include <stl/_istream.h> // Includes <ostream>, <ios>, <iosfwd>
00034 #endif
00035 
00036 #ifndef _STLP_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 {
00057 public:                         // Typedefs.
00058   typedef _CharT                     char_type;
00059   typedef typename _Traits::int_type int_type;
00060   typedef typename _Traits::pos_type pos_type;
00061   typedef typename _Traits::off_type off_type;
00062   typedef _Traits                    traits_type;
00063 
00064   typedef basic_streambuf<_CharT, _Traits>          _Base;
00065   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Self;
00066   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
00067 
00068 public:                         // Constructors, destructor.
00069   explicit basic_stringbuf(ios_base::openmode __mode
00070                                       = ios_base::in | ios_base::out);
00071   explicit basic_stringbuf(const _String& __s, ios_base::openmode __mode
00072                                       = ios_base::in | ios_base::out);
00073   virtual ~basic_stringbuf();
00074 
00075 public:                         // Get or set the string.
00076   _String str() const { _M_append_buffer(); return _M_str; }
00077   void str(const _String& __s);
00078 
00079 protected:                      // Overridden virtual member functions.
00080   virtual int_type underflow();
00081   virtual int_type uflow();
00082   virtual int_type pbackfail(int_type __c);
00083   virtual int_type overflow(int_type __c);
00084   int_type pbackfail() {return pbackfail(_Traits::eof());}
00085   int_type overflow() {return overflow(_Traits::eof());}
00086 
00087   virtual streamsize xsputn(const char_type* __s, streamsize __n);
00088   virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
00089 
00090   virtual _Base* setbuf(_CharT* __buf, streamsize __n);
00091   virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
00092                            ios_base::openmode __mode 
00093                                       = ios_base::in | ios_base::out);
00094   virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode 
00095                                       = ios_base::in | ios_base::out);
00096 
00097 private:                        // Helper functions.
00098   // Append the internal buffer to the string if necessary.
00099   void _M_append_buffer() const;
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 {
00122 public:                         // Typedefs
00123   typedef typename _Traits::char_type   char_type;
00124   typedef typename _Traits::int_type    int_type;
00125   typedef typename _Traits::pos_type    pos_type;
00126   typedef typename _Traits::off_type    off_type;
00127   typedef _Traits traits_type;
00128 
00129   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
00130   typedef basic_istream<_CharT, _Traits>            _Base;
00131   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
00132   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
00133 
00134 public:                         // Constructors, destructor.
00135   basic_istringstream(ios_base::openmode __mode = ios_base::in);
00136   basic_istringstream(const _String& __str,
00137                       ios_base::openmode __mode = ios_base::in);
00138   ~basic_istringstream();
00139 
00140 public:                         // Member functions
00141 
00142   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
00143     { return __CONST_CAST(_Buf*,&_M_buf); }
00144 
00145   _String str() const { return _M_buf.str(); }
00146   void str(const _String& __s) { _M_buf.str(__s); }
00147   
00148 private:
00149   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
00150 };
00151 
00152 
00153 //----------------------------------------------------------------------
00154 // Class basic_ostringstream, an output stream that uses a stringbuf.
00155 
00156 template <class _CharT, class _Traits, class _Alloc>
00157 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
00158 {
00159 public:                         // Typedefs
00160   typedef typename _Traits::char_type   char_type;
00161   typedef typename _Traits::int_type    int_type;
00162   typedef typename _Traits::pos_type    pos_type;
00163   typedef typename _Traits::off_type    off_type;
00164   typedef _Traits traits_type;
00165 
00166   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
00167   typedef basic_ostream<_CharT, _Traits>            _Base;
00168   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
00169   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
00170 
00171 public:                         // Constructors, destructor.
00172   basic_ostringstream(ios_base::openmode __mode = ios_base::out);
00173   basic_ostringstream(const _String& __str,
00174                       ios_base::openmode __mode = ios_base::out);
00175   ~basic_ostringstream();
00176 
00177 public:                         // Member functions.
00178 
00179   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
00180     { return __CONST_CAST(_Buf*,&_M_buf); }
00181 
00182   _String str() const { return _M_buf.str(); }
00183     void str(const _String& __s) { _M_buf.str(__s); } // dwa 02/07/00 - BUG STOMPER DAVE
00184 
00185 
00186 private:
00187   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
00188 };
00189 
00190 
00191 //----------------------------------------------------------------------
00192 // Class basic_stringstream, a bidirectional stream that uses a stringbuf.
00193 
00194 template <class _CharT, class _Traits, class _Alloc>
00195 class basic_stringstream : public basic_iostream<_CharT, _Traits>
00196 {
00197 public:                         // Typedefs
00198   typedef typename _Traits::char_type char_type;
00199   typedef typename _Traits::int_type  int_type;
00200   typedef typename _Traits::pos_type  pos_type;
00201   typedef typename _Traits::off_type  off_type;
00202   typedef _Traits  traits_type;
00203 
00204   typedef basic_ios<_CharT, _Traits>                 _Basic_ios;
00205   typedef basic_iostream<_CharT, _Traits>            _Base;
00206   typedef basic_string<_CharT, _Traits, _Alloc>      _String;
00207   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
00208   
00209   typedef ios_base::openmode openmode;
00210 
00211 public:                         // Constructors, destructor.
00212   basic_stringstream(openmode __mod = ios_base::in | ios_base::out);
00213   basic_stringstream(const _String& __str,
00214                      openmode __mod = ios_base::in | ios_base::out);
00215   ~basic_stringstream();
00216 
00217 public:                         // Member functions.
00218 
00219   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
00220     { return __CONST_CAST(_Buf*,&_M_buf); }
00221 
00222   _String str() const { return _M_buf.str(); }
00223     void str(const _String& __s) { _M_buf.str(__s); }
00224 
00225 private:
00226   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
00227 };
00228 
00229 
00230 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00231 _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<char, char_traits<char>, allocator<char> >;
00232 _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<char, char_traits<char>, allocator<char> >;
00233 _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<char, char_traits<char>, allocator<char> >;
00234 #  if !defined (_STLP_NO_WCHAR_T)
00235 _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
00236 _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
00237 _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
00238 #  endif
00239 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00240 
00241 _STLP_END_NAMESPACE
00242 
00243 # if  defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
00244 #  include <stl/_sstream.c>
00245 # endif
00246 
00247 #endif /* _STLP_SSTREAM_H */
00248 
00249 // Local Variables:
00250 // mode:C++
00251 // End:

Generated on Mon Jun 5 10:20:47 2006 for Intelligence.kdevelop by  doxygen 1.4.6