diff options
author | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-26 21:17:24 +0000 |
---|---|---|
committer | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-26 21:17:24 +0000 |
commit | b81637c3d9f2cf895b81d5b673b88668b6bc1322 (patch) | |
tree | 795f338852b7d40508e837ece010d64b4ad14d9f /base/file_util.cc | |
parent | eb0ef6ce145aab92fdf7f99f4ebe14ef7b03736c (diff) | |
download | chromium_src-b81637c3d9f2cf895b81d5b673b88668b6bc1322.zip chromium_src-b81637c3d9f2cf895b81d5b673b88668b6bc1322.tar.gz chromium_src-b81637c3d9f2cf895b81d5b673b88668b6bc1322.tar.bz2 |
Use platform-appropriate newlines in JSON output.
BUG=15462
TEST=base_unittests, unit_tests, check newlines in bookmarks file
Review URL: http://codereview.chromium.org/147220
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19418 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util.cc')
-rw-r--r-- | base/file_util.cc | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/base/file_util.cc b/base/file_util.cc index c366a76..505dfb4 100644 --- a/base/file_util.cc +++ b/base/file_util.cc @@ -129,21 +129,60 @@ bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { file1.read(buffer1, BUFFER_SIZE); file2.read(buffer2, BUFFER_SIZE); - if ((file1.eof() && !file2.eof()) || - (!file1.eof() && file2.eof()) || + if ((file1.eof() != file2.eof()) || (file1.gcount() != file2.gcount()) || (memcmp(buffer1, buffer2, file1.gcount()))) { file1.close(); file2.close(); return false; } - } while (!file1.eof() && !file2.eof()); + } while (!file1.eof() || !file2.eof()); file1.close(); file2.close(); return true; } +bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { + std::ifstream file1(filename1.value().c_str(), std::ios::in); + std::ifstream file2(filename2.value().c_str(), std::ios::in); + + // Even if both files aren't openable (and thus, in some sense, "equal"), + // any unusable file yields a result of "false". + if (!file1.is_open() || !file2.is_open()) + return false; + + do { + std::string line1, line2; + getline(file1, line1); + getline(file2, line2); + + // Check for mismatched EOF states, or any error state. + if ((file1.eof() != file2.eof()) || + file1.bad() || file2.bad()) { + return false; + } + + // Trim all '\r' and '\n' characters from the end of the line. + std::string::size_type end1 = line1.find_last_not_of("\r\n"); + if (end1 == std::string::npos) + line1.clear(); + else if (end1 + 1 < line1.length()) + line1.erase(end1 + 1); + + std::string::size_type end2 = line2.find_last_not_of("\r\n"); + if (end2 == std::string::npos) + line2.clear(); + else if (end2 + 1 < line2.length()) + line2.erase(end2 + 1); + + if (line1 != line2) + return false; + } while (!file1.eof() || !file2.eof()); + + return true; +} + bool ReadFileToString(const FilePath& path, std::string* contents) { FILE* file = OpenFile(path, "rb"); if (!file) { |