62 lines
1.4 KiB
C++
62 lines
1.4 KiB
C++
#include "Utility.hpp"
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
std::unordered_map<std::string, ParseResult> prs_cache
|
|
{
|
|
{ "FALSE", { true, 0 } },
|
|
{ "TRUE", { true, 1 } },
|
|
};
|
|
|
|
ParseResult prsi(std::string str)
|
|
{
|
|
auto found = prs_cache.find(str);
|
|
if (found != prs_cache.end()) return found->second; // cache hit
|
|
try
|
|
{ // cache miss, trying to parse and caching the result
|
|
int v;
|
|
if (str.starts_with("0x")) v = std::stoi(str.data(), 0, 16); // hex parsing
|
|
else v = std::stoi(str.data());
|
|
prs_cache[str] = { true, v };
|
|
}
|
|
catch (std::invalid_argument e)
|
|
{ // was not parsable, cache negative parse result
|
|
prs_cache[str] = { false, -1 }; // value doesn't really matter here
|
|
}
|
|
return prs_cache[str];
|
|
}
|
|
|
|
std::vector<char>* lf(std::string& path)
|
|
{
|
|
return lf(path.c_str());
|
|
}
|
|
|
|
std::vector<char>* lf(const char* path)
|
|
{
|
|
std::ifstream file(path, std::ios::binary);
|
|
if (!file.is_open()) return nullptr;
|
|
file.seekg(0, std::ios::end);
|
|
auto vec = new std::vector<char>(file.tellg());
|
|
if (vec->capacity() > 0)
|
|
{
|
|
file.seekg(0, std::ios::beg);
|
|
file.read(vec->data(), vec->capacity());
|
|
}
|
|
return vec;
|
|
}
|
|
|
|
bool lnsw(std::string& ln, int idx, const char* sw)
|
|
{
|
|
int len = strlen(sw);
|
|
return ln.length() - idx + 1 > len && ln.substr(idx, len) == sw;
|
|
}
|
|
|
|
void ssie(std::string& str, std::string repl)
|
|
{
|
|
if (str.empty()) str = repl;
|
|
}
|