M
mathieu
Dear all,
I am trying -again- to understand how to properly implement a
subclass of streambuf. In the following example I am trying to
reproduce a case where sync() should refuse to write any more
characters. For the purpose of the exercise I used a fixed size buffer
to quickly get a segfault. Could anyone of you comment on the code and
let me know how they would handle case where sync() should not write
anymore (obviously you do not have access to the size of the buffer as
it just an example).
Thanks !
#include <iostream>
#include <string>
#include <cassert>
#include <cstring>
class windowbuf : public std::streambuf {
char *buffer;
public:
windowbuf(char *b, size_t len):buffer(b) { setp(b, b+len); }
int_type sync ();
int_type write( const char *buffer, size_t len );
int_type overflow (int ch);
};
windowbuf::int_type windowbuf::write( const char *buf, size_t len )
{
memcpy(buffer, buf, len );
buffer += len;
return len;
}
windowbuf::int_type windowbuf::sync ()
{
if (pptr () && pbase () < pptr () && pptr () <= epptr ())
{
int n = write( pbase(), pptr() - pbase() );
setp (pbase (), pbase() + n);
}
return 0;
}
int windowbuf:
verflow (int ch)
{
if( pbase() == 0 ) return traits_type::eof();
if( ch == traits_type::eof() )
return sync();
if( pptr() == epptr() )
sync();
*pptr() = (char_type)ch;
pbump(1);
return ch;
}
int main (int argc, char*argv[])
{
static const int n = 5;
char fixedbuffer[n];
windowbuf wbuf( fixedbuffer, n);
std:
stream wstr(&wbuf);
wstr << "12" << std::flush;
wstr << "Hello" << std::flush;
wstr << "world!" << std::flush;
wstr << "Very long string" << std::flush;
std::string s( fixedbuffer, fixedbuffer + n );
std::cout << s << std::endl;
return 0;
}
I am trying -again- to understand how to properly implement a
subclass of streambuf. In the following example I am trying to
reproduce a case where sync() should refuse to write any more
characters. For the purpose of the exercise I used a fixed size buffer
to quickly get a segfault. Could anyone of you comment on the code and
let me know how they would handle case where sync() should not write
anymore (obviously you do not have access to the size of the buffer as
it just an example).
Thanks !
#include <iostream>
#include <string>
#include <cassert>
#include <cstring>
class windowbuf : public std::streambuf {
char *buffer;
public:
windowbuf(char *b, size_t len):buffer(b) { setp(b, b+len); }
int_type sync ();
int_type write( const char *buffer, size_t len );
int_type overflow (int ch);
};
windowbuf::int_type windowbuf::write( const char *buf, size_t len )
{
memcpy(buffer, buf, len );
buffer += len;
return len;
}
windowbuf::int_type windowbuf::sync ()
{
if (pptr () && pbase () < pptr () && pptr () <= epptr ())
{
int n = write( pbase(), pptr() - pbase() );
setp (pbase (), pbase() + n);
}
return 0;
}
int windowbuf:
{
if( pbase() == 0 ) return traits_type::eof();
if( ch == traits_type::eof() )
return sync();
if( pptr() == epptr() )
sync();
*pptr() = (char_type)ch;
pbump(1);
return ch;
}
int main (int argc, char*argv[])
{
static const int n = 5;
char fixedbuffer[n];
windowbuf wbuf( fixedbuffer, n);
std:
wstr << "12" << std::flush;
wstr << "Hello" << std::flush;
wstr << "world!" << std::flush;
wstr << "Very long string" << std::flush;
std::string s( fixedbuffer, fixedbuffer + n );
std::cout << s << std::endl;
return 0;
}