From 95d8d76fa71d6c161a3924f9ddf2b8fe83b07073 Mon Sep 17 00:00:00 2001 From: Xnoe Date: Fri, 10 Jul 2020 05:31:50 +0100 Subject: [PATCH] Made code more sane. Now allocates the exact space needed for things. --- main.cpp | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/main.cpp b/main.cpp index 90f982c..dfc1e90 100644 --- a/main.cpp +++ b/main.cpp @@ -6,20 +6,24 @@ namespace fs = std::filesystem; -const int MAX_FILE_SIZE = 65536; +char* read(const char* name) { + std::ifstream file_stream(name, std::ios_base::binary); + + int count = 0; + file_stream.seekg(0, file_stream.end); + int length = file_stream.tellg(); + file_stream.seekg(0, file_stream.beg); + char* data = (char*) calloc(length, 1); + file_stream.read(data, length); + + file_stream.close(); + return data; +} string32 readfile(const char* name) { - std::fstream file_stream; - file_stream.open(name); - - char* data = new char [MAX_FILE_SIZE]; - int count(0); - while (file_stream) - data[count++] = file_stream.get(); - file_stream.close(); - data[--count] = 0; + char* data = read(name); string32 file = string32 (data); - delete data; + free (data); return file; } @@ -31,14 +35,16 @@ int main() { for (const auto & file : fs::recursive_directory_iterator(path)) { if (file.is_regular_file()) { - if (file.path().stem().string().at(0) != '_') { - string32 newFile = layout; - newFile.replace("{{ content }}", readfile(file.path().c_str())); - fs::create_directories("output" + file.path().parent_path().string().substr(6)); - std::ofstream outfile("output" + file.path().string().substr(6)); - outfile << newFile; - outfile.close(); - } + if (file.path().stem().string().at(0) == '_') + continue; + std::string ext = file.path().extension().string(); + fs::create_directories("output" + file.path().parent_path().string().substr(6)); + std::ofstream outfile("output" + file.path().string().substr(6)); + if (ext == ".html") + outfile << layout.replace("{{ content }}", readfile(file.path().c_str())); + else + outfile << read(file.path().c_str()); + outfile.close(); } } }