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

View file

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

View file

@ -41,16 +41,18 @@ const static std::string HELP_MSG =
const static std::string VERSION_MSG = "ftsort ver. " xstr(VERSION); const static std::string VERSION_MSG = "ftsort ver. " xstr(VERSION);
struct CmdArgs { struct AppSettings {
std::string config_file_path; std::string config_file_path;
std::string input_file_name; std::string input_file_name;
std::string output_file_name; std::string output_file_name;
size_t memory_limit; size_t memory_limit;
bool version; bool version;
bool help; 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 #endif // !TAPE_CONFIG_H