From b0e64420df5a7a594f8515d878a2502b74177a96 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 8 Feb 2023 20:14:07 -0800 Subject: [PATCH] More refactoring --- .../locale/languagegen/CMakeLists.txt | 2 + src/dawntools/util/File.cpp | 89 +++++++++++++ src/dawntools/util/File.hpp | 119 +++++++----------- .../visualnovel/vnscenegen/CMakeLists.txt | 1 + 4 files changed, 139 insertions(+), 72 deletions(-) create mode 100644 src/dawntools/util/File.cpp diff --git a/src/dawntools/locale/languagegen/CMakeLists.txt b/src/dawntools/locale/languagegen/CMakeLists.txt index c37b7f5c..4d20d747 100644 --- a/src/dawntools/locale/languagegen/CMakeLists.txt +++ b/src/dawntools/locale/languagegen/CMakeLists.txt @@ -11,6 +11,8 @@ target_sources(languagegen ${DAWN_SHARED_SOURCES} LanguageGen.cpp ../../util/DawnTool.cpp + ../../util/File.cpp + ../../util/file.cpp ../../util/csv.cpp ../../util/xml.cpp diff --git a/src/dawntools/util/File.cpp b/src/dawntools/util/File.cpp new file mode 100644 index 00000000..4a3e030a --- /dev/null +++ b/src/dawntools/util/File.cpp @@ -0,0 +1,89 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "File.hpp" + +using namespace Dawn; + +File::File(std::string filename) { + this->filename = fileNormalizeSlashesNew(filename); +} + +bool_t File::open(enum FileMode mode) { + assertNull(this->file); + + this->mode = mode; + this->file = fopen( + this->filename.c_str(), + mode == FILE_MODE_READ ? "rb" : "wb" + ); + + if(this->file == NULL) return false; + + fseek(this->file, 0, SEEK_END); + this->length = ftell(this->file); + fseek(this->file, 0, SEEK_SET); + + if(this->length <= 0) { + this->close(); + return false; + } + + return true; +} + +bool_t File::isOpen() { + return this->file != nullptr; +} + +void File::close() { + assertNotNull(this->file); + fclose(this->file); + this->file = nullptr; +} + +bool_t File::mkdirp() { + fileMkdirp((char*)this->filename.c_str()); + return true; +} + +bool_t File::readString(std::string *out) { + assertNotNull(out); + + if(!this->isOpen()) { + if(!this->open(FILE_MODE_READ)) return false; + } + assertTrue(this->mode == FILE_MODE_READ); + out->clear(); + + size_t i = 0; + char buffer[FILE_BUFFER_SIZE + 1]; + while(i != this->length) { + size_t amt = mathMin(FILE_BUFFER_SIZE, (this->length - i)); + auto amtRead = fread(buffer, sizeof(char), amt, this->file); + if(amtRead != amt) return false; + i += amtRead; + buffer[amtRead + 1] = '\0'; + out->append(buffer); + } + + return true; +} + +bool_t File::writeString(std::string in) { + if(!this->isOpen()) { + if(!this->open(FILE_MODE_WRITE)) return false; + } + assertTrue(this->mode == FILE_MODE_WRITE); + + const char_t *strOut = in.c_str(); + // TODO: Validate write length. + fwrite(strOut, sizeof(char_t), in.size(), this->file); + return true; +} + +File::~File() { + if(this->file != nullptr) this->close(); +} \ No newline at end of file diff --git a/src/dawntools/util/File.hpp b/src/dawntools/util/File.hpp index b2c7659a..5a4a890a 100644 --- a/src/dawntools/util/File.hpp +++ b/src/dawntools/util/File.hpp @@ -25,84 +25,59 @@ namespace Dawn { public: std::string filename; - File(std::string filename) { - this->filename = fileNormalizeSlashesNew(filename); - } + /** + * Constructs a new File interface class. + * + * @param filename Filename that you want to interface with. + */ + File(std::string filename); - bool_t open(enum FileMode mode) { - assertNull(this->file); + /** + * Opens a connection to the file. + * + * @param mode File mode to use for this interface. + * @return True if success, otherwise for failure. + */ + bool_t open(enum FileMode mode); - this->mode = mode; - this->file = fopen( - this->filename.c_str(), - mode == FILE_MODE_READ ? "rb" : "wb" - ); + /** + * Returns whether or not the file connection is currently opened. + * + * @return True if the connection is open, otherwise if it's not. + */ + bool_t isOpen(); - if(this->file == NULL) return false; + /** + * Closes the currently open interface to the file. Done automatically + * when this object is disposed. + */ + void close(); - fseek(this->file, 0, SEEK_END); - this->length = ftell(this->file); - fseek(this->file, 0, SEEK_SET); + /** + * Makes all directories above this file's filename. + * + * @return True if successful, otherwise false. + */ + bool_t mkdirp(); - if(this->length <= 0) { - this->close(); - return false; - } + /** + * Reads the entire contents of a file to the given output string buffer. + * This is a bit dangerous since the length of the file isn't checked + * against the memory to be consumed. + * + * @param out Pointer to a string where the output data will be stored. + * @return True if the read was successful, otherwise false. + */ + bool_t readString(std::string *out); - return true; - } + /** + * Writes the entire contents of a string to a file. + * + * @param in String to write to this file. + * @return True if written successfully, otherwise false. + */ + bool_t writeString(std::string in); - bool_t isOpen() { - return this->file != nullptr; - } - - void close() { - assertNotNull(this->file); - fclose(this->file); - } - - bool_t readString(std::string *out) { - assertNotNull(out); - - if(!this->isOpen()) { - if(!this->open(FILE_MODE_READ)) return false; - } - assertTrue(this->mode == FILE_MODE_READ); - out->clear(); - - size_t i = 0; - char buffer[FILE_BUFFER_SIZE + 1]; - while(i != this->length) { - size_t amt = mathMin(FILE_BUFFER_SIZE, (this->length - i)); - auto amtRead = fread(buffer, sizeof(char), amt, this->file); - if(amtRead != amt) return false; - i += amtRead; - buffer[amtRead + 1] = '\0'; - out->append(buffer); - } - - return true; - } - - bool_t mkdirp() { - fileMkdirp((char *)this->filename.c_str()); - return true; - } - - bool_t writeString(std::string in) { - if(!this->isOpen()) { - if(!this->open(FILE_MODE_WRITE)) return false; - } - assertTrue(this->mode == FILE_MODE_WRITE); - - const char_t *strOut = in.c_str(); - // TODO: Validate write length. - fwrite(strOut, sizeof(char_t), in.size(), this->file); - return true; - } - - ~File() { - if(this->file != nullptr) this->close(); - } + ~File(); }; } \ No newline at end of file diff --git a/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt b/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt index 7b56295d..b4ec3631 100644 --- a/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt +++ b/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt @@ -11,6 +11,7 @@ target_sources(vnscenegen ${DAWN_SHARED_SOURCES} VnSceneGen.cpp ../../util/DawnTool.cpp + ../../util/File.cpp ../../util/file.cpp ../../util/xml.cpp )