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:
parent
7ecb76e493
commit
ec1f40e86c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2022 luca0N!
|
* Copyright (C) 2022-2023 luca0N!
|
||||||
*
|
*
|
||||||
* This file is part of Static Website Generator (swg).
|
* This file is part of Static Website Generator (swg).
|
||||||
*
|
*
|
||||||
|
@ -22,6 +22,7 @@
|
||||||
#include "Common.hxx"
|
#include "Common.hxx"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
const char* HR_MONTH[] = {
|
const char* HR_MONTH[] = {
|
||||||
"January",
|
"January",
|
||||||
|
@ -72,25 +73,22 @@ bool is_special_file(std::string const &filename) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_template(std::string const &path) {
|
std::string get_template(std::string const &path) {
|
||||||
|
std::string filePath = path;
|
||||||
// Template lookup
|
// Template lookup
|
||||||
std::string stPath = path;
|
filePath += "/__swg_template.html";
|
||||||
stPath += "/__swg_template.html";
|
if (!std::filesystem::exists(filePath)) {
|
||||||
FILE *swgTemplate = fopen(stPath.c_str(), "r");
|
std::cerr << "error: couldn't find the SWG HTML template file.\n";
|
||||||
if (swgTemplate == NULL) {
|
|
||||||
std::cerr << "error: couldn't open the SWG HTML template file; does it exist?\n";
|
|
||||||
perror(stPath.c_str());
|
|
||||||
exit(RETURN_FAILED_INVALID_DIRECTORY);
|
exit(RETURN_FAILED_INVALID_DIRECTORY);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for content placeholder
|
|
||||||
int buflen = 8;
|
|
||||||
char buf[buflen];
|
|
||||||
std::string htmlTemplate;
|
std::string htmlTemplate;
|
||||||
while (fgets(buf, buflen, swgTemplate) != NULL) {
|
std::ifstream swgTemplate(filePath);
|
||||||
htmlTemplate += buf;
|
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";
|
if (verbose) std::cout << "Loaded HTML template into memory.\n";
|
||||||
return htmlTemplate;
|
return htmlTemplate;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue