Skip to content

Commit

Permalink
Improve code & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
donho committed Oct 11, 2024
1 parent cd45afc commit 0ca0348
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
7 changes: 4 additions & 3 deletions PowerEditor/src/MISC/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
#include <locale>
#include "StaticDialog.h"
#include "CustomFileDialog.h"

#include "FileInterface.h"
#include "Common.h"
#include "Utf8.h"
#include <Parameters.h>
#include "Parameters.h"
#include "Buffer.h"

using namespace std;
Expand Down Expand Up @@ -2001,4 +2000,6 @@ HANDLE createFileWaitSec(const wchar_t* filePath, DWORD accessParam, DWORD share
*isNetWorkProblem = data._isNetworkFailure;

return data._hFile;
}
}


36 changes: 23 additions & 13 deletions PowerEditor/src/ScintillaComponent/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<int64_t>(1LL << 20, fileSize / 6);

Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 0ca0348

Please sign in to comment.