_streambuf.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #ifndef _STLP_INTERNAL_STREAMBUF
  19. #define _STLP_INTERNAL_STREAMBUF
  20. #ifndef _STLP_IOS_BASE_H
  21. # include <stl/_ios_base.h> // Needed for ios_base bitfield members.
  22. #endif // <ios_base> includes <iosfwd>.
  23. _STLP_BEGIN_NAMESPACE
  24. //----------------------------------------------------------------------
  25. // Class basic_streambuf<>, the base class of the streambuf hierarchy.
  26. // A basic_streambuf<> manages an input (get) area and an output (put)
  27. // area. Each is described by three pointers: a beginning, an end, and a
  28. // current position. basic_streambuf<> contains some very simple member
  29. // functions that manipulate those six pointers, but almost all of the real
  30. // functionality gets delegated to protected virtual member functions.
  31. // All of the public member functions are inline, and most of the protected
  32. // member functions are virtual.
  33. // Although basic_streambuf<> is not abstract, it is useful only as a base
  34. // class. Its virtual member functions have default definitions such that
  35. // reading from a basic_streambuf<> will always yield EOF, and writing to a
  36. // basic_streambuf<> will always fail.
  37. // The second template parameter, _Traits, defaults to char_traits<_CharT>.
  38. // The default is declared in header <iosfwd>, and it isn't declared here
  39. // because C++ language rules do not allow it to be declared twice.
  40. template <class _CharT, class _Traits>
  41. class basic_streambuf {
  42. friend class basic_istream<_CharT, _Traits>;
  43. friend class basic_ostream<_CharT, _Traits>;
  44. public: // Typedefs.
  45. typedef _CharT char_type;
  46. typedef typename _Traits::int_type int_type;
  47. typedef typename _Traits::pos_type pos_type;
  48. typedef typename _Traits::off_type off_type;
  49. typedef _Traits traits_type;
  50. private: // Data members.
  51. char_type* _M_gbegin; // Beginning of get area
  52. char_type* _M_gnext; // Current position within the get area
  53. char_type* _M_gend; // End of get area
  54. char_type* _M_pbegin; // Beginning of put area
  55. char_type* _M_pnext; // Current position within the put area
  56. char_type* _M_pend; // End of put area
  57. locale _M_locale; // The streambuf's locale object
  58. public: // Destructor.
  59. virtual ~basic_streambuf();
  60. protected: // The default constructor.
  61. basic_streambuf()
  62. #if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) && defined (_STLP_USE_STATIC_LIB)
  63. //We make it inline to avoid unresolved symbol.
  64. : _M_gbegin(0), _M_gnext(0), _M_gend(0),
  65. _M_pbegin(0), _M_pnext(0), _M_pend(0),
  66. _M_locale()
  67. {}
  68. #else
  69. ;
  70. #endif
  71. protected: // Protected interface to the get area.
  72. char_type* eback() const { return _M_gbegin; } // Beginning
  73. char_type* gptr() const { return _M_gnext; } // Current position
  74. char_type* egptr() const { return _M_gend; } // End
  75. void gbump(int __n) { _M_gnext += __n; }
  76. void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
  77. _M_gbegin = __gbegin;
  78. _M_gnext = __gnext;
  79. _M_gend = __gend;
  80. }
  81. public:
  82. // An alternate public interface to the above functions
  83. // which allows us to avoid using templated friends which
  84. // are not supported on some compilers.
  85. char_type* _M_eback() const { return eback(); }
  86. char_type* _M_gptr() const { return gptr(); }
  87. char_type* _M_egptr() const { return egptr(); }
  88. void _M_gbump(int __n) { gbump(__n); }
  89. void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
  90. { this->setg(__gbegin, __gnext, __gend); }
  91. protected: // Protected interface to the put area
  92. char_type* pbase() const { return _M_pbegin; } // Beginning
  93. char_type* pptr() const { return _M_pnext; } // Current position
  94. char_type* epptr() const { return _M_pend; } // End
  95. void pbump(int __n) { _M_pnext += __n; }
  96. void setp(char_type* __pbegin, char_type* __pend) {
  97. _M_pbegin = __pbegin;
  98. _M_pnext = __pbegin;
  99. _M_pend = __pend;
  100. }
  101. protected: // Virtual buffer management functions.
  102. virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
  103. // Alters the stream position, using an integer offset. In this
  104. // class seekoff does nothing; subclasses are expected to override it.
  105. virtual pos_type seekoff(off_type, ios_base::seekdir,
  106. ios_base::openmode = ios_base::in | ios_base::out);
  107. // Alters the stream position, using a previously obtained streampos. In
  108. // this class seekpos does nothing; subclasses are expected to override it.
  109. virtual pos_type
  110. seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
  111. // Synchronizes (i.e. flushes) the buffer. All subclasses are expected to
  112. // override this virtual member function.
  113. virtual int sync();
  114. public: // Buffer management.
  115. basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n)
  116. { return this->setbuf(__s, __n); }
  117. pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
  118. ios_base::openmode __mod = ios_base::in | ios_base::out)
  119. { return this->seekoff(__offset, __way, __mod); }
  120. pos_type pubseekpos(pos_type __sp,
  121. ios_base::openmode __mod = ios_base::in | ios_base::out)
  122. { return this->seekpos(__sp, __mod); }
  123. int pubsync() { return this->sync(); }
  124. protected: // Virtual get area functions, as defined in
  125. // 17.5.2.4.3 and 17.5.2.4.4 of the standard.
  126. // Returns a lower bound on the number of characters that we can read,
  127. // with underflow, before reaching end of file. (-1 is a special value:
  128. // it means that underflow will fail.) Most subclasses should probably
  129. // override this virtual member function.
  130. virtual streamsize showmanyc();
  131. // Reads up to __n characters. Return value is the number of
  132. // characters read.
  133. virtual streamsize xsgetn(char_type* __s, streamsize __n);
  134. // Called when there is no read position, i.e. when gptr() is null
  135. // or when gptr() >= egptr(). Subclasses are expected to override
  136. // this virtual member function.
  137. virtual int_type underflow();
  138. // Similar to underflow(), but used for unbuffered input. Most
  139. // subclasses should probably override this virtual member function.
  140. virtual int_type uflow();
  141. // Called when there is no putback position, i.e. when gptr() is null
  142. // or when gptr() == eback(). All subclasses are expected to override
  143. // this virtual member function.
  144. virtual int_type pbackfail(int_type = traits_type::eof());
  145. protected: // Virtual put area functions, as defined in
  146. // 27.5.2.4.5 of the standard.
  147. // Writes up to __n characters. Return value is the number of characters
  148. // written.
  149. virtual streamsize xsputn(const char_type* __s, streamsize __n);
  150. // Extension: writes up to __n copies of __c. Return value is the number
  151. // of characters written.
  152. virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
  153. // Called when there is no write position. All subclasses are expected to
  154. // override this virtual member function.
  155. virtual int_type overflow(int_type = traits_type::eof());
  156. public: // Public members for writing characters.
  157. // Write a single character.
  158. int_type sputc(char_type __c) {
  159. return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c)
  160. : this->overflow(_Traits::to_int_type(__c)));
  161. }
  162. // Write __n characters.
  163. streamsize sputn(const char_type* __s, streamsize __n)
  164. { return this->xsputn(__s, __n); }
  165. // Extension: write __n copies of __c.
  166. streamsize _M_sputnc(char_type __c, streamsize __n)
  167. { return this->_M_xsputnc(__c, __n); }
  168. private: // Helper functions.
  169. int_type _M_snextc_aux();
  170. public: // Public members for reading characters.
  171. streamsize in_avail() {
  172. return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc();
  173. }
  174. // Advance to the next character and return it.
  175. int_type snextc() {
  176. return ( _M_gend - _M_gnext > 1 ?
  177. _Traits::to_int_type(*++_M_gnext) :
  178. this->_M_snextc_aux());
  179. }
  180. // Return the current character and advance to the next.
  181. int_type sbumpc() {
  182. return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++)
  183. : this->uflow();
  184. }
  185. // Return the current character without advancing to the next.
  186. int_type sgetc() {
  187. return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext)
  188. : this->underflow();
  189. }
  190. streamsize sgetn(char_type* __s, streamsize __n)
  191. { return this->xsgetn(__s, __n); }
  192. int_type sputbackc(char_type __c) {
  193. return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1)))
  194. ? _Traits::to_int_type(*--_M_gnext)
  195. : this->pbackfail(_Traits::to_int_type(__c));
  196. }
  197. int_type sungetc() {
  198. return (_M_gbegin < _M_gnext)
  199. ? _Traits::to_int_type(*--_M_gnext)
  200. : this->pbackfail();
  201. }
  202. protected: // Virtual locale functions.
  203. // This is a hook, called by pubimbue() just before pubimbue()
  204. // sets the streambuf's locale to __loc. Note that imbue should
  205. // not (and cannot, since it has no access to streambuf's private
  206. // members) set the streambuf's locale itself.
  207. virtual void imbue(const locale&);
  208. public: // Locale-related functions.
  209. locale pubimbue(const locale&);
  210. locale getloc() const { return _M_locale; }
  211. #if !defined (_STLP_NO_ANACHRONISMS)
  212. void stossc() { this->sbumpc(); }
  213. #endif
  214. };
  215. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  216. _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<char, char_traits<char> >;
  217. # if !defined (_STLP_NO_WCHAR_T)
  218. _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<wchar_t, char_traits<wchar_t> >;
  219. # endif // _STLP_NO_WCHAR_T
  220. #endif // _STLP_USE_TEMPLATE_EXPORT
  221. _STLP_END_NAMESPACE
  222. #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  223. # include <stl/_streambuf.c>
  224. #endif
  225. #endif
  226. // Local Variables:
  227. // mode:C++
  228. // End: