_istreambuf_iterator.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // WARNING: This is an internal header file, included by other C++
  19. // standard library headers. You should not attempt to use this header
  20. // file directly.
  21. #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
  22. #define _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
  23. #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  24. # include <stl/_iterator_base.h>
  25. #endif
  26. #ifndef _STLP_INTERNAL_STREAMBUF
  27. # include <stl/_streambuf.h>
  28. #endif
  29. _STLP_BEGIN_NAMESPACE
  30. // defined in _istream.h
  31. template <class _CharT, class _Traits>
  32. extern basic_streambuf<_CharT, _Traits>* _STLP_CALL _M_get_istreambuf(basic_istream<_CharT, _Traits>& ) ;
  33. // We do not read any characters until operator* is called. operator* calls sgetc
  34. // unless the iterator is unchanged from the last call in which case a cached value is
  35. // used. Calls to operator++ use sbumpc.
  36. template<class _CharT, class _Traits>
  37. class istreambuf_iterator :
  38. public iterator<input_iterator_tag, _CharT, typename _Traits::off_type, _CharT*, _CharT&>
  39. {
  40. public:
  41. typedef _CharT char_type;
  42. typedef _Traits traits_type;
  43. typedef typename _Traits::int_type int_type;
  44. typedef basic_streambuf<_CharT, _Traits> streambuf_type;
  45. typedef basic_istream<_CharT, _Traits> istream_type;
  46. typedef input_iterator_tag iterator_category;
  47. typedef _CharT value_type;
  48. typedef typename _Traits::off_type difference_type;
  49. typedef const _CharT* pointer;
  50. typedef const _CharT& reference;
  51. public:
  52. istreambuf_iterator(streambuf_type* __p = 0) { this->_M_init(__p); }
  53. // istreambuf_iterator(basic_istream<_CharT, _Traits>& __is) { this->_M_init(_M_get_istreambuf(__is)); }
  54. inline istreambuf_iterator(basic_istream<_CharT, _Traits>& __is);
  55. char_type operator*() const { this->_M_getc(); return _M_c; }
  56. istreambuf_iterator<_CharT, _Traits>& operator++() {
  57. _M_buf->sbumpc();
  58. _M_have_c = false;
  59. return *this;
  60. }
  61. istreambuf_iterator<_CharT, _Traits> operator++(int);
  62. bool equal(const istreambuf_iterator<_CharT, _Traits>& __i) const {
  63. if (this->_M_buf)
  64. this->_M_getc();
  65. if (__i._M_buf)
  66. __i._M_getc();
  67. return this->_M_eof == __i._M_eof;
  68. }
  69. private:
  70. void _M_init(streambuf_type* __p) {
  71. _M_buf = __p;
  72. _M_eof = (__p == 0);
  73. _M_have_c = false;
  74. }
  75. void _M_getc() const {
  76. if (_M_have_c)
  77. return;
  78. int_type __c = _M_buf->sgetc();
  79. _STLP_MUTABLE(_Self, _M_c) = traits_type::to_char_type(__c);
  80. _STLP_MUTABLE(_Self, _M_eof) = traits_type::eq_int_type(__c, traits_type::eof());
  81. _STLP_MUTABLE(_Self, _M_have_c) = true;
  82. }
  83. private:
  84. streambuf_type* _M_buf;
  85. mutable _CharT _M_c;
  86. mutable bool _M_eof;
  87. mutable bool _M_have_c;
  88. };
  89. template<class _CharT, class _Traits>
  90. inline istreambuf_iterator<_CharT, _Traits>::istreambuf_iterator(basic_istream<_CharT, _Traits>& __is)
  91. { this->_M_init(_M_get_istreambuf(__is)); }
  92. template<class _CharT, class _Traits>
  93. inline bool _STLP_CALL operator==(const istreambuf_iterator<_CharT, _Traits>& __x,
  94. const istreambuf_iterator<_CharT, _Traits>& __y) {
  95. return __x.equal(__y);
  96. }
  97. #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
  98. template<class _CharT, class _Traits>
  99. inline bool _STLP_CALL operator!=(const istreambuf_iterator<_CharT, _Traits>& __x,
  100. const istreambuf_iterator<_CharT, _Traits>& __y) {
  101. return !__x.equal(__y);
  102. }
  103. #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
  104. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  105. _STLP_EXPORT_TEMPLATE_CLASS istreambuf_iterator<char, char_traits<char> >;
  106. # if defined (INSTANTIATE_WIDE_STREAMS)
  107. _STLP_EXPORT_TEMPLATE_CLASS istreambuf_iterator<wchar_t, char_traits<wchar_t> >;
  108. # endif
  109. # endif /* _STLP_USE_TEMPLATE_EXPORT */
  110. # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
  111. template <class _CharT, class _Traits>
  112. inline input_iterator_tag _STLP_CALL iterator_category(const istreambuf_iterator<_CharT, _Traits>&) { return input_iterator_tag(); }
  113. template <class _CharT, class _Traits>
  114. inline streamoff* _STLP_CALL
  115. distance_type(const istreambuf_iterator<_CharT, _Traits>&) { return (streamoff*)0; }
  116. template <class _CharT, class _Traits>
  117. inline _CharT* _STLP_CALL value_type(const istreambuf_iterator<_CharT, _Traits>&) { return (_CharT*)0; }
  118. # endif
  119. template <class _CharT, class _Traits>
  120. istreambuf_iterator<_CharT, _Traits>
  121. istreambuf_iterator<_CharT, _Traits>::operator++(int) {
  122. _M_getc(); // __tmp should avoid any future actions under
  123. // underlined buffer---during call of operator *()
  124. // (due to buffer for *this and __tmp are the same).
  125. istreambuf_iterator<_CharT, _Traits> __tmp = *this;
  126. _M_buf->sbumpc();
  127. _M_have_c = false;
  128. return __tmp;
  129. }
  130. _STLP_END_NAMESPACE
  131. #endif /* _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H */
  132. // Local Variables:
  133. // mode:C++
  134. // End: