You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.7 KiB
C++

#include "application.h"
#include "output_util.h"
#include "filepath_util.h"
#include <cstring>
#include <cctype>
/////////////////////////////////////////////////////////////////////////
static constexpr int NO_ARGC = 1;
static constexpr int MIN_ARGC = 2;
static constexpr int MAX_ARGC = 4;
static constexpr int DEFAULT_SPLITTING = 5;
static const std::string DEFAULT_PATH = "."; // current folder, I guess
/////////////////////////////////////////////////////////////////////////
static std::tuple<int, int, std::string> error(const char* msg)
{
std::cout << msg;
return {EXIT_FAILURE, -1, {}};
}
static std::tuple<int, int, std::string> parseInput(int argc, char **argv)
{
int splitting = DEFAULT_SPLITTING;
std::string path = DEFAULT_PATH;
switch (argc)
{
default:
return error(output::NO_ARG_MSG);
case NO_ARGC:
{
// Just launch the application with default parameters
std::cout << "No arguments given. Launching at \"" << DEFAULT_PATH << "\" with splitting " << std::to_string(DEFAULT_SPLITTING) << "\n"
<< "--help for more information.\n";
const auto &[ret_code, ret_path] = filepath::parsePath(DEFAULT_PATH);
if (ret_code)
return error(output::IMG_FAIL_MSG);
path = ret_path;
break;
}
case MIN_ARGC:
{
// maybe --help
if (strcmp(argv[1], output::HELP_FLAG) == 0)
return error(output::HELP_MSG);
const auto &[ret_code, ret_path] = filepath::parsePath(argv[1]);
if (ret_code)
return error(output::IMG_FAIL_MSG);
path = ret_path;
break;
}
case (MAX_ARGC - 1):
case MAX_ARGC:
{
// full stack
if (strcmp(argv[1], output::SPLITTING_FLAG) == 0)
{
splitting = -1; // here assuming user is providing it on their own
if (std::isdigit(*argv[2]))
splitting = std::stoi(argv[2]);
if (splitting < 2)
return error(output::SPLITTING_MSG);
const auto &[ret_code, ret_path] = filepath::parsePath(argc == MAX_ARGC ? argv[3] : DEFAULT_PATH);
if (ret_code)
return error(output::IMG_FAIL_MSG);
path = ret_path;
}
break;
}
}
return {EXIT_SUCCESS, splitting, path};
}
/////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
const auto&[ret_code, splitting, path] = parseInput(argc, argv);
if (ret_code) // Error code is EXIT_FAILURE
return ret_code;
Application app;
if (app.init(path, splitting))
{
app.run();
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}