From 68065c0ff21b98fa93b1d4dd99bc89da9eb11bd1 Mon Sep 17 00:00:00 2001 From: a25liang Date: Tue, 9 Jul 2024 21:56:05 -0400 Subject: [PATCH] - handle seed in args --- .vscode/settings.json | 57 +++++++++++++++++++++++++++++++++++++++++++ src/arguments.cc | 17 ++++++++++++- src/arguments.h | 5 +++- src/constants.h | 2 ++ src/main.cc | 8 ++++-- 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c9f71df --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,57 @@ +{ + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "cwchar": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "random": "cpp", + "string": "cpp", + "string_view": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "ranges": "cpp", + "sstream": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp", + "cwctype": "cpp", + "fstream": "cpp", + "numbers": "cpp", + "stdexcept": "cpp", + "system_error": "cpp", + "optional": "cpp" + } +} \ No newline at end of file diff --git a/src/arguments.cc b/src/arguments.cc index d45a863..e872b0d 100644 --- a/src/arguments.cc +++ b/src/arguments.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include "file_input.h" #include "file_output.h" #include "console_input.h" @@ -15,7 +16,8 @@ feature proc_args(int argc, char **argv, std::unique_ptr &curse, std::unique_ptr &in, std::unique_ptr &out, - std::unique_ptr &log) { + std::unique_ptr &log, + std::unique_ptr &rng) { feature result = 0; std::string str; std::string fn_fin; @@ -122,6 +124,16 @@ feature proc_args(int argc, char **argv, out = std::make_unique(curse); } + if (result & FEATURE_SEED) { + std::istringstream iss {seed}; + unsigned int tmp; + iss >> tmp; + if (!iss.good()) + return FEATURE_PANIC_SEED; + + rng = std::make_unique(tmp); + } + return result; } @@ -153,6 +165,9 @@ void panic_args(feature panic) { cerr << "Something must have went really, really, wrong..." << endl; } + } + else if (panic & FEATURE_PANIC_SEED) { + cerr << "Invalid seed" << endl; } else cerr << "Something must have went really, really, wrong..." << endl; diff --git a/src/arguments.h b/src/arguments.h index d211f0e..9af55de 100644 --- a/src/arguments.h +++ b/src/arguments.h @@ -5,6 +5,8 @@ #include "cursor.h" #include "display.h" #include "input.h" +#include "rng.h" + #include "constants.h" @@ -28,7 +30,8 @@ feature proc_args(int argc, char **argv, std::unique_ptr &curse, std::unique_ptr &in, std::unique_ptr &out, - std::unique_ptr &log); + std::unique_ptr &log, + std::unique_ptr &rng); void panic_args(feature panic); diff --git a/src/constants.h b/src/constants.h index f7ff3c3..80c1a1b 100644 --- a/src/constants.h +++ b/src/constants.h @@ -82,6 +82,8 @@ const feature FEATURE_SEED = 1 << 7; const feature FEATURE_MENU = 1 << 8; const feature FEATURE_IN_FILE = 1 << 9; const feature FEATURE_OUT_FILE = 1 << 10; + +const feature FEATURE_PANIC_SEED = 1 << 27; const feature FEATURE_PANIC_FILE = 1 << 28; const feature FEATURE_PANIC = 1 << 29; const feature FEATURE_CONFLICT = 1 << 30; diff --git a/src/main.cc b/src/main.cc index 10e8df9..10ff2b1 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,11 +6,15 @@ int main(int argc, char **argv) { std::unique_ptr in; std::unique_ptr out; std::unique_ptr log; + std::unique_ptr rng; - feature enabled_features = proc_args(argc, argv, curse, in, out, log); + + feature enabled_features = proc_args(argc, argv, + curse, in, out, log, rng); if (enabled_features & - (FEATURE_PANIC | FEATURE_PANIC_FILE | FEATURE_CONFLICT)) { + (FEATURE_PANIC | FEATURE_PANIC_FILE | + FEATURE_CONFLICT | FEATURE_PANIC_SEED)) { panic_args(enabled_features); return RETURN_PANICKED; }