From 2bc539fae9d778bc35a1a38851b39e5357271f24 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 26 Nov 2023 14:12:12 +0300 Subject: [PATCH] added missing files (part 2) --- src/util/command_line.cpp | 46 +++++++++++++++++++++++++++++++++++++++ src/util/command_line.h | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/util/command_line.cpp create mode 100644 src/util/command_line.h diff --git a/src/util/command_line.cpp b/src/util/command_line.cpp new file mode 100644 index 00000000..05b09352 --- /dev/null +++ b/src/util/command_line.cpp @@ -0,0 +1,46 @@ +#include "command_line.h" + +#include + +namespace fs = std::filesystem; + +using std::filesystem::path; +using std::string; +using std::cout; +using std::cerr; +using std::endl; + +bool parse_cmdline(int argc, char** argv, EnginePaths& paths) { + ArgsReader reader(argc, argv); + reader.skip(); + while (reader.hasNext()) { + string token = reader.next(); + if (reader.isKeywordArg()) { + if (token == "--res") { + token = reader.next(); + if (!fs::is_directory(path(token))) { + throw std::runtime_error(token+" is not a directory"); + } + paths.setResources(path(token)); + cout << "resources folder: " << token << std::endl; + } else if (token == "--dir") { + token = reader.next(); + if (!fs::is_directory(path(token))) { + fs::create_directories(path(token)); + } + paths.setUserfiles(path(token)); + cout << "userfiles folder: " << token << endl; + } else if (token == "--help" || token == "-h") { + cout << "VoxelEngine command-line arguments:" << endl; + cout << " --res [path] - set resources directory" << endl; + cout << " --dir [path] - set userfiles directory" << endl; + return false; + } else { + cerr << "unknown argument " << token << endl; + } + } else { + cerr << "unexpected token" << endl; + } + } + return true; +} \ No newline at end of file diff --git a/src/util/command_line.h b/src/util/command_line.h new file mode 100644 index 00000000..a1b6cd49 --- /dev/null +++ b/src/util/command_line.h @@ -0,0 +1,41 @@ +#ifndef UTIL_COMMAND_LINE_H_ +#define UTIL_COMMAND_LINE_H_ + +#include +#include +#include +#include "../files/engine_paths.h" + +class ArgsReader { + int argc; + char** argv; + int pos = 0; + const char* last = ""; +public: + ArgsReader(int argc, char** argv) : argc(argc), argv(argv) {} + + void skip() { + pos++; + } + + bool hasNext() const { + return pos < argc; + } + + bool isKeywordArg() const { + return last[0] == '-'; + } + + std::string next() { + if (pos >= argc) { + throw std::runtime_error("unexpected end"); + } + last = argv[pos]; + return argv[pos++]; + } +}; + +/* @return false if engine start can*/ +extern bool parse_cmdline(int argc, char** argv, EnginePaths& paths); + +#endif // UTIL_COMMAND_LINE_H_ \ No newline at end of file