ESMReader, ESMWriter: use Utf8Encoder

actorid
Emanuel Guevel 12 years ago
parent 9906c3051d
commit 02bf02f288

@ -13,6 +13,11 @@ ESM_Context ESMReader::getContext()
return mCtx;
}
ESMReader::ESMReader(void):
mBuffer(50*1024)
{
}
void ESMReader::restoreContext(const ESM_Context &rc)
{
// Reopen the file if necessary
@ -325,11 +330,21 @@ void ESMReader::getExact(void*x, int size)
std::string ESMReader::getString(int size)
{
char *ptr = ToUTF8::getBuffer(size);
mEsm->read(ptr, size);
size_t s = size;
if (mBuffer.size() <= s)
// Add some extra padding to reduce the chance of having to resize
// again later.
mBuffer.resize(3*s);
// And make sure the string is zero terminated
mBuffer[s] = 0;
// read ESM data
char *ptr = &mBuffer[0];
getExact(ptr, size);
// Convert to UTF8 and return
return ToUTF8::getUtf8(mEncoding);
return mEncoder.getUtf8(ptr, size);
}
void ESMReader::fail(const std::string &msg)
@ -350,6 +365,7 @@ void ESMReader::fail(const std::string &msg)
void ESMReader::setEncoding(const ToUTF8::FromType& encoding)
{
mEncoding = encoding;
mEncoder.setEncoding(encoding);
}
}

@ -20,6 +20,8 @@ class ESMReader
{
public:
ESMReader(void);
/*************************************************************************
*
* Public type definitions
@ -244,9 +246,13 @@ private:
// Special file signifier (see SpecialFile enum above)
int mSpf;
// Buffer for ESM strings
std::vector<char> mBuffer;
SaveData mSaveData;
MasterList mMasters;
ToUTF8::FromType mEncoding;
ToUTF8::Utf8Encoder mEncoder;
};
}
#endif

@ -157,12 +157,8 @@ void ESMWriter::writeHString(const std::string& data)
write("\0", 1);
else
{
char *ptr = ToUTF8::getBuffer(data.size()+1);
strncpy(ptr, &data[0], data.size());
ptr[data.size()] = '\0';
// Convert to UTF8 and return
std::string ascii = ToUTF8::getLegacyEnc(m_encoding);
std::string ascii = m_encoder.getLegacyEnc(data);
write(ascii.c_str(), ascii.size());
}
@ -207,6 +203,8 @@ void ESMWriter::setEncoding(const std::string& encoding)
// Default Latin encoding
m_encoding = ToUTF8::WINDOWS_1252;
}
m_encoder.setEncoding(m_encoding);
}
}

@ -95,6 +95,7 @@ private:
std::ostream* m_stream;
std::streampos m_headerPos;
ToUTF8::FromType m_encoding;
ToUTF8::Utf8Encoder m_encoder;
int m_recordCount;
HEDRstruct m_header;

Loading…
Cancel
Save