diff --git a/src/dawntools/util/File.cpp b/src/dawntools/util/File.cpp index 4a3e030a..8423f99d 100644 --- a/src/dawntools/util/File.cpp +++ b/src/dawntools/util/File.cpp @@ -22,13 +22,17 @@ bool_t File::open(enum FileMode mode) { if(this->file == NULL) return false; - fseek(this->file, 0, SEEK_END); - this->length = ftell(this->file); - fseek(this->file, 0, SEEK_SET); + if(mode == FILE_MODE_READ) { + 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; + if(this->length <= 0) { + this->close(); + return false; + } + } else { + this->length = 0; } return true; @@ -80,7 +84,7 @@ bool_t File::writeString(std::string in) { const char_t *strOut = in.c_str(); // TODO: Validate write length. - fwrite(strOut, sizeof(char_t), in.size(), this->file); + this->length = fwrite(strOut, sizeof(char_t), in.size(), this->file); return true; } diff --git a/src/dawntools/visualnovel/vnscenegen/VnSceneGen.cpp b/src/dawntools/visualnovel/vnscenegen/VnSceneGen.cpp index 0ae9e4f0..1c3dd0f3 100644 --- a/src/dawntools/visualnovel/vnscenegen/VnSceneGen.cpp +++ b/src/dawntools/visualnovel/vnscenegen/VnSceneGen.cpp @@ -192,39 +192,23 @@ std::string parseEvent(xml_t *evt, struct HeaderInformation *header) { return buffer; } -int oldmain(int32_t argc, char *args[]) { - if(argc != 3) { +int32_t VnSceneGen::start() { + if(this->args.size() != 3) { std::cout << "Invalid number of args passed to VNScene Generator" << std::endl; return 1; } // Open input file. - char fileIn[FILENAME_MAX]; - fileNormalizeSlashes(args[1]); - sprintf(fileIn, "%s", args[1]); - FILE *fin = fopen(fileIn, "rb"); - if(fin == NULL) { - std::cout << "Failed to open input file " << fileIn << std::endl; + File file(this->args[1]); + std::string buffer; + if(!file.readString(&buffer)) { + std::cout << "Failed to read scene " << file.filename << std::endl; return 1; } - // Tell file len - auto len = assetReadString(fin, NULL); - - // Read data. - char *buffer = (char *)malloc(sizeof(char) * (len + 1)); - if(buffer == NULL) { - std::cout << "Failed to create temporary memory." << std::endl; - fclose(fin); - return 1; - } - assetReadString(fin, buffer); - fclose(fin); - // Parse XML xml_t xml; - xmlLoad(&xml, buffer); - free(buffer); + xmlLoad(&xml, (char*)buffer.c_str()); // First, read the header information struct HeaderInformation header; @@ -322,26 +306,16 @@ int oldmain(int32_t argc, char *args[]) { // Finished with XML data, now we can write data out. xmlDispose(&xml); - - char fileOut[FILENAME_MAX]; - fileNormalizeSlashes(args[2]); - sprintf(fileOut, "%s.hpp", args[2]); - fileMkdirp(fileOut); - FILE *fout = fopen(fileOut, "wb"); - if(fout == NULL) { - std::cout << "Failed to open output file." << std::endl; + + File fileOut(this->args[2] + ".hpp"); + if(!fileOut.mkdirp()) { + std::cout << "Failed to make scene output dir" << std::endl; + return 1; + } + if(!fileOut.writeString(bufferOut)) { + std::cout << "Failed to generate scene " << fileOut.filename << std::endl; return 1; } - // Buffer out data. - const char *bufferOutStr = bufferOut.c_str(); - fwrite(bufferOutStr, sizeof(char), strlen(bufferOutStr), fout); - fclose(fout); - std::cout << "Generated Scene " << fileOut << std::endl; - - return 0; -} - -int32_t VnSceneGen::start() { return 0; } \ No newline at end of file diff --git a/src/dawntools/visualnovel/vnscenegen/VnSceneGen.hpp b/src/dawntools/visualnovel/vnscenegen/VnSceneGen.hpp index f0a858e6..6ad230f9 100644 --- a/src/dawntools/visualnovel/vnscenegen/VnSceneGen.hpp +++ b/src/dawntools/visualnovel/vnscenegen/VnSceneGen.hpp @@ -6,6 +6,7 @@ #pragma once #include "util/DawnTool.hpp" #include "util/Xml.hpp" +#include "util/File.hpp" #include "../../util/file.hpp" #include "../../util/xml.hpp" #include