From 31de9087734f049c82c790b79e6c51316cb575f4 Mon Sep 17 00:00:00 2001 From: Lexikos Date: Sat, 23 Dec 2023 10:13:34 +1000 Subject: [PATCH] Fixed file name comparisions by #include and FileInstall (non-compiled). Differences are as described for lstrcmpi (old) and CompareStringOrdinal with bIgnoreCase = true (new): https://learn.microsoft.com/en-us/windows/win32/intl/handling-sorting-in-your-applications#sort-strings-ordinally --- source/lib/file.cpp | 2 +- source/script.cpp | 2 +- source/util.h | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source/lib/file.cpp b/source/lib/file.cpp index c82555750..de4d3f69f 100644 --- a/source/lib/file.cpp +++ b/source/lib/file.cpp @@ -531,7 +531,7 @@ static bool FileInstallCopy(LPCTSTR aSource, LPCTSTR aDest, bool aOverwrite) SetCurrentDirectory(g_script.mFileDir); GetFullPathName(aSource, _countof(source_path), source_path, NULL); SetCurrentDirectory(g_WorkingDir); // Restore to proper value. - if (!lstrcmpi(source_path, dest_path) // Full paths are equal. + if (!ostrcmpi(source_path, dest_path) // Full paths are equal. && !(GetFileAttributes(source_path) & FILE_ATTRIBUTE_DIRECTORY)) // Source file exists and is not a directory (otherwise, an error should be thrown). return true; diff --git a/source/script.cpp b/source/script.cpp index 020f21dd3..e133b5e0f 100644 --- a/source/script.cpp +++ b/source/script.cpp @@ -1559,7 +1559,7 @@ ResultType Script::OpenIncludedFile(TextStream *&ts, LPCTSTR aFileSpec, bool aAl // to support automatic "include once" behavior. So just ignore repeats: if (!aAllowDuplicateInclude) for (int f = 0; f < source_file_index; ++f) // Here, source_file_index==Line::sSourceFileCount - if (!lstrcmpi(Line::sSourceFile[f], full_path)) // Case insensitive like the file system (testing shows that "Ä" == "ä" in the NTFS, which is hopefully how lstrcmpi works regardless of locale). + if (!ostrcmpi(Line::sSourceFile[f], full_path)) // Case insensitive like the file system (e.g. "Ä" == "ä" in the NTFS). return OK; // The path is copied into persistent memory further below, after the file has been opened, // in case the opening fails and aIgnoreLoadFailure==true. Initialize for the check below. diff --git a/source/util.h b/source/util.h index 890788cf6..b755fb95b 100644 --- a/source/util.h +++ b/source/util.h @@ -595,6 +595,10 @@ int FTOA(double aValue, LPTSTR aBuf, int aBufSize); // returns 0 on failure, but failure occurs only when parameter/flag is invalid, which should never happen in // this case. #define lstrcmpni(str1, len1, str2, len2) (CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, str1, (int)(len1), str2, (int)(len2)) - 2) // -2 for maintainability +// Compare string using the operating system uppercasing table, independent of locale but treating non-ASCII +// letters such as è and È as equivalent. Documentation indicates it will be consistent with NTFS file names. +#define ostrcmpni(str1, len1, str2, len2) (CompareStringOrdinal(str1, (int)(len1), str2, (int)(len2), TRUE) - 2) // -2 for maintainability +#define ostrcmpi(str1, str2) (ostrcmpni(str1, -1, str2, -1)) // The following macros simplify and make consistent the calls to MultiByteToWideChar().