Ensure the EOF \n in both UserPhrases and Replacement Map.

This commit is contained in:
ShikiSuen 2022-01-23 16:42:23 +08:00
parent d072b95a99
commit 79e30215a1
3 changed files with 35 additions and 2 deletions

View File

@ -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;

View File

@ -6,6 +6,7 @@
* Some rights reserved. See "LICENSE.TXT" for details.
*/
#include <syslog.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
@ -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");

View File

@ -7,12 +7,12 @@
*/
#include "UserPhrasesLM.h"
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fstream>
#include <unistd.h>
#include <syslog.h>
#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");