Merge branch 'feature-kmcompx' into chore/developer/cross-platform-fixups-for-wasm

This commit is contained in:
Marc Durdin 2023-03-09 16:29:25 +11:00 committed by GitHub
commit 7a57e2aba0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 33 deletions

View file

@ -1,6 +1,6 @@
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1
#include "pch.h"
#include "compfile.h"
#include <comperr.h>
#include "kmcmplib.h"
@ -8,6 +8,9 @@
#include <string>
#include "CheckFilenameConsistency.h"
#include "kmx_u16.h"
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
using std::experimental::filesystem::directory_iterator;
namespace kmcmp {
extern KMX_CHAR CompileDir[260]; // TODO: this should not be a fixed buffer
@ -59,7 +62,9 @@ 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
// 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;
@ -70,7 +75,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 +90,25 @@ 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::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;

View file

@ -352,7 +352,7 @@ extern "C" uint32_t kmcmp_CompileKeyboardFile(char* pszInfile,
PKMX_STR p;
if ((p = strrchr_LinWin(pszInfile)) != nullptr)
if ((p = strrchr_slash(pszInfile) != nullptr)
{
strncpy(kmcmp::CompileDir, pszInfile, (int)(p - pszInfile + 1)); // I3481
kmcmp::CompileDir[(int)(p - pszInfile + 1)] = 0;
@ -445,7 +445,7 @@ extern "C" uint32_t kmcmp_CompileKeyboardFileToBuffer(char* pszInfile, void* pfk
PKMX_STR p;
if ((p = strrchr_LinWin(pszInfile)) != nullptr)
if ((p = strrchr_slash(pszInfile)) != nullptr)
{
strncpy(kmcmp::CompileDir, pszInfile, (int)(p - pszInfile + 1)); // I3481
kmcmp::CompileDir[(int)(p - pszInfile + 1)] = 0;
@ -1333,7 +1333,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;
@ -3393,12 +3393,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)
@ -3417,7 +3425,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;

View file

@ -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

View file

@ -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, '\\');

View file

@ -33,8 +33,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);
@ -70,4 +70,4 @@ if (!(std::is_same<T,TT>::value))
#endif
return nfile;
}
#endif //KMX_U16_H
#endif //KMX_U16_H