Basically, it boils down to whether or not I can initialize a std::basic_fstream
with a custom buffer. I wanted to use protected inheritance for the std::basic_streambuf
so I had access to the get area pointers. Then simply pass my new object to the basic_fstream
so it is used instead of the default buffers.
Use this:
#include <streambuf>
class strbuffer : protected std::streambuf {
public:
strbuffer();
char_type* operator+(unsigned int slots);
char_type* cur;
char_type* beg;
char_type* eob;
};
strbuffer::strbuffer() : std::streambuf() {
cur = strbuffer::gptr();
beg = strbuffer::eback();
eob = strbuffer::egptr();
}
// returns val of cur
std::basic_streambuf<char, std::char_traits<char>>::char_type* strbuffer::operator+(unsigned int slots) {
int_type t;
for (int i = 0; i < slots; i++)
t = this->sbumpc();
return new char_type(t);
}
With something like:
class fstream0 : public std::basic_fstream<char, std::char_traits<char>> {
public:
fstream0();
void fInit();
void format();
bool connectFile(std::string fname);
bool connectFile(std::string path, std::string fname);
void closeFile();
std::ios_base::iostate get8(std::string& out); //32 bit size estimation?
}
Where the constructor of fstream0
would take an argument that is a stream_buf
(casting?) object:
fstream0::fstream0(arg) : std::basic_fstream<char, std::char_traits<char>>() {
// create bare bones file stream
// buffer is provided, constructor overloaded with support for many file types
// char*, std::array, etc...
strBuf = fstream0::rdbuf();
fBuf = fstream0::rdbuf();
}
Do I have to use pubsetbuf
to write over already initialized buffers in order to use many different data types for my custom buffer? Including one that inherits from std::streambuf
?