reorganized the project, some idea of how to handle settings

This commit is contained in:
Blue 2023-10-06 18:39:07 -03:00
parent e4cc5e8d0e
commit 870842f63d
Signed by: blue
GPG Key ID: 9B203B252A63EE38
17 changed files with 167 additions and 26 deletions

View File

@ -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

22
src/CMakeLists.txt Normal file
View File

@ -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})

View File

@ -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() {
}

View File

View File

@ -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();

84
src/settings.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "settings.h"
constexpr std::array<std::string_view, Settings::_actionsSize> 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<Settings::Action>(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;
}

39
src/settings.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include <string>
#include <string_view>
#include <optional>
#include <vector>
#include <array>
#include <algorithm>
#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<std::string_view> arguments;
std::optional<Action> action;
std::optional<std::string> input;
std::optional<std::string> output;
std::optional<Loggable::Severity> logLevel;
};