Text and cross-platform fix

This commit is contained in:
erorcun 2020-10-21 03:11:10 +03:00
parent 7ae25761d5
commit af53267b74
2 changed files with 43 additions and 36 deletions

View File

@ -26,34 +26,39 @@ void GetLocalTime_CP(SYSTEMTIME *out) {
// Compatible with Linux/POSIX and MinGW on Windows // Compatible with Linux/POSIX and MinGW on Windows
#ifndef _WIN32 #ifndef _WIN32
HANDLE FindFirstFile(const char* pathname, WIN32_FIND_DATA* firstfile) { HANDLE FindFirstFile(const char* pathname, WIN32_FIND_DATA* firstfile) {
char newpathname[32]; char pathCopy[32];
strncpy(newpathname, pathname, 32); strncpy(pathCopy, pathname, 32);
char* path = strtok(newpathname, "*"); char* folder = strtok(pathCopy, "*");
// Case-sensitivity and backslashes... // Case-sensitivity and backslashes...
char *real = casepath(path); char *realFolder = casepath(folder);
if (real) { char *extension = nil;
real[strlen(real)] = '*'; if (realFolder) {
char *extension = strtok(NULL, "*"); realFolder[strlen(realFolder)] = '*';
if (extension) extension = strtok(NULL, "*");
strcat(real, extension); if (extension) {
strcat(realFolder, extension);
}
strncpy(newpathname, real, 32); strncpy(pathCopy, realFolder, 32);
free(real); free(realFolder);
path = strtok(newpathname, "*"); folder = strtok(pathCopy, "*");
} else {
// Wildcard (*)
if (strlen(folder) + 1 != strlen(pathname))
extension = strtok(NULL, "*");
} }
strncpy(firstfile->folder, path, sizeof(firstfile->folder)); strncpy(firstfile->folder, folder, sizeof(firstfile->folder));
// Both w/ extension and w/o extension is ok if (extension)
if (strlen(path) + 1 != strlen(pathname)) strncpy(firstfile->extension, extension, sizeof(firstfile->extension));
strncpy(firstfile->extension, strtok(NULL, "*"), sizeof(firstfile->extension));
else else
strncpy(firstfile->extension, "", sizeof(firstfile->extension)); firstfile->extension[0] = '\0';
HANDLE d; HANDLE d;
if ((d = (HANDLE)opendir(path)) == NULL || !FindNextFile(d, firstfile)) if ((d = (HANDLE)opendir(folder)) == NULL || !FindNextFile(d, firstfile))
return NULL; return NULL;
return d; return d;

View File

@ -69,10 +69,7 @@ CMessages::WideStringCompare(wchar *str1, wchar *str2, uint16 size)
if (len1 != len2 && (len1 < size || len2 < size)) if (len1 != len2 && (len1 < size || len2 < size))
return false; return false;
for (int32 i = 0; i < size; i++) { for (int32 i = 0; FixupChar(str1[i]) != '\0' && i < size; i++) {
if (FixupChar(str1[i]) == '\0')
break;
if (FixupChar(str1[i]) != FixupChar(str2[i])) if (FixupChar(str1[i]) != FixupChar(str2[i]))
return false; return false;
} }
@ -328,9 +325,7 @@ void
CMessages::AddToPreviousBriefArray(wchar *text, int32 n1, int32 n2, int32 n3, int32 n4, int32 n5, int32 n6, wchar *string) CMessages::AddToPreviousBriefArray(wchar *text, int32 n1, int32 n2, int32 n3, int32 n4, int32 n5, int32 n6, wchar *string)
{ {
int32 i = 0; int32 i = 0;
while (i < NUMPREVIOUSBRIEFS) { for (i = 0; PreviousBriefs[i].m_pText && i < NUMPREVIOUSBRIEFS; i++) {
if (PreviousBriefs[i].m_pText == nil)
break;
if (PreviousBriefs[i].m_nNumber[0] == n1 if (PreviousBriefs[i].m_nNumber[0] == n1
&& PreviousBriefs[i].m_nNumber[1] == n2 && PreviousBriefs[i].m_nNumber[1] == n2
&& PreviousBriefs[i].m_nNumber[2] == n3 && PreviousBriefs[i].m_nNumber[2] == n3
@ -340,8 +335,6 @@ CMessages::AddToPreviousBriefArray(wchar *text, int32 n1, int32 n2, int32 n3, in
&& PreviousBriefs[i].m_pText == text && PreviousBriefs[i].m_pText == text
&& PreviousBriefs[i].m_pString == string) && PreviousBriefs[i].m_pString == string)
return; return;
i++;
} }
if (i != 0) { if (i != 0) {
@ -374,6 +367,14 @@ CMessages::InsertNumberInString(wchar *str, int32 n1, int32 n2, int32 n3, int32
return; return;
} }
sprintf(numStr, "%d", n1);
size_t outLen = strlen(numStr);
AsciiToUnicode(numStr, wNumStr);
if (str[0] == 0) {
*outstr = '\0';
return;
}
int32 size = GetWideStringLength(str); int32 size = GetWideStringLength(str);
int32 i = 0; int32 i = 0;
@ -385,6 +386,11 @@ CMessages::InsertNumberInString(wchar *str, int32 n1, int32 n2, int32 n3, int32
#else #else
if (str[c] == '~' && str[c + 1] == '1' && str[c + 2] == '~') { if (str[c] == '~' && str[c + 1] == '1' && str[c + 2] == '~') {
#endif #endif
c += 3;
for (int j = 0; j < outLen; j++)
*(outstr++) = wNumStr[j++];
i++;
switch (i) { switch (i) {
case 0: sprintf(numStr, "%d", n1); break; case 0: sprintf(numStr, "%d", n1); break;
case 1: sprintf(numStr, "%d", n2); break; case 1: sprintf(numStr, "%d", n2); break;
@ -393,14 +399,8 @@ CMessages::InsertNumberInString(wchar *str, int32 n1, int32 n2, int32 n3, int32
case 4: sprintf(numStr, "%d", n5); break; case 4: sprintf(numStr, "%d", n5); break;
case 5: sprintf(numStr, "%d", n6); break; case 5: sprintf(numStr, "%d", n6); break;
} }
i++; outLen = strlen(numStr);
AsciiToUnicode(numStr, wNumStr); AsciiToUnicode(numStr, wNumStr);
int j = 0;
while (wNumStr[j] != '\0')
*(outstr++) = wNumStr[j++];
c += 3;
} else { } else {
*(outstr++) = str[c++]; *(outstr++) = str[c++];
} }
@ -466,10 +466,12 @@ CMessages::InsertPlayerControlKeysInString(wchar *str)
if (str[i] == '~' && str[i + 1] == 'k' && str[i + 2] == '~') { if (str[i] == '~' && str[i + 1] == 'k' && str[i + 2] == '~') {
#endif #endif
i += 4; i += 4;
for (int32 cont = 0; cont < MAX_CONTROLLERACTIONS; cont++) { bool done = false;
for (int32 cont = 0; cont < MAX_CONTROLLERACTIONS && !done; cont++) {
uint16 contSize = GetWideStringLength(ControlsManager.m_aActionNames[cont]); uint16 contSize = GetWideStringLength(ControlsManager.m_aActionNames[cont]);
if (contSize != 0) { if (contSize != 0) {
if (WideStringCompare(&str[i], ControlsManager.m_aActionNames[cont], contSize)) { if (WideStringCompare(&str[i], ControlsManager.m_aActionNames[cont], contSize)) {
done = true;
ControlsManager.GetWideStringOfCommandKeys(cont, keybuf, 256); ControlsManager.GetWideStringOfCommandKeys(cont, keybuf, 256);
uint16 keybuf_size = GetWideStringLength(keybuf); uint16 keybuf_size = GetWideStringLength(keybuf);
for (uint16 j = 0; j < keybuf_size; j++) { for (uint16 j = 0; j < keybuf_size; j++) {
@ -751,7 +753,7 @@ CMessages::ClearThisPrint(wchar *str)
} }
BriefMessages[i].m_pText = nil; BriefMessages[i].m_pText = nil;
BriefMessages[0].m_nStartTime = CTimer::GetTimeInMilliseconds(); BriefMessages[0].m_nStartTime = CTimer::GetTimeInMilliseconds();
if (BriefMessages[0].m_pText == nil) if (BriefMessages[0].m_pText != nil)
AddToPreviousBriefArray( AddToPreviousBriefArray(
BriefMessages[0].m_pText, BriefMessages[0].m_pText,
BriefMessages[0].m_nNumber[0], BriefMessages[0].m_nNumber[0],