Made source files explicitly specified instead of globbing in cmake files
Moved ftsort sources to bin/ftsort Moved tape_config.h into bin/ftsort Implemented command line parsing for ftsort in tape_config.cpp ftsort binary is in working state
This commit is contained in:
parent
33fee958a5
commit
8f18f9ef8f
9 changed files with 184 additions and 40 deletions
|
@ -1,2 +1,8 @@
|
|||
add_executable(ftsort ${PROJECT_SOURCE_DIR}/bin/ftsort.cpp)
|
||||
add_compile_definitions(VERSION=${PROJECT_VERSION})
|
||||
|
||||
add_executable(ftsort)
|
||||
target_sources(ftsort PRIVATE ftsort/ftsort.cpp
|
||||
ftsort/tape_config.cpp
|
||||
ftsort/tape_config.h
|
||||
)
|
||||
target_link_libraries(ftsort PRIVATE tapelib)
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
#include <iostream>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
return 0;
|
||||
}
|
26
bin/ftsort/ftsort.cpp
Normal file
26
bin/ftsort/ftsort.cpp
Normal file
|
@ -0,0 +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) {
|
||||
std::cout << VERSION_MSG << std::endl;
|
||||
return 0;
|
||||
}
|
||||
if (cmd.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);
|
||||
// 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::external_sort(input, output, factory, cmd.memory_limit);
|
||||
return 0;
|
||||
}
|
89
bin/ftsort/tape_config.cpp
Normal file
89
bin/ftsort/tape_config.cpp
Normal file
|
@ -0,0 +1,89 @@
|
|||
#include "tape_config.h"
|
||||
#include <bits/getopt_core.h>
|
||||
#include <getopt.h>
|
||||
|
||||
const static struct std::vector<option> CMD_OPTS{
|
||||
{.name = "write-delay",
|
||||
.has_arg = no_argument,
|
||||
.flag = nullptr,
|
||||
.val = 'w'},
|
||||
{.name = "read-delay", .has_arg = no_argument, .flag = nullptr, .val = 'r'},
|
||||
{.name = "seek-forward-delay",
|
||||
.has_arg = no_argument,
|
||||
.flag = nullptr,
|
||||
.val = 'f'},
|
||||
{.name = "seek-backwards-delay",
|
||||
.has_arg = no_argument,
|
||||
.flag = nullptr,
|
||||
.val = 'b'},
|
||||
{.name = "memory-limit",
|
||||
.has_arg = no_argument,
|
||||
.flag = nullptr,
|
||||
.val = 'm'},
|
||||
{.name = "config-file",
|
||||
.has_arg = no_argument,
|
||||
.flag = nullptr,
|
||||
.val = 'c'},
|
||||
{.name = "version", .has_arg = no_argument, .flag = nullptr, .val = 'v'},
|
||||
{.name = "help", .has_arg = no_argument, .flag = nullptr, .val = 'h'},
|
||||
{.name = nullptr, .has_arg = no_argument, .flag = nullptr, .val = 0}};
|
||||
|
||||
std::chrono::milliseconds parse_delay(const std::string &input) {
|
||||
int delay_ms = std::stoi(input);
|
||||
return std::chrono::milliseconds(delay_ms);
|
||||
}
|
||||
|
||||
CmdArgs parse_command_line(int argc, char **argv) {
|
||||
CmdArgs cmd{
|
||||
.config_file_path = "",
|
||||
.input_file_name = "",
|
||||
.output_file_name = "",
|
||||
.memory_limit = SIZE_MAX,
|
||||
.version = false,
|
||||
.help = false,
|
||||
.settings = tape::FT_DEFAULT_SETTINGS,
|
||||
};
|
||||
int opt_index = 0;
|
||||
int opt = 0;
|
||||
// parse [OPTIONS...] using getopt_long
|
||||
while ((opt = getopt_long(argc, argv, "r:w:f:b:m:c:vh", CMD_OPTS.data(),
|
||||
&opt_index)) != -1) {
|
||||
switch (opt) {
|
||||
case 'r':
|
||||
cmd.settings.read_delay = parse_delay(optarg);
|
||||
break;
|
||||
case 'w':
|
||||
cmd.settings.write_delay = parse_delay(optarg);
|
||||
break;
|
||||
case 'f':
|
||||
cmd.settings.seek_forward_delay = parse_delay(optarg);
|
||||
break;
|
||||
case 'b':
|
||||
cmd.settings.seek_backwards_delay = parse_delay(optarg);
|
||||
break;
|
||||
case 'm':
|
||||
cmd.memory_limit = std::stoi(optarg);
|
||||
break;
|
||||
case 'c':
|
||||
cmd.config_file_path = optarg;
|
||||
break;
|
||||
case 'v':
|
||||
cmd.version = true;
|
||||
break;
|
||||
case 'h':
|
||||
case '?':
|
||||
default:
|
||||
cmd.help = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// parse positional args INPUT_FILE OUTPUT_FILE
|
||||
if (argc - optind < 2) {
|
||||
cmd.help = true;
|
||||
return cmd;
|
||||
}
|
||||
// no linting to disable pointer arithmetic warning
|
||||
cmd.input_file_name = argv[optind]; // NOLINT
|
||||
cmd.output_file_name = argv[optind + 1]; // NOLINT
|
||||
return cmd;
|
||||
}
|
56
bin/ftsort/tape_config.h
Normal file
56
bin/ftsort/tape_config.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
#ifndef TAPE_CONFIG_H
|
||||
#define TAPE_CONFIG_H
|
||||
|
||||
#include "filetape.h"
|
||||
|
||||
// stringizing opearator to convert macro into string literal
|
||||
#define xstr(s) str(s)
|
||||
#define str(s) #s
|
||||
|
||||
const static std::string HELP_MSG =
|
||||
"Usage: ftsort [OPTIONS...] INPUT_FILE OUTPUT_FILE\n"
|
||||
"Sorts a Tape provided by the INPUT_FILE into the OUTPUT_FILE\n\n"
|
||||
"Options:\n"
|
||||
"-w, --write-delay VALUE\t\t\t"
|
||||
"Sets the delay of write operations to VALUE milliseconds\n"
|
||||
"-r, --read-delay VALUE\t\t\t"
|
||||
"Sets the delay of read operations to VALUE milliseconds\n"
|
||||
"-f, --seek-forward-delay VALUE\t\t"
|
||||
"Sets the delay of seeking forward operations to VALUE milliseconds\n"
|
||||
"-b, --seek-backwards-delay VALUE\t"
|
||||
"Sets the delay of seeking backwards operations to VALUE milliseconds\n"
|
||||
"-m, --memory-limit VALUE\t\t"
|
||||
"Sets the amount of cells that the program can store in its memory during "
|
||||
"sorting to VALUE\n"
|
||||
"-c, --config-file FILE\t\t\t"
|
||||
"Applies options from the FILE, config shoud be a simple KEY=VALUE list\n"
|
||||
"-v, --version\t\t\t\t"
|
||||
"Print out version of the program\n"
|
||||
"-h, --help\t\t\t\t"
|
||||
"Print help message\n\n"
|
||||
"Exmaple of valid input Tape file with values 1, 1234 and 2222222222:\n\n"
|
||||
"0000000001\n"
|
||||
"0000001234\n"
|
||||
"2222222222\n\n"
|
||||
"Example of a valid config file:\n\n"
|
||||
"write-delay=100\n"
|
||||
"read-delay=100\n"
|
||||
"seek-forward-delay=100\n"
|
||||
"seek-backwards-delay=100\n"
|
||||
"memory-limit=1024\n";
|
||||
|
||||
const static std::string VERSION_MSG = "ftsort ver. " xstr(VERSION);
|
||||
|
||||
struct CmdArgs {
|
||||
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;
|
||||
};
|
||||
|
||||
CmdArgs parse_command_line(int argc, char **argv);
|
||||
|
||||
#endif // !TAPE_CONFIG_H
|
|
@ -1,12 +0,0 @@
|
|||
#ifndef TAPE_CONFIG_H
|
||||
#define TAPE_CONFIG_H
|
||||
|
||||
#include "filetape.h"
|
||||
|
||||
namespace tape {
|
||||
|
||||
FileTapeSettings init_settings(int argc, char **argv);
|
||||
|
||||
} // namespace tape
|
||||
|
||||
#endif // !TAPE_CONFIG_H
|
|
@ -1,4 +1,5 @@
|
|||
file(GLOB SOURCES CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/src/*.cpp)
|
||||
|
||||
add_library(tapelib ${SOURCES})
|
||||
add_library(tapelib)
|
||||
target_sources(tapelib PRIVATE filetape.cpp
|
||||
tape_util.cpp
|
||||
)
|
||||
target_include_directories(tapelib PUBLIC ${PROJECT_SOURCE_DIR}/include/tapelib)
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
#include "tape_config.h"
|
||||
|
||||
struct CommandLineArgs {
|
||||
std::string config_file_path;
|
||||
tape::FileTapeSettings settings;
|
||||
};
|
||||
|
||||
// const static struct Options COMMAND_LINE_OPTIONS[]{};
|
||||
|
||||
CommandLineArgs parse_command_line(int argc, char **argv) {
|
||||
return CommandLineArgs{};
|
||||
}
|
||||
|
||||
tape::FileTapeSettings tape::init_settings(int argc, char **argv) {
|
||||
return FileTapeSettings{};
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
find_package(Catch2 3 REQUIRED)
|
||||
|
||||
add_executable(filetape_tests ${PROJECT_SOURCE_DIR}/tests/filetape_tests.cpp)
|
||||
add_executable(filetape_tests filetape_tests.cpp)
|
||||
target_link_libraries(filetape_tests PRIVATE tapelib)
|
||||
target_link_libraries(filetape_tests PRIVATE Catch2::Catch2WithMain)
|
||||
|
||||
add_test(filetape_tests filetape_tests)
|
||||
|
||||
add_executable(filetape_sort_tests ${PROJECT_SOURCE_DIR}/tests/filetape_sort_tests.cpp)
|
||||
add_executable(filetape_sort_tests filetape_sort_tests.cpp)
|
||||
target_link_libraries(filetape_sort_tests PRIVATE tapelib)
|
||||
target_link_libraries(filetape_sort_tests PRIVATE Catch2::Catch2WithMain)
|
||||
|
||||
|
|
Loading…
Reference in a new issue