logic: fixed reading lines now also breaks at a single "\r" on windows

This commit is contained in:
mlangkabel
2018-09-21 18:00:34 +02:00
parent 951256c07b
commit 7c039a2fb8
+43 -1
View File
@@ -4,6 +4,48 @@
#include "logging.h"
namespace
{
std::istream& safeGetline(std::istream& is, std::string& t)
{
t.clear();
// The characters in the stream are read one-by-one using a std::streambuf.
// That is faster than reading them one-by-one using the std::istream.
// Code that uses streambuf this way must be guarded by a sentry object.
// The sentry object performs various tasks,
// such as thread synchronization and updating the stream state.
std::istream::sentry se(is, true);
std::streambuf* sb = is.rdbuf();
while (true)
{
int c = sb->sbumpc();
switch (c)
{
case '\n':
return is;
case '\r':
if (sb->sgetc() == '\n')
{
sb->sbumpc();
}
return is;
case std::streambuf::traits_type::eof():
// Also handle the case when the last line has no line ending
if (t.empty())
{
is.setstate(std::ios::eofbit);
}
return is;
default:
t += (char)c;
}
}
}
}
std::shared_ptr<TextAccess> TextAccess::createFromFile(const FilePath& filePath)
{
std::shared_ptr<TextAccess> result(new TextAccess());
@@ -93,7 +135,7 @@ std::vector<std::string> TextAccess::readFile(const FilePath& filePath)
while (!srcFile.eof())
{
std::string line;
std::getline(srcFile, line);
safeGetline(srcFile, line);
result.push_back(line + '\n');
}
srcFile.close();