[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,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();

View File

@ -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<const ubyte*>(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:

View File

@ -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;