to_utf8, Utf8Encoder: pass encoding as constructor parameter

Edit other files accordingly.
actorid
Emanuel Guevel 12 years ago
parent cc792da858
commit 63f09462fd

@ -165,23 +165,12 @@ bool parseOptions (int argc, char** argv, Arguments &info)
// Font encoding settings // Font encoding settings
info.encoding = variables["encoding"].as<std::string>(); info.encoding = variables["encoding"].as<std::string>();
if (info.encoding == "win1250") if(info.encoding != "win1250" && info.encoding != "win1251" && info.encoding != "win1252")
{ {
std::cout << "Using Central and Eastern European font encoding." << std::endl; std::cout << info.encoding << " is not a valid encoding option." << std::endl;
} info.encoding = "win1252";
else if (info.encoding == "win1251")
{
std::cout << "Using Cyrillic font encoding." << std::endl;
}
else
{
if(info.encoding != "win1252")
{
std::cout << info.encoding << " is not a valid encoding option." << std::endl;
info.encoding = "win1252";
}
std::cout << "Using default (English) font encoding." << std::endl;
} }
std::cout << ToUTF8::encodingUsingMessage(info.encoding) << std::endl;
return true; return true;
} }
@ -262,7 +251,8 @@ void printRaw(ESM::ESMReader &esm)
int load(Arguments& info) int load(Arguments& info)
{ {
ESM::ESMReader& esm = info.reader; ESM::ESMReader& esm = info.reader;
esm.setEncoding(ToUTF8::calculateEncoding(info.encoding)); ToUTF8::Utf8Encoder encoder (ToUTF8::calculateEncoding(info.encoding));
esm.setEncoder(&encoder);
std::string filename = info.filename; std::string filename = info.filename;
std::cout << "Loading file: " << filename << std::endl; std::cout << "Loading file: " << filename << std::endl;
@ -432,7 +422,8 @@ int clone(Arguments& info)
std::cout << std::endl << "Saving records to: " << info.outname << "..." << std::endl; std::cout << std::endl << "Saving records to: " << info.outname << "..." << std::endl;
ESM::ESMWriter& esm = info.writer; ESM::ESMWriter& esm = info.writer;
esm.setEncoding(info.encoding); ToUTF8::Utf8Encoder encoder (ToUTF8::calculateEncoding(info.encoding));
esm.setEncoder(&encoder);
esm.setAuthor(info.data.author); esm.setAuthor(info.data.author);
esm.setDescription(info.data.description); esm.setDescription(info.data.description);
esm.setVersion(info.data.version); esm.setVersion(info.data.version);

@ -272,7 +272,8 @@ void DataFilesModel::addMasters(const QString &path)
foreach (const QString &path, dir.entryList()) { foreach (const QString &path, dir.entryList()) {
try { try {
ESM::ESMReader fileReader; ESM::ESMReader fileReader;
fileReader.setEncoding(ToUTF8::calculateEncoding(mEncoding.toStdString())); ToUTF8::Utf8Encoder encoder (ToUTF8::calculateEncoding(mEncoding.toStdString()));
fileReader.setEncoder(&encoder);
fileReader.open(dir.absoluteFilePath(path).toStdString()); fileReader.open(dir.absoluteFilePath(path).toStdString());
ESM::ESMReader::MasterList mlist = fileReader.getMasters(); ESM::ESMReader::MasterList mlist = fileReader.getMasters();
@ -335,7 +336,8 @@ void DataFilesModel::addPlugins(const QString &path)
try { try {
ESM::ESMReader fileReader; ESM::ESMReader fileReader;
fileReader.setEncoding(ToUTF8::calculateEncoding(mEncoding.toStdString())); ToUTF8::Utf8Encoder encoder (ToUTF8::calculateEncoding(mEncoding.toStdString()));
fileReader.setEncoder(&encoder);
fileReader.open(dir.absoluteFilePath(path).toStdString()); fileReader.open(dir.absoluteFilePath(path).toStdString());
ESM::ESMReader::MasterList mlist = fileReader.getMasters(); ESM::ESMReader::MasterList mlist = fileReader.getMasters();

@ -649,8 +649,7 @@ MwIniImporter::multistrmap MwIniImporter::loadIniFile(std::string filename) {
std::string section(""); std::string section("");
MwIniImporter::multistrmap map; MwIniImporter::multistrmap map;
boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str()); boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
ToUTF8::Utf8Encoder encoder; ToUTF8::Utf8Encoder encoder(mEncoding);
encoder.setEncoding(mEncoding);
std::string line; std::string line;
while (std::getline(file, line)) { while (std::getline(file, line)) {

@ -331,11 +331,15 @@ void OMW::Engine::go()
// cursor replacer (converts the cursor from the bsa so they can be used by mygui) // cursor replacer (converts the cursor from the bsa so they can be used by mygui)
MWGui::CursorReplace replacer; MWGui::CursorReplace replacer;
// Create encoder
ToUTF8::Utf8Encoder encoder (mEncoding);
// Create the world // Create the world
mEnvironment.setWorld (new MWWorld::World (*mOgre, mFileCollections, mMaster, mEnvironment.setWorld (new MWWorld::World (*mOgre, mFileCollections, mMaster,
mResDir, mCfgMgr.getCachePath(), mNewGame, mEncoding, mFallbackMap)); mResDir, mCfgMgr.getCachePath(), mNewGame, &encoder, mFallbackMap));
//Load translation data //Load translation data
mTranslationDataStorage.setEncoder(&encoder);
mTranslationDataStorage.loadTranslationData(mFileCollections, mMaster); mTranslationDataStorage.loadTranslationData(mFileCollections, mMaster);
// Create window manager - this manages all the MW-specific GUI windows // Create window manager - this manages all the MW-specific GUI windows
@ -494,7 +498,6 @@ void OMW::Engine::showFPS(int level)
void OMW::Engine::setEncoding(const ToUTF8::FromType& encoding) void OMW::Engine::setEncoding(const ToUTF8::FromType& encoding)
{ {
mEncoding = encoding; mEncoding = encoding;
mTranslationDataStorage.setEncoding (encoding);
} }
void OMW::Engine::setFallbackValues(std::map<std::string,std::string> fallbackMap) void OMW::Engine::setFallbackValues(std::map<std::string,std::string> fallbackMap)

@ -170,7 +170,7 @@ namespace MWWorld
World::World (OEngine::Render::OgreRenderer& renderer, World::World (OEngine::Render::OgreRenderer& renderer,
const Files::Collections& fileCollections, const Files::Collections& fileCollections,
const std::string& master, const boost::filesystem::path& resDir, const boost::filesystem::path& cacheDir, bool newGame, const std::string& master, const boost::filesystem::path& resDir, const boost::filesystem::path& cacheDir, bool newGame,
const ToUTF8::FromType& encoding, std::map<std::string,std::string> fallbackMap) ToUTF8::Utf8Encoder* encoder, std::map<std::string,std::string> fallbackMap)
: mPlayer (0), mLocalScripts (mStore), mGlobalVariables (0), : mPlayer (0), mLocalScripts (mStore), mGlobalVariables (0),
mSky (true), mCells (mStore, mEsm), mSky (true), mCells (mStore, mEsm),
mNumFacing(0) mNumFacing(0)
@ -187,7 +187,7 @@ namespace MWWorld
std::cout << "Loading ESM " << masterPath.string() << "\n"; std::cout << "Loading ESM " << masterPath.string() << "\n";
// This parses the ESM file and loads a sample cell // This parses the ESM file and loads a sample cell
mEsm.setEncoding(encoding); mEsm.setEncoder(encoder);
mEsm.open (masterPath.string()); mEsm.open (masterPath.string());
mStore.load (mEsm); mStore.load (mEsm);

@ -95,7 +95,7 @@ namespace MWWorld
World (OEngine::Render::OgreRenderer& renderer, World (OEngine::Render::OgreRenderer& renderer,
const Files::Collections& fileCollections, const Files::Collections& fileCollections,
const std::string& master, const boost::filesystem::path& resDir, const boost::filesystem::path& cacheDir, bool newGame, const std::string& master, const boost::filesystem::path& resDir, const boost::filesystem::path& cacheDir, bool newGame,
const ToUTF8::FromType& encoding, std::map<std::string,std::string> fallbackMap); ToUTF8::Utf8Encoder* encoder, std::map<std::string,std::string> fallbackMap);
virtual ~World(); virtual ~World();

@ -344,7 +344,7 @@ std::string ESMReader::getString(int size)
getExact(ptr, size); getExact(ptr, size);
// Convert to UTF8 and return // Convert to UTF8 and return
return mEncoder.getUtf8(ptr, size); return mEncoder->getUtf8(ptr, size);
} }
void ESMReader::fail(const std::string &msg) void ESMReader::fail(const std::string &msg)
@ -362,10 +362,9 @@ void ESMReader::fail(const std::string &msg)
throw std::runtime_error(ss.str()); throw std::runtime_error(ss.str());
} }
void ESMReader::setEncoding(const ToUTF8::FromType& encoding) void ESMReader::setEncoder(ToUTF8::Utf8Encoder* encoder)
{ {
mEncoding = encoding; mEncoder = encoder;
mEncoder.setEncoding(encoding);
} }
} }

@ -235,8 +235,8 @@ public:
/// Used for error handling /// Used for error handling
void fail(const std::string &msg); void fail(const std::string &msg);
/// Sets font encoding for ESM strings /// Sets font encoder for ESM strings
void setEncoding(const ToUTF8::FromType& encoding); void setEncoder(ToUTF8::Utf8Encoder* encoder);
private: private:
Ogre::DataStreamPtr mEsm; Ogre::DataStreamPtr mEsm;
@ -251,8 +251,7 @@ private:
SaveData mSaveData; SaveData mSaveData;
MasterList mMasters; MasterList mMasters;
ToUTF8::FromType mEncoding; ToUTF8::Utf8Encoder* mEncoder;
ToUTF8::Utf8Encoder mEncoder;
}; };
} }
#endif #endif

