williamr@2: /* williamr@2: * Copyright (c) 1999 williamr@2: * Silicon Graphics Computer Systems, Inc. williamr@2: * williamr@4: * Copyright (c) 1999 williamr@2: * Boris Fomitchev williamr@2: * williamr@2: * This material is provided "as is", with absolutely no warranty expressed williamr@2: * or implied. Any use is at your own risk. williamr@2: * williamr@4: * Permission to use or copy this software for any purpose is hereby granted williamr@2: * without fee, provided the above notices are retained on all copies. williamr@2: * Permission to modify the code and to distribute modified code is granted, williamr@2: * provided the above notices are retained, and a notice that the code was williamr@2: * modified is included with the above copyright notice. williamr@2: * williamr@4: */ williamr@2: #ifndef _STLP_INTERNAL_STREAMBUF williamr@2: #define _STLP_INTERNAL_STREAMBUF williamr@2: williamr@2: #ifndef _STLP_IOS_BASE_H williamr@4: # include // Needed for ios_base bitfield members. williamr@4: #endif // includes . williamr@2: williamr@2: _STLP_BEGIN_NAMESPACE williamr@2: williamr@2: //---------------------------------------------------------------------- williamr@2: // Class basic_streambuf<>, the base class of the streambuf hierarchy. williamr@2: williamr@2: // A basic_streambuf<> manages an input (get) area and an output (put) williamr@2: // area. Each is described by three pointers: a beginning, an end, and a williamr@2: // current position. basic_streambuf<> contains some very simple member williamr@2: // functions that manipulate those six pointers, but almost all of the real williamr@2: // functionality gets delegated to protected virtual member functions. williamr@2: // All of the public member functions are inline, and most of the protected williamr@2: // member functions are virtual. williamr@2: williamr@2: // Although basic_streambuf<> is not abstract, it is useful only as a base williamr@2: // class. Its virtual member functions have default definitions such that williamr@2: // reading from a basic_streambuf<> will always yield EOF, and writing to a williamr@2: // basic_streambuf<> will always fail. williamr@2: williamr@2: // The second template parameter, _Traits, defaults to char_traits<_CharT>. williamr@2: // The default is declared in header , and it isn't declared here williamr@4: // because C++ language rules do not allow it to be declared twice. williamr@2: williamr@2: template williamr@4: class basic_streambuf { williamr@2: friend class basic_istream<_CharT, _Traits>; williamr@2: friend class basic_ostream<_CharT, _Traits>; williamr@2: williamr@2: public: // Typedefs. williamr@2: typedef _CharT char_type; williamr@2: typedef typename _Traits::int_type int_type; williamr@2: typedef typename _Traits::pos_type pos_type; williamr@2: typedef typename _Traits::off_type off_type; williamr@2: typedef _Traits traits_type; williamr@2: williamr@2: private: // Data members. williamr@2: williamr@2: char_type* _M_gbegin; // Beginning of get area williamr@2: char_type* _M_gnext; // Current position within the get area williamr@2: char_type* _M_gend; // End of get area williamr@2: williamr@2: char_type* _M_pbegin; // Beginning of put area williamr@2: char_type* _M_pnext; // Current position within the put area williamr@2: char_type* _M_pend; // End of put area williamr@2: williamr@2: locale _M_locale; // The streambuf's locale object williamr@2: williamr@4: //public: // Extension: locking, for thread safety. williamr@4: // _STLP_mutex _M_lock; williamr@2: williamr@2: public: // Destructor. williamr@4: virtual ~basic_streambuf(); williamr@2: williamr@2: protected: // The default constructor. williamr@4: basic_streambuf() williamr@4: #if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) && defined (_STLP_USE_STATIC_LIB) williamr@4: //We make it inline to avoid unresolved symbol. williamr@4: : _M_gbegin(0), _M_gnext(0), _M_gend(0), williamr@4: _M_pbegin(0), _M_pnext(0), _M_pend(0), williamr@4: _M_locale() williamr@4: {} williamr@4: #else williamr@4: ; williamr@4: #endif williamr@2: williamr@2: protected: // Protected interface to the get area. williamr@2: char_type* eback() const { return _M_gbegin; } // Beginning williamr@2: char_type* gptr() const { return _M_gnext; } // Current position williamr@2: char_type* egptr() const { return _M_gend; } // End williamr@4: williamr@2: void gbump(int __n) { _M_gnext += __n; } williamr@2: void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) { williamr@2: _M_gbegin = __gbegin; williamr@2: _M_gnext = __gnext; williamr@2: _M_gend = __gend; williamr@2: } williamr@2: williamr@2: public: williamr@2: // An alternate public interface to the above functions williamr@2: // which allows us to avoid using templated friends which williamr@2: // are not supported on some compilers. williamr@2: char_type* _M_eback() const { return eback(); } williamr@2: char_type* _M_gptr() const { return gptr(); } williamr@2: char_type* _M_egptr() const { return egptr(); } williamr@2: void _M_gbump(int __n) { gbump(__n); } williamr@2: void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) williamr@4: { this->setg(__gbegin, __gnext, __gend); } williamr@2: williamr@2: protected: // Protected interface to the put area williamr@2: williamr@2: char_type* pbase() const { return _M_pbegin; } // Beginning williamr@2: char_type* pptr() const { return _M_pnext; } // Current position williamr@2: char_type* epptr() const { return _M_pend; } // End williamr@2: williamr@2: void pbump(int __n) { _M_pnext += __n; } williamr@2: void setp(char_type* __pbegin, char_type* __pend) { williamr@2: _M_pbegin = __pbegin; williamr@2: _M_pnext = __pbegin; williamr@2: _M_pend = __pend; williamr@2: } williamr@2: williamr@2: protected: // Virtual buffer management functions. williamr@2: williamr@4: virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize); williamr@2: williamr@2: // Alters the stream position, using an integer offset. In this williamr@2: // class seekoff does nothing; subclasses are expected to override it. williamr@4: virtual pos_type seekoff(off_type, ios_base::seekdir, williamr@2: ios_base::openmode = ios_base::in | ios_base::out); williamr@2: williamr@2: // Alters the stream position, using a previously obtained streampos. In williamr@2: // this class seekpos does nothing; subclasses are expected to override it. williamr@4: virtual pos_type williamr@2: seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out); williamr@2: williamr@4: // Synchronizes (i.e. flushes) the buffer. All subclasses are expected to williamr@2: // override this virtual member function. williamr@4: virtual int sync(); williamr@2: williamr@2: williamr@2: public: // Buffer management. williamr@4: basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n) williamr@4: { return this->setbuf(__s, __n); } williamr@2: williamr@2: pos_type pubseekoff(off_type __offset, ios_base::seekdir __way, williamr@2: ios_base::openmode __mod = ios_base::in | ios_base::out) williamr@4: { return this->seekoff(__offset, __way, __mod); } williamr@2: williamr@2: pos_type pubseekpos(pos_type __sp, williamr@2: ios_base::openmode __mod = ios_base::in | ios_base::out) williamr@4: { return this->seekpos(__sp, __mod); } williamr@2: williamr@2: int pubsync() { return this->sync(); } williamr@2: williamr@2: protected: // Virtual get area functions, as defined in williamr@2: // 17.5.2.4.3 and 17.5.2.4.4 of the standard. williamr@2: // Returns a lower bound on the number of characters that we can read, williamr@2: // with underflow, before reaching end of file. (-1 is a special value: williamr@2: // it means that underflow will fail.) Most subclasses should probably williamr@2: // override this virtual member function. williamr@4: virtual streamsize showmanyc(); williamr@2: williamr@4: // Reads up to __n characters. Return value is the number of williamr@2: // characters read. williamr@4: virtual streamsize xsgetn(char_type* __s, streamsize __n); williamr@2: williamr@2: // Called when there is no read position, i.e. when gptr() is null williamr@2: // or when gptr() >= egptr(). Subclasses are expected to override williamr@2: // this virtual member function. williamr@4: virtual int_type underflow(); williamr@2: williamr@4: // Similar to underflow(), but used for unbuffered input. Most williamr@2: // subclasses should probably override this virtual member function. williamr@4: virtual int_type uflow(); williamr@2: williamr@2: // Called when there is no putback position, i.e. when gptr() is null williamr@2: // or when gptr() == eback(). All subclasses are expected to override williamr@2: // this virtual member function. williamr@4: virtual int_type pbackfail(int_type = traits_type::eof()); williamr@2: williamr@2: protected: // Virtual put area functions, as defined in williamr@2: // 27.5.2.4.5 of the standard. williamr@2: williamr@2: // Writes up to __n characters. Return value is the number of characters williamr@2: // written. williamr@4: virtual streamsize xsputn(const char_type* __s, streamsize __n); williamr@2: williamr@2: // Extension: writes up to __n copies of __c. Return value is the number williamr@2: // of characters written. williamr@4: virtual streamsize _M_xsputnc(char_type __c, streamsize __n); williamr@2: williamr@2: // Called when there is no write position. All subclasses are expected to williamr@2: // override this virtual member function. williamr@4: virtual int_type overflow(int_type = traits_type::eof()); williamr@2: williamr@2: public: // Public members for writing characters. williamr@2: // Write a single character. williamr@2: int_type sputc(char_type __c) { williamr@2: return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c) williamr@2: : this->overflow(_Traits::to_int_type(__c))); williamr@2: } williamr@2: williamr@2: // Write __n characters. williamr@2: streamsize sputn(const char_type* __s, streamsize __n) williamr@4: { return this->xsputn(__s, __n); } williamr@2: williamr@2: // Extension: write __n copies of __c. williamr@2: streamsize _M_sputnc(char_type __c, streamsize __n) williamr@4: { return this->_M_xsputnc(__c, __n); } williamr@2: williamr@2: private: // Helper functions. williamr@4: int_type _M_snextc_aux(); williamr@2: williamr@2: public: // Public members for reading characters. williamr@2: streamsize in_avail() { williamr@2: return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc(); williamr@2: } williamr@4: williamr@2: // Advance to the next character and return it. williamr@2: int_type snextc() { williamr@4: return ( _M_gend - _M_gnext > 1 ? williamr@2: _Traits::to_int_type(*++_M_gnext) : williamr@2: this->_M_snextc_aux()); williamr@2: } williamr@2: williamr@2: // Return the current character and advance to the next. williamr@2: int_type sbumpc() { williamr@4: return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++) williamr@2: : this->uflow(); williamr@2: } williamr@4: williamr@2: // Return the current character without advancing to the next. williamr@2: int_type sgetc() { williamr@4: return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext) williamr@2: : this->underflow(); williamr@2: } williamr@4: williamr@2: streamsize sgetn(char_type* __s, streamsize __n) williamr@2: { return this->xsgetn(__s, __n); } williamr@4: williamr@2: int_type sputbackc(char_type __c) { williamr@2: return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1))) williamr@2: ? _Traits::to_int_type(*--_M_gnext) williamr@2: : this->pbackfail(_Traits::to_int_type(__c)); williamr@2: } williamr@4: williamr@2: int_type sungetc() { williamr@2: return (_M_gbegin < _M_gnext) williamr@2: ? _Traits::to_int_type(*--_M_gnext) williamr@2: : this->pbackfail(); williamr@2: } williamr@2: williamr@2: protected: // Virtual locale functions. williamr@2: williamr@2: // This is a hook, called by pubimbue() just before pubimbue() williamr@2: // sets the streambuf's locale to __loc. Note that imbue should williamr@2: // not (and cannot, since it has no access to streambuf's private williamr@2: // members) set the streambuf's locale itself. williamr@4: virtual void imbue(const locale&); williamr@2: williamr@2: public: // Locale-related functions. williamr@4: locale pubimbue(const locale&); williamr@2: locale getloc() const { return _M_locale; } williamr@2: williamr@4: #if !defined (_STLP_NO_ANACHRONISMS) williamr@2: void stossc() { this->sbumpc(); } williamr@4: #endif williamr@4: #if defined (__MVS__) || defined (__OS400__) williamr@2: private: // Data members. williamr@2: williamr@2: char_type* _M_gbegin; // Beginning of get area williamr@2: char_type* _M_gnext; // Current position within the get area williamr@2: char_type* _M_gend; // End of get area williamr@2: williamr@2: char_type* _M_pbegin; // Beginning of put area williamr@2: char_type* _M_pnext; // Current position within the put area williamr@2: char_type* _M_pend; // End of put area williamr@2: #endif williamr@2: }; williamr@2: williamr@4: #if defined (_STLP_USE_TEMPLATE_EXPORT) williamr@4: _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf >; williamr@4: # if !defined (_STLP_NO_WCHAR_T) williamr@4: _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf >; williamr@4: # endif // _STLP_NO_WCHAR_T williamr@4: #endif // _STLP_USE_TEMPLATE_EXPORT williamr@2: williamr@4: _STLP_END_NAMESPACE williamr@2: williamr@4: #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION) williamr@4: # include williamr@2: #endif williamr@2: williamr@2: #endif williamr@2: williamr@2: // Local Variables: williamr@2: // mode:C++ williamr@2: // End: