Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: base/file_util.cc

Issue 147220: Use platform-appropriate newlines in JSON output (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <io.h> 8 #include <io.h>
9 #endif 9 #endif
10 #include <stdio.h> 10 #include <stdio.h>
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 // any unusable file yields a result of "false". 122 // any unusable file yields a result of "false".
123 if (!file1.is_open() || !file2.is_open()) 123 if (!file1.is_open() || !file2.is_open())
124 return false; 124 return false;
125 125
126 const int BUFFER_SIZE = 2056; 126 const int BUFFER_SIZE = 2056;
127 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; 127 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
128 do { 128 do {
129 file1.read(buffer1, BUFFER_SIZE); 129 file1.read(buffer1, BUFFER_SIZE);
130 file2.read(buffer2, BUFFER_SIZE); 130 file2.read(buffer2, BUFFER_SIZE);
131 131
132 if ((file1.eof() && !file2.eof()) || 132 if ((file1.eof() != file2.eof()) ||
133 (!file1.eof() && file2.eof()) ||
134 (file1.gcount() != file2.gcount()) || 133 (file1.gcount() != file2.gcount()) ||
135 (memcmp(buffer1, buffer2, file1.gcount()))) { 134 (memcmp(buffer1, buffer2, file1.gcount()))) {
136 file1.close(); 135 file1.close();
137 file2.close(); 136 file2.close();
138 return false; 137 return false;
139 } 138 }
140 } while (!file1.eof() && !file2.eof()); 139 } while (!file1.eof() || !file2.eof());
141 140
142 file1.close(); 141 file1.close();
143 file2.close(); 142 file2.close();
144 return true; 143 return true;
145 } 144 }
146 145
146 bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) {
147 std::ifstream file1(filename1.value().c_str(), std::ios::in);
148 std::ifstream file2(filename2.value().c_str(), std::ios::in);
149
150 // Even if both files aren't openable (and thus, in some sense, "equal"),
151 // any unusable file yields a result of "false".
152 if (!file1.is_open() || !file2.is_open())
153 return false;
154
155 do {
156 std::string line1, line2;
157 getline(file1, line1);
158 getline(file2, line2);
159
160 // Check for mismatched EOF states, or any error state.
161 if ((file1.eof() != file2.eof()) ||
162 file1.bad() || file2.bad()) {
163 return false;
164 }
165
166 // Trim all '\r' and '\n' characters from the end of the line.
167 std::string::size_type end1 = line1.find_last_not_of("\r\n");
168 if (end1 == std::string::npos)
169 line1.clear();
170 else if (end1 + 1 < line1.length())
171 line1.erase(end1 + 1);
172
173 std::string::size_type end2 = line2.find_last_not_of("\r\n");
174 if (end2 == std::string::npos)
175 line2.clear();
176 else if (end2 + 1 < line2.length())
177 line2.erase(end2 + 1);
178
179 if (line1 != line2)
180 return false;
181 } while (!file1.eof() || !file2.eof());
182
183 return true;
184 }
185
147 bool ReadFileToString(const FilePath& path, std::string* contents) { 186 bool ReadFileToString(const FilePath& path, std::string* contents) {
148 FILE* file = OpenFile(path, "rb"); 187 FILE* file = OpenFile(path, "rb");
149 if (!file) { 188 if (!file) {
150 return false; 189 return false;
151 } 190 }
152 191
153 char buf[1 << 16]; 192 char buf[1 << 16];
154 size_t len; 193 size_t len;
155 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { 194 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
156 contents->append(buf, len); 195 contents->append(buf, len);
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 450
412 bool FileEnumerator::IsDot(const FilePath& path) { 451 bool FileEnumerator::IsDot(const FilePath& path) {
413 return FILE_PATH_LITERAL(".") == path.BaseName().value(); 452 return FILE_PATH_LITERAL(".") == path.BaseName().value();
414 } 453 }
415 454
416 bool FileEnumerator::IsDotDot(const FilePath& path) { 455 bool FileEnumerator::IsDotDot(const FilePath& path) {
417 return FILE_PATH_LITERAL("..") == path.BaseName().value(); 456 return FILE_PATH_LITERAL("..") == path.BaseName().value();
418 } 457 }
419 458
420 } // namespace 459 } // namespace
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698