From 1a02319a8a7426f5d2e1b14e6101b7b387ef3196 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Thu, 21 Oct 2021 18:08:04 +0200 Subject: [PATCH] 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 --- PowerEditor/src/ScintillaComponent/Buffer.cpp | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/PowerEditor/src/ScintillaComponent/Buffer.cpp b/PowerEditor/src/ScintillaComponent/Buffer.cpp index a2019f7f4..8cf6c51cb 100644 --- a/PowerEditor/src/ScintillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScintillaComponent/Buffer.cpp @@ -1010,21 +1010,26 @@ SavingStatus FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool i else { WcharMbcsConvertor& wmc = WcharMbcsConvertor::getInstance(); - int grabSize; - for (int i = 0; i < lengthDoc; i += grabSize) - { - grabSize = lengthDoc - i; - if (grabSize > blockSize) - grabSize = blockSize; - - int newDataLen = 0; - int incompleteMultibyteChar = 0; - const char *newData = wmc.encode(SC_CP_UTF8, encoding, buf+i, grabSize, &newDataLen, &incompleteMultibyteChar); - grabSize -= incompleteMultibyteChar; - isWrittenSuccessful = UnicodeConvertor.writeFile(newData, static_cast(newDataLen)); - } if (lengthDoc == 0) - isWrittenSuccessful = true; + { + isWrittenSuccessful = UnicodeConvertor.writeFile(buf, 0); + } + else + { + int grabSize; + for (int i = 0; i < lengthDoc; i += grabSize) + { + grabSize = lengthDoc - i; + if (grabSize > blockSize) + grabSize = blockSize; + + int newDataLen = 0; + int incompleteMultibyteChar = 0; + const char* newData = wmc.encode(SC_CP_UTF8, encoding, buf + i, grabSize, &newDataLen, &incompleteMultibyteChar); + grabSize -= incompleteMultibyteChar; + isWrittenSuccessful = UnicodeConvertor.writeFile(newData, static_cast(newDataLen)); + } + } } // check the language du fichier