@ -158,7 +158,7 @@ void ESMWriter::writeHString(const std::string& data)
else else
{ {
// Convert to UTF8 and return // Convert to UTF8 and return
std::string ascii = m_encoder.getLegacyEnc(data); std::string ascii = m_encoder->getLegacyEnc(data);
write(ascii.c_str(), ascii.size()); write(ascii.c_str(), ascii.size());
} }
@ -188,23 +188,9 @@ void ESMWriter::write(const char* data, int size)
m_stream->write(data, size); m_stream->write(data, size);
} }
void ESMWriter::setEncoding(const std::string& encoding) void ESMWriter::setEncoder(ToUTF8::Utf8Encoder* encoder)
{ {
if (encoding == "win1250") m_encoder = encoder;
{
m_encoding = ToUTF8::WINDOWS_1250;
}
else if (encoding == "win1251")
{
m_encoding = ToUTF8::WINDOWS_1251;
}
else
{
// Default Latin encoding
m_encoding = ToUTF8::WINDOWS_1252;
}
m_encoder.setEncoding(m_encoding);
} }
} }

@ -6,7 +6,7 @@
#include <assert.h> #include <assert.h>
#include "esmcommon.hpp" #include "esmcommon.hpp"
#include "../to_utf8/to_utf8.hpp" #include <components/to_utf8/to_utf8.hpp>
namespace ESM { namespace ESM {
@ -24,7 +24,7 @@ public:
void setVersion(int ver); void setVersion(int ver);
int getType(); int getType();
void setType(int type); void setType(int type);
void setEncoding(const std::string& encoding); // Write strings as UTF-8? void setEncoder(ToUTF8::Utf8Encoder *encoding); // Write strings as UTF-8?
void setAuthor(const std::string& author); void setAuthor(const std::string& author);
void setDescription(const std::string& desc); void setDescription(const std::string& desc);
@ -94,12 +94,10 @@ private:
std::list<RecordData> m_records; std::list<RecordData> m_records;
std::ostream* m_stream; std::ostream* m_stream;
std::streampos m_headerPos; std::streampos m_headerPos;
ToUTF8::FromType m_encoding; ToUTF8::Utf8Encoder* m_encoder;
ToUTF8::Utf8Encoder m_encoder;
int m_recordCount; int m_recordCount;
HEDRstruct m_header; HEDRstruct m_header;
SaveData m_saveData;
}; };
} }

