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; 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) { bool parse_cmdline(int argc, char** argv, EnginePaths& paths) {
ArgsReader reader(argc, argv); ArgsReader reader(argc, argv);
reader.skip(); reader.skip();
while (reader.hasNext()) { while (reader.hasNext()) {
string token = reader.next(); std::string token = reader.next();
if (reader.isKeywordArg()) { if (reader.isKeywordArg()) {
if (token == "--res") { if (token == "--res") {
token = reader.next(); 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"); throw std::runtime_error(token+" is not a directory");
} }
paths.setResources(path(token)); paths.setResources(fs::path(token));
cout << "resources folder: " << token << std::endl; std::cout << "resources folder: " << token << std::endl;
} else if (token == "--dir") { } else if (token == "--dir") {
token = reader.next(); token = reader.next();
if (!fs::is_directory(path(token))) { if (!fs::is_directory(fs::path(token))) {
fs::create_directories(path(token)); fs::create_directories(fs::path(token));
} }
paths.setUserfiles(path(token)); paths.setUserfiles(fs::path(token));
cout << "userfiles folder: " << token << endl; std::cout << "userfiles folder: " << token << std::endl;
} else if (token == "--help" || token == "-h") { } else if (token == "--help" || token == "-h") {
cout << "VoxelEngine command-line arguments:" << endl; std::cout << "VoxelEngine command-line arguments:" << std::endl;
cout << " --res [path] - set resources directory" << endl; std::cout << " --res [path] - set resources directory" << std::endl;
cout << " --dir [path] - set userfiles directory" << endl; std::cout << " --dir [path] - set userfiles directory" << std::endl;
return false; return false;
} else { } else {
cerr << "unknown argument " << token << endl; std::cerr << "unknown argument " << token << std::endl;
} }
} else { } else {
cerr << "unexpected token" << endl; std::cerr << "unexpected token" << std::endl;
} }
} }
return true; return true;

View File

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