From 8aa3668731bd15e129ad5fc5abd41eba76bee838 Mon Sep 17 00:00:00 2001 From: m-kro Date: Mon, 16 Oct 2023 11:08:46 +0200 Subject: [PATCH] remember BOM when reading file to set pos correctly ref #13932 Signed-off-by: m-kro --- src/utils/importio/LineReader.cpp | 9 ++++++--- src/utils/importio/LineReader.h | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/utils/importio/LineReader.cpp b/src/utils/importio/LineReader.cpp index f3b1d86d131..7d96f0c1b71 100644 --- a/src/utils/importio/LineReader.cpp +++ b/src/utils/importio/LineReader.cpp @@ -15,6 +15,7 @@ /// @author Daniel Krajzewicz /// @author Laura Bieker /// @author Michael Behrisch +/// @author Mirko Barthauer /// @date Fri, 19 Jul 2002 /// // Retrieves a file linewise and reports the lines to a handler. @@ -119,7 +120,7 @@ std::string LineReader::readLine() { std::string toReport; while (toReport.length() == 0 && myStrm.good()) { - const std::string::size_type idx = myStrBuffer.find('\n'); + const std::string::size_type idx = myStrBuffer.find('\n'); if (idx == 0) { myStrBuffer = myStrBuffer.substr(1); myRread++; @@ -137,6 +138,7 @@ LineReader::readLine() { ? myAvailable - myRead : 1024); int noBytes = myAvailable - myRead; + bool bomAtStart = myBuffer[0] == '\xef' && myBuffer[1] == '\xbb' && myBuffer[2] == '\xbf'; noBytes = noBytes > 1024 ? 1024 : noBytes; myStrBuffer += std::string(myBuffer, noBytes); myRead += 1024; @@ -204,7 +206,8 @@ LineReader::reinit() { // check for BOM myStrm.read(myBuffer, 3); if (myBuffer[0] == '\xef' && myBuffer[1] == '\xbb' && myBuffer[2] == '\xbf') { - myAvailable -= 3; + mySkipBOM = 3; + myAvailable -= mySkipBOM; } else { myStrm.seekg(0, std::ios::beg); } @@ -218,7 +221,7 @@ LineReader::reinit() { void LineReader::setPos(unsigned long pos) { - myStrm.seekg(pos, std::ios::beg); + myStrm.seekg(pos + mySkipBOM, std::ios::beg); myRead = pos; myRread = pos; myStrBuffer = ""; diff --git a/src/utils/importio/LineReader.h b/src/utils/importio/LineReader.h index 4327c555f83..afa509aad41 100644 --- a/src/utils/importio/LineReader.h +++ b/src/utils/importio/LineReader.h @@ -169,4 +169,6 @@ class LineReader { /// @brief Information how many lines were read for meaningful error messages int myLinesRead; + /// @brief Number of skipped characters at the file begin (UTF-8 BOM) + int mySkipBOM; };