Skip to content

Commit

Permalink
Rename variable & enhance the code
Browse files Browse the repository at this point in the history
  • Loading branch information
donho committed Oct 12, 2024
1 parent 3b6f22b commit 5e3ee3e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
34 changes: 18 additions & 16 deletions PowerEditor/src/MISC/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,19 +1779,20 @@ struct GetDiskFreeSpaceParamResult
std::wstring _dirPath;
ULARGE_INTEGER _freeBytesForUser {};
DWORD _result = FALSE;
bool _isNetworkFailure = true;
bool _isTimeoutReached = true;

GetDiskFreeSpaceParamResult(wstring dirPath) : _dirPath(dirPath) {};
};

DWORD WINAPI getDiskFreeSpaceExWorker(void* data)
{
GetDiskFreeSpaceParamResult* inAndOut = static_cast<GetDiskFreeSpaceParamResult*>(data);
inAndOut->_result = ::GetDiskFreeSpaceExW(inAndOut->_dirPath.c_str(), &(inAndOut->_freeBytesForUser), nullptr, nullptr);
inAndOut->_isNetworkFailure = false;
inAndOut->_isTimeoutReached = false;
return ERROR_SUCCESS;
};

DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait, bool* isNetWorkProblem)
DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait, bool* isTimeoutReached)
{
GetDiskFreeSpaceParamResult data(dirPath);

Expand Down Expand Up @@ -1819,8 +1820,8 @@ DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBy

*freeBytesForUser = data._freeBytesForUser;

if (isNetWorkProblem != nullptr)
*isNetWorkProblem = data._isNetworkFailure;
if (isTimeoutReached != nullptr)
*isTimeoutReached = data._isTimeoutReached;

return data._result;
}
Expand All @@ -1833,7 +1834,8 @@ struct GetAttrExParamResult
wstring _filePath;
WIN32_FILE_ATTRIBUTE_DATA _attributes{};
DWORD _result = FALSE;
bool _isNetworkFailure = true;
bool _isTimeoutReached = true;

GetAttrExParamResult(wstring filePath): _filePath(filePath) {
_attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
}
Expand All @@ -1843,11 +1845,11 @@ DWORD WINAPI getFileAttributesExWorker(void* data)
{
GetAttrExParamResult* inAndOut = static_cast<GetAttrExParamResult*>(data);
inAndOut->_result = ::GetFileAttributesEx(inAndOut->_filePath.c_str(), GetFileExInfoStandard, &(inAndOut->_attributes));
inAndOut->_isNetworkFailure = false;
inAndOut->_isTimeoutReached = false;
return ERROR_SUCCESS;
};

DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait, bool* isNetWorkProblem)
DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait, bool* isTimeoutReached)
{
GetAttrExParamResult data(filePath);

Expand Down Expand Up @@ -1875,29 +1877,29 @@ DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBU

*fileAttr = data._attributes;

if (isNetWorkProblem != nullptr)
*isNetWorkProblem = data._isNetworkFailure;
if (isTimeoutReached != nullptr)
*isTimeoutReached = data._isTimeoutReached;

return data._result;
}

bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait, bool* isNetWorkProblem)
bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait, bool* isTimeoutReached)
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
getFileAttributesExWithTimeout(filePath, &attributes, milliSec2wait, isNetWorkProblem);
getFileAttributesExWithTimeout(filePath, &attributes, milliSec2wait, isTimeoutReached);
return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && !(attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
}

bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait, bool* isNetWorkProblem)
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait, bool* isTimeoutReached)
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
getFileAttributesExWithTimeout(dirPath, &attributes, milliSec2wait, isNetWorkProblem);
getFileAttributesExWithTimeout(dirPath, &attributes, milliSec2wait, isTimeoutReached);
return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && (attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
}

