basic_streambuf<> problems/fixes

Jack Reeves jackw_reeves@hotmail.com
Tue Jul 2 12:11:00 GMT 2002


The following will do a reasonable job of exercising certain parts of
the iostreams library.

#include <string>    // char_traits
#include <utility>   // pair
#include <sstream>
#include <fstream>

struct MyChar {
    unsigned long _c;
};

struct MyCharTraits {
    typedef MyChar		char_type;
    typedef std::pair<char_type, bool> int_type;
    typedef std::streamoff	off_type;
    typedef std::streampos	pos_type;
    typedef      mbstate_t	state_type;

    static void assign(char_type& c1, const char_type& c2);
    static bool eq(const char_type& c1, const char_type& c2);
    static bool lt(const char_type& c1, const char_type& c2);
    static int compare(const char_type* s1, const char_type* s2);
    static size_t length(const char_type* s1);
    static const char_type* find(const char_type* s, size_t n,
				 const char_type& a);
    static char_type* move(char_type* s1, const char_type* s2, size_t n);
    static char_type* copy(char_type* s1, const char_type* s2, size_t n);
    static char_type* assign(char_type* s, size_t n, char_type a);
    static int_type not_eof(const int_type& c);
    static char_type to_char_type(const int_type& c);
    static int_type to_int_type(const char_type& c);
    static bool eq_int_type(const int_type& c1, const int_type& c2);
    static int_type eof();
};

template std::basic_streambuf<MyChar, MyCharTraits>;
template std::basic_stringbuf<MyChar, MyCharTraits>;
template std::basic_stringstream<MyChar, MyCharTraits>;

template std::basic_filebuf<MyChar, MyCharTraits>;
template std::basic_istream<MyChar, MyCharTraits>;
template std::basic_ostream<MyChar, MyCharTraits>;

int main() {
    std::stringstream<MyChar, MyCharTraits> test;
    return 0;
}

Some fixes (not all) are provided below.

-------------------------
File: std/std_streambuf.h
94c94
<       int_type			_M_buf_size;
---
>       size_t			_M_buf_size;
97c97
<       int_type			_M_buf_size_opt;
---
>       size_t			_M_buf_size_opt;
129c129
<       static const int_type    	_S_pback_size = 1;
---
>       static const size_t	_S_pback_size = 1;
143,144c143,144
< 	    int_type __dist = _M_in_end - _M_in_cur;
< 	    int_type __len = min(_S_pback_size, __dist);
---
>	    size_t __dist = _M_in_end - _M_in_cur;
>	    size_t __len = min(_S_pback_size, __dist);
162c162
< 	    int_type __off_cur = _M_in_cur - _M_pback;
---
>	    size_t __off_cur = _M_in_cur - _M_pback;
165,167c165,167
< 	    int_type __off_end = 0;
< 	    int_type __pback_len = _M_in_end - _M_pback;
< 	    int_type __save_len = _M_pback_end_save - _M_buf;
---
>	    size_t __off_end = 0;
>	    size_t __pback_len = _M_in_end - _M_pback;
>	    size_t __save_len = _M_pback_end_save - _M_buf;
291,292c291,292
< 		int_type __save_len =  _M_pback_end_save - _M_pback_cur_save;
< 		int_type __pback_len = _M_in_cur - _M_pback;
---
>		size_t __save_len = _M_pback_end_save - _M_pback_cur_save;
>		size_t __pback_len = _M_in_cur - _M_pback;
307c307
< 	return (this->sbumpc() == __eof ? __eof : this->sgetc());
---
>	return (traits_type::eq_int_type(this->sbumpc(), __eof) ? __eof : 
>this->sgetc());
345,350c345,360
<       : _M_buf(NULL), _M_buf_size(0),
<       _M_buf_size_opt(static_cast<int_type>(BUFSIZ)), 
_M_buf_unified(false),
<       _M_in_beg(0), _M_in_cur(0), _M_in_end(0), _M_out_beg(0), 
_M_out_cur(0),
<       _M_out_end(0), _M_mode(ios_base::openmode(0)), 
_M_buf_locale(locale()),
<       _M_buf_locale_init(false), _M_pback_cur_save(0), 
_M_pback_end_save(0),
<       _M_pback_init(false)
---
>       : _M_buf(NULL),
>       	_M_buf_size(0),
>       	_M_buf_size_opt(BUFSIZ),
>       	_M_buf_unified(false),       	_M_in_beg(0),       	_M_in_cur(0),    
>    	_M_in_end(0),       	_M_out_beg(0),       	_M_out_cur(0),       
>	_M_out_end(0),       	_M_mode(ios_base::openmode(0)),       
>	_M_buf_locale(locale()),       	_M_buf_locale_init(false),       
>	_M_pback_cur_save(0),       	_M_pback_end_save(0),       
>	_M_pback_init(false)
441c451
< 	bool __testeof = this->underflow() == __ret;
---
>	bool __testeof = traits_type::eq_int_type(this->underflow(), __ret);

------------------------
File: bits/streambuf.tcc
43c43
<     const typename basic_streambuf<_CharT, _Traits>::int_type
---
>     const size_t
141c141
< 	      if (__c != traits_type::eof())
---
>	      if (!traits_type::eq_int_type(__c, traits_type::eof()))
180c180
< 	      if (__c != traits_type::eof())
---
>	      if (!traits_type::eq_int_type(__c, traits_type::eof()))
217c217
< 		  if (__sbin->sgetc() == _Traits::eof())
---
>		  if (_Traits::eq_int_type(__sbin->sgetc(), _Traits::eof()))

------------------------
File: bits/basic_ios.tcc
159c159
<       _M_fill = 0;
---
>       _M_fill = _CharT();

------------------------
File: bits/sstream.tcc
98c98
< 		__ret = this->sputc(__c);
---
>		__ret = this->sputc(traits_type::to_char_type(__c));
104c104
< 		  _M_buf_size = static_cast<int_type>(__len);
---
>		  _M_buf_size = __len;





_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com



More information about the Libstdc++ mailing list