From 83ec72e80ca8498638b697cbed0025af7d6d218c Mon Sep 17 00:00:00 2001 From: luca0N! Date: Fri, 1 Dec 2023 06:50:55 -0300 Subject: [PATCH 1/5] Perform ConfigUtils code refactoring Instead of manually reading lines and storing them on memory, parse lines while reading them on the fly using std::fstream (also bump the source file copyright years). --- src/ConfigUtils.cxx | 52 +++++---------------------------------------- 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/src/ConfigUtils.cxx b/src/ConfigUtils.cxx index c0d2436..d746ab6 100644 --- a/src/ConfigUtils.cxx +++ b/src/ConfigUtils.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 luca0N! + * Copyright (C) 2022-2023 luca0N! * * This file is part of Static Website Generator (swg). * @@ -22,57 +22,13 @@ #include "ConfigUtils.hxx" #include +#include #include #include #include "Common.hxx" void parse_config(SwgContext *ctx, std::string const &path) { - std::string ctxCfgPath = path; - ctxCfgPath += "/swg.cfg"; - - FILE *ctxCfgFile = fopen(ctxCfgPath.c_str(), "r"); - - if (ctxCfgFile == NULL) { - std::cerr << "error: cannot open swg.cfg file, are you sure " - << path << " is a valid SWG directory?" << std::endl; - perror(ctxCfgPath.c_str()); - exit(RETURN_FAILED_INVALID_DIRECTORY); - } - - int buflen = 16; - char cbuf[buflen]; - - // Yes, this looks ugly, but it works for now. Will probably optimize - // this in the future :) - // - // TODO: Optimize config file interpretation - - std::list lines = { "" }; - while (fgets(cbuf, buflen, ctxCfgFile) != NULL) { - int eol = -1; - for (int c = 0; c < buflen; c++) { - if (cbuf[c] == '\0') { - break; - } else if (cbuf[c] == '\n') { - eol = c; - break; - } - } - if (eol > -1) { - cbuf[eol] = '\0'; - *(--lines.end()) += cbuf; - lines.insert(lines.end(), ""); - } else - *(--lines.end()) += cbuf; - } - - fclose(ctxCfgFile); - - std::cout << "Done loading configuration file in memory" << std::endl; - - // Parse configuration file - enum ConfigNamespace { General, Blogs }; ConfigNamespace ns; @@ -81,7 +37,9 @@ void parse_config(SwgContext *ctx, std::string const &path) { // currentBlog->name = "asd"; - for (std::string line : lines) { + std::ifstream cfg_stream(path); + // Parse configuration file + for (std::string line; std::getline(cfg_stream, line);) { // Skip comment lines if (line.c_str()[0] == '#') continue; From bc843ffd4a208fd2f482435c6e909eaaaab7336d Mon Sep 17 00:00:00 2001 From: luca0N! Date: Fri, 1 Dec 2023 06:52:12 -0300 Subject: [PATCH 2/5] Remove unused comments --- src/ConfigUtils.cxx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ConfigUtils.cxx b/src/ConfigUtils.cxx index d746ab6..fa616b1 100644 --- a/src/ConfigUtils.cxx +++ b/src/ConfigUtils.cxx @@ -33,9 +33,7 @@ void parse_config(SwgContext *ctx, std::string const &path) { ConfigNamespace ns; int blogCount = 0; - Blog *currentBlog = NULL; // (Blog*) malloc(sizeof(Blog)); - - // currentBlog->name = "asd"; + Blog *currentBlog = NULL; std::ifstream cfg_stream(path); // Parse configuration file From 7ecb76e493771f3d7fe46614973298fe81511a11 Mon Sep 17 00:00:00 2001 From: luca0N! Date: Fri, 1 Dec 2023 06:57:35 -0300 Subject: [PATCH 3/5] Remove outdated references Remove outdated references to ongoing website development (also bump the copyright years). --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 399f59b..2e91934 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ # Static Website Generator -Static Website Generator (swg) is a utility currently under development that -does exactly what's written on the tin. This utility aims to be a simple yet -painless program to automate the boring work of static website development. +Static Website Generator (swg) is a lightweight utility that does exactly +what's written on the tin. This utility aims to be a simple yet painless +program to automate the boring work of static website development. -This utility was created originally for the (currenly ongoing) development of -version 3 of the luca0N! website. +This utility was created originally for the development of version 3 of the +luca0N! website. -Static Website Generator is currently under development, and production usage -is not advised. Expect bugs and bad unoptimized performance. +Static Website Generator is very limited and is not intended for anything other +than simple static websites. Some features are still in the works. This is free software. You are allowed to modify, redistribute and distribute modified versions of this software under the terms of the GNU Lesser General @@ -21,4 +21,4 @@ Coming soon!
-Copyright (C) 2022 luca0N! +Copyright © 2022–2023 luca0N! From ec1f40e86c41e26f101e12f6d9eeff71e775229e Mon Sep 17 00:00:00 2001 From: luca0N! Date: Fri, 1 Dec 2023 07:57:00 -0300 Subject: [PATCH 4/5] 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; } From cd4911519a689bb887c864a33e83c7a2387e3876 Mon Sep 17 00:00:00 2001 From: luca0N! Date: Fri, 1 Dec 2023 07:59:14 -0300 Subject: [PATCH 5/5] Rename variable to maintain consistency --- src/ConfigUtils.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ConfigUtils.cxx b/src/ConfigUtils.cxx index fa616b1..7395855 100644 --- a/src/ConfigUtils.cxx +++ b/src/ConfigUtils.cxx @@ -35,9 +35,9 @@ void parse_config(SwgContext *ctx, std::string const &path) { int blogCount = 0; Blog *currentBlog = NULL; - std::ifstream cfg_stream(path); + std::ifstream cfgStream(path); // Parse configuration file - for (std::string line; std::getline(cfg_stream, line);) { + for (std::string line; std::getline(cfgStream, line);) { // Skip comment lines if (line.c_str()[0] == '#') continue;