@ -43,16 +43,10 @@
using namespace ToUTF8; using namespace ToUTF8;
Utf8Encoder::Utf8Encoder(void): Utf8Encoder::Utf8Encoder(const FromType sourceEncoding):
mOutput(50*1024) mOutput(50*1024)
{ {
} switch (sourceEncoding)
void Utf8Encoder::setEncoding(const FromType sourceEncoding)
{
mEncoding = sourceEncoding;
switch (mEncoding)
{ {
case ToUTF8::WINDOWS_1252: case ToUTF8::WINDOWS_1252:
{ {

@ -24,9 +24,7 @@ namespace ToUTF8
class Utf8Encoder class Utf8Encoder
{ {
public: public:
Utf8Encoder(void); Utf8Encoder(FromType sourceEncoding);
void setEncoding(const FromType sourceEncoding);
// Convert to UTF8 from the previously given code page. // Convert to UTF8 from the previously given code page.
std::string getUtf8(const char *input, int size); std::string getUtf8(const char *input, int size);
@ -48,9 +46,7 @@ namespace ToUTF8
size_t getLength2(const char* input, bool &ascii); size_t getLength2(const char* input, bool &ascii);
void copyFromArray2(const char*& chp, char* &out); void copyFromArray2(const char*& chp, char* &out);
FromType mEncoding;
std::vector<char> mOutput; std::vector<char> mOutput;
int mSize;
char* translationArray; char* translationArray;
}; };
} }

@ -50,7 +50,7 @@ namespace Translation
if (!line.empty()) if (!line.empty())
{ {
line = mEncoder.getUtf8(line); line = mEncoder->getUtf8(line);
size_t tab_pos = line.find('\t'); size_t tab_pos = line.find('\t');
if (tab_pos != std::string::npos && tab_pos > 0 && tab_pos < line.size() - 1) if (tab_pos != std::string::npos && tab_pos > 0 && tab_pos < line.size() - 1)
@ -101,10 +101,9 @@ namespace Translation
return phrase; return phrase;
} }
void Storage::setEncoding (const ToUTF8::FromType& encoding) void Storage::setEncoder(ToUTF8::Utf8Encoder* encoder)
{ {
mEncoding = encoding; mEncoder = encoder;
mEncoder.setEncoding(encoding);
} }
bool Storage::hasTranslation() const bool Storage::hasTranslation() const

@ -19,7 +19,7 @@ namespace Translation
// Standard form usually means nominative case // Standard form usually means nominative case
std::string topicStandardForm(const std::string& phrase) const; std::string topicStandardForm(const std::string& phrase) const;
void setEncoding (const ToUTF8::FromType& encoding); void setEncoder(ToUTF8::Utf8Encoder* encoder);
bool hasTranslation() const; bool hasTranslation() const;
@ -34,8 +34,7 @@ namespace Translation
void loadDataFromStream(ContainerType& container, std::istream& stream); void loadDataFromStream(ContainerType& container, std::istream& stream);
ToUTF8::FromType mEncoding; ToUTF8::Utf8Encoder* mEncoder;
ToUTF8::Utf8Encoder mEncoder;
ContainerType mCellNamesTranslations, mTopicIDs, mPhraseForms; ContainerType mCellNamesTranslations, mTopicIDs, mPhraseForms;
}; };
} }

Loading…
Cancel
Save