home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _sstream.h < prev    next >
C/C++ Source or Header  |  2002-02-02  |  10KB  |  253 lines

  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.  
  19.  
  20. // This header defines classes basic_stringbuf, basic_istringstream,
  21. // basic_ostringstream, and basic_stringstream.  These classes 
  22. // represent streamsbufs and streams whose sources or destinations are
  23. // C++ strings.
  24.  
  25. #ifndef _STLP_SSTREAM_H
  26. #define _STLP_SSTREAM_H
  27.  
  28. #ifndef _STLP_INTERNAL_STREAMBUF
  29. # include <stl/_streambuf.h>
  30. #endif
  31.  
  32. #ifndef _STLP_INTERNAL_ISTREAM_H
  33. # include <stl/_istream.h> // Includes <ostream>, <ios>, <iosfwd>
  34. #endif
  35.  
  36. #ifndef _STLP_STRING_H
  37. # include <stl/_string.h>
  38. #endif
  39.  
  40. _STLP_BEGIN_NAMESPACE
  41.  
  42. //----------------------------------------------------------------------
  43. // This version of basic_stringbuf relies on the internal details of
  44. // basic_string.  It relies on the fact that, in this implementation,
  45. // basic_string's iterators are pointers.  It also assumes (as allowed
  46. // by the standard) that _CharT is a POD type.
  47.  
  48. // We have a very small buffer for the put area, just so that we don't
  49. // have to use append() for every sputc.  Conceptually, the buffer
  50. // immediately follows the end of the underlying string.  We use this
  51. // buffer when appending to write-only streambufs, but we don't use it
  52. // for read-write streambufs.
  53.  
  54. template <class _CharT, class _Traits, class _Alloc>
  55. class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
  56. {
  57. public:                         // Typedefs.
  58.   typedef _CharT                     char_type;
  59.   typedef typename _Traits::int_type int_type;
  60.   typedef typename _Traits::pos_type pos_type;
  61.   typedef typename _Traits::off_type off_type;
  62.   typedef _Traits                    traits_type;
  63.  
  64.   typedef basic_streambuf<_CharT, _Traits>          _Base;
  65.   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Self;
  66.   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
  67.  
  68. public:                         // Constructors, destructor.
  69.   explicit basic_stringbuf(ios_base::openmode __mode
  70.                                       = ios_base::in | ios_base::out);
  71.   explicit basic_stringbuf(const _String& __s, ios_base::openmode __mode
  72.                                       = ios_base::in | ios_base::out);
  73.   virtual ~basic_stringbuf();
  74.  
  75. public:                         // Get or set the string.
  76.   _String str() const { _M_append_buffer(); return _M_str; }
  77.   void str(const _String& __s);
  78.  
  79. protected:                      // Overridden virtual member functions.
  80.   virtual int_type underflow();
  81.   virtual int_type uflow();
  82.   virtual int_type pbackfail(int_type __c);
  83.   virtual int_type overflow(int_type __c);
  84.   int_type pbackfail() {return pbackfail(_Traits::eof());}
  85.   int_type overflow() {return overflow(_Traits::eof());}
  86.  
  87.   virtual streamsize xsputn(const char_type* __s, streamsize __n);
  88.   virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
  89.  
  90.   virtual _Base* setbuf(_CharT* __buf, streamsize __n);
  91.   virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
  92.                            ios_base::openmode __mode 
  93.                                       = ios_base::in | ios_base::out);
  94.   virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode 
  95.                                       = ios_base::in | ios_base::out);
  96.  
  97. private:                        // Helper functions.
  98.   // Append the internal buffer to the string if necessary.
  99.   void _M_append_buffer() const;
  100.   void _M_set_ptrs();
  101.  
  102. private:
  103.   ios_base::openmode _M_mode;
  104.   mutable basic_string<_CharT, _Traits, _Alloc> _M_str;
  105.  
  106.   enum _JustName { _S_BufSiz = 8 };
  107.   _CharT _M_Buf[ 8 /* _S_BufSiz */];
  108. };
  109.  
  110. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  111. _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<char, char_traits<char>, allocator<char> >;
  112. #  if !defined (_STLP_NO_WCHAR_T)
  113. _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
  114. #  endif
  115. # endif /* _STLP_USE_TEMPLATE_EXPORT */
  116.  
  117. //----------------------------------------------------------------------
  118. // Class basic_istringstream, an input stream that uses a stringbuf.
  119.  
  120. template <class _CharT, class _Traits, class _Alloc>
  121. class basic_istringstream : public basic_istream<_CharT, _Traits>
  122. {
  123. public:                         // Typedefs
  124.   typedef typename _Traits::char_type   char_type;
  125.   typedef typename _Traits::int_type    int_type;
  126.   typedef typename _Traits::pos_type    pos_type;
  127.   typedef typename _Traits::off_type    off_type;
  128.   typedef _Traits traits_type;
  129.  
  130.   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
  131.   typedef basic_istream<_CharT, _Traits>            _Base;
  132.   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
  133.   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
  134.  
  135. public:                         // Constructors, destructor.
  136.   basic_istringstream(ios_base::openmode __mode = ios_base::in);
  137.   basic_istringstream(const _String& __str,
  138.                       ios_base::openmode __mode = ios_base::in);
  139.   ~basic_istringstream();
  140.  
  141. public:                         // Member functions
  142.  
  143.   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
  144.     { return __CONST_CAST(_Buf*,&_M_buf); }
  145.  
  146.   _String str() const { return _M_buf.str(); }
  147.   void str(const _String& __s) { _M_buf.str(__s); }
  148.   
  149. private:
  150.   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
  151. };
  152.  
  153.  
  154. //----------------------------------------------------------------------
  155. // Class basic_ostringstream, an output stream that uses a stringbuf.
  156.  
  157. template <class _CharT, class _Traits, class _Alloc>
  158. class basic_ostringstream : public basic_ostream<_CharT, _Traits>
  159. {
  160. public:                         // Typedefs
  161.   typedef typename _Traits::char_type   char_type;
  162.   typedef typename _Traits::int_type    int_type;
  163.   typedef typename _Traits::pos_type    pos_type;
  164.   typedef typename _Traits::off_type    off_type;
  165.   typedef _Traits traits_type;
  166.  
  167.   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
  168.   typedef basic_ostream<_CharT, _Traits>            _Base;
  169.   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
  170.   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
  171.  
  172. public:                         // Constructors, destructor.
  173.   basic_ostringstream(ios_base::openmode __mode = ios_base::out);
  174.   basic_ostringstream(const _String& __str,
  175.                       ios_base::openmode __mode = ios_base::out);
  176.   ~basic_ostringstream();
  177.  
  178. public:                         // Member functions.
  179.  
  180.   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
  181.     { return __CONST_CAST(_Buf*,&_M_buf); }
  182.  
  183.   _String str() const { return _M_buf.str(); }
  184.     void str(const _String& __s) { _M_buf.str(__s); } // dwa 02/07/00 - BUG STOMPER DAVE
  185.  
  186.  
  187. private:
  188.   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
  189. };
  190.  
  191.  
  192. //----------------------------------------------------------------------
  193. // Class basic_stringstream, a bidirectional stream that uses a stringbuf.
  194.  
  195. template <class _CharT, class _Traits, class _Alloc>
  196. class basic_stringstream : public basic_iostream<_CharT, _Traits>
  197. {
  198. public:                         // Typedefs
  199.   typedef typename _Traits::char_type char_type;
  200.   typedef typename _Traits::int_type  int_type;
  201.   typedef typename _Traits::pos_type  pos_type;
  202.   typedef typename _Traits::off_type  off_type;
  203.   typedef _Traits  traits_type;
  204.  
  205.   typedef basic_ios<_CharT, _Traits>                 _Basic_ios;
  206.   typedef basic_iostream<_CharT, _Traits>            _Base;
  207.   typedef basic_string<_CharT, _Traits, _Alloc>      _String;
  208.   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
  209.   
  210.   typedef ios_base::openmode openmode;
  211.  
  212. public:                         // Constructors, destructor.
  213.   basic_stringstream(openmode __mod = ios_base::in | ios_base::out);
  214.   basic_stringstream(const _String& __str,
  215.                      openmode __mod = ios_base::in | ios_base::out);
  216.   ~basic_stringstream();
  217.  
  218. public:                         // Member functions.
  219.  
  220.   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
  221.     { return __CONST_CAST(_Buf*,&_M_buf); }
  222.  
  223.   _String str() const { return _M_buf.str(); }
  224.     void str(const _String& __s) { _M_buf.str(__s); }
  225.  
  226. private:
  227.   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
  228. };
  229.  
  230.  
  231. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  232. _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<char, char_traits<char>, allocator<char> >;
  233. _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<char, char_traits<char>, allocator<char> >;
  234. _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<char, char_traits<char>, allocator<char> >;
  235. #  if !defined (_STLP_NO_WCHAR_T)
  236. _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
  237. _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
  238. _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
  239. #  endif
  240. # endif /* _STLP_USE_TEMPLATE_EXPORT */
  241.  
  242. _STLP_END_NAMESPACE
  243.  
  244. # if  defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  245. #  include <stl/_sstream.c>
  246. # endif
  247.  
  248. #endif /* _STLP_SSTREAM_H */
  249.  
  250. // Local Variables:
  251. // mode:C++
  252. // End:
  253.