mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-10 19:05:31 +00:00
chore(developer): use char16_t in CheckFileNameConsistency
This commit is contained in:
parent
e8ba5c1702
commit
197bd6cee3
5 changed files with 74 additions and 49 deletions
|
|
@ -88,7 +88,7 @@ int main(int argc, char *argv[])
|
|||
if (error_vec[i] == error_val)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
return 4;
|
||||
}
|
||||
else
|
||||
return 1; // no CERR_ in Name => CompileKeyboardFile failed
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ bool IsRelativePath(KMX_CHAR const * p) {
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
bool IsRelativePath(KMX_WCHART const * p) {
|
||||
bool IsRelativePath(KMX_WCHAR const * p) {
|
||||
// Relative path (returns TRUE):
|
||||
// ..\...\BITMAP.BMP
|
||||
// PATH\BITMAP.BMP
|
||||
|
|
@ -48,40 +48,50 @@ bool IsRelativePath(KMX_WCHART const * p) {
|
|||
// \\SERVER\SHARE\...\BITMAP.BMP
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (*p == L'\\') return FALSE;
|
||||
if (*p == u'\\') return FALSE;
|
||||
#else
|
||||
if (*p == L'/') return FALSE;
|
||||
if (*p == u'/') return FALSE;
|
||||
#endif
|
||||
|
||||
if (*p && *(p + 1) == L':') return FALSE;
|
||||
if (*p && *(p + 1) == u':') return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
KMX_DWORD CheckFilenameConsistency( KMX_CHAR const * Filename, BOOL ReportMissingFile) {
|
||||
PKMX_WCHAR WFilename = strtowstr(( KMX_CHAR *)Filename);
|
||||
KMX_DWORD const result = CheckFilenameConsistency(u16fmt(WFilename).c_str(), ReportMissingFile);
|
||||
delete WFilename;
|
||||
KMX_DWORD const result = CheckFilenameConsistency(WFilename, ReportMissingFile);
|
||||
delete WFilename;
|
||||
return result;
|
||||
}
|
||||
|
||||
KMX_DWORD CheckFilenameConsistency(KMX_WCHART const * Filename, bool ReportMissingFile) {
|
||||
KMX_WCHART Name[_MAX_PATH], FName[_MAX_FNAME], Ext[_MAX_EXT];
|
||||
KMX_DWORD CheckFilenameConsistency(KMX_WCHAR const * Filename, bool ReportMissingFile) {
|
||||
KMX_WCHAR Name[_MAX_PATH], FName[_MAX_FNAME], Ext[_MAX_EXT];
|
||||
KMX_WCHAR ErrExtra[256];
|
||||
intptr_t n;
|
||||
|
||||
if (IsRelativePath(Filename)) {
|
||||
PKMX_WCHAR WCompileDir = strtowstr(CompileDir);
|
||||
wcscpy_s(Name, _countof(Name), u16fmt(WCompileDir).c_str()); // I3481
|
||||
wcscat_s(Name, _countof(Name), Filename); // I3481
|
||||
u16ncpy(Name, WCompileDir, _countof(Name)); // I3481
|
||||
u16ncat(Name, Filename, _countof(Name)); // I3481
|
||||
}
|
||||
else {
|
||||
wcscpy_s(Name, _countof(Name), Filename); // I3481
|
||||
u16ncpy(Name, Filename, _countof(Name)); // I3481 // _S2 wcscpy_s(Name, _countof(Name), Filename); // I3481
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
// convert char16_t to wchar_t*
|
||||
// char16_t -> std::u16string
|
||||
std::u16string u16str(Name);
|
||||
// std::u16string -> std::string
|
||||
std::string stri = string_from_u16string(u16str);
|
||||
// std::string -> std::wstring
|
||||
std::wstring wstr = wstring_from_string(stri);
|
||||
// std::wstring -> wchar_t*
|
||||
const KMX_WCHART* wchptr = wstr.c_str();
|
||||
|
||||
_wfinddata_t fi;
|
||||
n = _wfindfirst(Name, &fi);
|
||||
n = _wfindfirst(wchptr, &fi);
|
||||
_findclose(n);
|
||||
#else
|
||||
n= access(Name,F_OK);
|
||||
|
|
@ -96,15 +106,18 @@ KMX_DWORD CheckFilenameConsistency(KMX_WCHART const * Filename, bool ReportMissi
|
|||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
const wchar_t* cptr1 = wcsrchr(Name, '\\');
|
||||
const KMX_WCHAR* cptr1 = u16chr(Name, '\\'); // _S2 const wchar_t* cptr1 = wcsrchr(Name, '\\');
|
||||
#else
|
||||
const wchar_t* cptr1 = wcsrchr(Name, '/');
|
||||
const KMX_WCHAR* cptr1 = u16rchr(Name, '/');
|
||||
#endif
|
||||
cptr1++;
|
||||
|
||||
//TODO: sort out how to find common includes in non-Windows platforms:
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (wcscmp(cptr1, fi.name) != 0) {
|
||||
KMX_WCHAR fi_name_char16[260];
|
||||
u16sprintf(fi_name_char16,_countof(fi.name),fi.name);
|
||||
|
||||
if (u16cmp(cptr1, fi_name_char16) != 0) {
|
||||
u16sprintf(ErrExtra,_countof(ErrExtra),L"reference '%ls' does not match actual filename '%ls'", cptr1, &fi.name);
|
||||
AddWarning(CHINT_FilenameHasDifferingCase);
|
||||
}
|
||||
|
|
@ -119,17 +132,20 @@ KMX_DWORD CheckFilenameConsistencyForCalls(PFILE_KEYBOARD fk) {
|
|||
// where store(DllFunction) "my.dll:func" will look for a
|
||||
// file called function.call_js. This is ripe for rewrite!
|
||||
// But let's check what we have anyway
|
||||
|
||||
PFILE_STORE sp;
|
||||
DWORD i, msg;
|
||||
for (i = 0, sp = fk->dpStoreArray; i < fk->cxStoreArray; i++, sp++) {
|
||||
if (!sp->fIsCall) continue;
|
||||
|
||||
const std::wstring callsite(u16fmt(sp->dpString).c_str());
|
||||
const std::u16string callsite(sp->dpString);
|
||||
const auto colon = callsite.find(':');
|
||||
if (colon == std::wstring::npos) continue;
|
||||
if (colon == std::u16string::npos) continue;
|
||||
|
||||
auto func1 = callsite.substr(colon + 1);
|
||||
std::u16string str_js(u".call_js");
|
||||
std::u16string func = func1+ str_js;
|
||||
|
||||
auto func = callsite.substr(colon + 1);
|
||||
func.append(L".call_js");
|
||||
if ((msg = CheckFilenameConsistency(func.c_str(), FALSE)) != CERR_None) {
|
||||
return msg;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
KMX_DWORD CheckFilenameConsistencyForCalls(PFILE_KEYBOARD fk);
|
||||
KMX_DWORD CheckFilenameConsistency(KMX_CHAR const * Filename, bool ReportMissingFile);
|
||||
KMX_DWORD CheckFilenameConsistency(KMX_WCHART const * Filename, bool ReportMissingFile);
|
||||
KMX_DWORD CheckFilenameConsistency(KMX_WCHAR const * Filename, bool ReportMissingFile);
|
||||
|
||||
bool IsRelativePath(KMX_CHAR const * p);
|
||||
bool IsRelativePath(KMX_WCHART const * p);
|
||||
bool IsRelativePath(KMX_WCHAR const * p);
|
||||
|
|
|
|||
|
|
@ -1348,7 +1348,7 @@ KMX_DWORD ProcessSystemStore(PFILE_KEYBOARD fk, KMX_DWORD SystemID, PFILE_STORE
|
|||
delete[] sp->dpString;
|
||||
sp->dpString = q;
|
||||
|
||||
if ((msg = CheckFilenameConsistency(u16fmt(sp->dpString).c_str(), FALSE)) != CERR_None) {
|
||||
if ((msg = CheckFilenameConsistency( (sp->dpString), FALSE)) != CERR_None) {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
|
@ -1360,15 +1360,15 @@ KMX_DWORD ProcessSystemStore(PFILE_KEYBOARD fk, KMX_DWORD SystemID, PFILE_STORE
|
|||
|
||||
case TSS_KMW_HELPFILE:
|
||||
case TSS_KMW_EMBEDJS:
|
||||
VERIFY_KEYBOARD_VERSION(fk, VERSION_70, CERR_70FeatureOnly);
|
||||
if ((msg = CheckFilenameConsistency(u16fmt(sp->dpString).c_str(), FALSE)) != CERR_None) {
|
||||
VERIFY_KEYBOARD_VERSION(fk, VERSION_70, CERR_70FeatureOnly);
|
||||
if ((msg = CheckFilenameConsistency(sp->dpString, FALSE)) != CERR_None) {
|
||||
return msg;
|
||||
}
|
||||
break;
|
||||
|
||||
case TSS_KMW_EMBEDCSS:
|
||||
VERIFY_KEYBOARD_VERSION(fk, VERSION_90, CERR_90FeatureOnlyEmbedCSS);
|
||||
if ((msg = CheckFilenameConsistency(u16fmt(sp->dpString).c_str(), FALSE)) != CERR_None) {
|
||||
if ((msg = CheckFilenameConsistency(sp->dpString, FALSE)) != CERR_None) {
|
||||
return msg;
|
||||
}
|
||||
break;
|
||||
|
|
@ -1420,7 +1420,7 @@ KMX_DWORD ProcessSystemStore(PFILE_KEYBOARD fk, KMX_DWORD SystemID, PFILE_STORE
|
|||
|
||||
case TSS_LAYOUTFILE: // I3483
|
||||
VERIFY_KEYBOARD_VERSION(fk, VERSION_90, CERR_90FeatureOnlyLayoutFile); // I4140
|
||||
if ((msg = CheckFilenameConsistency(u16fmt(sp->dpString).c_str(), FALSE)) != CERR_None) {
|
||||
if ((msg = CheckFilenameConsistency(sp->dpString, FALSE)) != CERR_None) {
|
||||
return msg;
|
||||
}
|
||||
// Used by KMW compiler
|
||||
|
|
@ -3388,6 +3388,7 @@ KMX_DWORD ReadLine(FILE* fp_in , PKMX_WCHAR wstr, KMX_BOOL PreProcess)
|
|||
|
||||
// Always a "\r\n" to the EOF, avoids funny bugs
|
||||
u16ncat(str, u"\r\n", _countof(str)); // I3481
|
||||
//u16ncat(str, u"\n", _countof(str)); // I3481
|
||||
|
||||
if (len == 0) return CERR_EndOfFile;
|
||||
|
||||
|
|
@ -3514,8 +3515,9 @@ KMX_BOOL IsSameToken(PKMX_WCHAR *p, KMX_WCHAR const * token)
|
|||
KMX_DWORD ImportBitmapFile(PFILE_KEYBOARD fk, PKMX_WCHAR szName, PKMX_DWORD FileSize, PKMX_BYTE *Buf)
|
||||
{
|
||||
FILE *fp;
|
||||
KMX_WCHAR szNewName[260], *p;
|
||||
if (IsRelativePath(u16fmt(szName).c_str()))
|
||||
KMX_WCHAR szNewName[260], *p;
|
||||
//if (IsRelativePath(u16fmt(szName).c_str()))
|
||||
if (IsRelativePath(szName))
|
||||
{
|
||||
PKMX_WCHAR WCompileDir = strtowstr(CompileDir);
|
||||
u16ncpy(szNewName, WCompileDir, _countof(szNewName)); // I3481
|
||||
|
|
@ -3527,7 +3529,7 @@ KMX_DWORD ImportBitmapFile(PFILE_KEYBOARD fk, PKMX_WCHAR szName, PKMX_DWORD File
|
|||
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
fp =_wfsopen((wchar_t*)szNewName, L"rb", _SH_DENYWR);
|
||||
fp =_wfsopen((KMX_WCHART*)szNewName, L"rb", _SH_DENYWR);
|
||||
#else
|
||||
//fp = fopen( ( const PKMX_CHAR) szNewName, "rb");
|
||||
#endif
|
||||
|
|
@ -3541,7 +3543,7 @@ KMX_DWORD ImportBitmapFile(PFILE_KEYBOARD fk, PKMX_WCHAR szName, PKMX_DWORD File
|
|||
u16ncat(szNewName, u".bmp", _countof(szNewName)); // I3481
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
fp = _wfsopen((const wchar_t*)szNewName, L"rb", _SH_DENYWR);
|
||||
fp = _wfsopen((const KMX_WCHART*)szNewName, L"rb", _SH_DENYWR);
|
||||
#else
|
||||
fp = fopen(( const PKMX_CHAR) szNewName, "rb");
|
||||
#endif
|
||||
|
|
@ -3551,7 +3553,7 @@ KMX_DWORD ImportBitmapFile(PFILE_KEYBOARD fk, PKMX_WCHAR szName, PKMX_DWORD File
|
|||
}
|
||||
|
||||
KMX_DWORD msg;
|
||||
if ((msg = CheckFilenameConsistency(u16fmt(szNewName).c_str(), FALSE)) != CERR_None) {
|
||||
if ((msg = CheckFilenameConsistency(szNewName, FALSE)) != CERR_None) {
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
|
@ -3740,7 +3742,7 @@ FILE* UTF16TempFromUTF8(FILE* fp_in , KMX_BOOL hasPreamble)
|
|||
if(fp_out == NULL) // I3228 // I3510
|
||||
{
|
||||
fclose(fp_in);
|
||||
return NULL; //return INVALID_HANDLE_VALUE; can I exchange that?
|
||||
return NULL; //return INVALID_HANDLE_VALUE; _S2 can I exchange that?
|
||||
}
|
||||
|
||||
PKMX_BYTE buf, p;
|
||||
|
|
|
|||
|
|
@ -9,44 +9,41 @@
|
|||
#include <codecvt>
|
||||
#include <stdarg.h>
|
||||
|
||||
//std::wstring -> std::string
|
||||
std::string string_from_wstring(std::wstring const str) {
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
|
||||
return converter.to_bytes(str);
|
||||
}
|
||||
|
||||
//std::string -> std::wstring
|
||||
std::wstring wstring_from_string(std::string const str) {
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
|
||||
return converter.from_bytes(str);
|
||||
}
|
||||
|
||||
//std::string -> std::u16string
|
||||
std::u16string u16string_from_string(std::string const str) {
|
||||
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
|
||||
return converter.from_bytes(str);
|
||||
}
|
||||
|
||||
//std::u16string -> std::string
|
||||
std::string string_from_u16string(std::u16string const str) {
|
||||
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
|
||||
return converter.to_bytes(str);
|
||||
}
|
||||
|
||||
long int u16tol(const KMX_WCHAR* str, KMX_WCHAR** endptr, int base)
|
||||
{
|
||||
auto s = string_from_u16string(str);
|
||||
char* t;
|
||||
long int result = strtol(s.c_str(), &t, base);
|
||||
if(endptr != nullptr) *endptr = (KMX_WCHAR*) str + (t-s.c_str());
|
||||
return result;
|
||||
}
|
||||
|
||||
// often used with c_str() e.g. u16fmt( DEBUGSTORE_MATCH).c_str()
|
||||
// often used with c_str() e.g. u16fmt( DEBUGSTORE_MATCH).c_str()
|
||||
// const char16_t* -> std::u8string -> std::wstring
|
||||
std::wstring u16fmt(const km_kbp_cp* str) {
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert_wstring;
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
|
||||
std::string utf8str = convert.to_bytes(str);
|
||||
std::wstring wstr = convert_wstring.from_bytes(utf8str);
|
||||
|
||||
std::string utf8str = convert.to_bytes(str); // const char16_t*-> std::u8string
|
||||
std::wstring wstr = convert_wstring.from_bytes(utf8str); // std::u8string -> std::wstring
|
||||
return wstr;
|
||||
}
|
||||
|
||||
// const wchar_t* -> std::u8string -> std::u16string -> char16_t*
|
||||
void u16sprintf(km_kbp_cp* dst, const size_t sz, const wchar_t* fmt, ...) {
|
||||
wchar_t* wbuf = new wchar_t[sz];
|
||||
va_list args;
|
||||
|
|
@ -56,13 +53,22 @@ void u16sprintf(km_kbp_cp* dst, const size_t sz, const wchar_t* fmt, ...) {
|
|||
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert_wstring;
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
|
||||
std::string utf8str = convert_wstring.to_bytes(wbuf);
|
||||
std::u16string u16str = convert.from_bytes(utf8str);
|
||||
u16ncpy(dst, u16str.c_str(), sz);
|
||||
|
||||
std::string utf8str = convert_wstring.to_bytes(wbuf); // const wchar_t* -> std::u8string
|
||||
std::u16string u16str = convert.from_bytes(utf8str); // std::u8string -> std::u16string
|
||||
u16ncpy(dst, u16str.c_str(), sz); // std::u16string -> char16_t*
|
||||
delete[] wbuf;
|
||||
}
|
||||
|
||||
long int u16tol(const KMX_WCHAR* str, KMX_WCHAR** endptr, int base)
|
||||
{
|
||||
auto s = string_from_u16string(str);
|
||||
char* t;
|
||||
long int result = strtol(s.c_str(), &t, base);
|
||||
if (endptr != nullptr) *endptr = (KMX_WCHAR*)str + (t - s.c_str());
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string toHex(int num1) {
|
||||
if (num1 == 0)
|
||||
return "0";
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue