Initial Commit
Some checks failed
continuous-integration/drone/tag Build is failing

This commit is contained in:
2020-08-30 16:44:45 +02:00
commit 28674714e8
10 changed files with 993 additions and 0 deletions

114
src/LocalizationData.cpp Normal file
View File

@@ -0,0 +1,114 @@
#include "LocalizationData.hpp"
void LocalizationData::LoadFromPath(const std::filesystem::path& path) {
auto cfgPath = path / "config.cfg";
if (!std::filesystem::exists(cfgPath)) {
std::stringstream ss;
ss << "Config file 'config.cfg' does not exist in path " << path;
throw std::logic_error(ss.str());
}
std::ifstream cfgFile;
cfgFile.open(cfgPath, std::ios::in);
if (!cfgFile.is_open()) {
throw std::logic_error("Something went wrong loading config file.");
}
std::string line;
while (std::getline(cfgFile, line)) {
std::string key;
std::string value;
std::stringstream linestream(line);
std::getline(linestream, key, '=');
std::getline(linestream, value, '=');
if (key == "language-code")
_code = value;
else if (key == "language-display")
_display = value;
else if (key == "global-path")
_globalPath = value;
else if (key == "temp-path")
_tempPath = value;
}
cfgFile.close();
std::cout << "Language code: " << _code << std::endl;
std::cout << "Language Display: " << _display << std::endl;
std::cout << "Globals Path: " << _globalPath << std::endl;
std::cout << "Temp Path: " << _tempPath << std::endl;
for (const auto& p : std::filesystem::recursive_directory_iterator(path / _globalPath)) {
if (p.path().extension() != ".csv")
continue;
LocalizationFile file;
file.LoadFile(p);
_globalFiles[p.path().filename().stem().string()] = file;
}
for (const auto& p : std::filesystem::recursive_directory_iterator(path / _tempPath)) {
if (p.path().extension() != ".csv")
continue;
LocalizationFile file;
file.LoadFile(p);
_tempFiles[p.path().filename().stem().string()] = file;
}
}
void LocalizationData::WriteToFile(const std::filesystem::path& path) {
std::ofstream outFile;
outFile.open(path, std::ios::out | std::ios::trunc);
if (!outFile.is_open()) {
std::stringstream ss;
ss << "Something went wrong opening output file " << path;
throw std::logic_error(ss.str());
}
outFile << _code << "~" << _display << std::endl;
outFile << "global" << std::endl;
std::unordered_map<std::string, std::ofstream::pos_type> globalFilesPositions;
for (const auto& file : _globalFiles) {
outFile << file.first << "~";
auto p1 = outFile.tellp();
outFile << "0000~0000" << std::endl;
globalFilesPositions[file.first] = p1;
}
outFile << "temp" << std::endl;
std::unordered_map<std::string, std::ofstream::pos_type> tempFilesPositions;
for (const auto& file : _tempFiles) {
outFile << file.first << "~";
auto p1 = outFile.tellp();
outFile << "0000~0000" << std::endl;
tempFilesPositions[file.first] = p1;
}
outFile << "ENDDATA" << std::endl;
// actual localization
for (const auto& file : _globalFiles) {
auto pos = outFile.tellp();
for (const auto& kv : file.second.GetMap()) {
outFile << kv.first << "|" << kv.second << std::endl;
}
auto end = outFile.tellp();
auto length = end - pos;
auto data = globalFilesPositions[file.first];
outFile.seekp(data);
outFile << std::setfill ('0') << std::setw(4) << std::hex << pos;
outFile << "~";
outFile << std::setfill ('0') << std::setw(4) << std::hex << length;
outFile.seekp(end);
}
for (const auto& file : _tempFiles) {
auto pos = outFile.tellp();
for (const auto& kv : file.second.GetMap()) {
outFile << kv.first << "|" << kv.second << std::endl;
}
auto end = outFile.tellp();
auto length = end - pos;
auto data = tempFilesPositions[file.first];
outFile.seekp(data);
outFile << std::setfill ('0') << std::setw(4) << std::hex << pos;
outFile << "~";
outFile << std::setfill ('0') << std::setw(4) << std::hex << length;
outFile.seekp(end);
}
outFile.close();
std::cout << "Wrote language file to " << path << std::endl;
}

30
src/LocalizationData.hpp Normal file
View File

@@ -0,0 +1,30 @@
#ifndef LANGBUILDER_LOCALIZATIONDATA_HPP
#define LANGBUILDER_LOCALIZATIONDATA_HPP
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include "LocalizationFile.hpp"
class LocalizationData {
std::string _code;
std::string _display;
std::string _globalPath;
std::string _tempPath;
std::unordered_map<std::string, LocalizationFile> _globalFiles;
std::unordered_map<std::string, LocalizationFile> _tempFiles;
public:
void LoadFromPath(const std::filesystem::path& path);
void WriteToFile(const std::filesystem::path& path);
const std::string& GetCode() const noexcept { return _code; }
const std::string& GetDisplay() const noexcept { return _display; }
};
#endif // LANGBUILDER_LOCALIZATIONDATA_HPP

37
src/LocalizationFile.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "LocalizationFile.hpp"
#include <fstream>
#include <iostream>
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);
_map[key] = value;
}
file.close();
}

17
src/LocalizationFile.hpp Normal file
View File

@@ -0,0 +1,17 @@
#ifndef LANGBUILDER_LOCALIZATIONFILE_HPP
#define LANGBUILDER_LOCALIZATIONFILE_HPP
#include <filesystem>
#include <string>
#include <unordered_map>
class LocalizationFile {
std::unordered_map<std::string, std::string> _map;
public:
void LoadFile(const std::filesystem::path& path);
const std::unordered_map<std::string, std::string>& GetMap() const { return _map; }
};
#endif // LANGBUILDER_LOCALIZATIONFILE_HPP

38
src/main.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <filesystem>
#include <iostream>
#include "../extern/argparse.hpp"
#include "LocalizationData.hpp"
int main(int argc, const char* argv[]) {
argparse::ArgumentParser parser("example", "Argument parser example");
parser.add_argument("-p", "--path", "path", false).description("Base path of the data files for the language.");
parser.add_argument("-o", "--output", "output", false).description("Output file for language file.");
parser.enable_help();
auto err = parser.parse(argc, argv);
if (err) {
std::cout << err << std::endl;
return -1;
}
if (parser.exists("help")) {
parser.print_help();
return 0;
}
auto path = std::filesystem::current_path();
if (parser.exists("path")) {
path = parser.get<std::string>("path");
}
std::cout << "Building localization file from path " << path << "." << std::endl;
LocalizationData data;
data.LoadFromPath(path);
std::filesystem::path outFile;
if (parser.exists("output")) {
outFile = parser.get<std::string>("output");
} else {
outFile = path / data.GetCode();
outFile.replace_extension("l10n");
}
data.WriteToFile(outFile);
return 0;
}