aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backends/fs/ps2/ps2-fs.cpp6
-rw-r--r--backends/fs/stdiostream.cpp24
-rw-r--r--backends/platform/ps2/fileio.cpp86
-rw-r--r--backends/platform/ps2/fileio.h62
-rw-r--r--backends/platform/ps2/systemps2.cpp3
5 files changed, 97 insertions, 84 deletions
diff --git a/backends/fs/ps2/ps2-fs.cpp b/backends/fs/ps2/ps2-fs.cpp
index c8d27e4890..0968ecc9d3 100644
--- a/backends/fs/ps2/ps2-fs.cpp
+++ b/backends/fs/ps2/ps2-fs.cpp
@@ -23,11 +23,9 @@
*/
#include "backends/fs/abstract-fs.h"
-#include "backends/fs/stdiostream.h"
#include <kernel.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include "backends/platform/ps2/asyncfio.h"
#include "backends/platform/ps2/fileio.h"
#include "backends/platform/ps2/systemps2.h"
@@ -504,10 +502,10 @@ const char *Ps2FilesystemNode::getDeviceDescription() const {
}
Common::SeekableReadStream *Ps2FilesystemNode::createReadStream() {
- Common::SeekableReadStream *ss = StdioStream::makeFromPath(getPath().c_str(), false);
+ Common::SeekableReadStream *ss = PS2FileStream::makeFromPath(getPath().c_str(), false);
return ss;
}
Common::WriteStream *Ps2FilesystemNode::createWriteStream() {
- return StdioStream::makeFromPath(getPath().c_str(), true);
+ return PS2FileStream::makeFromPath(getPath().c_str(), true);
}
diff --git a/backends/fs/stdiostream.cpp b/backends/fs/stdiostream.cpp
index 07c631b3ad..8845d796c6 100644
--- a/backends/fs/stdiostream.cpp
+++ b/backends/fs/stdiostream.cpp
@@ -25,30 +25,6 @@
#include "backends/fs/stdiostream.h"
-#ifdef __PLAYSTATION2__
- // for those replaced fopen/fread/etc functions
- #include "backends/platform/ps2/fileio.h"
-
- #define fopen(a, b) ps2_fopen(a, b)
- #define fclose(a) ps2_fclose(a)
- #define fseek(a, b, c) ps2_fseek(a, b, c)
- #define ftell(a) ps2_ftell(a)
- #define feof(a) ps2_feof(a)
- #define fread(a, b, c, d) ps2_fread(a, b, c, d)
- #define fwrite(a, b, c, d) ps2_fwrite(a, b, c, d)
-
- #define fflush(a) ps2_fflush(a) // used in common/util.cpp
- #define ferror(a) ps2_ferror(a)
- #define clearerr(a) ps2_clearerr(a)
-
- //#define fgetc(a) ps2_fgetc(a) // not used
- //#define fgets(a, b, c) ps2_fgets(a, b, c) // not used
- //#define fputc(a, b) ps2_fputc(a, b) // not used
- //#define fputs(a, b) ps2_fputs(a, b) // not used
-
- //#define fsize(a) ps2_fsize(a) // not used -- and it is not a standard function either
-#endif
-
StdioStream::StdioStream(void *handle) : _handle(handle) {
assert(handle);
}
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");
}