aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMax Horn2002-09-13 18:02:34 +0000
committerMax Horn2002-09-13 18:02:34 +0000
commitf2da602963351a9acc9e0288c439789f9f7dfb3d (patch)
treea49f43275628186f6cdda05d187d97ed904c369c /common
parentf43ecbf6286ad0944a27d40f18aa4dfe1c417c71 (diff)
downloadscummvm-rg350-f2da602963351a9acc9e0288c439789f9f7dfb3d.tar.gz
scummvm-rg350-f2da602963351a9acc9e0288c439789f9f7dfb3d.tar.bz2
scummvm-rg350-f2da602963351a9acc9e0288c439789f9f7dfb3d.zip
factored out the case-insensitive fopen into its own function - makes it easier to adapt all the code to use it. TODO: improve it to work like in exult, i.e. sometimes other parts of the path have to be changed to upper/lower case (e.g. video vs. VIDEO)
svn-id: r4935
Diffstat (limited to 'common')
-rw-r--r--common/file.cpp96
-rw-r--r--common/file.h11
2 files changed, 63 insertions, 44 deletions
diff --git a/common/file.cpp b/common/file.cpp
index b111975795..f7dfa2ae8f 100644
--- a/common/file.cpp
+++ b/common/file.cpp
@@ -22,6 +22,40 @@
#include "file.h"
#include "engine.h" // For debug/warning/error
+FILE *fopen_nocase(const char *path, const char *mode)
+{
+ FILE *file;
+
+ file = fopen(path, mode);
+ if (file)
+ return file;
+
+ char buf[256], *ptr;
+ int32 i = 0, pos = 0;
+
+ strcpy(buf, path);
+ while (buf[i] != 0) {
+ if ((buf[i] == '/') || (buf[i] == '\\')) {
+ pos = i + 1;
+ }
+ i++;
+ }
+
+ ptr = buf + pos;
+ do
+ *ptr++ = toupper(*ptr);
+ while (*ptr);
+ file = fopen(buf, mode);
+ if (file)
+ return file;
+
+ ptr = buf + pos;
+ do
+ *ptr++ = tolower(*ptr);
+ while (*ptr);
+ return fopen(buf, mode);
+}
+
File::File() {
_handle = NULL;
_ioFailed = false;
@@ -33,7 +67,7 @@ File::~File() {
}
bool File::open(const char *filename, int mode, byte encbyte) {
- char buf[256], *ptr;
+
if (_handle) {
debug(2, "File %s already opened", filename);
return false;
@@ -41,53 +75,15 @@ bool File::open(const char *filename, int mode, byte encbyte) {
clearIOFailed();
- int32 i = 0, pos = 0;
-
- strcpy(buf, filename);
- while (buf[i] != 0) {
- if ((buf[i] == '/') || (buf[i] == '\\')) {
- pos = i + 1;
- }
- i++;
- }
-
- if (mode == 1) {
- _handle = fopen(buf, "rb");
- if (_handle == NULL) {
- ptr = buf + pos;
- do
- *ptr++ = toupper(*ptr);
- while (*ptr);
- _handle = fopen(buf, "rb");
- }
- if (_handle == NULL) {
- ptr = buf + pos;
- do
- *ptr++ = tolower(*ptr);
- while (*ptr);
- _handle = fopen(buf, "rb");
- }
+ if (mode == kFileReadMode) {
+ _handle = fopen_nocase(filename, "rb");
if (_handle == NULL) {
debug(2, "File %s not found", filename);
return false;
}
}
- else if (mode == 2) {
- _handle = fopen(buf, "wb");
- if (_handle == NULL) {
- ptr = buf + pos;
- do
- *ptr++ = toupper(*ptr);
- while (*ptr);
- _handle = fopen(buf, "wb");
- }
- if (_handle == NULL) {
- ptr = buf + pos;
- do
- *ptr++ = tolower(*ptr);
- while (*ptr);
- _handle = fopen(buf, "wb");
- }
+ else if (mode == kFileWriteMode) {
+ _handle = fopen_nocase(filename, "wb");
if (_handle == NULL) {
debug(2, "File %s not opened", filename);
return false;
@@ -138,6 +134,20 @@ uint32 File::pos() {
return ftell(_handle);
}
+uint32 File::size() {
+ if (_handle == NULL) {
+ error("File is not open!");
+ return 0;
+ }
+
+ uint32 oldPos = ftell(_handle);
+ fseek(_handle, 0, SEEK_END);
+ uint32 length = ftell(_handle);
+ fseek(_handle, oldPos, SEEK_SET);
+
+ return length;
+}
+
void File::seek(int32 offs, int whence) {
if (_handle == NULL) {
error("File is not open!");
diff --git a/common/file.h b/common/file.h
index e2a2e97b94..b037b6cdc2 100644
--- a/common/file.h
+++ b/common/file.h
@@ -26,6 +26,10 @@
#include "stdafx.h"
#include "scummsys.h"
+// fopen_nocase is like fopen only that it will try various variations
+// of the given filename (with different cases) if the initial one isn't found.
+FILE *fopen_nocase(const char *path, const char *mode);
+
class File {
private:
@@ -34,16 +38,21 @@ private:
byte _encbyte;
public:
+ enum {
+ kFileReadMode = 1,
+ kFileWriteMode = 2
+ };
File();
~File();
- bool open(const char *filename, int mode = 1, byte encbyte = 0);
+ bool open(const char *filename, int mode = kFileReadMode, byte encbyte = 0);
void close();
bool isOpen();
bool ioFailed();
void clearIOFailed();
bool eof();
uint32 pos();
+ uint32 size();
void seek(int32 offs, int whence);
uint32 read(void *ptr, uint32 size);
byte readByte();