cleanup: moved functions to separate files.

This commit is contained in:
Kaley, Fischer 2024-10-09 21:40:34 +02:00
parent 988b785193
commit ba3bcb622b
6 changed files with 84 additions and 48 deletions

19
src/ConfigManager.cpp Normal file
View file

@ -0,0 +1,19 @@
#include <cctype>
#include <cpptoml.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <string>
#include "includes/ConfigManager.hpp"
#include "includes/Strings.hpp"
void from_toml(const cpptoml::table &t, Action &a) {
try {
a.names = *t.get_as<std::string>("names");
a.description = *t.get_as<std::string>("description");
a.command = *t.get_as<std::string>("command");
} catch (const cpptoml::parse_exception &e) {
throw std::invalid_argument(invalidvalue.c_str());
}
}

View file

@ -0,0 +1,14 @@
#ifndef CONFIGMANAGER_H
#define CONFIGMANAGER_H
#include <cpptoml.h>
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

14
src/includes/Strings.hpp Normal file
View file

@ -0,0 +1,14 @@
#pragma once
#include <string>
#include <string_view>
const std::string_view USAGE = R"#(usage: RapidMenu [flags] [<command> [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: )#";

5
src/includes/utils.hpp Normal file
View file

@ -0,0 +1,5 @@
#ifndef UTILS_H
#define UTILS_H
void clearBuffer();
void checkIfExtractionFailed();
#endif

View file

@ -5,54 +5,12 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <ios>
#include <iostream> #include <iostream>
#include <limits>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include "includes/ConfigManager.hpp"
void clearBuffer() { #include "includes/Strings.hpp"
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); #include "includes/utils.hpp"
}
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] [<command> [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<std::string>("names");
a.description = *t.get_as<std::string>("description");
a.command = *t.get_as<std::string>("command");
} catch (const cpptoml::parse_exception &e) {
throw std::invalid_argument(invalidvalue.c_str());
}
}
int main(int argc, char **argv, char **envp) { int main(int argc, char **argv, char **envp) {
@ -89,7 +47,7 @@ int main(int argc, char **argv, char **envp) {
return 1; return 1;
} else if (ARGS[i] == "-c") { } else if (ARGS[i] == "-c") {
if (argc < 3 || argv[2][0] == '-') { if (argc < 3 || argv[2][0] == '-') {
std::cerr << USAGE.c_str(); std::cerr << USAGE;
return 1; return 1;
} }
const char *configFile = nullptr; const char *configFile = nullptr;
@ -111,7 +69,7 @@ int main(int argc, char **argv, char **envp) {
reversedNamesList.push_back(a.names); reversedNamesList.push_back(a.names);
} catch (const std::invalid_argument &e) { } catch (const std::invalid_argument &e) {
std::cerr << invalidvalue.c_str() << e.what(); std::cerr << invalidvalue << e.what();
return 1; return 1;
} }
} }
@ -170,7 +128,7 @@ int main(int argc, char **argv, char **envp) {
break; break;
} else if (ARGS[i] == "-b") { } else if (ARGS[i] == "-b") {
if (argc < 3 || argv[2][0] == '-') { if (argc < 3 || argv[2][0] == '-') {
std::cerr << USAGE.c_str(); std::cerr << USAGE;
return 1; return 1;
} }
// executable // executable

26
src/utils.cpp Normal file
View file

@ -0,0 +1,26 @@
#include <ios>
#include <limits>
#include <cctype>
#include <cpptoml.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ios>
#include <iostream>
#include <limits>
// clearBuffer
void clearBuffer() {
std::cin.ignore(std::numeric_limits<std::streamsize>::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
}