Skip to content

Commit

Permalink
Allow file class to write whole blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
carstene1ns committed Sep 26, 2024
1 parent 76e34f5 commit ba01eba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/io/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
* @param pathType Kind of directory
* @param write Whether or not the file can be written to
*/
File::File (const char* name, int pathType, bool write) {
File::File (const char* name, int pathType, bool write) :
file(nullptr), filePath(nullptr), forWriting(write) {

Path* path = gamePaths.paths;

Expand Down Expand Up @@ -96,43 +97,37 @@ File::~File () {
* @param write Whether or not the file can be written to
*/
bool File::open (const char* path, const char* name, bool write) {
const char *fileMode = write ? "wb": "rb";

// Create the file path for the given directory
filePath = createString(path, name);

// Open the file from the path
file = fopen(filePath, write ? "wb": "rb");
file = fopen(filePath, fileMode);

#ifdef UPPERCASE_FILENAMES
if (!file) {

uppercaseString(filePath + strlen(path));
file = fopen(filePath, write ? "wb": "rb");

file = fopen(filePath, fileMode);
}
#endif

#ifdef LOWERCASE_FILENAMES
if (!file) {

lowercaseString(filePath + strlen(path));
file = fopen(filePath, write ? "wb": "rb");

file = fopen(filePath, fileMode);
}
#endif

if (file) {

LOG_DEBUG("Opened file: %s", filePath);

return true;

}

delete[] filePath;

return false;

}


Expand Down Expand Up @@ -284,6 +279,18 @@ void File::storeInt (signed int val) {
}


void File::storeData (void* data, int length) {

if(!forWriting) {
LOG_ERROR("File %s not opened for writing!", filePath);
return;
}

fwrite (data, length, 1, file);

}


/**
* Load a block of uncompressed data from the file.
*
Expand Down
2 changes: 2 additions & 0 deletions src/io/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class File {
private:
FILE* file;
char* filePath;
bool forWriting;

bool open (const char* path, const char* name, bool write);

Expand All @@ -53,6 +54,7 @@ class File {
void storeShort (unsigned short int val);
signed int loadInt ();
void storeInt (signed int val);
void storeData (void* data, int length);
unsigned char* loadBlock (int length);
unsigned char* loadRLE (int length, bool checkSize = true);
void skipRLE ();
Expand Down

0 comments on commit ba01eba

Please sign in to comment.