From 870842f63d58b2d24aa2bcaa5f2030e16ad115b9 Mon Sep 17 00:00:00 2001 From: blue Date: Fri, 6 Oct 2023 18:39:07 -0300 Subject: [PATCH] reorganized the project, some idea of how to handle settings --- CMakeLists.txt | 12 +--- src/CMakeLists.txt | 22 +++++++ collection.cpp => src/collection.cpp | 7 +-- collection.h => src/collection.h | 0 decoded.cpp => src/decoded.cpp | 0 decoded.h => src/decoded.h | 0 flactomp3.cpp => src/flactomp3.cpp | 0 flactomp3.h => src/flactomp3.h | 0 help.cpp => src/help.cpp | 0 help.h => src/help.h | 0 loggable.cpp => src/loggable.cpp | 0 loggable.h => src/loggable.h | 0 main.cpp => src/main.cpp | 29 +++++---- src/settings.cpp | 84 ++++++++++++++++++++++++++ src/settings.h | 39 ++++++++++++ taskmanager.cpp => src/taskmanager.cpp | 0 taskmanager.h => src/taskmanager.h | 0 17 files changed, 167 insertions(+), 26 deletions(-) create mode 100644 src/CMakeLists.txt rename collection.cpp => src/collection.cpp (99%) rename collection.h => src/collection.h (100%) rename decoded.cpp => src/decoded.cpp (100%) rename decoded.h => src/decoded.h (100%) rename flactomp3.cpp => src/flactomp3.cpp (100%) rename flactomp3.h => src/flactomp3.h (100%) rename help.cpp => src/help.cpp (100%) rename help.h => src/help.h (100%) rename loggable.cpp => src/loggable.cpp (100%) rename loggable.h => src/loggable.h (100%) rename main.cpp => src/main.cpp (55%) create mode 100644 src/settings.cpp create mode 100644 src/settings.h rename taskmanager.cpp => src/taskmanager.cpp (100%) rename taskmanager.h => src/taskmanager.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36d71b4..6bee5d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,15 +19,9 @@ find_package(JPEG REQUIRED) pkg_check_modules(LAME REQUIRED IMPORTED_TARGET lame) pkg_check_modules(TAGLIB REQUIRED IMPORTED_TARGET taglib) -add_executable(mlc - main.cpp - help.cpp - decoded.cpp - flactomp3.cpp - collection.cpp - taskmanager.cpp - loggable.cpp -) +add_executable(mlc) + +add_subdirectory(src) target_link_libraries(mlc FLAC::FLAC diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..baca389 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,22 @@ +set(SOURCES + main.cpp + help.cpp + decoded.cpp + flactomp3.cpp + collection.cpp + taskmanager.cpp + loggable.cpp + settings.cpp +) + +set(HEADERS + help.h + decoded.h + flactomp3.h + collection.h + taskmanager.h + loggable.h + settings.h +) + +target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) diff --git a/collection.cpp b/src/collection.cpp similarity index 99% rename from collection.cpp rename to src/collection.cpp index cc2868d..39bee9b 100644 --- a/collection.cpp +++ b/src/collection.cpp @@ -11,17 +11,14 @@ Collection::Collection(const std::string& path, TaskManager* tm) : countMusical(0), counted(false), taskManager(tm) -{ -} +{} Collection::Collection(const std::filesystem::path& path, TaskManager* tm): path(fs::canonical(path)), countMusical(0), counted(false), taskManager(tm) -{ -} - +{} Collection::~Collection() { } diff --git a/collection.h b/src/collection.h similarity index 100% rename from collection.h rename to src/collection.h diff --git a/decoded.cpp b/src/decoded.cpp similarity index 100% rename from decoded.cpp rename to src/decoded.cpp diff --git a/decoded.h b/src/decoded.h similarity index 100% rename from decoded.h rename to src/decoded.h diff --git a/flactomp3.cpp b/src/flactomp3.cpp similarity index 100% rename from flactomp3.cpp rename to src/flactomp3.cpp diff --git a/flactomp3.h b/src/flactomp3.h similarity index 100% rename from flactomp3.h rename to src/flactomp3.h diff --git a/help.cpp b/src/help.cpp similarity index 100% rename from help.cpp rename to src/help.cpp diff --git a/help.h b/src/help.h similarity index 100% rename from help.h rename to src/help.h diff --git a/loggable.cpp b/src/loggable.cpp similarity index 100% rename from loggable.cpp rename to src/loggable.cpp diff --git a/loggable.h b/src/loggable.h similarity index 100% rename from loggable.h rename to src/loggable.h diff --git a/main.cpp b/src/main.cpp similarity index 55% rename from main.cpp rename to src/main.cpp index a002979..6b2d6de 100644 --- a/main.cpp +++ b/src/main.cpp @@ -9,27 +9,32 @@ #include "help.h" #include "collection.h" #include "taskmanager.h" +#include "settings.h" int main(int argc, char **argv) { - if (argc < 3) { - std::cout << "Insufficient amount of arguments, launch with \"--help\" argument to see usage" << std::endl; - return 1; - } + Settings settings(argc, argv); - const std::string firstArgument(argv[1]); - if (firstArgument == "--help") { - printHelp(); - return 0; + switch (settings.getAction()) { + case Settings::help: + printHelp(); + return 0; + case Settings::printConfig: + //printHelp(); + return 0; + case Settings::convert: + std::cout << "Converting..." << std::endl; + break; + default: + std::cout << "Error in action" << std::endl; + return -1; } - const std::string secondArgument(argv[2]); - TaskManager taskManager; taskManager.start(); std::chrono::time_point start = std::chrono::system_clock::now(); - Collection collection(firstArgument, &taskManager); - collection.convert(secondArgument); + Collection collection(settings.getInput(), &taskManager); + collection.convert(settings.getOutput()); taskManager.printProgress(); taskManager.wait(); diff --git a/src/settings.cpp b/src/settings.cpp new file mode 100644 index 0000000..a524081 --- /dev/null +++ b/src/settings.cpp @@ -0,0 +1,84 @@ +#include "settings.h" + +constexpr std::array actions({ + "convert", + "help", + "printConfig" +}); + +Settings::Settings(int argc, char ** argv): + arguments(), + action(std::nullopt), + input(std::nullopt), + output(std::nullopt), + logLevel(std::nullopt) +{ + for (int i = 1; i < argc; ++i) + arguments.push_back(argv[i]); + + parseArguments(); +} + +void Settings::parseArguments() { + for (int i = 0; i < arguments.size(); ++i) { + const std::string_view& arg = arguments[i]; + if (i == 0) { + std::string_view act = stripFlags(arg); + int dist = std::distance(actions.begin(), std::find(actions.begin(), actions.end(), act)); + if (dist < _actionsSize) { + action = static_cast(dist); + continue; + } + } + + if (getAction() == convert) { + if (!input.has_value()) { + input = arg; + continue; + } + + if (!output.has_value()) { + input = arg; + continue; + } + } + } +} + +Settings::Action Settings::getAction() const { + if (action.has_value()) + return action.value(); + else + return convert; +} + +std::string Settings::getInput() const { + if (input.has_value()) + return input.value(); + else + return ""; +} + +std::string Settings::getOutput() const { + if (output.has_value()) + return output.value(); + else + return ""; +} + +Loggable::Severity Settings::getLogLevel() const { + if (logLevel.has_value()) + return logLevel.value(); + else + return Loggable::info; +} + +std::string_view Settings::stripFlags(const std::string_view& option) { + if (option[0] == '-') { + if (option[1] == '-') + return option.substr(2); + + return option.substr(1); + } + return option; +} diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..ab84029 --- /dev/null +++ b/src/settings.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "loggable.h" + +class Settings { +public: + enum Action { + convert, + help, + printConfig, + _actionsSize + }; + + Settings(int argc, char **argv); + + std::string getInput() const; + std::string getOutput() const; + Loggable::Severity getLogLevel() const; + Action getAction() const; + +private: + void parseArguments(); + + static std::string_view stripFlags(const std::string_view& option); + +private: + std::vector arguments; + std::optional action; + std::optional input; + std::optional output; + std::optional logLevel; +}; diff --git a/taskmanager.cpp b/src/taskmanager.cpp similarity index 100% rename from taskmanager.cpp rename to src/taskmanager.cpp diff --git a/taskmanager.h b/src/taskmanager.h similarity index 100% rename from taskmanager.h rename to src/taskmanager.h