From ba3bcb622b47856e4c3f6e71690ed5c6bfc3efd2 Mon Sep 17 00:00:00 2001 From: DRAGONTOS Date: Wed, 9 Oct 2024 21:40:34 +0200 Subject: [PATCH] cleanup: moved functions to separate files. --- src/ConfigManager.cpp | 19 ++++++++++++ src/includes/ConfigManager.hpp | 14 +++++++++ src/includes/Strings.hpp | 14 +++++++++ src/includes/utils.hpp | 5 ++++ src/main.cpp | 54 ++++------------------------------ src/utils.cpp | 26 ++++++++++++++++ 6 files changed, 84 insertions(+), 48 deletions(-) create mode 100644 src/ConfigManager.cpp create mode 100644 src/includes/ConfigManager.hpp create mode 100644 src/includes/Strings.hpp create mode 100644 src/includes/utils.hpp create mode 100644 src/utils.cpp diff --git a/src/ConfigManager.cpp b/src/ConfigManager.cpp new file mode 100644 index 0000000..e230cec --- /dev/null +++ b/src/ConfigManager.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include +#include +#include +#include "includes/ConfigManager.hpp" +#include "includes/Strings.hpp" + +void from_toml(const cpptoml::table &t, Action &a) { + try { + a.names = *t.get_as("names"); + a.description = *t.get_as("description"); + a.command = *t.get_as("command"); + } catch (const cpptoml::parse_exception &e) { + throw std::invalid_argument(invalidvalue.c_str()); + } +} diff --git a/src/includes/ConfigManager.hpp b/src/includes/ConfigManager.hpp new file mode 100644 index 0000000..86b8b6e --- /dev/null +++ b/src/includes/ConfigManager.hpp @@ -0,0 +1,14 @@ +#ifndef CONFIGMANAGER_H +#define CONFIGMANAGER_H +#include + +struct Action { + std::string names; + std::string description; + std::string command; + + Action(const std::string &nms = "", const std::string &desc = "", const std::string &cmd = "") : names(nms), description(desc), command(cmd) {} +}; + +void from_toml(const cpptoml::table &t, Action &a); +#endif diff --git a/src/includes/Strings.hpp b/src/includes/Strings.hpp new file mode 100644 index 0000000..86eb0e1 --- /dev/null +++ b/src/includes/Strings.hpp @@ -0,0 +1,14 @@ +#pragma once +#include +#include + +const std::string_view USAGE = R"#(usage: RapidMenu [flags] [ [args]] +LISTING COMMANDS: + -c: To specify which config to use. + -b: Make a executable out of a config. +)#"; + +const std::string invalidvalue = R"#(Invalid value in config: )#"; +const std::string invalidconfig = R"#(Not a valid config: )#"; + + diff --git a/src/includes/utils.hpp b/src/includes/utils.hpp new file mode 100644 index 0000000..29ea2bf --- /dev/null +++ b/src/includes/utils.hpp @@ -0,0 +1,5 @@ +#ifndef UTILS_H +#define UTILS_H +void clearBuffer(); +void checkIfExtractionFailed(); +#endif diff --git a/src/main.cpp b/src/main.cpp index 81d4176..f4c786e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,54 +5,12 @@ #include #include #include -#include #include -#include #include #include - -void clearBuffer() { - std::cin.ignore(std::numeric_limits::max(), '\n'); -} - -inline void checkIfExtractionFailed() { - if (!std::cin) { // if previous extraction failed - if (std::cin.eof()) { // check if eof and if yes aborts the program - std::abort(); - } - - std::cin.clear(); // put std::cin back into normal mode - } - clearBuffer(); // remove bad input -} - -const std::string USAGE = R"#(usage: RapidMenu [flags] [ [args]] -LISTING COMMANDS: - -c: To specify which config to use. - -b: Make a executable out of a config. -)#"; - -const std::string invalidvalue = R"#(Invalid value in config: )#"; -const std::string invalidconfig = R"#(Not a valid config: )#"; - -struct Action { - std::string names; - std::string description; - std::string command; - - Action(const std::string &nms = "", const std::string &desc = "", - const std::string &cmd = "") : names(nms), description(desc), command(cmd) {} -}; - -void from_toml(const cpptoml::table &t, Action &a) { - try { - a.names = *t.get_as("names"); - a.description = *t.get_as("description"); - a.command = *t.get_as("command"); - } catch (const cpptoml::parse_exception &e) { - throw std::invalid_argument(invalidvalue.c_str()); - } -} +#include "includes/ConfigManager.hpp" +#include "includes/Strings.hpp" +#include "includes/utils.hpp" int main(int argc, char **argv, char **envp) { @@ -89,7 +47,7 @@ int main(int argc, char **argv, char **envp) { return 1; } else if (ARGS[i] == "-c") { if (argc < 3 || argv[2][0] == '-') { - std::cerr << USAGE.c_str(); + std::cerr << USAGE; return 1; } const char *configFile = nullptr; @@ -111,7 +69,7 @@ int main(int argc, char **argv, char **envp) { reversedNamesList.push_back(a.names); } catch (const std::invalid_argument &e) { - std::cerr << invalidvalue.c_str() << e.what(); + std::cerr << invalidvalue << e.what(); return 1; } } @@ -170,7 +128,7 @@ int main(int argc, char **argv, char **envp) { break; } else if (ARGS[i] == "-b") { if (argc < 3 || argv[2][0] == '-') { - std::cerr << USAGE.c_str(); + std::cerr << USAGE; return 1; } // executable diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..a92b505 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// clearBuffer +void clearBuffer() { + std::cin.ignore(std::numeric_limits::max(), '\n'); +} + +void checkIfExtractionFailed() { + if (!std::cin) { // if previous extraction failed + if (std::cin.eof()) { // check if eof and if yes aborts the program + std::abort(); + } + + std::cin.clear(); // put std::cin back into normal mode + } + clearBuffer(); // remove bad input +}