[BUG_FIXED] (Author: François-R Boyer) Fix saving UCS-2 text file corrupted bug.

git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@671 f5eea248-9336-0410-98b8-ebc06183d4e3
remotes/trunk
Don Ho 2010-09-25 18:14:22 +00:00
parent bb7cb36df9
commit 5be72543f8
3 changed files with 25 additions and 30 deletions

View File

@ -603,8 +603,15 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy) {
{ {
_pscratchTilla->execute(SCI_SETDOCPOINTER, 0, buffer->_doc); //generate new document _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, buffer->_doc); //generate new document
char data[blockSize + 1];
int lengthDoc = _pscratchTilla->getCurrentDocLen(); int lengthDoc = _pscratchTilla->getCurrentDocLen();
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
{
UnicodeConvertor.fwrite(buf, lengthDoc);
}
else
{
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
int grabSize; int grabSize;
for (int i = 0; i < lengthDoc; i += grabSize) for (int i = 0; i < lengthDoc; i += grabSize)
{ {
@ -612,20 +619,12 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy) {
if (grabSize > blockSize) if (grabSize > blockSize)
grabSize = blockSize; grabSize = blockSize;
_pscratchTilla->getText(data, i, i + grabSize);
if (encoding != -1)
{
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
int newDataLen = 0; int newDataLen = 0;
int incompleteMultibyteChar = 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; grabSize -= incompleteMultibyteChar;
UnicodeConvertor.fwrite(newData, newDataLen); UnicodeConvertor.fwrite(newData, newDataLen);
} }
else
{
UnicodeConvertor.fwrite(data, grabSize);
}
} }
UnicodeConvertor.fclose(); UnicodeConvertor.fclose();

View File

@ -262,7 +262,6 @@ Utf8_16_Write::Utf8_16_Write()
{ {
m_eEncoding = uni8Bit; m_eEncoding = uni8Bit;
m_pFile = NULL; m_pFile = NULL;
m_pBuf = NULL;
m_pNewBuf = NULL; m_pNewBuf = NULL;
m_bFirstWrite = true; m_bFirstWrite = true;
m_nBufSize = 0; m_nBufSize = 0;
@ -325,26 +324,24 @@ size_t Utf8_16_Write::fwrite(const void* p, size_t _size)
case uni16LE_NoBOM: case uni16LE_NoBOM:
case uni16BE: case uni16BE:
case uni16LE: { case uni16LE: {
if (_size > m_nBufSize) static const int bufSize = 64*1024;
{ utf16 buf[bufSize];
m_nBufSize = _size;
if (m_pBuf != NULL)
delete [] m_pBuf;
m_pBuf = NULL;
m_pBuf = new utf16[_size + 1];
}
Utf8_Iter iter8; Utf8_Iter iter8;
iter8.set(static_cast<const ubyte*>(p), _size, m_eEncoding); iter8.set(static_cast<const ubyte*>(p), _size, m_eEncoding);
utf16* pCur = m_pBuf; int bufIndex = 0;
while (iter8) {
for (; iter8; ++iter8) {
if (iter8.canGet()) { 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; break;
} }
default: default:

View File

@ -152,7 +152,6 @@ public:
protected: protected:
UniMode m_eEncoding; UniMode m_eEncoding;
FILE* m_pFile; FILE* m_pFile;
utf16* m_pBuf;
ubyte* m_pNewBuf; ubyte* m_pNewBuf;
size_t m_nBufSize; size_t m_nBufSize;
bool m_bFirstWrite; bool m_bFirstWrite;