From 79e30215a15c08f68495b960d7409a062554ecc8 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 23 Jan 2022 16:42:23 +0800 Subject: [PATCH] Ensure the EOF \n in both UserPhrases and Replacement Map. --- Source/Engine/LanguageModel/FastLM.cpp | 2 +- .../LanguageModel/PhraseReplacementMap.cpp | 17 +++++++++++++++++ Source/Engine/LanguageModel/UserPhrasesLM.cpp | 18 +++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Source/Engine/LanguageModel/FastLM.cpp b/Source/Engine/LanguageModel/FastLM.cpp index bcfcf194..66b9418b 100644 --- a/Source/Engine/LanguageModel/FastLM.cpp +++ b/Source/Engine/LanguageModel/FastLM.cpp @@ -41,7 +41,7 @@ bool FastLM::open(const char *path) char z; zfd.get(z); if(z!='\n'){ - syslog(LOG_CONS, "REPORT: File is not ended with a new line.\n"); + syslog(LOG_CONS, "REPORT: Core Language Data file is not ended with a new line.\n"); syslog(LOG_CONS, "PROCEDURE: Trying to insert a new line as EOF before per-line check process.\n"); ofstream zfdo(path, std::ios_base::app); zfdo << std::endl; diff --git a/Source/Engine/LanguageModel/PhraseReplacementMap.cpp b/Source/Engine/LanguageModel/PhraseReplacementMap.cpp index 032173d3..0374100e 100644 --- a/Source/Engine/LanguageModel/PhraseReplacementMap.cpp +++ b/Source/Engine/LanguageModel/PhraseReplacementMap.cpp @@ -6,6 +6,7 @@ * Some rights reserved. See "LICENSE.TXT" for details. */ +#include #include #include #include @@ -38,6 +39,22 @@ bool PhraseReplacementMap::open(const char *path) return false; } + std::fstream zfd(path); + zfd.seekg(-1,std::ios_base::end); + char z; + zfd.get(z); + if(z!='\n'){ + syslog(LOG_CONS, "REPORT: Phrase Replacement Map File is not ended with a new line.\n"); + syslog(LOG_CONS, "PROCEDURE: Trying to insert a new line as EOF before per-line check process.\n"); + std::ofstream zfdo(path, std::ios_base::app); + zfdo << std::endl; + zfdo.close(); + if (zfdo.fail()) { + syslog(LOG_CONS, "REPORT: Failed to append a newline to the data file. Insufficient Privileges?\n"); + return false; + } + } + fd = ::open(path, O_RDONLY); if (fd == -1) { printf("open:: file not exist"); diff --git a/Source/Engine/LanguageModel/UserPhrasesLM.cpp b/Source/Engine/LanguageModel/UserPhrasesLM.cpp index 583e9a4a..733a371d 100644 --- a/Source/Engine/LanguageModel/UserPhrasesLM.cpp +++ b/Source/Engine/LanguageModel/UserPhrasesLM.cpp @@ -7,12 +7,12 @@ */ #include "UserPhrasesLM.h" - #include #include #include #include #include +#include #include "KeyValueBlobReader.h" @@ -38,6 +38,22 @@ bool UserPhrasesLM::open(const char *path) return false; } + std::fstream zfd(path); + zfd.seekg(-1,std::ios_base::end); + char z; + zfd.get(z); + if(z!='\n'){ + syslog(LOG_CONS, "REPORT: User Phrase Data File is not ended with a new line.\n"); + syslog(LOG_CONS, "PROCEDURE: Trying to insert a new line as EOF before per-line check process.\n"); + std::ofstream zfdo(path, std::ios_base::app); + zfdo << std::endl; + zfdo.close(); + if (zfdo.fail()) { + syslog(LOG_CONS, "REPORT: Failed to append a newline to the data file. Insufficient Privileges?\n"); + return false; + } + } + fd = ::open(path, O_RDONLY); if (fd == -1) { printf("open:: file not exist");