mwiniimporter: handle ini file encodings

Use components/to_utf8 to handle encodings.
Add an --encoding option for choosing between win1250, win1251 and
win1252 encodings for the imported ini file.
pull/16/head
Emanuel Guevel 12 years ago
parent e9a464c9f7
commit 2a7336c310

@ -653,6 +653,8 @@ MwIniImporter::multistrmap MwIniImporter::loadIniFile(std::string filename) {
std::string line;
while (std::getline(file, line)) {
line = toUTF8(line);
// unify Unix-style and Windows file ending
if (!(line.empty()) && (line[line.length()-1]) == '\r') {
line = line.substr(0, line.length()-1);
@ -826,3 +828,16 @@ void MwIniImporter::writeToFile(boost::iostreams::stream<boost::iostreams::file_
}
}
}
std::string MwIniImporter::toUTF8(const std::string &str) {
char *ptr = ToUTF8::getBuffer(str.length());
strncpy(ptr, str.c_str(), str.length());
// Convert to UTF8 and return
return ToUTF8::getUtf8(mEncoding);
}
void MwIniImporter::setInputEncoding(const ToUTF8::FromType &encoding)
{
mEncoding = encoding;
}

@ -8,12 +8,15 @@
#include <vector>
#include <exception>
#include "../../components/to_utf8/to_utf8.hpp"
class MwIniImporter {
public:
typedef std::map<std::string, std::string> strmap;
typedef std::map<std::string, std::vector<std::string> > multistrmap;
MwIniImporter();
void setInputEncoding(const ToUTF8::FromType& encoding);
void setVerbose(bool verbose);
multistrmap loadIniFile(std::string filename);
multistrmap loadCfgFile(std::string filename);
@ -25,9 +28,11 @@ class MwIniImporter {
private:
void insertMultistrmap(multistrmap &cfg, std::string key, std::string value);
std::string numberToString(int n);
std::string toUTF8(const std::string &str);
bool mVerbose;
strmap mMergeMap;
std::vector<std::string> mMergeFallback;
ToUTF8::FromType mEncoding;
};

@ -18,6 +18,11 @@ int main(int argc, char *argv[]) {
("cfg,c", bpo::value<std::string>(), "openmw.cfg file")
("output,o", bpo::value<std::string>()->default_value(""), "openmw.cfg file")
("game-files,g", "import esm and esp files")
("encoding,e", bpo::value<std::string>()-> default_value("win1252"),
"Character encoding used in OpenMW game messages:\n"
"\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages\n"
"\n\twin1251 - Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages\n"
"\n\twin1252 - Western European (Latin) alphabet, used by default")
;
p_desc.add("ini", 1).add("cfg", 1);
@ -57,6 +62,10 @@ int main(int argc, char *argv[]) {
MwIniImporter importer;
importer.setVerbose(vm.count("verbose"));
// Font encoding settings
std::string encoding(vm["encoding"].as<std::string>());
importer.setInputEncoding(ToUTF8::calculateEncoding(encoding));
MwIniImporter::multistrmap ini = importer.loadIniFile(iniFile);
MwIniImporter::multistrmap cfg = importer.loadCfgFile(cfgFile);

Loading…
Cancel
Save