Renamed CmdArgs to AppSettings

This commit is contained in:
Egor 2024-10-27 18:53:34 +03:00
parent 8f18f9ef8f
commit d7bcbbfd08
3 changed files with 29 additions and 27 deletions

View file

@ -1,26 +1,26 @@
#include "filetape.h"
#include "tape_config.h"
#include "tape_util.h"
#include <iostream>
int main(int argc, char *argv[]) {
CmdArgs cmd = parse_command_line(argc, argv);
if (cmd.version) {
AppSettings settings = parse_command_line(argc, argv);
if (settings.version) {
std::cout << VERSION_MSG << std::endl;
return 0;
}
if (cmd.help) {
if (settings.help) {
std::cerr << HELP_MSG << std::endl;
return 0;
}
tape::FileTape input(cmd.input_file_name, cmd.settings);
tape::FileTape output(input, cmd.output_file_name, cmd.settings);
tape::FileTape input(settings.input_file_name, settings.ft_settings);
tape::FileTape output(input, settings.output_file_name,
settings.ft_settings);
// tmp tape factory that captures cmd.settings from local scope
tape::TempTapeFactory factory =
[&](size_t cells) -> std::unique_ptr<tape::Tape> {
return std::make_unique<tape::FileTape>(
tape::FileTape(cells, cmd.settings));
tape::FileTape(cells, settings.ft_settings));
};
tape::external_sort(input, output, factory, cmd.memory_limit);
tape::external_sort(input, output, factory, settings.memory_limit);
return 0;
}

View file

@ -33,15 +33,15 @@ std::chrono::milliseconds parse_delay(const std::string &input) {
return std::chrono::milliseconds(delay_ms);
}
CmdArgs parse_command_line(int argc, char **argv) {
CmdArgs cmd{
AppSettings parse_command_line(int argc, char **argv) {
AppSettings settings{
.config_file_path = "",
.input_file_name = "",
.output_file_name = "",
.memory_limit = SIZE_MAX,
.version = false,
.help = false,
.settings = tape::FT_DEFAULT_SETTINGS,
.ft_settings = tape::FT_DEFAULT_SETTINGS,
};
int opt_index = 0;
int opt = 0;
@ -50,40 +50,40 @@ CmdArgs parse_command_line(int argc, char **argv) {
&opt_index)) != -1) {
switch (opt) {
case 'r':
cmd.settings.read_delay = parse_delay(optarg);
settings.ft_settings.read_delay = parse_delay(optarg);
break;
case 'w':
cmd.settings.write_delay = parse_delay(optarg);
settings.ft_settings.write_delay = parse_delay(optarg);
break;
case 'f':
cmd.settings.seek_forward_delay = parse_delay(optarg);
settings.ft_settings.seek_forward_delay = parse_delay(optarg);
break;
case 'b':
cmd.settings.seek_backwards_delay = parse_delay(optarg);
settings.ft_settings.seek_backwards_delay = parse_delay(optarg);
break;
case 'm':
cmd.memory_limit = std::stoi(optarg);
settings.memory_limit = std::stoi(optarg);
break;
case 'c':
cmd.config_file_path = optarg;
settings.config_file_path = optarg;
break;
case 'v':
cmd.version = true;
settings.version = true;
break;
case 'h':
case '?':
default:
cmd.help = true;
settings.help = true;
break;
}
}
// parse positional args INPUT_FILE OUTPUT_FILE
if (argc - optind < 2) {
cmd.help = true;
return cmd;
settings.help = true;
return settings;
}
// no linting to disable pointer arithmetic warning
cmd.input_file_name = argv[optind]; // NOLINT
cmd.output_file_name = argv[optind + 1]; // NOLINT
return cmd;
settings.input_file_name = argv[optind]; // NOLINT
settings.output_file_name = argv[optind + 1]; // NOLINT
return settings;
}

View file

@ -41,16 +41,18 @@ const static std::string HELP_MSG =
const static std::string VERSION_MSG = "ftsort ver. " xstr(VERSION);
struct CmdArgs {
struct AppSettings {
std::string config_file_path;
std::string input_file_name;
std::string output_file_name;
size_t memory_limit;
bool version;
bool help;
tape::FileTapeSettings settings;
tape::FileTapeSettings ft_settings;
};
CmdArgs parse_command_line(int argc, char **argv);
AppSettings parse_command_line(int argc, char **argv);
void read_settings_from_file(AppSettings &settings);
#endif // !TAPE_CONFIG_H