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).
This commit is contained in:
luca0N! 2023-12-01 07:57:00 -03:00
parent 7ecb76e493
commit ec1f40e86c
Signed by: luca0N
GPG Key ID: 5978D960572B449E
1 changed files with 11 additions and 13 deletions

View File

@ -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 <iostream>
#include <fstream>
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;
}