More refactoring

This commit is contained in:
2023-02-08 20:14:07 -08:00
parent bbe30d9253
commit b0e64420df
4 changed files with 139 additions and 72 deletions

View File

@ -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

View File

@ -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<size_t>(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();
}

View File

@ -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<size_t>(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();
};
}

View File

@ -11,6 +11,7 @@ target_sources(vnscenegen
${DAWN_SHARED_SOURCES}
VnSceneGen.cpp
../../util/DawnTool.cpp
../../util/File.cpp
../../util/file.cpp
../../util/xml.cpp
)