diff --git a/src/Common.cxx b/src/Common.cxx index a54dbe5..a8315f8 100644 --- a/src/Common.cxx +++ b/src/Common.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 luca0N! + * Copyright (C) 2022-2023 luca0N! * * This file is part of Static Website Generator (swg). * @@ -22,6 +22,7 @@ #include "Common.hxx" #include +#include const char* HR_MONTH[] = { "January", @@ -72,25 +73,22 @@ bool is_special_file(std::string const &filename) { } std::string get_template(std::string const &path) { + std::string filePath = path; // Template lookup - std::string stPath = path; - stPath += "/__swg_template.html"; - FILE *swgTemplate = fopen(stPath.c_str(), "r"); - if (swgTemplate == NULL) { - std::cerr << "error: couldn't open the SWG HTML template file; does it exist?\n"; - perror(stPath.c_str()); + filePath += "/__swg_template.html"; + if (!std::filesystem::exists(filePath)) { + std::cerr << "error: couldn't find the SWG HTML template file.\n"; exit(RETURN_FAILED_INVALID_DIRECTORY); } - // Check for content placeholder - int buflen = 8; - char buf[buflen]; std::string htmlTemplate; - while (fgets(buf, buflen, swgTemplate) != NULL) { - htmlTemplate += buf; + std::ifstream swgTemplate(filePath); + for (std::string line; std::getline(swgTemplate, line);) { + htmlTemplate += line; + if (!swgTemplate.eof()) + htmlTemplate += '\n'; } - fclose(swgTemplate); if (verbose) std::cout << "Loaded HTML template into memory.\n"; return htmlTemplate; }