-
Binary data -> istream
Assume I have a chunk of binary data in memory, I would like to turn it into an istream. How is this done? I've been thinking of using an std::stringstream first, but since the data is binary it's a bit risky I guess.
Something along these lines:
Code:
void fun(char* data, int dataSize)
{
std::istream s(data, dataSize);
stuff = s.get();
s.read(&junk, sizeof(junk));
}
-
Why do you need to convert the bytes into a stream? It's far simpler just to work with them directly, or write your own wrapper to make sure the buffer is worked with safely. If you really do need a stream, my first attempt would be to derive from basic_streambuf and then pass a pointer to an object of that derived class to istream's constructor.
At the moment that's the only safe and portable way I can think of.
-
Well, the iostreams were never really intended for binary I/O. (On the other hand, we're not given an alternative...)
Anyway, it's true that it's a little risky to use stringstreams. I don't know very much about this. I know that the C++ standard dictates that the strings works correctly even with embedded NUL characters. However, I do not know the requirements on char_traits when faced with weird characters. Neither do I know the requirements on basic_stringbuf.
I agree with Sake. Writing your own streambuffer is probably the best choice. And make sure you post your code - something like that could be very useful. I know of at least one other person who requires what you do.
-
The reason I need this is I have a generic loader function that uses an istream, then depending on where I want to load from (a file on the HD, a file located in memory) I pass different streams to it.
I thought of writing something of my own too, but thought if there already exists a way it would be better...
-
Are the STL header files intentionally made totally unreadable, or are the creators just bad coders???
-
>>Are the STL header files intentionally made totally unreadable, or are the creators just bad coders???
Templates were designed poorly. The result is a nearly impossible mass of dependencies to unravel.
-
Perhaps you could try a basic_istringstream<unsigned char>? According to the docs provided with MSVC Beta 2005, istringstream is just a typedef for basic_istringstream<char>. On the other hand, I'm not sure how char_traits<unsigned char> works..
Code:
template <
classElem,
classTr= char_traits<Elem>,
classAlloc= allocator<Elem>
>
class basic_istringstream : public basic_istream<Elem,Tr>
It'd be nice if you can find a way to fiddle around with that so that it behaves as you want. Unfortunately, I have no idea how any of the char_traits or allocator stuff works, so I can't be of any help there.
**EDIT**
[useless thought of the day]
Or you could write the data to a file and open it with a binary ifstream :p
[/useless thought of the day]
-
>>On the other hand, I'm not sure how char_traits<unsigned char> works..
It doesn't. You would have to write it first because char_traits isn't specialized for unsigned char[*].
>>Or you could write the data to a file and open it with a binary ifstream
This is an option, though it would slow things down a bit because of the device I/O.
[*] Unless the default char is unsigned for the implementation, but that's a dangerous assumption for this particular problem.
-
I think MS's STL headers are intentionally unreadable.