-
July 11th, 2011, 04:37 PM
#1
C++ Streams
I want to write a class to handle my file archive format and want it to easily work with c++ streams. Problem is I have little experience inheriting/composing with any of the ios classes. I want the archive object to be able to be bound to both files and also to a memory buffer (eg. having an archive embedded in the executable and constructing an archive object from a pointer to this memory).
What would be the best way to approach this?
-
July 11th, 2011, 05:37 PM
#2
Re: C++ Streams
Unfortunately your question is not clear to me. At first I thought you were talking about "integration" with C++ streams... so I wondered what else besides overloads of operator<< and operator>> would you need.
But then I got the impression that it could be about the io itself. Is this correct? Anyway, I think I can only help you by pointing to the best documentation of the iostreams library I'm aware of (in the case you don't know yet): IOStreams, which is actually part of this book.
-
July 11th, 2011, 06:19 PM
#3
Re: C++ Streams
Well, what I want is to make the access of files contained in an archive indistinguishable from the access of normal files. Something like:
Code:
Archive MyArchive("somearchive.arc");
ifstream ifs = MyArchive.open("/dir1/somefile.txt");
Now ifs acts like a normal ifstream, even though somefile.txt is contained in the archive and may be compressed and/or encrypted.
-
July 12th, 2011, 08:56 AM
#4
Re: C++ Streams
Googling 'c++ zipstream' may help.
-
July 12th, 2011, 09:03 AM
#5
Re: C++ Streams
Originally Posted by Chris_F
Well, what I want is to make the access of files contained in an archive indistinguishable from the access of normal files. Something like:
Code:
Archive MyArchive("somearchive.arc");
ifstream ifs = MyArchive.open("/dir1/somefile.txt");
Now ifs acts like a normal ifstream, even though somefile.txt is contained in the archive and may be compressed and/or encrypted.
This example might be hard to achieve, because streams aren't copy constructable...
That said, given the design, why not just return a pointer to an archive-owned stringstream, typed as a base std::iostream?
Something like this:
Code:
class Archive
{
std::iostream* open(const std::string& filename, std::ios::openmode mode)
{
std::string raw_data;
unpack(filename, raw_data);
std::iostream p = new std::stringstream(raw_data, mode);
//Manage p's lifecycle somewhere inside the current object
return p;
}
};
int main()
{
Archive MyArchive(Archive.zip);
std::iostream* pStream = MyArchive.open(/MyFile.txt);
//Use pStream like an abstract stream
}
Of course, this is missing 95% of the code, but you should get the point.
If the goal was to not unpack the archive though, then things will get harder, as you'll necessarily have to write your own stream class (stream_buffer actually), but that's a whole different ballgame.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|