C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how do I properly use a filebuf

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jacob Oursland
Guest





PostPosted: Thu Apr 14, 2005 6:59 am    Post subject: how do I properly use a filebuf Reply with quote



Hi,

I am working on playing with an old game's data files (Descent II) and I
want to build a framework for a generic file type so that I request a
file and it opens one in the local directory, if not found open the game
data archive and find the file and return the stream to that one.

The problem I am having is that I want to build a stream class for this
archive file format. Its real simple, file name, size, data, repeatedly
until EOF. My plan of attack is to create a custom filebuf and seek
to file start position, when data is requested from the buffer it is fed
and underflow() is used to keep the buffer full and check that it hasn't
exceeded the archived file's EOF.

The precise problem I have is how to actually access the file. I have
the buffer part pretty much handled (not much, since it just passes the
data) but I haven't a clue how to open, read, and write (eventually) the
file.

The filebuf::open() opens and filebuf::close() closes nicely, but how do
I then implement underflow() to read() or overflow() to write?

Thanks
-Jake

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Sat Apr 16, 2005 12:03 am    Post subject: Re: how do I properly use a filebuf Reply with quote



Jacob Oursland wrote:

Quote:
I am working on playing with an old game's data files (Descent
II) and I want to build a framework for a generic file type so
that I request a file and it opens one in the local directory,
if not found open the game data archive and find the file and
return the stream to that one.

The problem I am having is that I want to build a stream class
for this archive file format. Its real simple, file name,
size, data, repeatedly until EOF. My plan of attack is to
create a custom filebuf and seek to file start position, when
data is requested from the buffer it is fed and underflow() is
used to keep the buffer full and check that it hasn't exceeded
the archived file's EOF.

The precise problem I have is how to actually access the file.
I have the buffer part pretty much handled (not much, since it
just passes the data) but I haven't a clue how to open, read,
and write (eventually) the file.

The filebuf::open() opens and filebuf::close() closes nicely,
but how do I then implement underflow() to read() or
overflow() to write?

I'm not sure I understand. What does your streambuf do that
filebuf doesn't? From the description, the only thing
particular about your application is the format of the files;
streambuf's aren't normally concerned with format. (I suppose
that if your file uses a binary format, a filtering streambuf
could be used to convert it to text, but even then, I'm not sure
if this is the right approach.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jacob Oursland
Guest





PostPosted: Sat Apr 16, 2005 5:46 pm    Post subject: Re: how do I properly use a filebuf Reply with quote



[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
Quote:
I'm not sure I understand. What does your streambuf do that
filebuf doesn't? From the description, the only thing
particular about your application is the format of the files;
streambuf's aren't normally concerned with format. (I suppose
that if your file uses a binary format, a filtering streambuf
could be used to convert it to text, but even then, I'm not sure
if this is the right approach.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sorry for being a bit unclear. My issue is I want to write a custom
filebuf underflow() and overflow() and I don't know how to retrieve data
from the file opened by the filebuf's open(). How do I read() and
write() to the filebuf's file?


Thanks
-Jake

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Sun Apr 17, 2005 6:07 pm    Post subject: Re: how do I properly use a filebuf Reply with quote

Jacob Oursland <oursland.nospam (AT) deleteme (DOT) fastmail.fm> wrote:

Quote:
Hi,

I am working on playing with an old game's data files (Descent II) and I
want to build a framework for a generic file type so that I request a
file and it opens one in the local directory, if not found open the game
data archive and find the file and return the stream to that one.

The problem I am having is that I want to build a stream class for this
archive file format. Its real simple, file name, size, data, repeatedly
until EOF. My plan of attack is to create a custom filebuf and seek
to file start position, when data is requested from the buffer it is fed
and underflow() is used to keep the buffer full and check that it hasn't
exceeded the archived file's EOF.

The precise problem I have is how to actually access the file. I have
the buffer part pretty much handled (not much, since it just passes the
data) but I haven't a clue how to open, read, and write (eventually) the
file.

The filebuf::open() opens and filebuf::close() closes nicely, but how do
I then implement underflow() to read() or overflow() to write?

Thanks
-Jake

Do you need a custom streambuf I don' think so your archived file

is an array of easy to handle blocks if you can hold the each file in
the archive in memory then I would use a struct/class to represent the
file using std::string,std::vector<char> for the name and the data,
then writing it becomse just using filebuf and its member sputn to write
the file in one call

struct archived_block
{
std::string name;
std::size_t len;
std::vecctor<unsigned char> data;
bool get(std::istream &in);// fill the struct from in
};

bool decode_file(std::istream &in)
{
archived_block block;
while(block.get(in))
{
std::filebuf
buf(in.name.c_str(),std::ios_base::binary|std::ios_base::in);
if(!buf.is_open())
{
// report error to cerr
return false;
}
std::streamsize bytes_wrote =buf.sputn(&block.data[0],block.len);
if(bytes_wrote != block.len)
{
// report error to cerr
return false;
}
}
return in.eof();
}

I leave archived_block::get to the reader as it depends on details
missing.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Sun Apr 17, 2005 9:04 pm    Post subject: Re: how do I properly use a filebuf Reply with quote

Jacob Oursland wrote:
Quote:
kanze (AT) gabi-soft (DOT) fr wrote:

I'm not sure I understand. What does your streambuf do that
filebuf doesn't? From the description, the only thing
particular about your application is the format of the files;
streambuf's aren't normally concerned with format. (I suppose
that if your file uses a binary format, a filtering streambuf
could be used to convert it to text, but even then, I'm not
sure if this is the right approach.)

Sorry for being a bit unclear. My issue is I want to write a
custom filebuf underflow() and overflow() and I don't know how
to retrieve data from the file opened by the filebuf's open().
How do I read() and write() to the filebuf's file?

The question remains : what are you trying to do? Filebuf is a
concrete class, and you shouldn't normally derive from it. If
you want a custom streambuf, you derive from streambuf, not from
filebuf.

If the goal is to massage the data read by the filebuf, the
obvious solution is a filtering streambuf, a streambuf which
uses a filebuf as its data source. But from what little you have
said, I'm far from sure that this is the case.

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.