LangBuilder/src/LocalizationFile.cpp

61 lines
1.7 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;
}
const char PossibleSeparators[] = {
',', ';', '|', '\t', ':'
};
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;
for (auto& s :PossibleSeparators) {
if (line.find(s) != std::string::npos){
sep = s;
break;
}
}
}
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 (key == "key" && value == "value")
continue;
if (!IsKeyValid(key)){
std::cout << "Key not valid: '" << key << "'. Skipping key." << std::endl;
}
_map[key] = value;
}
file.close();
}