Commit c44652ee authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'SystemTools-TextFilesDiffer'

3f35954d SystemTools: Add TextFilesDiffer method
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: Taylor Braun-Jones's avatarTaylor Braun-Jones <taylor@braun-jones.org>
Merge-request: !121
parents 0789493f 3f35954d
......@@ -2161,6 +2161,32 @@ bool SystemTools::FilesDiffer(const std::string& source,
return false;
}
bool SystemTools::TextFilesDiffer(const std::string& path1,
const std::string& path2)
{
kwsys::ifstream if1(path1.c_str());
kwsys::ifstream if2(path2.c_str());
if (!if1 || !if2) {
return true;
}
for (;;) {
std::string line1, line2;
bool hasData1 = GetLineFromStream(if1, line1);
bool hasData2 = GetLineFromStream(if2, line2);
if (hasData1 != hasData2) {
return true;
}
if (!hasData1) {
break;
}
if (line1 != line2) {
return true;
}
}
return false;
}
/**
* Blockwise copy source to destination file
*/
......
......@@ -578,6 +578,13 @@ public:
static bool FilesDiffer(const std::string& source,
const std::string& destination);
/**
* Compare the contents of two files, ignoring line ending differences.
* Return true if different
*/
static bool TextFilesDiffer(const std::string& path1,
const std::string& path2);
/**
* Return true if the two files are the same file
*/
......
......@@ -984,6 +984,50 @@ static bool CheckGetLineFromStreamLongLine()
return true;
}
static bool writeFile(const char* fileName, const char* data)
{
kwsys::ofstream out(fileName, std::ios::binary);
out << data;
if (!out) {
std::cerr << "Failed to write file: " << fileName << std::endl;
return false;
}
return true;
}
static bool CheckTextFilesDiffer()
{
struct
{
const char* a;
const char* b;
bool differ;
} test_cases[] = { { "one", "one", false },
{ "one", "two", true },
{ "", "", false },
{ "\n", "\r\n", false },
{ "one\n", "one\n", false },
{ "one\r\n", "one\n", false },
{ "one\n", "one", false },
{ "one\ntwo", "one\ntwo", false },
{ "one\ntwo", "one\r\ntwo", false } };
const int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]);
for (int i = 0; i < num_test_cases; ++i) {
if (!writeFile("file_a", test_cases[i].a) ||
!writeFile("file_b", test_cases[i].b)) {
return false;
}
if (kwsys::SystemTools::TextFilesDiffer("file_a", "file_b") !=
test_cases[i].differ) {
std::cerr << "Incorrect TextFilesDiffer result for test case " << i + 1
<< "." << std::endl;
return false;
}
}
return true;
}
int testSystemTools(int, char* [])
{
bool res = true;
......@@ -1027,5 +1071,7 @@ int testSystemTools(int, char* [])
res &= CheckGetFilenameName();
res &= CheckTextFilesDiffer();
return res ? 0 : 1;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment