aboutsummaryrefslogtreecommitdiff
path: root/backends/file
diff options
context:
space:
mode:
Diffstat (limited to 'backends/file')
-rw-r--r--backends/file/base-file.cpp226
-rw-r--r--backends/file/base-file.h180
-rw-r--r--backends/file/ds/ds-file.cpp0
-rw-r--r--backends/file/ds/ds-file.h46
-rw-r--r--backends/file/posix/posix-file.cpp41
-rw-r--r--backends/file/posix/posix-file.h50
-rw-r--r--backends/file/ps2/ps2-file.cpp0
-rw-r--r--backends/file/ps2/ps2-file.h24
-rw-r--r--backends/file/symbian/symbian-file.cpp0
-rw-r--r--backends/file/symbian/symbian-file.h29
10 files changed, 596 insertions, 0 deletions
diff --git a/backends/file/base-file.cpp b/backends/file/base-file.cpp
new file mode 100644
index 0000000000..3174f5828c
--- /dev/null
+++ b/backends/file/base-file.cpp
@@ -0,0 +1,226 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/branches/gsoc2007-fsnode/common/file.cpp $
+ * $Id: file.cpp 28150 2007-07-20 19:42:38Z david_corrales $
+ *
+ */
+
+#include "backends/file/base-file.h"
+#include "common/fs.h"
+#include "common/hashmap.h"
+#include "common/util.h"
+#include "common/hash-str.h"
+
+#if defined(UNIX) || defined(__SYMBIAN32__)
+#include <errno.h>
+#endif
+
+#ifdef MACOSX
+#include "CoreFoundation/CoreFoundation.h"
+#endif
+
+BaseFile::BaseFile() {
+ _handle = 0;
+ _ioFailed = false;
+}
+
+//#define DEBUG_FILE_REFCOUNT
+
+BaseFile::~BaseFile() {
+#ifdef DEBUG_FILE_REFCOUNT
+ warning("File::~File on file '%s'", _name.c_str());
+#endif
+ close();
+}
+
+bool BaseFile::open(const String &filename, AccessMode mode) {
+ assert(mode == kFileReadMode || mode == kFileWriteMode);
+
+ if (filename.empty()) {
+ error("File::open: No filename was specified");
+ }
+
+ if (_handle) {
+ error("File::open: This file object already is opened (%s), won't open '%s'", _name.c_str(), filename.c_str());
+ }
+
+ _name.clear();
+ clearIOFailed();
+
+ const char *modeStr = (mode == kFileReadMode) ? "rb" : "wb";
+ _handle = _fopen(filename.c_str(), modeStr);
+
+ if (_handle == NULL) {
+ if (mode == kFileReadMode)
+ debug(2, "File %s not found", filename.c_str());
+ else
+ debug(2, "File %s not opened", filename.c_str());
+ return false;
+ }
+
+ _name = filename;
+
+#ifdef DEBUG_FILE_REFCOUNT
+ warning("File::open on file '%s'", _name.c_str());
+#endif
+
+ return true;
+}
+
+bool BaseFile::remove(const String &filename){
+ if (remove(filename.c_str()) != 0) {
+ if(errno == EACCES)
+ ;//TODO: read-only file
+ if(errno == ENOENT)
+ ;//TODO: non-existent file
+
+ return false;
+ } else {
+ return true;
+ }
+}
+
+void BaseFile::close() {
+ if (_handle)
+ _fclose((FILE *)_handle);
+ _handle = NULL;
+}
+
+bool BaseFile::isOpen() const {
+ return _handle != NULL;
+}
+
+bool BaseFile::ioFailed() const {
+ return _ioFailed != 0;
+}
+
+void BaseFile::clearIOFailed() {
+ _ioFailed = false;
+}
+
+bool BaseFile::eof() const {
+ if (_handle == NULL) {
+ error("File::eof: File is not open!");
+ return false;
+ }
+
+ return _feof((FILE *)_handle) != 0;
+}
+
+uint32 BaseFile::pos() const {
+ if (_handle == NULL) {
+ error("File::pos: File is not open!");
+ return 0;
+ }
+
+ return _ftell((FILE *)_handle);
+}
+
+uint32 BaseFile::size() const {
+ if (_handle == NULL) {
+ error("File::size: File is not open!");
+ return 0;
+ }
+
+ uint32 oldPos = _ftell((FILE *)_handle);
+ _fseek((FILE *)_handle, 0, SEEK_END);
+ uint32 length = _ftell((FILE *)_handle);
+ _fseek((FILE *)_handle, oldPos, SEEK_SET);
+
+ return length;
+}
+
+void BaseFile::seek(int32 offs, int whence) {
+ if (_handle == NULL) {
+ error("File::seek: File is not open!");
+ return;
+ }
+
+ if (_fseek((FILE *)_handle, offs, whence) != 0)
+ _clearerr((FILE *)_handle);
+}
+
+uint32 BaseFile::read(void *ptr, uint32 len) {
+ byte *ptr2 = (byte *)ptr;
+ uint32 real_len;
+
+ if (_handle == NULL) {
+ error("File::read: File is not open!");
+ return 0;
+ }
+
+ if (len == 0)
+ return 0;
+
+ real_len = _fread(ptr2, 1, len, (FILE *)_handle);
+ if (real_len < len) {
+ _ioFailed = true;
+ }
+
+ return real_len;
+}
+
+/*uint32 File::write(const void *ptr, uint32 len) {
+ if (_handle == NULL) {
+ error("File::write: File is not open!");
+ return 0;
+ }
+
+ if (len == 0)
+ return 0;
+
+ if ((uint32)_fwrite(ptr, 1, len, (FILE *)_handle) != len) {
+ _ioFailed = true;
+ }
+
+ return len;
+}*/
+
+void BaseFile::_clearerr(FILE *stream) {
+ clearerr(stream);
+}
+
+int BaseFile::_fclose(FILE *stream) {
+ return fclose(stream);
+}
+
+int BaseFile::_feof(FILE *stream) const {
+ return feof(stream);
+}
+FILE *BaseFile::_fopen(const char * filename, const char * mode) {
+ return fopen(filename, mode);
+}
+
+int BaseFile::_fread(void *buffer, size_t obj_size, size_t num, FILE *stream) {
+ return fread(buffer, obj_size, num, stream);
+}
+
+int BaseFile::_fseek(FILE * stream, long int offset, int origin) const {
+ return fseek(stream, offset, origin);
+}
+
+long BaseFile::_ftell(FILE *stream) const {
+ return ftell(stream);
+}
+
+int BaseFile::_fwrite(const void * ptr, size_t obj_size, size_t count, FILE * stream) {
+ return fwrite(ptr, obj_size, count, stream);
+}
diff --git a/backends/file/base-file.h b/backends/file/base-file.h
new file mode 100644
index 0000000000..f383a1a453
--- /dev/null
+++ b/backends/file/base-file.h
@@ -0,0 +1,180 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL: $
+ * $Id: $
+ */
+
+#ifndef BACKENDS_BASE_FILE_H
+#define BACKENDS_BASE_FILE_H
+
+#include "common/stdafx.h"
+#include "common/scummsys.h"
+#include "common/str.h"
+#include "common/stream.h"
+
+using namespace Common;
+
+/**
+ * Implements several file related functions used by the Common::File wrapper.
+ */
+class BaseFile : public Common::SeekableReadStream {
+protected:
+ /** File handle to the actual file; 0 if no file is open. */
+ void *_handle;
+
+ /** Status flag which tells about recent I/O failures. */
+ bool _ioFailed;
+
+ /** The name of this file, for debugging. */
+ String _name;
+
+ /**
+ * The following functions are meant to be redefined by subclasses if needed. E.g. ps2-file.h or ds-file.h
+ * They behave as the C++ standard I/O methods so refer to the standard documentation for usage.
+ *
+ * This design was inspired on the Template pattern.
+ */
+ void _clearerr(FILE *stream);
+ int _fclose(FILE *stream);
+ int _feof(FILE *stream) const;
+ FILE *_fopen(const char * filename, const char * mode);
+ int _fread(void *buffer, size_t obj_size, size_t num, FILE *stream);
+ int _fseek(FILE * stream, long int offset, int origin) const;
+ long _ftell(FILE *stream) const;
+ int _fwrite(const void * ptr, size_t obj_size, size_t count, FILE * stream);
+
+private:
+ // Disallow copying BaseFile objects. There is not strict reason for this,
+ // except that so far we never had real need for such a feature, and
+ // code that accidentally copied File objects tended to break in strange
+ // ways.
+ BaseFile(const BaseFile &f);
+ BaseFile &operator =(const BaseFile &f);
+
+public:
+ enum AccessMode {
+ kFileReadMode = 1,
+ kFileWriteMode = 2
+ };
+
+ BaseFile();
+ virtual ~BaseFile();
+
+ /**
+ * Clears the flag for the last ocurred IO failure.
+ */
+ void clearIOFailed();
+
+ /**
+ * Closes the file handle.
+ */
+ virtual void close();
+
+ /**
+ * Checks for end of file.
+ *
+ * @return: true if the end of file is reached, false otherwise.
+ */
+ bool eof() const;
+
+ /**
+ * Checks for the end of the stream. In this case it's equivalent to eof().
+ *
+ * @return: true if the end of the stream is reached, false otherwise.
+ */
+ bool eos() const { return eof(); }
+
+ /**
+ * Checks whether the last IO operation failed.
+ *
+ * @return: true if the last IO operation failed, false otherwise.
+ */
+ bool ioFailed() const;
+
+ /**
+ * Obtain the filename of the opened file.
+ *
+ * @return: the filename of the opened file.
+ */
+ const char *name() const { return _name.c_str(); }
+
+ /**
+ * Checks if the object opened a file successfully.
+ *
+ * @return: true if any file is opened, false otherwise.
+ */
+ bool isOpen() const;
+
+ /**
+ * Opens a given file.
+ *
+ * @param filename Path of the file to be opened.
+ * @param mode Mode to open to file. Read or write.
+ */
+ virtual bool open(const String &filename, AccessMode mode = kFileReadMode);
+
+ /**
+ * Obtain the position of the seek pointer.
+ *
+ * @return The position of the seek pointer within the file.
+ */
+ uint32 pos() const;
+
+ /**
+ * Read a chunk of data from the file.
+ *
+ * @param dataPtr Buffer to the place the read contents.
+ * @param dataSize Amount of bytes to read from the file.
+ * @return Amount of read bytes.
+ */
+ uint32 read(void *dataPtr, uint32 dataSize);
+
+ /**
+ * Remove a given file from the filesystem.
+ *
+ * @param filename Path to the file to be removed.
+ * @return true if the file was removed succesfully, false otherwise.
+ */
+ virtual bool remove(const String &filename);
+
+ /**
+ * Move the seek pointer within the file.
+ *
+ * @param offs Amount of bytes to move the pointer within the file.
+ * @param whence Starting point of the seek cursor.
+ */
+ void seek(int32 offs, int whence = SEEK_SET);
+
+ /**
+ * Obtain the size of the file.
+ *
+ * @return The size of the file in bytes.
+ */
+ uint32 size() const;
+
+ //TODO: Remove the write functions? Also remove the enum then
+ /**
+ * Write a chunk of data to the file.
+ */
+ //uint32 write(const void *dataPtr, uint32 dataSize);
+};
+
+#endif //BACKENDS_BASE_FILE_H
diff --git a/backends/file/ds/ds-file.cpp b/backends/file/ds/ds-file.cpp
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/backends/file/ds/ds-file.cpp
diff --git a/backends/file/ds/ds-file.h b/backends/file/ds/ds-file.h
new file mode 100644
index 0000000000..e133def548
--- /dev/null
+++ b/backends/file/ds/ds-file.h
@@ -0,0 +1,46 @@
+#ifdef __DS__
+
+ // These functions replease the standard library functions of the same name.
+ // As this header is included after the standard one, I have the chance to #define
+ // all of these to my own code.
+ //
+ // A #define is the only way, as redefinig the functions would cause linker errors.
+
+ // These functions need to be #undef'ed, as their original definition
+ // in devkitarm is done with #includes (ugh!)
+ #undef feof
+ #undef clearerr
+ //#undef getc
+ //#undef ferror
+
+
+ //void std_fprintf(FILE* handle, const char* fmt, ...); // used in common/util.cpp
+ //void std_fflush(FILE* handle); // used in common/util.cpp
+
+ //char* std_fgets(char* str, int size, FILE* file); // not used
+ //int std_getc(FILE* handle); // not used
+ //char* std_getcwd(char* dir, int dunno); // not used
+ //void std_cwd(char* dir); // not used
+ //int std_ferror(FILE* handle); // not used
+
+ // Only functions used in the ScummVM source have been defined here!
+ #define fopen(name, mode) DS::std_fopen(name, mode)
+ #define fclose(handle) DS::std_fclose(handle)
+ #define fread(ptr, size, items, file) DS::std_fread(ptr, size, items, file)
+ #define fwrite(ptr, size, items, file) DS::std_fwrite(ptr, size, items, file)
+ #define feof(handle) DS::std_feof(handle)
+ #define ftell(handle) DS::std_ftell(handle)
+ #define fseek(handle, offset, whence) DS::std_fseek(handle, offset, whence)
+ #define clearerr(handle) DS::std_clearerr(handle)
+
+ //#define printf(fmt, ...) consolePrintf(fmt, ##__VA_ARGS__)
+
+ //#define fprintf(file, fmt, ...) { char str[128]; sprintf(str, fmt, ##__VA_ARGS__); DS::std_fwrite(str, strlen(str), 1, file); }
+ //#define fflush(file) DS::std_fflush(file) // used in common/util.cpp
+
+ //#define fgets(str, size, file) DS::std_fgets(str, size, file) // not used
+ //#define getc(handle) DS::std_getc(handle) // not used
+ //#define getcwd(dir, dunno) DS::std_getcwd(dir, dunno) // not used
+ //#define ferror(handle) DS::std_ferror(handle) // not used
+
+#endif \ No newline at end of file
diff --git a/backends/file/posix/posix-file.cpp b/backends/file/posix/posix-file.cpp
new file mode 100644
index 0000000000..84c82fa5c5
--- /dev/null
+++ b/backends/file/posix/posix-file.cpp
@@ -0,0 +1,41 @@
+#include "backends/file/posix/posix-file.h"
+
+POSIXFile::POSIXFile() : BaseFile() {
+ //
+}
+
+POSIXFile::~POSIXFile() {
+ close();
+}
+
+void POSIXFile::_clearerr(FILE *stream) {
+ clearerr(stream);
+}
+
+int POSIXFile::_fclose(FILE *stream) {
+ return fclose(stream);
+}
+
+int POSIXFile::_feof(FILE *stream) const {
+ return feof(stream);
+}
+FILE *POSIXFile::_fopen(const char * filename, const char * mode) {
+ printf("Opened a file!\n");
+ return fopen(filename, mode);
+}
+
+int POSIXFile::_fread(void *buffer, size_t obj_size, size_t num, FILE *stream) {
+ return fread(buffer, obj_size, num, stream);
+}
+
+int POSIXFile::_fseek(FILE * stream, long int offset, int origin) const {
+ return fseek(stream, offset, origin);
+}
+
+long POSIXFile::_ftell(FILE *stream) const {
+ return ftell(stream);
+}
+
+int POSIXFile::_fwrite(const void * ptr, size_t obj_size, size_t count, FILE * stream) {
+ return fwrite(ptr, obj_size, count, stream);
+}
diff --git a/backends/file/posix/posix-file.h b/backends/file/posix/posix-file.h
new file mode 100644
index 0000000000..00ce307df5
--- /dev/null
+++ b/backends/file/posix/posix-file.h
@@ -0,0 +1,50 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL: $
+ * $Id: $
+ */
+
+#ifndef BACKENDS_POSIX_FILE_H
+#define BACKENDS_POSIX_FILE_H
+
+#include "backends/file/base-file.cpp"
+
+/**
+ * Implements several POSIX specific file related functions used by the Common::File wrapper.
+ *
+ * Parts of this class are documented in the base file class, BaseFile.
+ */
+class POSIXFile : public BaseFile {
+public:
+ POSIXFile();
+ ~POSIXFile();
+protected:
+ void _clearerr(FILE *stream);
+ int _fclose(FILE *stream);
+ int _feof(FILE *stream) const;
+ FILE *_fopen(const char * filename, const char * mode);
+ int _fread(void *buffer, size_t obj_size, size_t num, FILE *stream);
+ int _fseek(FILE * stream, long int offset, int origin) const;
+ long _ftell(FILE *stream) const;
+ int _fwrite(const void * ptr, size_t obj_size, size_t count, FILE * stream);
+};
+
+#endif //BACKENDS_POSIX_FILE_H
diff --git a/backends/file/ps2/ps2-file.cpp b/backends/file/ps2/ps2-file.cpp
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/backends/file/ps2/ps2-file.cpp
diff --git a/backends/file/ps2/ps2-file.h b/backends/file/ps2/ps2-file.h
new file mode 100644
index 0000000000..e7acfd801f
--- /dev/null
+++ b/backends/file/ps2/ps2-file.h
@@ -0,0 +1,24 @@
+#ifdef __PLAYSTATION2__
+ // for those replaced fopen/fread/etc functions
+ typedef unsigned long uint64;
+ typedef signed long int64;
+ #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 fprintf ps2_fprintf // used in common/util.cpp
+ //#define fflush(a) ps2_fflush(a) // used in common/util.cpp
+
+ //#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 \ No newline at end of file
diff --git a/backends/file/symbian/symbian-file.cpp b/backends/file/symbian/symbian-file.cpp
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/backends/file/symbian/symbian-file.cpp
diff --git a/backends/file/symbian/symbian-file.h b/backends/file/symbian/symbian-file.h
new file mode 100644
index 0000000000..7807a9355a
--- /dev/null
+++ b/backends/file/symbian/symbian-file.h
@@ -0,0 +1,29 @@
+#ifdef __SYMBIAN32__
+ #undef feof
+ #undef clearerr
+
+ #define FILE void
+
+ FILE* symbian_fopen(const char* name, const char* mode);
+ void symbian_fclose(FILE* handle);
+ size_t symbian_fread(const void* ptr, size_t size, size_t numItems, FILE* handle);
+ size_t symbian_fwrite(const void* ptr, size_t size, size_t numItems, FILE* handle);
+ bool symbian_feof(FILE* handle);
+ long int symbian_ftell(FILE* handle);
+ int symbian_fseek(FILE* handle, long int offset, int whence);
+ void symbian_clearerr(FILE* handle);
+
+ // Only functions used in the ScummVM source have been defined here!
+ #define fopen(name, mode) symbian_fopen(name, mode)
+ #define fclose(handle) symbian_fclose(handle)
+ #define fread(ptr, size, items, file) symbian_fread(ptr, size, items, file)
+ #define fwrite(ptr, size, items, file) symbian_fwrite(ptr, size, items, file)
+ #define feof(handle) symbian_feof(handle)
+ #define ftell(handle) symbian_ftell(handle)
+ #define fseek(handle, offset, whence) symbian_fseek(handle, offset, whence)
+ #define clearerr(handle) symbian_clearerr(handle)
+#endif
+
+#if defined(UNIX) || defined(__SYMBIAN32__)
+#include <errno.h>
+#endif \ No newline at end of file