Normalize line endings when reading text files

Closes #250
This commit is contained in:
Alexander Batalov 2023-04-11 20:00:38 +03:00
parent 11472e8be9
commit c8d45854ba
5 changed files with 36 additions and 4 deletions

View File

@ -289,7 +289,7 @@ bool configRead(Config* config, const char* filePath, bool isDb)
} else { } else {
FILE* stream = compat_fopen(filePath, "rt"); FILE* stream = compat_fopen(filePath, "rt");
if (stream != NULL) { if (stream != NULL) {
while (fgets(string, sizeof(string), stream) != NULL) { while (compat_fgets(string, sizeof(string), stream) != NULL) {
configParseLine(config, string); configParseLine(config, string);
} }

View File

@ -447,7 +447,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
return -1; return -1;
} }
if (fgets(entry->key, keyLength + 1, stream) == NULL) { if (compat_fgets(entry->key, keyLength + 1, stream) == NULL) {
return -1; return -1;
} }

View File

@ -239,6 +239,36 @@ gzFile compat_gzopen(const char* path, const char* mode)
return gzopen(nativePath, mode); return gzopen(nativePath, mode);
} }
char* compat_fgets(char* buffer, int maxCount, FILE* stream)
{
buffer = fgets(buffer, maxCount, stream);
if (buffer != NULL) {
size_t len = strlen(buffer);
if (len >= 2 && buffer[len - 1] == '\n' && buffer[len - 2] == '\r') {
buffer[len - 2] = '\n';
buffer[len - 1] = '\0';
}
}
return buffer;
}
char* compat_gzgets(gzFile stream, char* buffer, int maxCount)
{
buffer = gzgets(stream, buffer, maxCount);
if (buffer != NULL) {
size_t len = strlen(buffer);
if (len >= 2 && buffer[len - 1] == '\n' && buffer[len - 2] == '\r') {
buffer[len - 2] = '\n';
buffer[len - 1] = '\0';
}
}
return buffer;
}
int compat_remove(const char* path) int compat_remove(const char* path)
{ {
char nativePath[COMPAT_MAX_PATH]; char nativePath[COMPAT_MAX_PATH];

View File

@ -35,6 +35,8 @@ int compat_mkdir(const char* path);
unsigned int compat_timeGetTime(); unsigned int compat_timeGetTime();
FILE* compat_fopen(const char* path, const char* mode); FILE* compat_fopen(const char* path, const char* mode);
gzFile compat_gzopen(const char* path, const char* mode); gzFile compat_gzopen(const char* path, const char* mode);
char* compat_fgets(char* buffer, int maxCount, FILE* stream);
char* compat_gzgets(gzFile stream, char* buffer, int maxCount);
int compat_remove(const char* path); int compat_remove(const char* path);
int compat_rename(const char* oldFileName, const char* newFileName); int compat_rename(const char* oldFileName, const char* newFileName);
void compat_windows_path_to_native(char* path); void compat_windows_path_to_native(char* path);

View File

@ -234,10 +234,10 @@ char* xfileReadString(char* string, int size, XFile* stream)
result = dfileReadString(string, size, stream->dfile); result = dfileReadString(string, size, stream->dfile);
break; break;
case XFILE_TYPE_GZFILE: case XFILE_TYPE_GZFILE:
result = gzgets(stream->gzfile, string, size); result = compat_gzgets(stream->gzfile, string, size);
break; break;
default: default:
result = fgets(string, size, stream->file); result = compat_fgets(string, size, stream->file);
break; break;
} }