2 Commits

Author SHA1 Message Date
Alexander Batalov
cbdb0eeba4 Make configGetInt compatible with atoi
Fixes #94
2022-07-27 10:24:45 +03:00
Alexander Batalov
73b6a16551 Fix loading save game crash on Windows 2022-07-27 10:11:33 +03:00
3 changed files with 19 additions and 19 deletions

View File

@@ -188,7 +188,7 @@ bool configSetString(Config* config, const char* sectionKey, const char* key, co
return true;
}
// 0x42C05C customized: atoi() replaced with strtol()
// 0x42C05C
bool configGetInt(Config* config, const char* sectionKey, const char* key, int* valuePtr, unsigned char base /* = 0 */ )
{
if (valuePtr == NULL) {
@@ -203,8 +203,17 @@ bool configGetInt(Config* config, const char* sectionKey, const char* key, int*
char* end;
errno = 0;
long l = strtol(stringValue, &end, base); // see https://stackoverflow.com/a/6154614
if (((errno == ERANGE && 1 == LONG_MAX) || l > INT_MAX) || ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) || (*stringValue == '\0' || *end != '\0'))
return false;
// The link above says right things about converting strings to numbers,
// however we need to maintain compatibility with atoi implementation and
// original game data. One example of the problem is worldmap.txt where
// frequency values expressed as percentages (Frequent=38%). If we handle
// the result like the link above suggests (and what previous implementation
// provided), we'll simply end up returning `false`, since there will be
// unconverted characters left. On the other hand, this function is also
// used to parse Sfall config values, which uses hexadecimal notation to
// represent colors. We're not going to need any of these in the long run so
// for now simply ignore any error that could arise during conversion.
*valuePtr = l;

View File

@@ -10,6 +10,8 @@
#include <vector>
static void fileCopy(const char* existingFilePath, const char* newFilePath);
// 0x452740
int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath)
{
@@ -51,7 +53,7 @@ int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath)
return -1;
}
} else {
fileCopy(existingFilePath, newFilePath, true);
fileCopy(existingFilePath, newFilePath);
}
return 0;
@@ -74,7 +76,7 @@ int fileCopyCompressed(const char* existingFilePath, const char* newFilePath)
// Source file is already gzipped, there is no need to do anything
// besides copying.
fclose(inStream);
fileCopy(existingFilePath, newFilePath, true);
fileCopy(existingFilePath, newFilePath);
} else {
gzFile outStream = compat_gzopen(newFilePath, "wb");
if (outStream == NULL) {
@@ -137,15 +139,13 @@ int _gzdecompress_file(const char* existingFilePath, const char* newFilePath)
gzclose(gzstream);
fclose(stream);
} else {
fileCopy(existingFilePath, newFilePath, true);
fileCopy(existingFilePath, newFilePath);
}
return 0;
}
// Modelled as replacement for `CopyFileA`, but `overwrite` is the opposite to
// `bFailIfExists` param. Update callers accordingly.
void fileCopy(const char* existingFilePath, const char* newFilePath, bool overwrite)
static void fileCopy(const char* existingFilePath, const char* newFilePath)
{
char nativeExistingFilePath[COMPAT_MAX_PATH];
strcpy(nativeExistingFilePath, existingFilePath);
@@ -155,16 +155,8 @@ void fileCopy(const char* existingFilePath, const char* newFilePath, bool overwr
strcpy(nativeNewFilePath, newFilePath);
compat_windows_path_to_native(nativeNewFilePath);
char outMode[4];
outMode[0] = 'w';
outMode[1] = 'b';
if (!overwrite) {
outMode[2] = 'x';
}
FILE* in = fopen(nativeExistingFilePath, "rb");
FILE* out = fopen(nativeNewFilePath, outMode);
FILE* out = fopen(nativeNewFilePath, "wb");
if (in != NULL && out != NULL) {
std::vector<unsigned char> buffer(0xFFFF);

View File

@@ -4,6 +4,5 @@
int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath);
int fileCopyCompressed(const char* existingFilePath, const char* newFilePath);
int _gzdecompress_file(const char* existingFilePath, const char* newFilePath);
void fileCopy(const char* existingFilePath, const char* newFilePath, bool overwrite);
#endif /* FILE_UTILS_H */