53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
#include "LocalizationFile.hpp"
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <regex>
|
|
|
|
const std::regex keyChecker = std::regex("[^a-z0-9_]+");
|
|
|
|
static bool IsKeyValid(const std::string_view & key){
|
|
if (std::regex_search(key.begin(), key.end(), keyChecker)){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void LocalizationFile::LoadFile(const std::filesystem::path& path) {
|
|
if (!std::filesystem::exists(path)) {
|
|
std::stringstream ss;
|
|
ss << "File at path " << path << " does not exist.";
|
|
throw std::logic_error(ss.str());
|
|
}
|
|
std::ifstream file;
|
|
file.open(path, std::ios::in);
|
|
if (!file.is_open()) {
|
|
std::stringstream ss;
|
|
ss << "Something went wrong opening file at path " << path;
|
|
throw std::logic_error(ss.str());
|
|
}
|
|
std::string line;
|
|
char sep = ',';
|
|
bool firstLine = true;
|
|
while (std::getline(file, line)) {
|
|
if (firstLine) {
|
|
firstLine = false;
|
|
if (line.substr(0, 4) == "sep=") {
|
|
sep = line[4];
|
|
continue;
|
|
}
|
|
}
|
|
std::string key;
|
|
std::string value;
|
|
std::stringstream linestream(line);
|
|
std::getline(linestream, key, sep);
|
|
std::getline(linestream, value, sep);
|
|
if (key.empty())
|
|
continue;
|
|
if (!IsKeyValid(key)){
|
|
std::cout << "Key not valid: '" << key << "'. Skipping key." << std::endl;
|
|
}
|
|
_map[key] = value;
|
|
}
|
|
file.close();
|
|
}
|