From febafc62868b050195b352d66f85f653f92c37a8 Mon Sep 17 00:00:00 2001 From: Sabine Date: Wed, 1 Mar 2023 17:01:47 +0100 Subject: [PATCH 1/5] chore(developer): CheckFilenameConsistency/CHINT_FilenameHasDifferingCase, rename new functions --- .../kmcmplib/src/CheckFilenameConsistency.cpp | 43 ++++++++++--------- developer/src/kmcmplib/src/Compiler.cpp | 6 +-- .../src/kmcmplib/src/NamedCodeConstants.cpp | 2 +- developer/src/kmcmplib/src/kmx_u16.cpp | 14 ++++-- developer/src/kmcmplib/src/kmx_u16.h | 5 ++- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/developer/src/kmcmplib/src/CheckFilenameConsistency.cpp b/developer/src/kmcmplib/src/CheckFilenameConsistency.cpp index 3aade14b4a..3fb6c70d1e 100644 --- a/developer/src/kmcmplib/src/CheckFilenameConsistency.cpp +++ b/developer/src/kmcmplib/src/CheckFilenameConsistency.cpp @@ -1,6 +1,6 @@ +#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1 #include "pch.h" - #include "compfile.h" #include #include "kmcmpdll.h" @@ -8,6 +8,9 @@ #include #include "CheckFilenameConsistency.h" #include "kmx_u16.h" +#include +namespace fs = std::experimental::filesystem; +using std::experimental::filesystem::directory_iterator; namespace kmcmp { extern KMX_CHAR CompileDir[MAX_PATH]; @@ -59,7 +62,6 @@ KMX_DWORD CheckFilenameConsistency( KMX_CHAR const * Filename, bool ReportMissin KMX_DWORD CheckFilenameConsistency(KMX_WCHAR const * Filename, bool ReportMissingFile) { - // not ready yet: needs more attention-> common includes for non-Windows platforms KMX_WCHAR Name[_MAX_PATH], FName[_MAX_FNAME], Ext[_MAX_EXT]; intptr_t n; FILE* nfile; @@ -70,7 +72,7 @@ KMX_DWORD CheckFilenameConsistency(KMX_WCHAR const * Filename, bool ReportMissin u16ncat(Name, Filename, _countof(Name)); // I3481 } else - u16ncpy(Name, Filename, _countof(Name)); // I3481 // _S2 wcscpy_s(Name, _countof(Name), Filename); // I3481 + u16ncpy(Name, Filename, _countof(Name)); // I3481 const KMX_WCHAR* pName = Name; nfile = Open_File(pName, u"rb"); @@ -85,27 +87,26 @@ KMX_DWORD CheckFilenameConsistency(KMX_WCHAR const * Filename, bool ReportMissin } fclose(nfile); - const KMX_WCHAR* cptr1 = u16rchr_LinWin((const PKMX_WCHAR) Name); - + const KMX_WCHAR* cptr1 = u16rchr_slash((const PKMX_WCHAR) Name); cptr1++; -//TODO: sort out how to find common includes in non-Windows platforms: (Works for windows though) + const KMX_WCHAR* dir_file_16; - std::wstring Name_wstr = convert_pchar16T_To_wstr(Name); - const KMX_WCHART* Name_wchptr = Name_wstr.c_str(); - KMX_WCHAR fi_name_char16[260]; -#if defined(_WIN32) || defined(_WIN64) - _wfinddata_t fi; - n = _wfindfirst(Name_wchptr, &fi); - _findclose(n); - u16sprintf(fi_name_char16,_countof(fi.name),fi.name); -#else - #error Missing implementation for finding common includes -#endif - if (u16cmp(cptr1, fi_name_char16) != 0) { - u16sprintf(ErrExtraW,256,L"reference '%ls' does not match actual filename '%ls'", cptr1, &fi.name); - strcpy(ErrExtraLIB, wstrtostr2(ErrExtraW)); - AddWarning(CHINT_FilenameHasDifferingCase); + for (const auto & file : directory_iterator(kmcmp::CompileDir)) { + std::string dir_file_path{ file.path().u8string() }; + std::replace(dir_file_path.begin(), dir_file_path.end(), '\\', '/'); + std::u16string dir_file_path_str = u16string_from_string(dir_file_path); + const KMX_WCHAR* dir_file_path_16 = dir_file_path_str.c_str(); + dir_file_16 = u16rchr_slash(dir_file_path_16); + dir_file_16++; + + if (u16icmp(cptr1, dir_file_16) == 0) { + if (u16cmp(cptr1, dir_file_16) != 0) { + u16sprintf(ErrExtraW, 256, L"reference '%ls' does not match actual filename '%ls'", cptr1, dir_file_16); + strcpy(ErrExtraLIB, wstrtostr2(ErrExtraW)); + AddWarning(CHINT_FilenameHasDifferingCase); + } + } } return CERR_None; diff --git a/developer/src/kmcmplib/src/Compiler.cpp b/developer/src/kmcmplib/src/Compiler.cpp index bc07ef8bb6..5090f6d82e 100644 --- a/developer/src/kmcmplib/src/Compiler.cpp +++ b/developer/src/kmcmplib/src/Compiler.cpp @@ -354,7 +354,7 @@ extern "C" BOOL __declspec(dllexport) kmcmp_CompileKeyboardFile(PKMX_STR pszInfi PKMX_STR p; - if (p = strrchr_LinWin(pszInfile)) + if (p = strrchr_slash(pszInfile)) { strncpy_s(kmcmp::CompileDir, _countof(kmcmp::CompileDir), pszInfile, (INT_PTR)(p - pszInfile + 1)); // I3481 kmcmp::CompileDir[(INT_PTR)(p - pszInfile + 1)] = 0; @@ -445,7 +445,7 @@ extern "C" BOOL __declspec(dllexport) kmcmp_CompileKeyboardFileToBuffer(PKMX_ST PKMX_STR p; - if (p = strrchr_LinWin(pszInfile)) + if (p = strrchr_slash(pszInfile)) { strncpy_s(kmcmp::CompileDir, _countof(kmcmp::CompileDir), pszInfile, (INT_PTR)(p - pszInfile + 1)); // I3481 kmcmp::CompileDir[(INT_PTR)(p - pszInfile + 1)] = 0; @@ -1350,7 +1350,7 @@ KMX_DWORD ProcessSystemStore(PFILE_KEYBOARD fk, KMX_DWORD SystemID, PFILE_STORE // Strip path from the store, leaving bare filename only p = sp->dpString; - KMX_WCHAR *pp = (KMX_WCHAR*) u16rchr_LinWin((const PKMX_WCHAR) p); + KMX_WCHAR *pp = (KMX_WCHAR*) u16rchr_slash((const PKMX_WCHAR) p); if (!pp) { pp = p; diff --git a/developer/src/kmcmplib/src/NamedCodeConstants.cpp b/developer/src/kmcmplib/src/NamedCodeConstants.cpp index a4d3b00dfa..f35f369dd3 100644 --- a/developer/src/kmcmplib/src/NamedCodeConstants.cpp +++ b/developer/src/kmcmplib/src/NamedCodeConstants.cpp @@ -188,7 +188,7 @@ KMX_BOOL NamedCodeConstants::LoadFile(const KMX_CHAR *filename) // Finally look in kmcmpdll.dll directory GetModuleFileName(0, buf, buf_size); - KMX_CHAR *p = strrchr_LinWin(buf); + KMX_CHAR *p = strrchr_slash(buf); if(p) p++; else diff --git a/developer/src/kmcmplib/src/kmx_u16.cpp b/developer/src/kmcmplib/src/kmx_u16.cpp index fae64435f4..c78776c0b9 100644 --- a/developer/src/kmcmplib/src/kmx_u16.cpp +++ b/developer/src/kmcmplib/src/kmx_u16.cpp @@ -63,7 +63,7 @@ void u16sprintf(KMX_WCHAR * dst, const size_t sz, const wchar_t* fmt, ...) { } std::wstring convert_pchar16T_To_wstr(KMX_WCHAR Name[_MAX_PATH]){ - // convert char16_t* -> std::u16string -> std::string -> std::wstring + // convert char16_t* -> std::u16string -> std::string // char16_t* -> std::u16string std::u16string u16str(Name); // std::u16string -> std::string @@ -73,6 +73,14 @@ void u16sprintf(KMX_WCHAR * dst, const size_t sz, const wchar_t* fmt, ...) { return wstr; } +std::string convert_pchar16T_To_str(KMX_WCHAR Name[_MAX_PATH]){ + // convert char16_t* -> std::wstring -> std::string - + // char16_t* -> std::wstring + std::wstring Name_ws = u16fmt(Name); + // std::wstring -> std::string + std::string Name_s = string_from_wstring((std::wstring const)Name_ws ); + return Name_s; +} long int u16tol(const KMX_WCHAR* str, KMX_WCHAR** endptr, int base) { @@ -113,7 +121,7 @@ const KMX_WCHAR * u16ncat(KMX_WCHAR *dst, const KMX_WCHAR *src, size_t max) { return o; } -const KMX_WCHAR* u16rchr_LinWin(KMX_WCHAR const* Name) +const KMX_WCHAR* u16rchr_slash(KMX_WCHAR const* Name) { const KMX_WCHAR* cp = NULL; cp = u16rchr(Name, '\\'); @@ -122,7 +130,7 @@ const KMX_WCHAR* u16rchr_LinWin(KMX_WCHAR const* Name) return cp; } -KMX_CHAR* strrchr_LinWin(KMX_CHAR* Name) +KMX_CHAR* strrchr_slash(KMX_CHAR* Name) { KMX_CHAR* cp = NULL; cp = strrchr(Name, '\\'); diff --git a/developer/src/kmcmplib/src/kmx_u16.h b/developer/src/kmcmplib/src/kmx_u16.h index 2c75961092..9f4460edc3 100644 --- a/developer/src/kmcmplib/src/kmx_u16.h +++ b/developer/src/kmcmplib/src/kmx_u16.h @@ -17,6 +17,7 @@ std::wstring u16fmt(const KMX_WCHAR * str); void u16sprintf(KMX_WCHAR * dst, const size_t sz, const wchar_t* fmt, ...) ; std::wstring convert_pchar16T_To_wstr(KMX_WCHAR Name[_MAX_PATH]); +std::string convert_pchar16T_To_str(KMX_WCHAR Name[_MAX_PATH]); size_t u16len(const KMX_WCHAR *p); int u16cmp(const KMX_WCHAR *p, const KMX_WCHAR *q); @@ -33,8 +34,8 @@ KMX_WCHAR * u16tok(KMX_WCHAR* p, KMX_WCHAR* ch, KMX_WCHAR** ctx) ; long int u16tol(const KMX_WCHAR* str, KMX_WCHAR** endptr, int base) ; double u16tof( KMX_WCHAR* str); -KMX_CHAR* strrchr_LinWin(KMX_CHAR* Name); -const KMX_WCHAR* u16rchr_LinWin(KMX_WCHAR const* Name); +KMX_CHAR* strrchr_slash(KMX_CHAR* Name); +const KMX_WCHAR* u16rchr_slash(KMX_WCHAR const* Name); std::string toHex(int num1); PKMX_STR wstrtostr2(PKMX_WCHAR in); From 680398057a4584143a5f84e8193be6d845e7292b Mon Sep 17 00:00:00 2001 From: Sabine Date: Wed, 1 Mar 2023 17:03:34 +0100 Subject: [PATCH 2/5] chore(developer): CheckFilenameConsistency/CHINT_FilenameHasDifferingCase, rename new functions --- developer/src/kmcmplib/src/kmx_u16.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/developer/src/kmcmplib/src/kmx_u16.h b/developer/src/kmcmplib/src/kmx_u16.h index 9f4460edc3..fae2f4946c 100644 --- a/developer/src/kmcmplib/src/kmx_u16.h +++ b/developer/src/kmcmplib/src/kmx_u16.h @@ -71,4 +71,4 @@ if (!(std::is_same::value)) #endif return nfile; } -#endif //KMX_U16_H \ No newline at end of file +#endif //KMX_U16_H From 750eb6b96c305e736d4ca1e7e52f9f14591d98b8 Mon Sep 17 00:00:00 2001 From: Sabine Date: Wed, 1 Mar 2023 17:03:34 +0100 Subject: [PATCH 3/5] chore(developer): CheckFilenameConsistency/CHINT_FilenameHasDifferingCase, rename new functions --- developer/src/kmcmplib/src/CheckFilenameConsistency.cpp | 4 +++- developer/src/kmcmplib/src/kmx_u16.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/developer/src/kmcmplib/src/CheckFilenameConsistency.cpp b/developer/src/kmcmplib/src/CheckFilenameConsistency.cpp index 3fb6c70d1e..9f1aa5d33d 100644 --- a/developer/src/kmcmplib/src/CheckFilenameConsistency.cpp +++ b/developer/src/kmcmplib/src/CheckFilenameConsistency.cpp @@ -62,6 +62,9 @@ KMX_DWORD CheckFilenameConsistency( KMX_CHAR const * Filename, bool ReportMissin KMX_DWORD CheckFilenameConsistency(KMX_WCHAR const * Filename, bool ReportMissingFile) { + // Comment for non-windows platforms: If files are different in casing only CWARN_MissingFile + // will be added. CHINT_FilenameHasDifferingCase will not be added on those platforms. + KMX_WCHAR Name[_MAX_PATH], FName[_MAX_FNAME], Ext[_MAX_EXT]; intptr_t n; FILE* nfile; @@ -94,7 +97,6 @@ KMX_DWORD CheckFilenameConsistency(KMX_WCHAR const * Filename, bool ReportMissin for (const auto & file : directory_iterator(kmcmp::CompileDir)) { std::string dir_file_path{ file.path().u8string() }; - std::replace(dir_file_path.begin(), dir_file_path.end(), '\\', '/'); std::u16string dir_file_path_str = u16string_from_string(dir_file_path); const KMX_WCHAR* dir_file_path_16 = dir_file_path_str.c_str(); dir_file_16 = u16rchr_slash(dir_file_path_16); diff --git a/developer/src/kmcmplib/src/kmx_u16.h b/developer/src/kmcmplib/src/kmx_u16.h index 9f4460edc3..fae2f4946c 100644 --- a/developer/src/kmcmplib/src/kmx_u16.h +++ b/developer/src/kmcmplib/src/kmx_u16.h @@ -71,4 +71,4 @@ if (!(std::is_same::value)) #endif return nfile; } -#endif //KMX_U16_H \ No newline at end of file +#endif //KMX_U16_H From 349d970a390585eb7df1adfa6616cce1892d20e2 Mon Sep 17 00:00:00 2001 From: Sabine Date: Tue, 7 Mar 2023 10:29:19 +0100 Subject: [PATCH 4/5] chore(developer): undo previous change in Readline --- developer/src/kmcmplib/src/Compiler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/developer/src/kmcmplib/src/Compiler.cpp b/developer/src/kmcmplib/src/Compiler.cpp index 66a3481875..2aac4c4cad 100644 --- a/developer/src/kmcmplib/src/Compiler.cpp +++ b/developer/src/kmcmplib/src/Compiler.cpp @@ -3413,7 +3413,7 @@ KMX_DWORD ReadLine(FILE* fp_in , PKMX_WCHAR wstr, KMX_BOOL PreProcess) *p = L' '; continue; } - if((*p == L'\\') || (*p == L'/')) { + if((*p == L'\\')) { LineCarry = TRUE; *p = L' '; continue; From 2b19fecce5261d88fcd1d6c05cf7f6682c24adc3 Mon Sep 17 00:00:00 2001 From: Sabine Date: Tue, 7 Mar 2023 11:06:58 +0100 Subject: [PATCH 5/5] chore(developer): use \n as line endings (instead of \r\n) --- developer/src/kmcmplib/src/Compiler.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/developer/src/kmcmplib/src/Compiler.cpp b/developer/src/kmcmplib/src/Compiler.cpp index 2aac4c4cad..fed75a4d7a 100644 --- a/developer/src/kmcmplib/src/Compiler.cpp +++ b/developer/src/kmcmplib/src/Compiler.cpp @@ -3389,12 +3389,20 @@ KMX_DWORD ReadLine(FILE* fp_in , PKMX_WCHAR wstr, KMX_BOOL PreProcess) if (cur == fsize) - // S: Is replacing "\r\n" with "\n" here sufficient or do we need changes at other places as well when we skip "\r"? + // \r\n is still added here even though Linux doesn`t use \r. + // This is to ensure to still have a working windows-only-version u16ncat(str, u"\r\n", _countof(str)); // I3481 // Always a "\r\n" to the EOF, avoids funny bugs - // u16ncat(str, u"\n", _countof(str)); // I3481 if (len == 0) return CERR_EndOfFile; + // neccessary to add this block for using on non-windows platforms (removes all \r for platforms that use \n instead of \r\n) + for (p = str, n = 0; n < len; n++, p++) { + if (*p == L'\r') + *p = L' '; + } + + // \r is still left in this block even though Linux doesn`t use \r. + // This is to ensure to still have a working windows-only-version for (p = str, n = 0; n < len; n++, p++) { if (currentQuotes != 0)