From ec1f40e86c41e26f101e12f6d9eeff71e775229e Mon Sep 17 00:00:00 2001 From: luca0N! Date: Fri, 1 Dec 2023 07:57:00 -0300 Subject: [PATCH] Perform minor code refactoring Use std::getline in the template-loading function instead of reading it using the C I/O library (also bump the copyright years). --- src/Common.cxx | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) 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; }