Okay, figures, I figure it out a few minutes after desperately asking for help. I'm still interested to know how to work with streams better and figure out their length instead of failing like original post. Also probably more correct way of doing this, since it's so very ugly, but works so I don't give a damn if that's the best I can get :P
Code:
std::vector<uint8_t> all;
while(data.sgetc() != EOF)
{
all.push_back((uint8_t)data.sbumpc());
}
for(size_t i = 0, l = all.size(); i < l; ++i)
{
sprintf(ch, "\\x%02x", (uint8_t)all[i]);
output += ch;
data.sputc((uint8_t)all[i]);
}
------ original post:
This thing eludes me. I write to it, and have no problem reading it certain ways, but not others. What's most annoying is in_avail isn't working, and wrapped in istream tellg returns -1.
asio::streambuf data; // just a wrapper for std::streambuf I belive
I want to know the length so I can transverse the buffer without removing items, so I can print it in the correct format. Trying to print the stream as hex, eg. \x4C, and for some reason this doesn't work:
Code:
std::cout.setf(std::ios::hex | std::ios::showbase);
According to the example and Google it's suppose to work, but it just shows sqaure boxes representing the bits to me. This works but only the first character:
Code:
std::cout << std::ios::hex << std::ios::showbase << &data << std::endl;
This works because it doesn't rely on length..
Code:
std::vector<uint8_t> all;
while(data.sgetc() != EOF)
{
all.push_back((uint8_t)data.sbumpc());
}
This is something I figured would work if I could ever get the correct length..
Code:
std::vector<uint8_t> all;
data.pubseekoff(0, std::ios_base::begin);
std::streamsize l = self->data.in_avail(); // 0
for(size_t i = 0; i < l; ++i)
{
data.pubseekoff(i, std::ios_base::beg);
all.push_back((uint8_t)data.sgetc());
}
Of course these work (off top of my head)..
Code:
std::istream data_stream(&data);
std::string stuff((std::istreambuf_iterator<char>(data_stream)), std::istreambuf_iterator<char>());
Code:
std::istream data_stream(&data);
std::string stuff;
while(data_stream >> stuff) {}
But this doesn't..
Code:
std::istream data_stream(&data);
data_stream.seekg(0, std::ios::end);
std::streamsize l = data_stream.tellg(); // -1
data_stream.seekg(0, std::ios::beg);
char buffer[l];
data_stream.read(buffer, l);
std::string all(buffer);
data_stream.write(buffer, l);
I could write a hundred different attempts and not get this...