changed implementation of getFileContent() to match Sourcetrail code
This commit is contained in:
+80
-7
@@ -19,9 +19,52 @@
|
||||
#include <algorithm>
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include "SourcetrailException.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace sourcetrail
|
||||
{
|
||||
namespace utility
|
||||
@@ -34,22 +77,52 @@ bool getFileExists(const std::string& filePath)
|
||||
|
||||
std::string getFileContent(const std::string& filePath)
|
||||
{
|
||||
std::string content;
|
||||
std::vector<std::string> lines;
|
||||
|
||||
std::ifstream file;
|
||||
file.open(filePath);
|
||||
try
|
||||
{
|
||||
std::ifstream srcFile;
|
||||
srcFile.open(filePath, std::ios::binary | std::ios::in);
|
||||
|
||||
if (file.fail())
|
||||
if (srcFile.fail())
|
||||
{
|
||||
throw SourcetrailException("Could not open file " + filePath);
|
||||
}
|
||||
|
||||
for (std::string line; std::getline(file, line);)
|
||||
while (!srcFile.eof())
|
||||
{
|
||||
content += line + "\n";
|
||||
std::string line;
|
||||
safeGetline(srcFile, line);
|
||||
lines.push_back(line + '\n');
|
||||
}
|
||||
|
||||
file.close();
|
||||
srcFile.close();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
throw SourcetrailException("Exception thrown while reading file \"" + filePath + "\": " + e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw SourcetrailException("Unknown exception thrown while reading file \"" + filePath + "\"");
|
||||
}
|
||||
|
||||
if (!lines.empty())
|
||||
{
|
||||
std::string last = lines.back().substr(0, lines.back().size() - 1);
|
||||
lines.pop_back();
|
||||
if (!last.empty())
|
||||
{
|
||||
lines.push_back(last);
|
||||
}
|
||||
}
|
||||
|
||||
std::string content;
|
||||
|
||||
for (const std::string& line: lines)
|
||||
{
|
||||
content += line;
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user