From 3e4d91b6b029374813cfd0900db1fa8984ad7aa3 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 18 Feb 2022 18:40:37 +0100 Subject: refactor: use string_view & improve argument parsing --- src/conversion.cpp | 4 ++-- src/conversion.hpp | 4 ++-- src/mazerator.cpp | 57 ++++++++++++++++++++++++++++-------------------------- 3 files changed, 34 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/conversion.cpp b/src/conversion.cpp index b8fb942..71ba1b2 100644 --- a/src/conversion.cpp +++ b/src/conversion.cpp @@ -3,7 +3,7 @@ #include #include -unsigned int str_to_uint(std::string str) +unsigned int str_to_uint(std::string_view str) { if (str.at(0) == '-') { @@ -16,7 +16,7 @@ unsigned int str_to_uint(std::string str) try { - num = std::stoul(str, &waste_pos, NUMBER_BASE); + num = std::stoul(str.data(), &waste_pos, NUMBER_BASE); } catch (const std::invalid_argument &exc) { diff --git a/src/conversion.hpp b/src/conversion.hpp index 460a3ac..6da7606 100644 --- a/src/conversion.hpp +++ b/src/conversion.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include constexpr unsigned int NUMBER_BASE = 10U; @@ -10,4 +10,4 @@ constexpr unsigned int NUMBER_BASE = 10U; * @param str A string that possibly is a unsigned integer * @returns A unsigned integer */ -unsigned int str_to_uint(std::string str); +unsigned int str_to_uint(std::string_view str); diff --git a/src/mazerator.cpp b/src/mazerator.cpp index 6a0d1aa..a073615 100644 --- a/src/mazerator.cpp +++ b/src/mazerator.cpp @@ -1,13 +1,11 @@ -#include "conversion.hpp" - #include "app/app.hpp" +#include "conversion.hpp" #include "engine/vector2.hpp" #include "random_generator.hpp" #include #include #include -#include #include #include @@ -18,7 +16,7 @@ constexpr std::string_view DEFAULT_MAZE_WALL = "█"; namespace { -void optarg_error(int arg, const std::string &error) +void optarg_error(char arg, const std::string_view &error) { std::cout << "Error: Invalid option argument for -" << arg << ". " << error << std::endl; @@ -26,27 +24,38 @@ void optarg_error(int arg, const std::string &error) } /** - * Parses a unsigned integer command-line argument. + * Returns the current optarg as a string view. + */ +std::string_view get_str_optarg() +{ + return std::string_view(optarg); +} + +/** + * Returns the current optarg as a unsigned integer. * - * @param num_dst A pointer to a place to store the result value - * @param arg The command-line argument character + * @param arg The current command-line argument character * @param check_zero Whether or not to make sure that the result is not zero */ -void parse_uint_arg(unsigned int *num_dst, int arg, bool check_zero = false) +unsigned int get_uint_optarg(char arg, bool check_zero = false) { + unsigned int num = 0; + try { - *num_dst = str_to_uint(std::string(optarg)); + num = str_to_uint(get_str_optarg()); - if (check_zero && *num_dst == 0) + if (check_zero && num == 0) { throw "It should not be 0"; } } catch (const char *error) { - optarg_error(arg, std::string(error)); + optarg_error(arg, std::string_view(error)); } + + return num; } } // namespace @@ -62,7 +71,7 @@ const std::array options = { int main(int argc, char *argv[]) { - auto args = std::vector(argv, argv + argc); + auto args = std::vector(argv, argv + argc); AppOptions app_options; @@ -75,46 +84,40 @@ int main(int argc, char *argv[]) std::unique_ptr start_x = nullptr; std::unique_ptr start_y = nullptr; - int arg = 0; - while ((arg = getopt_long(argc, argv, "w:h:W:s:x:y:", options.data(), nullptr)) != -1) + char arg = 0; + while ((arg = static_cast( + getopt_long(argc, argv, "w:h:W:s:x:y:", options.data(), nullptr))) != -1) { switch (arg) { case 'w': { - app_options.maze_bounds->width( - static_cast(std::stoul(optarg, nullptr, NUMBER_BASE))); + app_options.maze_bounds->width(get_uint_optarg(arg, true)); break; } case 'h': { - app_options.maze_bounds->height( - static_cast(std::stoul(optarg, nullptr, NUMBER_BASE))); + app_options.maze_bounds->height(get_uint_optarg(arg, true)); break; } case 'x': { - start_x = std::make_unique(); - - parse_uint_arg(start_x.get(), arg); + start_x = std::make_unique(get_uint_optarg(arg)); break; } case 'y': { - start_y = std::make_unique(); - - parse_uint_arg(start_y.get(), arg); + start_y = std::make_unique(get_uint_optarg(arg)); break; } case 'W': { - app_options.wall = optarg; + app_options.wall = get_str_optarg(); break; } case 's': { - unsigned int seed = 0; - parse_uint_arg(&seed, arg, true); + auto seed = get_uint_optarg(arg, true); app_options.random_gen = std::make_shared(seed); break; -- cgit v1.2.3-18-g5258