From c8d45854ba8be7834f2547a1240ff1e033f13ce3 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Tue, 11 Apr 2023 20:00:38 +0300 Subject: [PATCH] Normalize line endings when reading text files Closes #250 --- src/config.cc | 2 +- src/dictionary.cc | 2 +- src/platform_compat.cc | 30 ++++++++++++++++++++++++++++++ src/platform_compat.h | 2 ++ src/xfile.cc | 4 ++-- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/config.cc b/src/config.cc index 9143e55..966b0d4 100644 --- a/src/config.cc +++ b/src/config.cc @@ -289,7 +289,7 @@ bool configRead(Config* config, const char* filePath, bool isDb) } else { FILE* stream = compat_fopen(filePath, "rt"); if (stream != NULL) { - while (fgets(string, sizeof(string), stream) != NULL) { + while (compat_fgets(string, sizeof(string), stream) != NULL) { configParseLine(config, string); } diff --git a/src/dictionary.cc b/src/dictionary.cc index af873e9..31acacb 100644 --- a/src/dictionary.cc +++ b/src/dictionary.cc @@ -447,7 +447,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3) return -1; } - if (fgets(entry->key, keyLength + 1, stream) == NULL) { + if (compat_fgets(entry->key, keyLength + 1, stream) == NULL) { return -1; } diff --git a/src/platform_compat.cc b/src/platform_compat.cc index 5297592..b629fb6 100644 --- a/src/platform_compat.cc +++ b/src/platform_compat.cc @@ -239,6 +239,36 @@ gzFile compat_gzopen(const char* path, const char* 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) { char nativePath[COMPAT_MAX_PATH]; diff --git a/src/platform_compat.h b/src/platform_compat.h index be88974..9fb7574 100644 --- a/src/platform_compat.h +++ b/src/platform_compat.h @@ -35,6 +35,8 @@ int compat_mkdir(const char* path); unsigned int compat_timeGetTime(); FILE* compat_fopen(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_rename(const char* oldFileName, const char* newFileName); void compat_windows_path_to_native(char* path); diff --git a/src/xfile.cc b/src/xfile.cc index f2ce72c..60bcea2 100644 --- a/src/xfile.cc +++ b/src/xfile.cc @@ -234,10 +234,10 @@ char* xfileReadString(char* string, int size, XFile* stream) result = dfileReadString(string, size, stream->dfile); break; case XFILE_TYPE_GZFILE: - result = gzgets(stream->gzfile, string, size); + result = compat_gzgets(stream->gzfile, string, size); break; default: - result = fgets(string, size, stream->file); + result = compat_fgets(string, size, stream->file); break; }