From b8f008aa5a2de0332163c74a324cd45fda5a8293 Mon Sep 17 00:00:00 2001 From: somewhatlurker <52014015+somewhatlurker@users.noreply.github.com> Date: Tue, 3 Sep 2019 16:48:35 +1000 Subject: [PATCH] add BOM check to config reads --- source-code/source/plugins/Launcher/framework.h | 17 +++++++++++++++++ .../plugins/TLAC/FileSystem/ConfigFile.cpp | 9 ++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/source-code/source/plugins/Launcher/framework.h b/source-code/source/plugins/Launcher/framework.h index f89f43e..adfdd7b 100644 --- a/source-code/source/plugins/Launcher/framework.h +++ b/source-code/source/plugins/Launcher/framework.h @@ -232,6 +232,13 @@ bool IsLineInFile(LPCSTR searchLine, LPCWSTR fileName) std::string line; + // check for BOM + std::getline(fileStream, line); + if (line.size() >= 3 & line.rfind("\xEF\xBB\xBF", 0) == 0) + fileStream.seekg(3); + else + fileStream.seekg(0); + while (std::getline(fileStream, line)) { if (line.compare(searchLine) == 0) @@ -259,8 +266,18 @@ void PrependFile(LPCSTR newStr, LPCWSTR fileName) fileStream.seekg(0, std::ios::end); origStr.reserve(fileStream.tellg()); fileStream.seekg(0, std::ios::beg); + + // check for BOM + std::string BOMcheckLine; + std::getline(fileStream, BOMcheckLine); + if (BOMcheckLine.size() >= 3 & BOMcheckLine.rfind("\xEF\xBB\xBF", 0) == 0) + fileStream.seekg(3); + else + fileStream.seekg(0); + origStr.assign((std::istreambuf_iterator(fileStream)), std::istreambuf_iterator()); + std::string outStr = newStr; outStr += origStr; diff --git a/source-code/source/plugins/TLAC/FileSystem/ConfigFile.cpp b/source-code/source/plugins/TLAC/FileSystem/ConfigFile.cpp index 9eb3b8f..b365338 100644 --- a/source-code/source/plugins/TLAC/FileSystem/ConfigFile.cpp +++ b/source-code/source/plugins/TLAC/FileSystem/ConfigFile.cpp @@ -51,6 +51,13 @@ namespace TLAC::FileSystem { std::string line; + // check for BOM + std::getline(fileStream, line); + if (line.size() >= 3 & line.rfind("\xEF\xBB\xBF", 0) == 0) + fileStream.seekg(3); + else + fileStream.seekg(0); + while (std::getline(fileStream, line)) { if (IsComment(line)) @@ -67,6 +74,6 @@ namespace TLAC::FileSystem bool ConfigFile::IsComment(const std::string &line) { - return line.size() <= 0 || line[0] == '#' || line[0] == '[' || line._Starts_with("//"); + return line.size() <= 0 || line[0] == '#' || line[0] == '[' || (line.size() >= 2 & line.rfind("//", 0) == 0); } }