IFileStreambuf Class Reference

This class implements a C++ streambuf object for physfs files. More...

#include <ifile_streambuf.hpp>

Inherits streambuf.

List of all members.

Public Member Functions

 IFileStreambuf (const std::string &filename)
 ~IFileStreambuf ()

Protected Member Functions

virtual int underflow ()
virtual pos_type seekoff (off_type pos, std::ios_base::seekdir, std::ios_base::openmode)
virtual pos_type seekpos (pos_type pos, std::ios_base::openmode)

Private Member Functions

 IFileStreambuf (const IFileStreambuf &)
IFileStreambufoperator= (const IFileStreambuf &)

Private Attributes

PHYSFS_file * file
char buf [1024]


Detailed Description

This class implements a C++ streambuf object for physfs files.

So that you can use normal istream operations on them

Definition at line 26 of file ifile_streambuf.hpp.


Constructor & Destructor Documentation

IFileStreambuf::IFileStreambuf ( const std::string &  filename  ) 

Definition at line 24 of file ifile_streambuf.cpp.

References file.

00024                                                         :
00025   file()
00026 {
00027   // check this as PHYSFS seems to be buggy and still returns a
00028   // valid pointer in this case
00029   if(filename == "") {
00030     throw std::runtime_error("Couldn't open file: empty filename");
00031   }
00032   file = PHYSFS_openRead(filename.c_str());
00033   if(file == 0) {
00034     std::stringstream msg;
00035     msg << "Couldn't open file '" << filename << "': "
00036         << PHYSFS_getLastError();
00037     throw std::runtime_error(msg.str());
00038   }
00039 }

IFileStreambuf::~IFileStreambuf (  ) 

Definition at line 41 of file ifile_streambuf.cpp.

References file.

00042 {
00043   PHYSFS_close(file);
00044 }

IFileStreambuf::IFileStreambuf ( const IFileStreambuf  )  [private]


Member Function Documentation

int IFileStreambuf::underflow (  )  [protected, virtual]

Definition at line 47 of file ifile_streambuf.cpp.

References buf, and file.

00048 {
00049   if(PHYSFS_eof(file)) {
00050     return traits_type::eof();
00051   }
00052 
00053   PHYSFS_sint64 bytesread = PHYSFS_read(file, buf, 1, sizeof(buf));
00054   if(bytesread <= 0) {
00055     return traits_type::eof();
00056   }
00057   setg(buf, buf, buf + bytesread);
00058 
00059   return buf[0];
00060 }

IFileStreambuf::pos_type IFileStreambuf::seekoff ( off_type  pos,
std::ios_base::seekdir  ,
std::ios_base::openmode   
) [protected, virtual]

Definition at line 75 of file ifile_streambuf.cpp.

References file, and seekpos().

00077 {
00078   off_type pos = off;
00079   PHYSFS_sint64 ptell = PHYSFS_tell(file);
00080 
00081   switch(dir) {
00082     case std::ios_base::beg:
00083       break;
00084     case std::ios_base::cur:
00085       if(off == 0)
00086         return static_cast<pos_type> (ptell) - static_cast<pos_type> (egptr() - gptr());
00087       pos += static_cast<off_type> (ptell) - static_cast<off_type> (egptr() - gptr());
00088       break;
00089     case std::ios_base::end:
00090       pos += static_cast<off_type> (PHYSFS_fileLength(file));
00091       break;
00092     default:
00093       assert(false);
00094       return pos_type(off_type(-1));
00095   }
00096 
00097   return seekpos(static_cast<pos_type> (pos), mode);
00098 }

IFileStreambuf::pos_type IFileStreambuf::seekpos ( pos_type  pos,
std::ios_base::openmode   
) [protected, virtual]

Definition at line 63 of file ifile_streambuf.cpp.

References buf, and file.

Referenced by seekoff().

00064 {
00065   if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (pos)) == 0) {
00066     return pos_type(off_type(-1));
00067   }
00068 
00069   // the seek invalidated the buffer
00070   setg(buf, buf, buf);
00071   return pos;
00072 }

IFileStreambuf& IFileStreambuf::operator= ( const IFileStreambuf  )  [private]


Member Data Documentation

PHYSFS_file* IFileStreambuf::file [private]

Definition at line 39 of file ifile_streambuf.hpp.

Referenced by IFileStreambuf(), seekoff(), seekpos(), underflow(), and ~IFileStreambuf().

char IFileStreambuf::buf[1024] [private]

Definition at line 40 of file ifile_streambuf.hpp.

Referenced by seekpos(), and underflow().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:31 2014 for SuperTux by  doxygen 1.5.1