/project/jni/stlport/stlport/stl/_streambuf.h

https://github.com/aichunyu/FFPlayer · C Header · 301 lines · 159 code · 62 blank · 80 comment · 6 complexity · d5205f0ad12c52461baebb9597dbe2d3 MD5 · raw file

  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: // Extension: locking, for thread safety.
  59. // _STLP_mutex _M_lock;
  60. public: // Destructor.
  61. virtual ~basic_streambuf();
  62. protected: // The default constructor.
  63. basic_streambuf()
  64. #if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) && defined (_STLP_USE_STATIC_LIB)
  65. //We make it inline to avoid unresolved symbol.
  66. : _M_gbegin(0), _M_gnext(0), _M_gend(0),
  67. _M_pbegin(0), _M_pnext(0), _M_pend(0),
  68. _M_locale()
  69. {}
  70. #else
  71. ;
  72. #endif
  73. protected: // Protected interface to the get area.
  74. char_type* eback() const { return _M_gbegin; } // Beginning
  75. char_type* gptr() const { return _M_gnext; } // Current position
  76. char_type* egptr() const { return _M_gend; } // End
  77. void gbump(int __n) { _M_gnext += __n; }
  78. void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
  79. _M_gbegin = __gbegin;
  80. _M_gnext = __gnext;
  81. _M_gend = __gend;
  82. }
  83. public:
  84. // An alternate public interface to the above functions
  85. // which allows us to avoid using templated friends which
  86. // are not supported on some compilers.
  87. char_type* _M_eback() const { return eback(); }
  88. char_type* _M_gptr() const { return gptr(); }
  89. char_type* _M_egptr() const { return egptr(); }
  90. void _M_gbump(int __n) { gbump(__n); }
  91. void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
  92. { this->setg(__gbegin, __gnext, __gend); }
  93. protected: // Protected interface to the put area
  94. char_type* pbase() const { return _M_pbegin; } // Beginning
  95. char_type* pptr() const { return _M_pnext; } // Current position
  96. char_type* epptr() const { return _M_pend; } // End
  97. void pbump(int __n) { _M_pnext += __n; }
  98. void setp(char_type* __pbegin, char_type* __pend) {
  99. _M_pbegin = __pbegin;
  100. _M_pnext = __pbegin;
  101. _M_pend = __pend;
  102. }
  103. protected: // Virtual buffer management functions.
  104. virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
  105. // Alters the stream position, using an integer offset. In this
  106. // class seekoff does nothing; subclasses are expected to override it.
  107. virtual pos_type seekoff(off_type, ios_base::seekdir,
  108. ios_base::openmode = ios_base::in | ios_base::out);
  109. // Alters the stream position, using a previously obtained streampos. In
  110. // this class seekpos does nothing; subclasses are expected to override it.
  111. virtual pos_type
  112. seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
  113. // Synchronizes (i.e. flushes) the buffer. All subclasses are expected to
  114. // override this virtual member function.
  115. virtual int sync();
  116. public: // Buffer management.
  117. basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n)
  118. { return this->setbuf(__s, __n); }
  119. pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
  120. ios_base::openmode __mod = ios_base::in | ios_base::out)
  121. { return this->seekoff(__offset, __way, __mod); }
  122. pos_type pubseekpos(pos_type __sp,
  123. ios_base::openmode __mod = ios_base::in | ios_base::out)
  124. { return this->seekpos(__sp, __mod); }
  125. int pubsync() { return this->sync(); }
  126. protected: // Virtual get area functions, as defined in
  127. // 17.5.2.4.3 and 17.5.2.4.4 of the standard.
  128. // Returns a lower bound on the number of characters that we can read,
  129. // with underflow, before reaching end of file. (-1 is a special value:
  130. // it means that underflow will fail.) Most subclasses should probably
  131. // override this virtual member function.
  132. virtual streamsize showmanyc();
  133. // Reads up to __n characters. Return value is the number of
  134. // characters read.
  135. virtual streamsize xsgetn(char_type* __s, streamsize __n);
  136. // Called when there is no read position, i.e. when gptr() is null
  137. // or when gptr() >= egptr(). Subclasses are expected to override
  138. // this virtual member function.
  139. virtual int_type underflow();
  140. // Similar to underflow(), but used for unbuffered input. Most
  141. // subclasses should probably override this virtual member function.
  142. virtual int_type uflow();
  143. // Called when there is no putback position, i.e. when gptr() is null
  144. // or when gptr() == eback(). All subclasses are expected to override
  145. // this virtual member function.
  146. virtual int_type pbackfail(int_type = traits_type::eof());
  147. protected: // Virtual put area functions, as defined in
  148. // 27.5.2.4.5 of the standard.
  149. // Writes up to __n characters. Return value is the number of characters
  150. // written.
  151. virtual streamsize xsputn(const char_type* __s, streamsize __n);
  152. // Extension: writes up to __n copies of __c. Return value is the number
  153. // of characters written.
  154. virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
  155. // Called when there is no write position. All subclasses are expected to
  156. // override this virtual member function.
  157. virtual int_type overflow(int_type = traits_type::eof());
  158. public: // Public members for writing characters.
  159. // Write a single character.
  160. int_type sputc(char_type __c) {
  161. return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c)
  162. : this->overflow(_Traits::to_int_type(__c)));
  163. }
  164. // Write __n characters.
  165. streamsize sputn(const char_type* __s, streamsize __n)
  166. { return this->xsputn(__s, __n); }
  167. // Extension: write __n copies of __c.
  168. streamsize _M_sputnc(char_type __c, streamsize __n)
  169. { return this->_M_xsputnc(__c, __n); }
  170. private: // Helper functions.
  171. int_type _M_snextc_aux();
  172. public: // Public members for reading characters.
  173. streamsize in_avail() {
  174. return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc();
  175. }
  176. // Advance to the next character and return it.
  177. int_type snextc() {
  178. return ( _M_gend - _M_gnext > 1 ?
  179. _Traits::to_int_type(*++_M_gnext) :
  180. this->_M_snextc_aux());
  181. }
  182. // Return the current character and advance to the next.
  183. int_type sbumpc() {
  184. return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++)
  185. : this->uflow();
  186. }
  187. // Return the current character without advancing to the next.
  188. int_type sgetc() {
  189. return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext)
  190. : this->underflow();
  191. }
  192. streamsize sgetn(char_type* __s, streamsize __n)
  193. { return this->xsgetn(__s, __n); }
  194. int_type sputbackc(char_type __c) {
  195. return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1)))
  196. ? _Traits::to_int_type(*--_M_gnext)
  197. : this->pbackfail(_Traits::to_int_type(__c));
  198. }
  199. int_type sungetc() {
  200. return (_M_gbegin < _M_gnext)
  201. ? _Traits::to_int_type(*--_M_gnext)
  202. : this->pbackfail();
  203. }
  204. protected: // Virtual locale functions.
  205. // This is a hook, called by pubimbue() just before pubimbue()
  206. // sets the streambuf's locale to __loc. Note that imbue should
  207. // not (and cannot, since it has no access to streambuf's private
  208. // members) set the streambuf's locale itself.
  209. virtual void imbue(const locale&);
  210. public: // Locale-related functions.
  211. locale pubimbue(const locale&);
  212. locale getloc() const { return _M_locale; }
  213. #if !defined (_STLP_NO_ANACHRONISMS)
  214. void stossc() { this->sbumpc(); }
  215. #endif
  216. #if defined (__MVS__) || defined (__OS400__)
  217. private: // Data members.
  218. char_type* _M_gbegin; // Beginning of get area
  219. char_type* _M_gnext; // Current position within the get area
  220. char_type* _M_gend; // End of get area
  221. char_type* _M_pbegin; // Beginning of put area
  222. char_type* _M_pnext; // Current position within the put area
  223. char_type* _M_pend; // End of put area
  224. #endif
  225. };
  226. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  227. _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<char, char_traits<char> >;
  228. # if !defined (_STLP_NO_WCHAR_T)
  229. _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<wchar_t, char_traits<wchar_t> >;
  230. # endif // _STLP_NO_WCHAR_T
  231. #endif // _STLP_USE_TEMPLATE_EXPORT
  232. _STLP_END_NAMESPACE
  233. #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  234. # include <stl/_streambuf.c>
  235. #endif
  236. #endif
  237. // Local Variables:
  238. // mode:C++
  239. // End: