From 0ca0348e7fbdcad200b2cc9690f96cb1fd6e6af8 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Mon, 7 Oct 2024 03:10:57 +0200 Subject: [PATCH] Improve code & refactoring Close #15681 --- PowerEditor/src/MISC/Common/Common.cpp | 7 ++-- PowerEditor/src/ScintillaComponent/Buffer.cpp | 36 ++++++++++++------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index 4b86174d5..40c90bce8 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -22,11 +22,10 @@ #include #include "StaticDialog.h" #include "CustomFileDialog.h" - #include "FileInterface.h" #include "Common.h" #include "Utf8.h" -#include +#include "Parameters.h" #include "Buffer.h" using namespace std; @@ -2001,4 +2000,6 @@ HANDLE createFileWaitSec(const wchar_t* filePath, DWORD accessParam, DWORD share *isNetWorkProblem = data._isNetworkFailure; return data._hFile; -} \ No newline at end of file +} + + diff --git a/PowerEditor/src/ScintillaComponent/Buffer.cpp b/PowerEditor/src/ScintillaComponent/Buffer.cpp index 48ac7a051..e37a53573 100644 --- a/PowerEditor/src/ScintillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScintillaComponent/Buffer.cpp @@ -843,12 +843,22 @@ bool FileManager::reloadBuffer(BufferID id) // Set _isLoadedDirty false before calling "_pscratchTilla->execute(SCI_CLEARALL);" in loadFileData() to avoid setDirty in SCN_SAVEPOINTREACHED / SCN_SAVEPOINTLEFT //Get file size - FILE* fp = _wfopen(buf->getFullPathName(), L"rb"); - if (!fp) + int64_t fileSize = 0; + WIN32_FILE_ATTRIBUTE_DATA attributes{}; + getFileAttributesExWaitSec(buf->getFullPathName(), &attributes); + if (attributes.dwFileAttributes == INVALID_FILE_ATTRIBUTES) + { return false; - _fseeki64(fp, 0, SEEK_END); - int64_t fileSize = _ftelli64(fp); - fclose(fp); + } + else + { + LARGE_INTEGER size{}; + size.LowPart = attributes.nFileSizeLow; + size.HighPart = attributes.nFileSizeHigh; + + fileSize = size.QuadPart; + } + char* data = new char[blockSize + 8]; // +8 for incomplete multibyte char @@ -1561,10 +1571,7 @@ LangType FileManager::detectLanguageFromTextBegining(const unsigned char *data, bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * filename, char* data, Utf8_16_Read * unicodeConvertor, LoadedFileFormat& fileFormat) { - FILE *fp = _wfopen(filename, L"rb"); - if (!fp) - return false; - + // Check file size firstly // size/6 is the normal room Scintilla keeps for editing, but here we limit it to 1MiB when loading (maybe we want to load big files without editing them too much) int64_t bufferSizeRequested = fileSize + std::min(1LL << 20, fileSize / 6); @@ -1582,7 +1589,6 @@ bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * f L"File size problem", MB_OK | MB_APPLMODAL); - fclose(fp); return false; } else // x64 @@ -1602,13 +1608,17 @@ bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * f } else { - fclose(fp); return false; } } } } + FILE* fp = _wfopen(filename, L"rb"); + + if (!fp) + return false; + //Setup scratchtilla for new filedata _pscratchTilla->execute(SCI_SETSTATUS, SC_STATUS_OK); // reset error status _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, doc); @@ -1641,7 +1651,7 @@ bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * f bool success = true; EolType format = EolType::unknown; int sciStatus = SC_STATUS_OK; - wchar_t szException[64] = { '\0' }; + wchar_t szException[64] = {'\0'}; __try { // First allocate enough memory for the whole file (this will reduce memory copy during loading) @@ -1730,7 +1740,7 @@ bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * f } while (lenFile > 0); } - __except(EXCEPTION_EXECUTE_HANDLER) + __except (EXCEPTION_EXECUTE_HANDLER) { switch (sciStatus) {