From 5be72543f8ddbf52ef475d73cce9774962bd0f0e Mon Sep 17 00:00:00 2001 From: Don Ho Date: Sat, 25 Sep 2010 18:14:22 +0000 Subject: [PATCH] =?UTF-8?q?[BUG=5FFIXED]=20(Author:=20Fran=C3=A7ois-R=20Bo?= =?UTF-8?q?yer)=20Fix=20saving=20UCS-2=20text=20file=20corrupted=20bug.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@671 f5eea248-9336-0410-98b8-ebc06183d4e3 --- PowerEditor/src/ScitillaComponent/Buffer.cpp | 29 ++++++++++---------- PowerEditor/src/Utf8_16.cpp | 25 ++++++++--------- PowerEditor/src/Utf8_16.h | 1 - 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/PowerEditor/src/ScitillaComponent/Buffer.cpp b/PowerEditor/src/ScitillaComponent/Buffer.cpp index a39919c5c..936ce1a62 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScitillaComponent/Buffer.cpp @@ -603,29 +603,28 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy) { { _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, buffer->_doc); //generate new document - char data[blockSize + 1]; int lengthDoc = _pscratchTilla->getCurrentDocLen(); - int grabSize; - for (int i = 0; i < lengthDoc; i += grabSize) + char* buf = (char*)_pscratchTilla->execute(SCI_GETCHARACTERPOINTER); //to get characters directly from Scintilla buffer + if (encoding == -1) //no special encoding; can be handled directly by Utf8_16_Write { - grabSize = lengthDoc - i; - if (grabSize > blockSize) - grabSize = blockSize; - - _pscratchTilla->getText(data, i, i + grabSize); - if (encoding != -1) + UnicodeConvertor.fwrite(buf, lengthDoc); + } + else + { + WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); + int grabSize; + for (int i = 0; i < lengthDoc; i += grabSize) { - WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); + grabSize = lengthDoc - i; + if (grabSize > blockSize) + grabSize = blockSize; + int newDataLen = 0; int incompleteMultibyteChar = 0; - const char *newData = wmc->encode(SC_CP_UTF8, encoding, data, grabSize, &newDataLen, &incompleteMultibyteChar); + const char *newData = wmc->encode(SC_CP_UTF8, encoding, buf+i, grabSize, &newDataLen, &incompleteMultibyteChar); grabSize -= incompleteMultibyteChar; UnicodeConvertor.fwrite(newData, newDataLen); } - else - { - UnicodeConvertor.fwrite(data, grabSize); - } } UnicodeConvertor.fclose(); diff --git a/PowerEditor/src/Utf8_16.cpp b/PowerEditor/src/Utf8_16.cpp index 6bee0631e..cf5ce564d 100644 --- a/PowerEditor/src/Utf8_16.cpp +++ b/PowerEditor/src/Utf8_16.cpp @@ -262,7 +262,6 @@ Utf8_16_Write::Utf8_16_Write() { m_eEncoding = uni8Bit; m_pFile = NULL; - m_pBuf = NULL; m_pNewBuf = NULL; m_bFirstWrite = true; m_nBufSize = 0; @@ -325,26 +324,24 @@ size_t Utf8_16_Write::fwrite(const void* p, size_t _size) case uni16LE_NoBOM: case uni16BE: case uni16LE: { - if (_size > m_nBufSize) - { - m_nBufSize = _size; - if (m_pBuf != NULL) - delete [] m_pBuf; - m_pBuf = NULL; - m_pBuf = new utf16[_size + 1]; - } + static const int bufSize = 64*1024; + utf16 buf[bufSize]; Utf8_Iter iter8; iter8.set(static_cast(p), _size, m_eEncoding); - utf16* pCur = m_pBuf; - - for (; iter8; ++iter8) { + int bufIndex = 0; + while (iter8) { if (iter8.canGet()) { - *pCur++ = iter8.get(); + buf[bufIndex++] = iter8.get(); } + ++iter8; + if(bufIndex == bufSize || !iter8) { + if(!::fwrite(buf, bufIndex*sizeof(utf16), 1, m_pFile)) return 0; + bufIndex = 0; + } } - ret = ::fwrite(m_pBuf, (const char*)pCur - (const char*)m_pBuf, 1, m_pFile); + ret = 1; break; } default: diff --git a/PowerEditor/src/Utf8_16.h b/PowerEditor/src/Utf8_16.h index aa8600568..c47d964bd 100644 --- a/PowerEditor/src/Utf8_16.h +++ b/PowerEditor/src/Utf8_16.h @@ -152,7 +152,6 @@ public: protected: UniMode m_eEncoding; FILE* m_pFile; - utf16* m_pBuf; ubyte* m_pNewBuf; size_t m_nBufSize; bool m_bFirstWrite;