Minor refactor

This commit is contained in:
MihailRis 2024-01-04 22:57:18 +03:00
parent 5bd78c09ad
commit 73595cd558
2 changed files with 25 additions and 37 deletions

View File

@ -4,42 +4,36 @@
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();
std::string token = reader.next();
if (reader.isKeywordArg()) {
if (token == "--res") {
token = reader.next();
if (!fs::is_directory(path(token))) {
if (!fs::is_directory(fs::path(token))) {
throw std::runtime_error(token+" is not a directory");
}
paths.setResources(path(token));
cout << "resources folder: " << token << std::endl;
paths.setResources(fs::path(token));
std::cout << "resources folder: " << token << std::endl;
} else if (token == "--dir") {
token = reader.next();
if (!fs::is_directory(path(token))) {
fs::create_directories(path(token));
if (!fs::is_directory(fs::path(token))) {
fs::create_directories(fs::path(token));
}
paths.setUserfiles(path(token));
cout << "userfiles folder: " << token << endl;
paths.setUserfiles(fs::path(token));
std::cout << "userfiles folder: " << token << std::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;
std::cout << "VoxelEngine command-line arguments:" << std::endl;
std::cout << " --res [path] - set resources directory" << std::endl;
std::cout << " --dir [path] - set userfiles directory" << std::endl;
return false;
} else {
cerr << "unknown argument " << token << endl;
std::cerr << "unknown argument " << token << std::endl;
}
} else {
cerr << "unexpected token" << endl;
std::cerr << "unexpected token" << std::endl;
}
}
return true;

View File

@ -6,17 +6,11 @@
#include <stdexcept>
#include <algorithm>
using std::vector;
using std::string;
using std::stringstream;
using std::wstring;
using std::wstringstream;
wstring util::lfill(wstring s, uint length, wchar_t c) {
std::wstring util::lfill(std::wstring s, uint length, wchar_t c) {
if (s.length() >= length) {
return s;
}
wstringstream ss;
std::wstringstream ss;
for (uint i = 0; i < length-s.length(); i++) {
ss << c;
}
@ -24,11 +18,11 @@ wstring util::lfill(wstring s, uint length, wchar_t c) {
return ss.str();
}
wstring util::rfill(wstring s, uint length, wchar_t c) {
std::wstring util::rfill(std::wstring s, uint length, wchar_t c) {
if (s.length() >= length) {
return s;
}
wstringstream ss;
std::wstringstream ss;
ss << s;
for (uint i = 0; i < length-s.length(); i++) {
ss << c;
@ -103,8 +97,8 @@ extern uint32_t util::decode_utf8(uint& size, const char* chr) {
return code;
}
string util::wstr2str_utf8(const wstring ws) {
vector<char> chars;
std::string util::wstr2str_utf8(const std::wstring ws) {
std::vector<char> chars;
char buffer[4];
for (wchar_t wc : ws) {
uint size = encode_utf8((uint)wc, (ubyte*)buffer);
@ -112,21 +106,21 @@ string util::wstr2str_utf8(const wstring ws) {
chars.push_back(buffer[i]);
}
}
return string(chars.data(), chars.size());
return std::string(chars.data(), chars.size());
}
wstring util::str2wstr_utf8(const string s) {
vector<wchar_t> chars;
std::wstring util::str2wstr_utf8(const std::string s) {
std::vector<wchar_t> chars;
size_t pos = 0;
uint size = 0;
while (pos < s.length()) {
chars.push_back(decode_utf8(size, &s.at(pos)));
pos += size;
}
return wstring(chars.data(), chars.size());
return std::wstring(chars.data(), chars.size());
}
bool util::is_integer(string text) {
bool util::is_integer(std::string text) {
for (char c : text) {
if (c < '0' || c > '9')
return false;
@ -134,7 +128,7 @@ bool util::is_integer(string text) {
return true;
}
bool util::is_integer(wstring text) {
bool util::is_integer(std::wstring text) {
for (wchar_t c : text) {
if (c < L'0' || c > L'9')
return false;