epoc32/include/stdapis/stlport/stl/_streambuf.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /*
     2  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
     3  *
     4  * Copyright (c) 1999
     5  * Silicon Graphics Computer Systems, Inc.
     6  *
     7  * Copyright (c) 1999 
     8  * Boris Fomitchev
     9  *
    10  * This material is provided "as is", with absolutely no warranty expressed
    11  * or implied. Any use is at your own risk.
    12  *
    13  * Permission to use or copy this software for any purpose is hereby granted 
    14  * without fee, provided the above notices are retained on all copies.
    15  * Permission to modify the code and to distribute modified code is granted,
    16  * provided the above notices are retained, and a notice that the code was
    17  * modified is included with the above copyright notice.
    18  *
    19  */ 
    20 #ifndef _STLP_INTERNAL_STREAMBUF
    21 #define _STLP_INTERNAL_STREAMBUF
    22 
    23 #ifndef _STLP_IOS_BASE_H
    24 #include <stl/_ios_base.h>      // Needed for ios_base bitfield members.
    25                                 // <ios_base> includes <iosfwd>.
    26 #endif
    27 
    28 #ifndef _STLP_STDIO_FILE_H
    29 #include <stl/_stdio_file.h>     // Declaration of struct FILE, and of
    30                                 // functions to manipulate it.
    31 #endif
    32 
    33 _STLP_BEGIN_NAMESPACE
    34 
    35 //----------------------------------------------------------------------
    36 // Class basic_streambuf<>, the base class of the streambuf hierarchy.
    37 
    38 // A basic_streambuf<> manages an input (get) area and an output (put)
    39 // area.  Each is described by three pointers: a beginning, an end, and a
    40 // current position.  basic_streambuf<> contains some very simple member
    41 // functions that manipulate those six pointers, but almost all of the real
    42 // functionality gets delegated to protected virtual member functions.
    43 // All of the public member functions are inline, and most of the protected
    44 // member functions are virtual.
    45 
    46 // Although basic_streambuf<> is not abstract, it is useful only as a base
    47 // class.  Its virtual member functions have default definitions such that
    48 // reading from a basic_streambuf<> will always yield EOF, and writing to a
    49 // basic_streambuf<> will always fail.
    50 
    51 // The second template parameter, _Traits, defaults to char_traits<_CharT>.
    52 // The default is declared in header <iosfwd>, and it isn't declared here
    53 // because C++ language rules do not allow it to be declared twice. 
    54 
    55 template <class _CharT, class _Traits>
    56 #ifdef __SYMBIAN32__
    57 class basic_streambuf 
    58 #else
    59 class basic_streambuf
    60 #endif
    61 {
    62   friend class basic_istream<_CharT, _Traits>;
    63   friend class basic_ostream<_CharT, _Traits>;
    64 
    65 public:                         // Typedefs.
    66   typedef _CharT                     char_type;
    67   typedef typename _Traits::int_type int_type;
    68   typedef typename _Traits::pos_type pos_type;
    69   typedef typename _Traits::off_type off_type;
    70   typedef _Traits                    traits_type;
    71 
    72 private:                        // Data members.
    73 
    74   char_type* _M_gbegin;         // Beginning of get area
    75   char_type* _M_gnext;          // Current position within the get area
    76   char_type* _M_gend;           // End of get area
    77 
    78   char_type* _M_pbegin;         // Beginning of put area
    79   char_type* _M_pnext;          // Current position within the put area
    80   char_type* _M_pend;           // End of put area
    81 
    82   locale _M_locale;             // The streambuf's locale object
    83 
    84 public:                         // Extension: locking, for thread safety.
    85   _STLP_mutex _M_lock;
    86 
    87 public:                         // Destructor.
    88   _STLP_DECLSPEC virtual ~basic_streambuf();
    89 
    90 protected:                      // The default constructor.
    91   _STLP_DECLSPEC basic_streambuf();
    92 
    93 protected:                      // Protected interface to the get area.
    94   char_type* eback() const { return _M_gbegin; } // Beginning
    95   char_type* gptr()  const { return _M_gnext; }  // Current position
    96   char_type* egptr() const { return _M_gend; }   // End
    97   
    98   void gbump(int __n) { _M_gnext += __n; }
    99   void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
   100     _M_gbegin = __gbegin;
   101     _M_gnext  = __gnext;
   102     _M_gend   = __gend;
   103   }
   104 
   105 public:
   106   // An alternate public interface to the above functions
   107   // which allows us to avoid using templated friends which
   108   // are not supported on some compilers.
   109 
   110   char_type* _M_eback() const { return eback(); }
   111   char_type* _M_gptr()  const { return gptr(); }
   112   char_type* _M_egptr() const { return egptr(); }
   113   void _M_gbump(int __n)      { gbump(__n); }
   114   void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
   115     { setg(__gbegin, __gnext, __gend); }
   116 
   117 protected:                      // Protected interface to the put area
   118 
   119   char_type* pbase() const { return _M_pbegin; } // Beginning
   120   char_type* pptr()  const { return _M_pnext; }  // Current position
   121   char_type* epptr() const { return _M_pend; }   // End
   122 
   123   void pbump(int __n) { _M_pnext += __n; }
   124   void setp(char_type* __pbegin, char_type* __pend) {
   125     _M_pbegin = __pbegin;
   126     _M_pnext  = __pbegin;
   127     _M_pend   = __pend;
   128   }
   129 
   130 protected:                      // Virtual buffer management functions.
   131 
   132   _STLP_DECLSPEC virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
   133 
   134   // Alters the stream position, using an integer offset.  In this
   135   // class seekoff does nothing; subclasses are expected to override it.
   136   _STLP_DECLSPEC virtual pos_type seekoff(off_type, ios_base::seekdir,
   137                            ios_base::openmode = ios_base::in | ios_base::out);
   138 
   139   // Alters the stream position, using a previously obtained streampos.  In
   140   // this class seekpos does nothing; subclasses are expected to override it.
   141   _STLP_DECLSPEC virtual pos_type
   142   seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
   143 
   144   // Synchronizes (i.e. flushes) the buffer.  All subclasses are expected to 
   145   // override this virtual member function.
   146   _STLP_DECLSPEC virtual int sync();
   147 
   148 
   149 public:                         // Buffer management.
   150   basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n) 
   151     { return this->setbuf(__s, __n); }
   152 
   153   pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
   154                       ios_base::openmode __mod = ios_base::in | ios_base::out)
   155     { return this->seekoff(__offset, __way, __mod); }
   156 
   157   pos_type pubseekpos(pos_type __sp,
   158                       ios_base::openmode __mod = ios_base::in | ios_base::out)
   159     { return this->seekpos(__sp, __mod); }
   160 
   161   int pubsync() { return this->sync(); }
   162 
   163 protected:                      // Virtual get area functions, as defined in
   164                                 // 17.5.2.4.3 and 17.5.2.4.4 of the standard.
   165   // Returns a lower bound on the number of characters that we can read,
   166   // with underflow, before reaching end of file.  (-1 is a special value:
   167   // it means that underflow will fail.)  Most subclasses should probably
   168   // override this virtual member function.
   169   _STLP_DECLSPEC virtual streamsize showmanyc();
   170 
   171   // Reads up to __n characters.  Return value is the number of 
   172   // characters read.
   173   _STLP_DECLSPEC virtual streamsize xsgetn(char_type* __s, streamsize __n);
   174 
   175   // Called when there is no read position, i.e. when gptr() is null
   176   // or when gptr() >= egptr().  Subclasses are expected to override
   177   // this virtual member function.
   178   _STLP_DECLSPEC virtual int_type underflow();
   179 
   180   // Similar to underflow(), but used for unbuffered input.  Most 
   181   // subclasses should probably override this virtual member function.
   182   _STLP_DECLSPEC virtual int_type uflow();
   183 
   184   // Called when there is no putback position, i.e. when gptr() is null
   185   // or when gptr() == eback().  All subclasses are expected to override
   186   // this virtual member function.
   187   _STLP_DECLSPEC virtual int_type pbackfail(int_type = traits_type::eof());
   188 
   189 protected:                      // Virtual put area functions, as defined in
   190                                 // 27.5.2.4.5 of the standard.
   191 
   192   // Writes up to __n characters.  Return value is the number of characters
   193   // written.
   194   _STLP_DECLSPEC virtual streamsize xsputn(const char_type* __s, streamsize __n);
   195 
   196   // Extension: writes up to __n copies of __c.  Return value is the number
   197   // of characters written.
   198   _STLP_DECLSPEC virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
   199 
   200   // Called when there is no write position.  All subclasses are expected to
   201   // override this virtual member function.
   202   _STLP_DECLSPEC virtual int_type overflow(int_type = traits_type::eof());
   203 
   204 public:                         // Public members for writing characters.
   205   // Write a single character.
   206   int_type sputc(char_type __c) {
   207     return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c)
   208       : this->overflow(_Traits::to_int_type(__c)));
   209   }
   210 
   211   // Write __n characters.
   212   streamsize sputn(const char_type* __s, streamsize __n)
   213     { return this->xsputn(__s, __n); }
   214 
   215   // Extension: write __n copies of __c.
   216   streamsize _M_sputnc(char_type __c, streamsize __n)
   217     { return this->_M_xsputnc(__c, __n); }
   218 
   219 private:                        // Helper functions.
   220   _STLP_DECLSPEC int_type _M_snextc_aux();
   221 
   222 
   223 public:                         // Public members for reading characters.
   224   streamsize in_avail() {
   225     return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc();
   226   }
   227   
   228   // Advance to the next character and return it.
   229   int_type snextc() {
   230 	return ( _M_gend - _M_gnext > 1 ?
   231              _Traits::to_int_type(*++_M_gnext) :
   232              this->_M_snextc_aux());
   233   }
   234 
   235   // Return the current character and advance to the next.
   236   int_type sbumpc() {
   237     return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++) 
   238       : this->uflow();
   239   }
   240   
   241   // Return the current character without advancing to the next.
   242   int_type sgetc() {
   243     return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext) 
   244       : this->underflow();
   245   }
   246   
   247   streamsize sgetn(char_type* __s, streamsize __n)
   248   { return this->xsgetn(__s, __n); }
   249   
   250   int_type sputbackc(char_type __c) {
   251     return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1)))
   252       ? _Traits::to_int_type(*--_M_gnext)
   253       : this->pbackfail(_Traits::to_int_type(__c));
   254   }
   255   
   256   int_type sungetc() {
   257     return (_M_gbegin < _M_gnext)
   258       ? _Traits::to_int_type(*--_M_gnext)
   259       : this->pbackfail();
   260   }
   261 
   262 protected:                      // Virtual locale functions.
   263 
   264   // This is a hook, called by pubimbue() just before pubimbue()
   265   // sets the streambuf's locale to __loc.  Note that imbue should
   266   // not (and cannot, since it has no access to streambuf's private
   267   // members) set the streambuf's locale itself.
   268   _STLP_DECLSPEC virtual void imbue(const locale&);
   269 
   270 public:                         // Locale-related functions.
   271   _STLP_DECLSPEC locale pubimbue(const locale&);
   272   locale getloc() const { return _M_locale; }
   273 
   274 # ifndef _STLP_NO_ANACHRONISMS
   275   void stossc() { this->sbumpc(); }
   276 # endif
   277 #if defined(__MVS__) || defined(__OS400__)
   278 private: // Data members.
   279 
   280   char_type* _M_gbegin; // Beginning of get area
   281   char_type* _M_gnext; // Current position within the get area
   282   char_type* _M_gend; // End of get area
   283 
   284   char_type* _M_pbegin; // Beginning of put area
   285   char_type* _M_pnext; // Current position within the put area
   286   char_type* _M_pend; // End of put area
   287 #endif
   288 };
   289 
   290 
   291 //----------------------------------------------------------------------
   292 // Specialization: basic_streambuf<char, char_traits<char> >
   293 
   294 // We implement basic_streambuf<char, char_traits<char> > very differently
   295 // than the general basic_streambuf<> template.  The main reason for this
   296 // difference is a requirement in the C++ standard: the standard input
   297 // and output streams cin and cout are required by default to be synchronized
   298 // with the C library components stdin and stdout.  This means it must be
   299 // possible to synchronize a basic_streambuf<char> with a C buffer.
   300 //
   301 // There are two basic ways to do that.  First, the streambuf could be
   302 // unbuffered and delegate all buffering to stdio operations.  This
   303 // would be correct, but slow: it would require at least one virtual
   304 // function call for every character.  Second, the streambuf could use 
   305 // a C stdio FILE as its buffer.  
   306 //
   307 // We choose the latter option.  Every streambuf has pointers to two
   308 // FILE objects, one for the get area and one for the put area.  Ordinarily
   309 // it just uses a FILE object as a convenient way to package the three
   310 // get/put area pointers.  If a basic_streambuf<char> is synchronized with
   311 // a stdio stream, though, then the pointers are to a FILE object that's
   312 // also used by the C library.
   313 //
   314 // The header <stl/_stdio_file.h> encapsulates the implementation details
   315 // of struct FILE.  It contains low-level inline functions that convert
   316 // between whe FILE's internal representation and the three-pointer 
   317 // representation that basic_streambuf<> needs.
   318 
   319 _STLP_TEMPLATE_NULL 
   320 #ifdef __SYMBIAN32__
   321 class basic_streambuf<char, char_traits<char> >
   322 #else
   323 class _STLP_CLASS_DECLSPEC basic_streambuf<char, char_traits<char> >
   324 #endif
   325 {
   326   friend class basic_istream<char, char_traits<char> >;
   327   friend class basic_ostream<char, char_traits<char> >;
   328 public:                         // Typedefs.
   329   typedef char                        char_type;
   330   typedef char_traits<char>::int_type int_type;
   331   typedef char_traits<char>::pos_type pos_type;
   332   typedef char_traits<char>::off_type off_type;
   333   typedef char_traits<char>           traits_type;
   334 
   335 private:                        // Data members.
   336 
   337   FILE* _M_get;                 // Reference to the get area
   338   FILE* _M_put;                 // Reference to the put area
   339 
   340 #if defined(__hpux)
   341   _FILEX  _M_default_get;          // Get area, unless we're syncing with stdio.
   342   _FILEX  _M_default_put;          // Put area, unless we're syncing with stdio.
   343 #else
   344   FILE  _M_default_get;          // Get area, unless we're syncing with stdio.
   345   FILE  _M_default_put;          // Put area, unless we're syncing with stdio.
   346 #endif
   347 
   348   locale _M_locale;
   349 
   350 public:                         // Extension: locking, for thread safety.
   351   _STLP_mutex _M_lock;
   352 
   353 public:                         // Destructor.
   354   _STLP_DECLSPEC virtual ~basic_streambuf _STLP_PSPEC2(char, char_traits<char>) ();
   355 
   356 public:
   357   // The default constructor; defined here inline as some compilers require it
   358   _STLP_DECLSPEC basic_streambuf _STLP_PSPEC2(char, char_traits<char>) ();
   359   // Extension: a constructor for streambufs synchronized with C stdio files.
   360   _STLP_DECLSPEC basic_streambuf _STLP_PSPEC2(char, char_traits<char>) (FILE* __get, FILE* __put);
   361 
   362 protected:                      // Protected interface to the get area.
   363   char_type* eback() const { return _FILE_I_begin(_M_get); }
   364   char_type* gptr()  const { return _FILE_I_next(_M_get); }
   365   char_type* egptr() const { return _FILE_I_end(_M_get); }
   366   void gbump(int __n) { _FILE_I_bump(_M_get, __n); }
   367   void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
   368     { 
   369     _FILE_I_set(_M_get, __gbegin, __gnext, __gend); 
   370 #ifdef __SYMBIAN32__
   371     _change_input_mode();
   372 #endif
   373     }
   374 
   375 public:
   376   // An alternate public interface to the above functions
   377   // which allows us to avoid using templated friends which
   378   // are not supported on some compilers.
   379 
   380   char_type* _M_eback() const { return _FILE_I_begin(_M_get); }
   381   char_type* _M_gptr()  const { return _FILE_I_next(_M_get); }
   382   char_type* _M_egptr() const { return _FILE_I_end(_M_get); }
   383 
   384   void _M_gbump(int __n) { _FILE_I_bump(_M_get, __n); }
   385   void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
   386     { _FILE_I_set(_M_get, __gbegin, __gnext, __gend); }
   387 
   388 protected:                      // Protected interface to the put area
   389   char_type* pbase() const { return _FILE_O_begin(_M_put); }
   390   char_type* pptr()  const { return _FILE_O_next(_M_put); }
   391   char_type* epptr() const { return _FILE_O_end(_M_put); }
   392 
   393   void pbump(int __n) { _FILE_O_bump(_M_put, __n); }
   394   void setp(char_type* __pbegin, char_type* __pend)
   395     { _FILE_O_set(_M_put, __pbegin, __pbegin, __pend); }
   396 
   397 protected:                      // Virtual buffer-management functions.
   398   _STLP_DECLSPEC virtual basic_streambuf<char, char_traits<char> >* setbuf(char_type*, streamsize);
   399   _STLP_DECLSPEC virtual pos_type seekoff(off_type, ios_base::seekdir,
   400                            ios_base::openmode = ios_base::in | ios_base::out);
   401   _STLP_DECLSPEC  virtual pos_type
   402   seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
   403   _STLP_DECLSPEC virtual int sync();
   404 
   405 public:                         // Buffer management.
   406   basic_streambuf<char, char_traits<char> >* pubsetbuf(char_type* __s, streamsize __n) 
   407     { return this->setbuf(__s, __n); }
   408 
   409   pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
   410                       ios_base::openmode __mod = ios_base::in | ios_base::out)
   411     { return this->seekoff(__offset, __way, __mod); }
   412 
   413   pos_type pubseekpos(pos_type __sp,
   414                       ios_base::openmode __mod = ios_base::in | ios_base::out)
   415     { return this->seekpos(__sp, __mod); }
   416 
   417   int pubsync() { return this->sync(); }
   418 
   419 protected:                      // Virtual get area functions.
   420   _STLP_DECLSPEC virtual streamsize showmanyc();
   421   _STLP_DECLSPEC virtual streamsize xsgetn(char_type* __s, streamsize __n);
   422   _STLP_DECLSPEC virtual int_type underflow();
   423   _STLP_DECLSPEC virtual int_type uflow();
   424   _STLP_DECLSPEC virtual int_type pbackfail(int_type __c = traits_type::eof());
   425 
   426 protected:                      // Virtual put area functions.
   427   _STLP_DECLSPEC virtual streamsize xsputn(const char_type* __s, streamsize __n);
   428   _STLP_DECLSPEC virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
   429   _STLP_DECLSPEC virtual int_type overflow(int_type = traits_type::eof());
   430 #ifdef __SYMBIAN32__  
   431   virtual int save_read_buffer () { return 0; }
   432   virtual void _change_input_mode() {};
   433 #endif
   434 public:                         // Public members for writing characters.
   435   // Write a single character.
   436   int_type sputc(char_type __c) {
   437     int_type __res;
   438 	if( _FILE_O_avail(_M_put) > 0 )
   439 	{
   440 		_FILE_O_postincr(_M_put) = __c;
   441 		__res = traits_type::to_int_type(__c);
   442 	}
   443 	else
   444       __res = this->overflow(traits_type::to_int_type(__c));
   445     return __res;
   446   }
   447 
   448   // Write __n characters.
   449   streamsize sputn(const char_type* __s, streamsize __n)
   450     { return this->xsputn(__s, __n); }
   451 
   452   // Extension: write __n copies of __c.
   453   streamsize _M_sputnc(char_type __c, streamsize __n)
   454     { return this->_M_xsputnc(__c, __n); }
   455 
   456 private:                        // Helper functions.
   457   _STLP_DECLSPEC int_type _M_snextc_aux();
   458 
   459 public:                         // Public members for reading characters.
   460   streamsize in_avail()
   461     { return _FILE_I_avail(_M_get) > 0 ? _FILE_I_avail(_M_get)
   462 #ifdef __SYMBIAN32__  
   463                                      + save_read_buffer()
   464 #endif
   465                                      : this->showmanyc(); }
   466   
   467   // Advance to the next character and return it.
   468   int_type snextc() {
   469     return _FILE_I_avail(_M_get) > 1
   470       ? traits_type::to_int_type(_FILE_I_preincr(_M_get))
   471       : this->_M_snextc_aux();
   472   }
   473 
   474   // Return the current character and advance to the next.
   475   int_type sbumpc() {
   476     return _FILE_I_avail(_M_get) > 0
   477       ? traits_type::to_int_type(_FILE_I_postincr(_M_get))
   478       : this->uflow();
   479   }
   480 
   481   // Return the current character without advancing to the next.
   482   int_type sgetc() {
   483     return _FILE_I_avail(_M_get) > 0
   484       ? traits_type::to_int_type(*_FILE_I_next(_M_get))
   485       : this->underflow();
   486   }
   487     
   488   streamsize sgetn(char_type* __s, streamsize __n)
   489     { return this->xsgetn(__s, __n); }
   490 
   491   int_type sputbackc(char_type __c) {
   492     return _FILE_I_begin(_M_get) < _FILE_I_next(_M_get) &&
   493            __c == *(_FILE_I_next(_M_get) - 1)
   494       ? traits_type::to_int_type(_FILE_I_predecr(_M_get))
   495       : this->pbackfail(traits_type::to_int_type(__c));
   496   }
   497 
   498   int_type sungetc() {
   499     return _FILE_I_begin(_M_get) < _FILE_I_next(_M_get)
   500       ? traits_type::to_int_type(_FILE_I_predecr(_M_get))
   501       : this->pbackfail();
   502   }
   503 
   504 protected:                      // Virtual locale functions.
   505   _STLP_DECLSPEC virtual void imbue(const locale&);
   506 public:                         // Locale-related functions.
   507   _STLP_DECLSPEC locale pubimbue(const locale&);
   508   locale getloc() const { return _M_locale; }
   509 
   510 # ifndef _STLP_NO_ANACHRONISMS
   511 public:
   512   void stossc() { this->sbumpc(); }
   513 # endif
   514 
   515 #if defined(__MVS__) || defined(__OS400__)
   516 private: // Data members.
   517 
   518   char_type* _M_gbegin; // Beginning of get area
   519   char_type* _M_gnext; // Current position within the get area
   520   char_type* _M_gend; // End of get area
   521 
   522   char_type* _M_pbegin; // Beginning of put area
   523   char_type* _M_pnext; // Current position within the put area
   524   char_type* _M_pend; // End of put area
   525 #endif
   526 
   527 };
   528 _STLP_END_NAMESPACE
   529 
   530 # if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
   531 #  include <stl/_streambuf.c>
   532 # endif
   533 
   534 #endif
   535 // Local Variables:
   536 // mode:C++
   537 // End: