Further optimizations to UTF-8 saving code.

Originally committed to SVN as r2073.
This commit is contained in:
Rodrigo Braz Monteiro 2008-03-17 02:05:37 +00:00
parent c870465dee
commit 939b49b82d
4 changed files with 27 additions and 10 deletions

View File

@ -71,8 +71,8 @@ void Controller::SaveFile(const String filename,const String encoding)
{
const FormatPtr handler = FormatManager::GetFormatFromFilename(filename,true);
wxFFileOutputStream stream(filename);
wxBufferedOutputStream buffer(stream);
model.Save(buffer,handler,encoding);
//wxBufferedOutputStream buffer(stream);
model.Save(stream,handler,encoding);
}

View File

@ -49,6 +49,8 @@ TextFileWriter::TextFileWriter(wxOutputStream &stream,String enc)
{
// Setup
IsFirst = true;
buffer.resize(16384);
bufferPos = 0;
SetEncoding(enc);
}
@ -56,6 +58,8 @@ TextFileWriter::TextFileWriter(wxOutputStream &stream,String enc)
//////////////
// Destructor
TextFileWriter::~TextFileWriter() {
// Flush
if (bufferPos) file.Write(&buffer[0],(std::streamsize)bufferPos);
}
@ -64,7 +68,7 @@ TextFileWriter::~TextFileWriter() {
void TextFileWriter::WriteLineToFile(Gorgonsub::String line,bool addLineBreak) {
// Add line break
wxString temp = line;
if (addLineBreak) temp += _T("\r\n");
if (addLineBreak && Is16) temp += _T("\r\n");
// Add BOM if it's the first line and the target format is Unicode
if (IsFirst && IsUnicode) {
@ -76,8 +80,7 @@ void TextFileWriter::WriteLineToFile(Gorgonsub::String line,bool addLineBreak) {
// 16-bit
if (Is16) {
wxWCharBuffer buf = temp.wc_str(*conv);
if (!buf.data())
return;
if (!buf.data()) return;
size_t len = wcslen(buf.data());
file.Write((const char*)buf.data(),(std::streamsize)len*sizeof(wchar_t));
}
@ -85,12 +88,25 @@ void TextFileWriter::WriteLineToFile(Gorgonsub::String line,bool addLineBreak) {
// 8-bit
else {
if (encoding == _T("UTF-8")) {
// Calculate metrics
const wchar_t* src = temp.c_str();
//size_t len = GetUTF8Len(src);
size_t len = temp.Length() * 2 + 2;
if (buffer.size() < len) buffer.resize(len);
size_t toWrite = UTF16toUTF8(src,&buffer[0]);
file.Write(&buffer[0],(std::streamsize)toWrite);
if (addLineBreak) len += 2;
if (buffer.size()-bufferPos < len) {
// Flush
file.Write(&buffer[0],(std::streamsize)bufferPos);
bufferPos = 0;
// Resize if it still doesn't fit
if (buffer.size() < len) buffer.resize(len);
}
// Convert to UTF-8
bufferPos += UTF16toUTF8(src,&buffer[bufferPos]);
if (addLineBreak) {
buffer[bufferPos++] = '\r';
buffer[bufferPos++] = '\n';
}
}
else {
wxCharBuffer buf = temp.mb_str(*conv);

View File

@ -45,6 +45,7 @@ namespace Gorgonsub {
wxString encoding;
wxOutputStream &file;
std::vector<char> buffer;
size_t bufferPos;
shared_ptr<wxMBConv> conv;
bool Is16;

View File

@ -239,7 +239,7 @@ size_t Gorgonsub::UTF16toUTF8(const wchar_t *utf16,char *utf8)
wchar_t curChar = 1;
size_t value;
size_t written = 0;
for (size_t i=0;curChar;i++) {
for (size_t i=0;;i++) {
// Get next
curChar = utf16[i];