bool doesPathExist(const wchar_t* path, DWORD milliSec2wait, bool* isNetWorkProblem)
bool doesPathExist(const wchar_t* path, DWORD milliSec2wait, bool* isTimeoutReached)
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
getFileAttributesExWithTimeout(path, &attributes, milliSec2wait, isNetWorkProblem);
getFileAttributesExWithTimeout(path, &attributes, milliSec2wait, isTimeoutReached);
return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES);
}
10 changes: 5 additions & 5 deletions PowerEditor/src/MISC/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ class Version final
};


DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);

bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
bool doesPathExist(const wchar_t* path, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
bool doesPathExist(const wchar_t* path, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
6 changes: 3 additions & 3 deletions PowerEditor/src/MISC/Common/FileInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Win32_IO_File::Win32_IO_File(const wchar_t *fname)
WIN32_FILE_ATTRIBUTE_DATA attributes_original{};
DWORD dispParam = CREATE_ALWAYS;
bool fileExists = false;
bool hasNetworkProblem = false;
bool isTimeoutReached = false;
// Store the file creation date & attributes for a possible use later...
if (getFileAttributesExWithTimeout(fname, &attributes_original, 0, &hasNetworkProblem))
if (getFileAttributesExWithTimeout(fname, &attributes_original, 0, &isTimeoutReached))
{
fileExists = (attributes_original.dwFileAttributes != INVALID_FILE_ATTRIBUTES);
}
Expand All @@ -54,7 +54,7 @@ Win32_IO_File::Win32_IO_File(const wchar_t *fname)
else
{
bool isFromNetwork = PathIsNetworkPath(fname);
if (isFromNetwork && hasNetworkProblem) // The file doesn't exist, and the file is a network file, plus the network problem has been detected due to timeout
if (isFromNetwork && isTimeoutReached) // The file doesn't exist, and the file is a network file, plus the network problem has been detected due to timeout
return; // In this case, we don't call createFile to prevent hanging
}

Expand Down
1 change: 1 addition & 0 deletions PowerEditor/src/Notepad_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7762,6 +7762,7 @@ static const QuoteParams quotes[] =
{L"Notepad++ #1", QuoteParams::rapid, true, SC_CP_UTF8, L_TEXT, L"I hate reading other people's code.\nSo I wrote mine, made it as open source project, and watch others suffer."},
{L"Notepad++ #2", QuoteParams::rapid, true, SC_CP_UTF8, L_TEXT, L"Good programmers use Notepad++ to code.\nExtreme programmers use MS Word to code, in Comic Sans, center aligned."},
{L"Notepad++ #3", QuoteParams::rapid, true, SC_CP_UTF8, L_TEXT, L"The best things in life are free.\nNotepad++ is free.\nSo Notepad++ is the best.\n"},
{L"Notepad++ #4", QuoteParams::rapid, true, SC_CP_UTF8, L_TEXT, L"Whatever you do, always give 100%.\nUnless you're donating to Notepad++, then 50% is OK.\nhttps://notepad-plus-plus.org/donate/\n"},
{L"Richard Stallman", QuoteParams::rapid, true, SC_CP_UTF8, L_TEXT, L"If I'm the Father of Open Source, it was conceived through artificial insemination using stolen sperm without my knowledge or consent."},
{L"Martin Golding", QuoteParams::rapid, true, SC_CP_UTF8, L_TEXT, L"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."},
{L"L. Peter Deutsch", QuoteParams::slow, false, SC_CP_UTF8, L_TEXT, L"To iterate is human, to recurse divine."},
Expand Down
2 changes: 1 addition & 1 deletion PowerEditor/src/ScintillaComponent/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ SavingStatus FileManager::saveBuffer(BufferID id, const wchar_t* filename, bool

WIN32_FILE_ATTRIBUTE_DATA attributes{};
getFileAttributesExWithTimeout(fullpath, &attributes);
if (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES)
if (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && !(attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
isHiddenOrSys = (attributes.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
if (isHiddenOrSys)
Expand Down

0 comments on commit 5e3ee3e

Please sign in to comment.