Fix empty file with non-Unicode encoding cannot be saved issue

The PR fixes the regression due to the saving file API being changecd from POSIX functions to Win32 native API:
The old used function "fopen" using "wbc" as argument, according Microsoft document:
"w" 	Opens an empty file for writing. If the given file exists, its contents are destroyed.
(https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-160)
so to save a 0 length document, it was unecessary to "write nothing" explicitely on disk, since fopen did it for you.
Whereas our new implementation which uses Win32 native API passes "OPEN_ALWAYS" - that needs to "write nothing" explicitely.

Fix #10699, close #10702
This commit is contained in:
Don Ho 2021-10-21 18:08:04 +02:00
parent 38de8b2306
commit 1a02319a8a

View File

@ -1010,6 +1010,12 @@ SavingStatus FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool i
else else
{ {
WcharMbcsConvertor& wmc = WcharMbcsConvertor::getInstance(); WcharMbcsConvertor& wmc = WcharMbcsConvertor::getInstance();
if (lengthDoc == 0)
{
isWrittenSuccessful = UnicodeConvertor.writeFile(buf, 0);
}
else
{
int grabSize; int grabSize;
for (int i = 0; i < lengthDoc; i += grabSize) for (int i = 0; i < lengthDoc; i += grabSize)
{ {
@ -1023,8 +1029,7 @@ SavingStatus FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool i
grabSize -= incompleteMultibyteChar; grabSize -= incompleteMultibyteChar;
isWrittenSuccessful = UnicodeConvertor.writeFile(newData, static_cast<unsigned long>(newDataLen)); isWrittenSuccessful = UnicodeConvertor.writeFile(newData, static_cast<unsigned long>(newDataLen));
} }
if (lengthDoc == 0) }
isWrittenSuccessful = true;
} }
// check the language du fichier // check the language du fichier