From 6f90babd8eefa2bf3631d6f5b6c14b454a1d5c60 Mon Sep 17 00:00:00 2001 From: Thomaltarix Date: Mon, 26 Feb 2024 14:56:11 +0100 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=A9=B9=20Fix=20the=20regexes=20to=20a?= =?UTF-8?q?ccept=20spaces=20and=20tabs=20before=20lines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FileContainer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/FileContainer.cpp b/src/FileContainer.cpp index 84c39ef..bbc267e 100644 --- a/src/FileContainer.cpp +++ b/src/FileContainer.cpp @@ -130,7 +130,7 @@ void nts::FileContainer::extractChipsetsAndLinks(const std::string &content) void nts::FileContainer::fillChipsets(std::string &str) { - std::regex reg("^([a-zA-Z0-9]+)\\s+(\\w+)(\\s+)?"); + std::regex reg("^\\s*([a-zA-Z0-9]+)\\s+(\\w+)\\s*(#.*)?$"); std::smatch match2; char *token; std::string str2; @@ -153,7 +153,7 @@ void nts::FileContainer::fillChipsets(std::string &str) void nts::FileContainer::fillLinks(std::string &str) { - std::regex reg("^([a-zA-Z0-9_]+)((\\s+)?:(\\s+)?)([0-9]+)\\s+([a-zA-Z0-9_]+)((\\s+)?:(\\s+)?)([0-9]+)( +)?"); + std::regex reg("^\\s*([a-zA-Z0-9_]+)\\s*:\\s*([0-9]+)\\s+([a-zA-Z0-9_]+)\\s*:\\s*([0-9]+)\\s*(#.*)?$"); std::smatch match2; char *token; std::string str2; @@ -165,7 +165,7 @@ void nts::FileContainer::fillLinks(std::string &str) while (token != NULL) { str2 = std::string(token); if (std::regex_search(str2, match2, reg)) { - str2 = match2[1].str() + " " + match2[5].str() + " " + match2[6].str() + " " + match2[10].str(); + str2 = match2[1].str() + " " + match2[2].str() + " " + match2[3].str() + " " + match2[4].str(); this->_links.push_back(str2); } else { throw nts::Error("Invalid file format."); From ecb6a965a9753400dce70113f1a6e8e7e1d51695 Mon Sep 17 00:00:00 2001 From: Thomaltarix Date: Mon, 26 Feb 2024 14:56:41 +0100 Subject: [PATCH 2/4] :bug: Fix the type checker in the chipsets parsing --- src/FileContainer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FileContainer.cpp b/src/FileContainer.cpp index bbc267e..12abe3d 100644 --- a/src/FileContainer.cpp +++ b/src/FileContainer.cpp @@ -71,7 +71,7 @@ void nts::FileContainer::buildMap(ComponentFactory &factory) name = strtok(NULL, " "); if (name == NULL || type == NULL) throw nts::Error("Invalid file format."); - if (!factory.isRegistered(name)) + if (!factory.isRegistered(type)) throw nts::Error("Component type does not exist."); if (this->_pins.find(name) != this->_pins.end()) throw nts::Error("Component name already exists."); From cc8b7fa4e9021b91e7f1c24c2a78f9b827073c61 Mon Sep 17 00:00:00 2001 From: Thomaltarix Date: Mon, 26 Feb 2024 15:15:50 +0100 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=A5=85=20Catch=20all=20memory=20leaks?= =?UTF-8?q?=20of=20the=20project?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ComponentFactory.cpp | 7 ++++++- src/FileContainer.cpp | 8 ++++++++ src/FileContainer.hpp | 2 +- src/components/ComposedComponent.cpp | 6 ++++++ src/components/ComposedComponent.hpp | 2 +- 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/ComponentFactory.cpp b/src/ComponentFactory.cpp index 59fb835..cc69cd0 100644 --- a/src/ComponentFactory.cpp +++ b/src/ComponentFactory.cpp @@ -13,7 +13,12 @@ namespace nts { _components = std::unordered_map(); } - ComponentFactory::~ComponentFactory() = default; + ComponentFactory::~ComponentFactory() + { + for (auto &component : _components) { + delete component.second; + } + } void ComponentFactory::registerComponent(const std::string &name, IComponent *component) { diff --git a/src/FileContainer.cpp b/src/FileContainer.cpp index 12abe3d..f09ddea 100644 --- a/src/FileContainer.cpp +++ b/src/FileContainer.cpp @@ -21,6 +21,13 @@ nts::FileContainer::FileContainer(const std::string &filename) this->_pins = std::unordered_map(); } +nts::FileContainer::~FileContainer() +{ + for (auto &pin : this->_pins) { + delete pin.second; + } +} + void nts::FileContainer::extractFileContent() { ssize_t count; @@ -44,6 +51,7 @@ void nts::FileContainer::extractFileContent() content = removeComments(content); this->extractChipsetsAndLinks(content); close(fd); + delete[] buffer; } std::vector nts::FileContainer::getChipsets(void) const diff --git a/src/FileContainer.hpp b/src/FileContainer.hpp index 3d11d67..2fe554d 100644 --- a/src/FileContainer.hpp +++ b/src/FileContainer.hpp @@ -19,7 +19,7 @@ namespace nts class FileContainer { public: FileContainer(const std::string &filename); - ~FileContainer() = default; + ~FileContainer(); void extractFileContent(); std::vector getChipsets(void) const; diff --git a/src/components/ComposedComponent.cpp b/src/components/ComposedComponent.cpp index 47ad35c..889a80f 100644 --- a/src/components/ComposedComponent.cpp +++ b/src/components/ComposedComponent.cpp @@ -13,3 +13,9 @@ using namespace nts::Components; ComposedComponent::ComposedComponent(std::size_t nbPins, std::size_t nbInternals) : AbstractComponent(nbPins) { this->_internal.reserve(nbInternals); } + +ComposedComponent::~ComposedComponent() { + for (auto &internal : this->_internal) { + delete internal.second; + } +} \ No newline at end of file diff --git a/src/components/ComposedComponent.hpp b/src/components/ComposedComponent.hpp index 0dbd560..a6f6e13 100644 --- a/src/components/ComposedComponent.hpp +++ b/src/components/ComposedComponent.hpp @@ -17,6 +17,6 @@ namespace nts::Components { ComposedComponent(std::size_t nbPins, std::size_t nbInternals); public: - ~ComposedComponent() override = default; + ~ComposedComponent() override; }; } From de7c6db061974e48c095e208933b0bbe94ea9bac Mon Sep 17 00:00:00 2001 From: Thomaltarix Date: Mon, 26 Feb 2024 20:18:06 +0100 Subject: [PATCH 4/4] :bug: Fix a bad timing bug with CTRL+C in the loop --- src/Simulation.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Simulation.cpp b/src/Simulation.cpp index bf59e01..7c41bc3 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -123,9 +123,10 @@ void nts::Simulation::loop() this->_loop = true; handleLoop(this); while (this->_loop) { + sigaction(SIGINT, &sigIntHandler, NULL); simulate(); - display(); sigaction(SIGINT, &sigIntHandler, NULL); + display(); } sigIntHandler.sa_handler = SIG_DFL; sigaction(SIGINT, &sigIntHandler, NULL);