ESMReader, ESMWriter: use Utf8Encoder

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

@ -13,6 +13,11 @@ ESM_Context ESMReader::getContext()
return mCtx; return mCtx;
} }
ESMReader::ESMReader(void):
mBuffer(50*1024)
{
}
void ESMReader::restoreContext(const ESM_Context &rc) void ESMReader::restoreContext(const ESM_Context &rc)
{ {
// Reopen the file if necessary // Reopen the file if necessary
@ -325,11 +330,21 @@ void ESMReader::getExact(void*x, int size)
std::string ESMReader::getString(int size) std::string ESMReader::getString(int size)
{ {
char *ptr = ToUTF8::getBuffer(size); size_t s = size;
mEsm->read(ptr, 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 // Convert to UTF8 and return
return ToUTF8::getUtf8(mEncoding); return mEncoder.getUtf8(ptr, size);
} }
void ESMReader::fail(const std::string &msg) 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) void ESMReader::setEncoding(const ToUTF8::FromType& encoding)
{ {
mEncoding = encoding; mEncoding = encoding;
mEncoder.setEncoding(encoding);
} }
} }

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

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

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

Loading…
Cancel
Save