Added new interface SeekableReadStream

svn-id: r16493
This commit is contained in:
Max Horn 2005-01-09 01:41:43 +00:00
parent 9c7a604c8b
commit bc44b5ec15
6 changed files with 47 additions and 21 deletions

View file

@ -201,7 +201,7 @@ void File::clearIOFailed() {
_ioFailed = false; _ioFailed = false;
} }
bool File::eof() { bool File::eof() const {
if (_handle == NULL) { if (_handle == NULL) {
error("File::eof: File is not open!"); error("File::eof: File is not open!");
return false; return false;
@ -210,7 +210,7 @@ bool File::eof() {
return feof(_handle) != 0; return feof(_handle) != 0;
} }
uint32 File::pos() { uint32 File::pos() const {
if (_handle == NULL) { if (_handle == NULL) {
error("File::pos: File is not open!"); error("File::pos: File is not open!");
return 0; return 0;
@ -219,7 +219,7 @@ uint32 File::pos() {
return ftell(_handle); return ftell(_handle);
} }
uint32 File::size() { uint32 File::size() const {
if (_handle == NULL) { if (_handle == NULL) {
error("File::size: File is not open!"); error("File::size: File is not open!");
return 0; return 0;

View file

@ -27,7 +27,7 @@
#include "common/str.h" #include "common/str.h"
#include "common/stream.h" #include "common/stream.h"
class File : public Common::ReadStream, public Common::WriteStream { class File : public Common::SeekableReadStream, public Common::WriteStream {
protected: protected:
/** POSIX file handle to the actual file; 0 if no file is open. */ /** POSIX file handle to the actual file; 0 if no file is open. */
FILE *_handle; FILE *_handle;
@ -66,11 +66,11 @@ public:
bool isOpen() const; bool isOpen() const;
bool ioFailed() const; bool ioFailed() const;
void clearIOFailed(); void clearIOFailed();
virtual bool eof(); bool eof() const;
virtual uint32 pos(); uint32 pos() const;
virtual uint32 size(); uint32 size() const;
const char *name() const { return _name.c_str(); } const char *name() const { return _name.c_str(); }
virtual void seek(int32 offs, int whence = SEEK_SET); void seek(int32 offs, int whence = SEEK_SET);
uint32 read(void *ptr, uint32 size); uint32 read(void *ptr, uint32 size);
uint32 write(const void *ptr, uint32 size); uint32 write(const void *ptr, uint32 size);
char *gets(void *ptr, uint32 size); char *gets(void *ptr, uint32 size);

View file

@ -25,9 +25,9 @@
namespace Common { namespace Common {
void MemoryReadStream::seek(uint32 offs, int whence) { void MemoryReadStream::seek(int32 offs, int whence) {
// Pre-Condition // Pre-Condition
assert(_pos <= _bufSize); assert(0 <= _pos && _pos <= _bufSize);
switch (whence) { switch (whence) {
case SEEK_END: case SEEK_END:
// SEEK_END works just like SEEK_SET, only 'reversed', // SEEK_END works just like SEEK_SET, only 'reversed',
@ -45,7 +45,7 @@ void MemoryReadStream::seek(uint32 offs, int whence) {
break; break;
} }
// Post-Condition // Post-Condition
assert(_pos <= _bufSize); assert(0 <= _pos && _pos <= _bufSize);
} }

View file

@ -166,6 +166,24 @@ public:
} }
}; };
/**
* Interface for a seekable & readable data stream.
*
* @todo We really need better error handling here!
* Like seek should somehow indicate whether it failed.
*/
class SeekableReadStream : public ReadStream {
public:
virtual bool eof() const = 0;
virtual uint32 pos() const = 0;
virtual uint32 size() const = 0;
virtual void seek(int32 offs, int whence = SEEK_SET) = 0;
};
/** /**
* XORReadStream is a wrapper around an arbitrary other ReadStream, * XORReadStream is a wrapper around an arbitrary other ReadStream,
* which 'decrypts' the data being read by XORing all data bytes with the given * which 'decrypts' the data being read by XORing all data bytes with the given
@ -200,7 +218,7 @@ public:
* Simple memory based 'stream', which implements the ReadStream interface for * Simple memory based 'stream', which implements the ReadStream interface for
* a plain memory block. * a plain memory block.
*/ */
class MemoryReadStream : public ReadStream { class MemoryReadStream : public SeekableReadStream {
private: private:
const byte *_ptr; const byte *_ptr;
const byte * const _ptrOrig; const byte * const _ptrOrig;
@ -223,7 +241,7 @@ public:
uint32 pos() const { return _pos; } uint32 pos() const { return _pos; }
uint32 size() const { return _bufSize; } uint32 size() const { return _bufSize; }
void seek(uint32 offs, int whence = SEEK_SET); void seek(int32 offs, int whence = SEEK_SET);
}; };
} // End of namespace Common } // End of namespace Common

View file

@ -35,6 +35,10 @@ DECLARE_SINGLETON(OSystem);
template <> template <>
OSystem *Common::Singleton<OSystem>::makeInstance() { OSystem *Common::Singleton<OSystem>::makeInstance() {
return OSystem::createSystem();
}
OSystem *OSystem::createSystem() {
// Attention: Do not call parseGraphicsMode() here, nor any other function // Attention: Do not call parseGraphicsMode() here, nor any other function
// which needs to access the OSystem instance, else you get stuck in an // which needs to access the OSystem instance, else you get stuck in an
// endless loop. // endless loop.

View file

@ -29,14 +29,6 @@
#include "common/savefile.h" #include "common/savefile.h"
#include "common/singleton.h" #include "common/singleton.h"
class OSystem;
/**
* Custom object factory for OSystem.
*/
template <>
OSystem *Common::Singleton<OSystem>::makeInstance();
/** /**
* Interface for ScummVM backends. If you want to port ScummVM to a system * Interface for ScummVM backends. If you want to port ScummVM to a system
@ -49,6 +41,10 @@ OSystem *Common::Singleton<OSystem>::makeInstance();
* control audio CD playback, and sound output. * control audio CD playback, and sound output.
*/ */
class OSystem : public Common::Singleton<OSystem> { class OSystem : public Common::Singleton<OSystem> {
protected:
static OSystem *createSystem();
friend class Common::Singleton<SingletonBaseType>;
public: public:
/** @name Feature flags */ /** @name Feature flags */
@ -682,6 +678,14 @@ public:
//@} //@}
}; };
/**
* Custom object factory for OSystem.
*/
template <>
OSystem *Common::Singleton<OSystem>::makeInstance();
/** The global OSystem instance. Inited in main(). */ /** The global OSystem instance. Inited in main(). */
#define g_system (&OSystem::instance()) #define g_system (&OSystem::instance())