diff options
author | Max Horn | 2009-11-23 23:17:50 +0000 |
---|---|---|
committer | Max Horn | 2009-11-23 23:17:50 +0000 |
commit | fc84e258b29dc441f63dd53abd4cd494e03e4463 (patch) | |
tree | e790ac09ad7a77c8df7611063f6281bec5a639e8 /backends/platform | |
parent | 9b8c088af5c56366f176fee334f636f40b1b156d (diff) | |
download | scummvm-rg350-fc84e258b29dc441f63dd53abd4cd494e03e4463.tar.gz scummvm-rg350-fc84e258b29dc441f63dd53abd4cd494e03e4463.tar.bz2 scummvm-rg350-fc84e258b29dc441f63dd53abd4cd494e03e4463.zip |
PS2: Add new PS2FileStream class for std I/O.
* Add new class PS2FileStream as substitute for class StdioStream on PS2
* Remove PS2 specific hacks from stdiostream.cpp / class StdioStream
* Remove various ps2_f*() wrapper funcs, merging them into PS2FileStream
TODO: Merge class Ps2File into PS2FileStream
svn-id: r46111
Diffstat (limited to 'backends/platform')
-rw-r--r-- | backends/platform/ps2/fileio.cpp | 86 | ||||
-rw-r--r-- | backends/platform/ps2/fileio.h | 62 | ||||
-rw-r--r-- | backends/platform/ps2/systemps2.cpp | 3 |
3 files changed, 95 insertions, 56 deletions
diff --git a/backends/platform/ps2/fileio.cpp b/backends/platform/ps2/fileio.cpp index 047992b13d..1f2198eeff 100644 --- a/backends/platform/ps2/fileio.cpp +++ b/backends/platform/ps2/fileio.cpp @@ -43,7 +43,7 @@ AsyncFio fio; -Ps2File::Ps2File(void) { +Ps2File::Ps2File() { _fd = -1; _fileSize = 0; _filePos = 0; @@ -70,7 +70,7 @@ Ps2File::Ps2File(void) { #endif } -Ps2File::~Ps2File(void) { +Ps2File::~Ps2File() { uint32 w; if (_fd >= 0) { @@ -177,7 +177,7 @@ bool Ps2File::open(const char *name, int mode) { #endif } -int32 Ps2File::tell(void) { +int32 Ps2File::tell() { #ifdef __PS2_FILE_SEMA__ WaitSema(_sema); #endif @@ -188,7 +188,7 @@ int32 Ps2File::tell(void) { return res; } -int32 Ps2File::size(void) { +int32 Ps2File::size() { #ifdef __PS2_FILE_SEMA__ WaitSema(_sema); #endif @@ -199,7 +199,7 @@ int32 Ps2File::size(void) { return res; } -bool Ps2File::eof(void) { +bool Ps2File::eof() { #ifdef __PS2_FILE_SEMA__ WaitSema(_sema); #endif @@ -213,7 +213,7 @@ bool Ps2File::eof(void) { return res; } -bool Ps2File::getErr(void) { +bool Ps2File::getErr() { return _err; } @@ -265,7 +265,7 @@ int Ps2File::seek(int32 offset, int origin) { return res; } -void Ps2File::cacheReadAhead(void) { +void Ps2File::cacheReadAhead() { if (_cacheOpRunning) { // there's already some cache read running if (fio.poll(_fd)) // did it finish? @@ -321,7 +321,7 @@ void Ps2File::cacheReadAhead(void) { } } -void Ps2File::cacheReadSync(void) { +void Ps2File::cacheReadSync() { if (_cacheOpRunning) { int res = fio.sync(_fd); assert(res >= 0); @@ -429,6 +429,19 @@ uint32 Ps2File::write(const void *src, uint32 len) { return len; } + +PS2FileStream *PS2FileStream::makeFromPath(const Common::String &path, bool writeMode) { + FILE *handle = ps2_fopen(path.c_str(), writeMode ? "wb" : "rb"); + + if (handle) + return new PS2FileStream(handle); + return 0; +} + +PS2FileStream::PS2FileStream(FILE *handle) : _handle(handle) { + assert(handle); +} + FILE *ps2_fopen(const char *fname, const char *mode) { Ps2File *file = new Ps2File(); int _mode = O_RDONLY; @@ -448,6 +461,10 @@ FILE *ps2_fopen(const char *fname, const char *mode) { return NULL; } +PS2FileStream::~PS2FileStream() { + ps2_fclose(_handle); +} + int ps2_fclose(FILE *stream) { Ps2File *file = (Ps2File*)stream; @@ -456,16 +473,20 @@ int ps2_fclose(FILE *stream) { return 0; } -int ps2_fseek(FILE *stream, long offset, int origin) { - return ((Ps2File*)stream)->seek(offset, origin); +bool PS2FileStream::seek(int32 offs, int whence) { + return ((Ps2File*)_handle)->seek(offs, whence) == 0; +} + +int32 PS2FileStream::pos() const { + return ((Ps2File*)_handle)->tell(); } -uint32 ps2_ftell(FILE *stream) { - return ((Ps2File*)stream)->tell(); +bool PS2FileStream::eos() const { + return ((Ps2File*)_handle)->eof(); } -int ps2_feof(FILE *stream) { - return ((Ps2File*)stream)->eof(); +uint32 PS2FileStream::read(void *ptr, uint32 len) { + return ps2_fread((byte *)ptr, 1, len, _handle); } size_t ps2_fread(void *buf, size_t r, size_t n, FILE *stream) { @@ -473,12 +494,8 @@ size_t ps2_fread(void *buf, size_t r, size_t n, FILE *stream) { return ((Ps2File*)stream)->read(buf, r * n) / r; } -int ps2_fgetc(FILE *stream) { - uint8 temp; - if (((Ps2File*)stream)->read(&temp, 1)) - return temp; - else - return EOF; +uint32 PS2FileStream::write(const void *ptr, uint32 len) { + return ps2_fwrite(ptr, 1, len, _handle); } size_t ps2_fwrite(const void *buf, size_t r, size_t n, FILE *stream) { @@ -486,13 +503,6 @@ size_t ps2_fwrite(const void *buf, size_t r, size_t n, FILE *stream) { return ((Ps2File*)stream)->write(buf, r * n) / r; } -int ps2_fputc(int c, FILE *stream) { - if (((Ps2File*)stream)->write(&c, 1) == 1) - return c; - else - return -1; -} - int ps2_fputs(const char *s, FILE *stream) { int len = strlen(s); @@ -508,21 +518,29 @@ int ps2_fputs(const char *s, FILE *stream) { return EOF; } +bool PS2FileStream::flush() { + return ps2_fflush(_handle) == 0; +} + int ps2_fflush(FILE *stream) { // printf("fflush not implemented\n"); return 0; } -int ps2_ferror(FILE *stream) { - int err = ((Ps2File*)stream)->getErr(); +bool PS2FileStream::err() const { + int errVal = ((Ps2File*)_handle)->getErr(); - if (err) { - printf("ferror -> %d\n", err); + if (errVal) { + printf("ferror -> %d\n", errVal); } - return err; + return errVal != 0; +} + +void PS2FileStream::clearErr() { + ((Ps2File*)_handle)->setErr(false); } -void ps2_clearerr(FILE *stream) { - ((Ps2File*)stream)->setErr(false); +int32 PS2FileStream::size() const { + return ((Ps2File*)_handle)->size(); } diff --git a/backends/platform/ps2/fileio.h b/backends/platform/ps2/fileio.h index ce470cebd1..bb22b064a8 100644 --- a/backends/platform/ps2/fileio.h +++ b/backends/platform/ps2/fileio.h @@ -26,8 +26,11 @@ #ifndef __PS2FILE_IO__ #define __PS2FILE_IO__ -#include <stdio.h> +#include <stdio.h> // FIXME: Only for FILE -- get rid of this! + #include "common/scummsys.h" +#include "common/noncopyable.h" +#include "common/stream.h" enum { CACHE_SIZE = 2048 * 32, @@ -43,22 +46,22 @@ enum { // See also StdioStream. class Ps2File { public: - Ps2File(void); - virtual ~Ps2File(void); + Ps2File(); + virtual ~Ps2File(); virtual bool open(const char *name, int mode); virtual uint32 read(void *dest, uint32 len); virtual uint32 write(const void *src, uint32 len); - virtual int32 tell(void); - virtual int32 size(void); + virtual int32 tell(); + virtual int32 size(); virtual int seek(int32 offset, int origin); - virtual bool eof(void); - virtual bool getErr(void); + virtual bool eof(); + virtual bool getErr(); virtual void setErr(bool); private: - void cacheReadAhead(void); - void cacheReadSync(void); + void cacheReadAhead(); + void cacheReadSync(); int _fd; uint32 _mode; @@ -83,23 +86,42 @@ private: bool _stream; }; +// TODO: Merge Ps2File into PS2FileStream +class PS2FileStream : public Common::SeekableReadStream, public Common::WriteStream, public Common::NonCopyable { +protected: + /** File handle to the actual file. */ + FILE *_handle; + +public: + /** + * Given a path, invokes fopen on that path and wrap the result in a + * PS2FileStream instance. + */ + static PS2FileStream *makeFromPath(const Common::String &path, bool writeMode); + + PS2FileStream(FILE *handle); + virtual ~PS2FileStream(); + + virtual bool err() const; + virtual void clearErr(); + virtual bool eos() const; + + virtual uint32 write(const void *dataPtr, uint32 dataSize); + virtual bool flush(); + + virtual int32 pos() const; + virtual int32 size() const; + virtual bool seek(int32 offs, int whence = SEEK_SET); + virtual uint32 read(void *dataPtr, uint32 dataSize); +}; + +// TODO: Get rid of the following, instead use PS2FileStream directly. FILE *ps2_fopen(const char *fname, const char *mode); int ps2_fclose(FILE *stream); int ps2_fflush(FILE *stream); -int ps2_fseek(FILE *stream, long offset, int origin); -uint32 ps2_ftell(FILE *stream); -int ps2_feof(FILE *stream); size_t ps2_fread(void *buf, size_t r, size_t n, FILE *stream); -int ps2_fgetc(FILE *stream); -char *ps2_fgets(char *buf, int n, FILE *stream); - size_t ps2_fwrite(const void *buf, size_t r, size_t n, FILE *stream); -int ps2_fputc(int c, FILE *stream); int ps2_fputs(const char *s, FILE *stream); -int ps2_ferror(FILE *stream); -void ps2_clearerr(FILE *stream); - #endif // __PS2FILE_IO__ - diff --git a/backends/platform/ps2/systemps2.cpp b/backends/platform/ps2/systemps2.cpp index 1a3bf071c9..7bc90a7f0a 100644 --- a/backends/platform/ps2/systemps2.cpp +++ b/backends/platform/ps2/systemps2.cpp @@ -955,8 +955,7 @@ void OSystem_PS2::makeConfigPath() { ps2_fwrite(buf, size, 1, dst); ps2_fclose(dst); sprintf(path, "mc0:ScummVM/ScummVM.ini"); - } - else { + } else { sprintf(path, "cdfs:ScummVM.ini"); } |