mirror of https://github.com/odrling/Aegisub
Further optimizations to UTF-8 saving code.
Originally committed to SVN as r2073.
This commit is contained in:
parent
c870465dee
commit
939b49b82d
|
@ -71,8 +71,8 @@ void Controller::SaveFile(const String filename,const String encoding)
|
||||||
{
|
{
|
||||||
const FormatPtr handler = FormatManager::GetFormatFromFilename(filename,true);
|
const FormatPtr handler = FormatManager::GetFormatFromFilename(filename,true);
|
||||||
wxFFileOutputStream stream(filename);
|
wxFFileOutputStream stream(filename);
|
||||||
wxBufferedOutputStream buffer(stream);
|
//wxBufferedOutputStream buffer(stream);
|
||||||
model.Save(buffer,handler,encoding);
|
model.Save(stream,handler,encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,8 @@ TextFileWriter::TextFileWriter(wxOutputStream &stream,String enc)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
IsFirst = true;
|
IsFirst = true;
|
||||||
|
buffer.resize(16384);
|
||||||
|
bufferPos = 0;
|
||||||
SetEncoding(enc);
|
SetEncoding(enc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,6 +58,8 @@ TextFileWriter::TextFileWriter(wxOutputStream &stream,String enc)
|
||||||
//////////////
|
//////////////
|
||||||
// Destructor
|
// Destructor
|
||||||
TextFileWriter::~TextFileWriter() {
|
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) {
|
void TextFileWriter::WriteLineToFile(Gorgonsub::String line,bool addLineBreak) {
|
||||||
// Add line break
|
// Add line break
|
||||||
wxString temp = line;
|
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
|
// Add BOM if it's the first line and the target format is Unicode
|
||||||
if (IsFirst && IsUnicode) {
|
if (IsFirst && IsUnicode) {
|
||||||
|
@ -76,8 +80,7 @@ void TextFileWriter::WriteLineToFile(Gorgonsub::String line,bool addLineBreak) {
|
||||||
// 16-bit
|
// 16-bit
|
||||||
if (Is16) {
|
if (Is16) {
|
||||||
wxWCharBuffer buf = temp.wc_str(*conv);
|
wxWCharBuffer buf = temp.wc_str(*conv);
|
||||||
if (!buf.data())
|
if (!buf.data()) return;
|
||||||
return;
|
|
||||||
size_t len = wcslen(buf.data());
|
size_t len = wcslen(buf.data());
|
||||||
file.Write((const char*)buf.data(),(std::streamsize)len*sizeof(wchar_t));
|
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
|
// 8-bit
|
||||||
else {
|
else {
|
||||||
if (encoding == _T("UTF-8")) {
|
if (encoding == _T("UTF-8")) {
|
||||||
|
// Calculate metrics
|
||||||
const wchar_t* src = temp.c_str();
|
const wchar_t* src = temp.c_str();
|
||||||
//size_t len = GetUTF8Len(src);
|
|
||||||
size_t len = temp.Length() * 2 + 2;
|
size_t len = temp.Length() * 2 + 2;
|
||||||
|
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);
|
if (buffer.size() < len) buffer.resize(len);
|
||||||
size_t toWrite = UTF16toUTF8(src,&buffer[0]);
|
}
|
||||||
file.Write(&buffer[0],(std::streamsize)toWrite);
|
|
||||||
|
// Convert to UTF-8
|
||||||
|
bufferPos += UTF16toUTF8(src,&buffer[bufferPos]);
|
||||||
|
if (addLineBreak) {
|
||||||
|
buffer[bufferPos++] = '\r';
|
||||||
|
buffer[bufferPos++] = '\n';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
wxCharBuffer buf = temp.mb_str(*conv);
|
wxCharBuffer buf = temp.mb_str(*conv);
|
||||||
|
|
|
@ -45,6 +45,7 @@ namespace Gorgonsub {
|
||||||
wxString encoding;
|
wxString encoding;
|
||||||
wxOutputStream &file;
|
wxOutputStream &file;
|
||||||
std::vector<char> buffer;
|
std::vector<char> buffer;
|
||||||
|
size_t bufferPos;
|
||||||
|
|
||||||
shared_ptr<wxMBConv> conv;
|
shared_ptr<wxMBConv> conv;
|
||||||
bool Is16;
|
bool Is16;
|
||||||
|
|
|
@ -239,7 +239,7 @@ size_t Gorgonsub::UTF16toUTF8(const wchar_t *utf16,char *utf8)
|
||||||
wchar_t curChar = 1;
|
wchar_t curChar = 1;
|
||||||
size_t value;
|
size_t value;
|
||||||
size_t written = 0;
|
size_t written = 0;
|
||||||
for (size_t i=0;curChar;i++) {
|
for (size_t i=0;;i++) {
|
||||||
// Get next
|
// Get next
|
||||||
curChar = utf16[i];
|
curChar = utf16[i];
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue