Updated locale gen
This commit is contained in:
@ -8,18 +8,23 @@ project(languagegen VERSION 2.0)
|
||||
add_executable(languagegen)
|
||||
target_sources(languagegen
|
||||
PRIVATE
|
||||
${DAWN_SHARED_SOURCES}
|
||||
LanguageGen.cpp
|
||||
../../util/DawnTool.cpp
|
||||
../../util/XmlNew.cpp
|
||||
../../util/file.cpp
|
||||
../../util/csv.cpp
|
||||
../../util/xml.cpp
|
||||
)
|
||||
|
||||
target_include_directories(languagegen
|
||||
PUBLIC
|
||||
${DAWN_SHARED_INCLUDES}
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
|
||||
target_link_libraries(languagegen
|
||||
PUBLIC
|
||||
${DAWN_BUILD_HOST_LIBS}
|
||||
|
@ -14,7 +14,7 @@ LanguageGen::LanguageGen(const int argc, const char *argv[]) :
|
||||
{
|
||||
}
|
||||
|
||||
int32_t LanguageGen::start() {
|
||||
int32_t LanguageGen::start() {
|
||||
if(this->args.size() != 3) {
|
||||
std::cout << "Invalid number of arguments provided to language gen!" << std::endl;
|
||||
return 1;
|
||||
@ -32,53 +32,47 @@ int32_t LanguageGen::start() {
|
||||
if(buffer == NULL) {
|
||||
std::cout << "Failed to allocate memory for locale string XML" << std::endl;
|
||||
fclose(fileIn);
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
assetReadString(fileIn, buffer);
|
||||
fclose(fileIn);
|
||||
|
||||
xml_t xml;
|
||||
xmlLoad(&xml, buffer);
|
||||
auto xml = Xml::load(std::string(buffer));
|
||||
free(buffer);
|
||||
|
||||
|
||||
// Begin parsing. Start by looking for the <language> tags
|
||||
std::vector<std::string> languages;
|
||||
for(int32_t i = 0; i < xml.childrenCount; i++) {
|
||||
auto c = xml.children + i;
|
||||
if(std::string(c->node) != "language") continue;
|
||||
auto attrName = xmlGetAttributeByName(c, "name");
|
||||
|
||||
if(attrName == -1) {
|
||||
std::cout << "Missing name param on language node" << std::endl;
|
||||
xmlDispose(&xml);
|
||||
return 1;
|
||||
auto itChildren = xml.children.begin();
|
||||
while(itChildren != xml.children.end()) {
|
||||
auto child = *itChildren;
|
||||
if(child->node == "language") {
|
||||
auto attrName = child->attributes.find("name");
|
||||
if(attrName == child->attributes.end()) {
|
||||
std::cout << "Missing name param on language node" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
languages.push_back(attrName->second);
|
||||
}
|
||||
languages.push_back(std::string(c->attributeDatas[attrName]));
|
||||
++itChildren;
|
||||
}
|
||||
|
||||
// Now begin actually parsing
|
||||
std::map<std::string, std::vector<struct LanguageString>> strings;
|
||||
for(int32_t i = 0; i < xml.childrenCount; i++) {
|
||||
auto c = xml.children + i;
|
||||
if(std::string(c->node) == "group") {
|
||||
auto ret = this->parseGroup(c, "", &strings);
|
||||
if(ret != 0) {
|
||||
xmlDispose(&xml);
|
||||
return ret;
|
||||
}
|
||||
} else if(std::string(c->node) == "string") {
|
||||
itChildren = xml.children.begin();
|
||||
while(itChildren != xml.children.end()) {
|
||||
auto child = *itChildren;
|
||||
if(child->node == "group") {
|
||||
auto ret = this->parseGroup(child, "", &strings);
|
||||
if(ret != 0) return ret;
|
||||
} else if(child->node == "string") {
|
||||
std::cout << "String cannot be a root node" << std::endl;
|
||||
xmlDispose(&xml);
|
||||
return 1;
|
||||
}
|
||||
++itChildren;
|
||||
}
|
||||
|
||||
xmlDispose(&xml);
|
||||
|
||||
|
||||
// Now we validate each lang has each key.
|
||||
std::vector<std::string> keys;
|
||||
|
||||
auto it = strings.begin();
|
||||
while(it != strings.end()) {
|
||||
auto it2 = it->second.begin();
|
||||
@ -98,7 +92,7 @@ int32_t LanguageGen::start() {
|
||||
while(it != strings.end()) {
|
||||
std::vector<std::string> itKeys;
|
||||
|
||||
std::string bufferOut = "";
|
||||
std::string bufferOut;
|
||||
|
||||
auto it2 = it->second.begin();
|
||||
while(it2 != it->second.end()) {
|
||||
@ -119,8 +113,8 @@ int32_t LanguageGen::start() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *strOut = bufferOut.c_str();
|
||||
fwrite(strOut, sizeof(char), strlen(strOut), fileOut);
|
||||
const char_t *strOut = bufferOut.c_str();
|
||||
fwrite(strOut, sizeof(char_t), strlen(strOut), fileOut);
|
||||
fclose(fileOut);
|
||||
|
||||
auto it3 = keys.begin();
|
||||
@ -144,54 +138,55 @@ int32_t LanguageGen::start() {
|
||||
}
|
||||
|
||||
int32_t LanguageGen::parseString(
|
||||
xml_t *stringNode,
|
||||
Xml *stringNode,
|
||||
std::string key,
|
||||
std::map<std::string,std::vector<struct LanguageString>> *strings
|
||||
) {
|
||||
auto attrLang = xmlGetAttributeByName(stringNode, "lang");
|
||||
if(attrLang == -1) {
|
||||
auto attrLang = stringNode->attributes.find("lang");
|
||||
if(attrLang == stringNode->attributes.end()) {
|
||||
std::cout << "String is missing lang parameter." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string lang(stringNode->attributeDatas[attrLang]);
|
||||
struct LanguageString str;
|
||||
str.key = key;
|
||||
str.value = std::string(stringNode->value);
|
||||
str.value = stringNode->value;
|
||||
|
||||
auto existing = (*strings).find(lang);
|
||||
auto existing = (*strings).find(attrLang->second);
|
||||
if(existing == (*strings).end()) {
|
||||
(*strings).insert(std::make_pair(lang, std::vector<struct LanguageString>()));
|
||||
(*strings).insert(std::make_pair(attrLang->second, std::vector<struct LanguageString>()));
|
||||
}
|
||||
(*strings)[lang].push_back(str);
|
||||
(*strings)[attrLang->second].push_back(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t LanguageGen::parseGroup(
|
||||
xml_t *groupNode,
|
||||
Xml *groupNode,
|
||||
std::string key,
|
||||
std::map<std::string,std::vector<struct LanguageString>> *strings
|
||||
std::map<std::string, std::vector<struct LanguageString>> *strings
|
||||
) {
|
||||
int32_t ret;
|
||||
|
||||
auto attrKey = xmlGetAttributeByName(groupNode, "key");
|
||||
if(attrKey == -1) {
|
||||
auto attrKey = groupNode->attributes.find("key");
|
||||
if(attrKey == groupNode->attributes.end()) {
|
||||
std::cout << "Group node is missing key" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(key.size() > 0) key += ".";
|
||||
key += std::string(groupNode->attributeDatas[attrKey]);
|
||||
key += attrKey->second;
|
||||
|
||||
for(int32_t i = 0; i < groupNode->childrenCount; i++) {
|
||||
auto c = groupNode->children + i;
|
||||
if(std::string(c->node) == "string") {
|
||||
ret = this->parseString(c, key, strings);
|
||||
auto itChildren = groupNode->children.begin();
|
||||
while(itChildren != groupNode->children.end()) {
|
||||
auto child = *itChildren;
|
||||
if(child->node == "string") {
|
||||
ret = this->parseString(child, key, strings);
|
||||
if(ret != 0) return ret;
|
||||
} else if(std::string(c->node) == "group") {
|
||||
ret = this->parseGroup(c, key, strings);
|
||||
} else if(child->node == "group") {
|
||||
ret = this->parseGroup(child, key, strings);
|
||||
if(ret != 0) return ret;
|
||||
}
|
||||
++itChildren;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "util/DawnTool.hpp"
|
||||
#include "util/XmlNew.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct LanguageString {
|
||||
@ -15,13 +16,13 @@ namespace Dawn {
|
||||
class LanguageGen : public DawnTool {
|
||||
protected:
|
||||
int32_t parseGroup(
|
||||
xml_t *node,
|
||||
Xml *node,
|
||||
std::string key,
|
||||
std::map<std::string, std::vector<struct LanguageString>> *strings
|
||||
);
|
||||
|
||||
int32_t parseString(
|
||||
xml_t *node,
|
||||
Xml *node,
|
||||
std::string key,
|
||||
std::map<std::string, std::vector<struct LanguageString>> *strings
|
||||
);
|
||||
|
Reference in New Issue
Block a user