From 83ec72e80ca8498638b697cbed0025af7d6d218c Mon Sep 17 00:00:00 2001 From: luca0N! Date: Fri, 1 Dec 2023 06:50:55 -0300 Subject: [PATCH 1/2] 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/2] 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