Reputation:
I am defining a custom std::streambuf
class for use in file I/O. In this class, I am overloading the functions open
, close
, is_open
, xsgetn
, xsputn
, overflow
, underflow
, and uflow
. I then insert this buffer into an std::ifstream
as follows (the same scenario holds for std::ofstream
):
std::ifstream temp;
Filebuffer *buffer = new Filebuffer();
buffer->open(fileName.c_str(), std::ios_base::in | std::ios_base::binary);
temp.basic_ios<char>::rdbuf(buffer);
The implementation for Filebuffer
(my custom class) is as follows:
std::streamsize
Filebuffer::xsputn(const char *s, std::streamsize n)
{
std::streamsize result = 0;
if(file && n)
{
for(;;)
{
size_t buffer_use = this->epptr() - this->pptr();
if(buffer_use > 0)
{
if(static_cast<int>(buffer_use) > n)
{
buffer_use = n;
}
std::char_traits<char>::copy(this->pptr(), s, buffer_use);
this->pbump(buffer_use);
result += buffer_use;
n -= buffer_use;
if(n == 0)
{
break;
}
s += buffer_use;
}
this->overflow(static_cast<int>(*s));
}
}
return result;
}
std::streamsize
Filebuffer::xsgetn(char *s, std::streamsize n)
{
std::streamsize result = 0;
if(file && n)
{
int ch; do
{
size_t buffer_use = this->egptr() - this->gptr();
if(buffer_use > 0)
{
if(static_cast<int>(buffer_use) > n)
{
buffer_use = n;
}
std::char_traits<char>::copy(s, this->gptr(), buffer_use);
this->gbump(buffer_use);
result += buffer_use;
n -= buffer_use;
if(n == 0)
{
break;
}
s += buffer_use;
}
ch = this->underflow();
} while(!(ch == std::char_traits<char>::eof()));
}
return result;
}
int
Filebuffer::underflow()
{
if(file)
{
nullify_put_area(); //clears write buffer
if(!buffer) //allocated in open()
{
int value = ungetc(fgetc(file), file);
if(value == EOF)
{
return std::char_traits<char>::eof();
}
return value;
}
char *begin = buffer;
char *end = buffer + bufferSize - 1; //bufferSize set to 4 KB
char *next = end;
if(this->gptr() < this->egptr())
{
size_t buffer_use = this->egptr() - this->gptr();
memmove(begin, next, buffer_use);
begin += buffer_use;
}
setg(begin, begin, begin);
size_t m = (bufferSize - 1 - (begin - this->eback()));
if(m > 0)
{
size_t status = fread(begin, 1, m, file);
if(status == 0)
{
return std::char_traits<char>::eof();
}
setg(this->eback(), this->gptr(), begin + status);
}
return static_cast<int>(*this->gptr());
}
return std::char_traits<char>::eof();
}
int
Filebuffer::uflow()
{
if(!file)
{
return std::char_traits<char>::eof();
}
int ch = underflow();
if(ch != std::char_traits<char>::eof())
{
if(buffer)
{
this->gbump(1);
}
else
{
fgetc(file);
}
}
return ch;
}
int
Filebuffer::overflow(int c)
{
if(!file)
{
return std::char_traits<char>::eof();
}
const char *begin = this->pbase();
char *next = this->pptr();
if(buffer)
{
setp(buffer, buffer + bufferSize - 1);
}
nullify_get_area(); //clears read buffer
char temp;
if(c == std::char_traits<char>::eof())
{
c = std::char_traits<char>::not_eof(std::char_traits<char>::eof());
}
else
{
if(!next)
{
begin = next = &temp;
}
assert(next == &temp || buffer <= next);
assert(next == &temp || next < buffer + bufferSize);
*next++ = static_cast<char>(c);
}
if(begin != next)
{
if(begin == &temp)
{
fputc(temp, file);
return c;
}
size_t n = next - begin;
size_t status = fwrite(begin, 1, n, file);
if(status != n)
{
return std::char_traits<char>::eof();
}
return c;
}
return std::char_traits<char>::eof();
}
Unfortunately, whenever I use (e.g.) temp >> readVar;
, it does not use any of my overloaded functions - the call stack shows that the STL original implementations were called - and instead, simply reads in garbage. Per this question, I need to define overflow
for ofstream
(and presumably, underflow
for ifstream
), which I have done. Am I defining xsputn
and xsgetn
correctly? Why are my overloaded functions not being called?
Upvotes: 1
Views: 2366
Reputation:
It appears that trying to do this with std::ifstream
will not work, and I need to change it std::istream
. Once I make this change, the overloaded functions are called and everything works properly.
Upvotes: 1