aboutsummaryrefslogtreecommitdiff
path: root/src/mazerator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mazerator.cpp')
-rw-r--r--src/mazerator.cpp57
1 files changed, 30 insertions, 27 deletions
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 <getopt.h>
#include <iostream>
#include <memory>
-#include <string>
#include <string_view>
#include <vector>
@@ -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<option, 8> options = {
int main(int argc, char *argv[])
{
- auto args = std::vector<std::string>(argv, argv + argc);
+ auto args = std::vector<std::string_view>(argv, argv + argc);
AppOptions app_options;
@@ -75,46 +84,40 @@ int main(int argc, char *argv[])
std::unique_ptr<unsigned int> start_x = nullptr;
std::unique_ptr<unsigned int> 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<char>(
+ 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<unsigned int>(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<unsigned int>(std::stoul(optarg, nullptr, NUMBER_BASE)));
+ app_options.maze_bounds->height(get_uint_optarg(arg, true));
break;
}
case 'x':
{
- start_x = std::make_unique<unsigned int>();
-
- parse_uint_arg(start_x.get(), arg);
+ start_x = std::make_unique<unsigned int>(get_uint_optarg(arg));
break;
}
case 'y':
{
- start_y = std::make_unique<unsigned int>();
-
- parse_uint_arg(start_y.get(), arg);
+ start_y = std::make_unique<unsigned int>(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<RandomNumberGenerator>(seed);
break;