TW-1736: Error on detection of BOM in files.

This commit is contained in:
Paul Beckingham
2016-01-11 21:54:57 -05:00
parent 92f22149c0
commit 19f57ffead
5 changed files with 71 additions and 1 deletions

View File

@@ -375,6 +375,17 @@ bool File::remove () const
return unlink (_data.c_str ()) == 0 ? true : false;
}
////////////////////////////////////////////////////////////////////////////////
std::string File::removeBOM (const std::string& input)
{
if (input[0] && input[0] == '\xEF' &&
input[1] && input[1] == '\xBB' &&
input[2] && input[2] == '\xBF')
return input.substr (3);
return input;
}
////////////////////////////////////////////////////////////////////////////////
bool File::open ()
{
@@ -457,10 +468,20 @@ void File::read (std::string& contents)
std::ifstream in (_data.c_str ());
if (in.good ())
{
bool first = true;
std::string line;
line.reserve (512 * 1024);
while (getline (in, line))
{
// Detect forbidden BOM on first line.
if (first)
{
line = File::removeBOM (line);
first = false;
}
contents += line + "\n";
}
in.close ();
}
@@ -475,10 +496,20 @@ void File::read (std::vector <std::string>& contents)
std::ifstream in (_data.c_str ());
if (in.good ())
{
bool first = true;
std::string line;
line.reserve (512 * 1024);
while (getline (in, line))
{
// Detect forbidden BOM on first line.
if (first)
{
line = File::removeBOM (line);
first = false;
}
contents.push_back (line);
}
in.close ();
}
@@ -627,10 +658,20 @@ bool File::read (const std::string& name, std::string& contents)
std::ifstream in (name.c_str ());
if (in.good ())
{
bool first = true;
std::string line;
line.reserve (1024);
while (getline (in, line))
{
// Detect forbidden BOM on first line.
if (first)
{
line = File::removeBOM (line);
first = false;
}
contents += line + "\n";
}
in.close ();
return true;
@@ -647,10 +688,20 @@ bool File::read (const std::string& name, std::vector <std::string>& contents)
std::ifstream in (name.c_str ());
if (in.good ())
{
bool first = true;
std::string line;
line.reserve (1024);
while (getline (in, line))
{
// Detect forbidden BOM on first line.
if (first)
{
line = File::removeBOM (line);
first = false;
}
contents.push_back (line);
}
in.close ();
return true;