OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include <fstream> | 9 #include <fstream> |
10 | 10 |
| 11 #include "base/file_path.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
13 #include "unicode/uniset.h" | 14 #include "unicode/uniset.h" |
14 | 15 |
15 namespace file_util { | 16 namespace file_util { |
16 | 17 |
17 const wchar_t kExtensionSeparator = L'.'; | 18 const wchar_t kExtensionSeparator = L'.'; |
18 | 19 |
19 void PathComponents(const std::wstring& path, | 20 void PathComponents(const std::wstring& path, |
20 std::vector<std::wstring>* components) { | 21 std::vector<std::wstring>* components) { |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 // replace it, just append the supplied extension. For example | 223 // replace it, just append the supplied extension. For example |
223 // 'c:\tmp.bar\foo'. | 224 // 'c:\tmp.bar\foo'. |
224 AppendExtension(extension, file_name); | 225 AppendExtension(extension, file_name); |
225 return; | 226 return; |
226 } | 227 } |
227 std::wstring result = file_name->substr(0, last_dot); | 228 std::wstring result = file_name->substr(0, last_dot); |
228 AppendExtension(extension, &result); | 229 AppendExtension(extension, &result); |
229 file_name->swap(result); | 230 file_name->swap(result); |
230 } | 231 } |
231 | 232 |
232 bool ContentsEqual(const std::wstring& filename1, | 233 bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
233 const std::wstring& filename2) { | |
234 // We open the file in binary format even if they are text files because | 234 // We open the file in binary format even if they are text files because |
235 // we are just comparing that bytes are exactly same in both files and not | 235 // we are just comparing that bytes are exactly same in both files and not |
236 // doing anything smart with text formatting. | 236 // doing anything smart with text formatting. |
237 #if defined(OS_WIN) | 237 std::ifstream file1(filename1.value().c_str(), |
238 std::ifstream file1(filename1.c_str(), std::ios::in | std::ios::binary); | |
239 std::ifstream file2(filename2.c_str(), std::ios::in | std::ios::binary); | |
240 #elif defined(OS_POSIX) | |
241 std::ifstream file1(WideToUTF8(filename1).c_str(), | |
242 std::ios::in | std::ios::binary); | 238 std::ios::in | std::ios::binary); |
243 std::ifstream file2(WideToUTF8(filename2).c_str(), | 239 std::ifstream file2(filename2.value().c_str(), |
244 std::ios::in | std::ios::binary); | 240 std::ios::in | std::ios::binary); |
245 #endif | |
246 | 241 |
247 // Even if both files aren't openable (and thus, in some sense, "equal"), | 242 // Even if both files aren't openable (and thus, in some sense, "equal"), |
248 // any unusable file yields a result of "false". | 243 // any unusable file yields a result of "false". |
249 if (!file1.is_open() || !file2.is_open()) | 244 if (!file1.is_open() || !file2.is_open()) |
250 return false; | 245 return false; |
251 | 246 |
252 const int BUFFER_SIZE = 2056; | 247 const int BUFFER_SIZE = 2056; |
253 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; | 248 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
254 do { | 249 do { |
255 file1.read(buffer1, BUFFER_SIZE); | 250 file1.read(buffer1, BUFFER_SIZE); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 *file_size = info.size; | 288 *file_size = info.size; |
294 return true; | 289 return true; |
295 } | 290 } |
296 | 291 |
297 bool CloseFile(FILE* file) { | 292 bool CloseFile(FILE* file) { |
298 if (file == NULL) | 293 if (file == NULL) |
299 return true; | 294 return true; |
300 return fclose(file) == 0; | 295 return fclose(file) == 0; |
301 } | 296 } |
302 | 297 |
| 298 // Deprecated functions ---------------------------------------------------- |
| 299 |
| 300 bool AbsolutePath(std::wstring* path_str) { |
| 301 FilePath path; |
| 302 if (!AbsolutePath(&path)) |
| 303 return false; |
| 304 *path_str = path.ToWStringHack(); |
| 305 return true; |
| 306 } |
| 307 bool Delete(const std::wstring& path, bool recursive) { |
| 308 return Delete(FilePath::FromWStringHack(path), recursive); |
| 309 } |
| 310 bool Move(const std::wstring& from_path, const std::wstring& to_path) { |
| 311 return Move(FilePath::FromWStringHack(from_path), |
| 312 FilePath::FromWStringHack(to_path)); |
| 313 } |
| 314 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { |
| 315 return CopyFile(FilePath::FromWStringHack(from_path), |
| 316 FilePath::FromWStringHack(to_path)); |
| 317 } |
| 318 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, |
| 319 bool recursive) { |
| 320 return CopyDirectory(FilePath::FromWStringHack(from_path), |
| 321 FilePath::FromWStringHack(to_path), |
| 322 recursive); |
| 323 } |
| 324 bool PathExists(const std::wstring& path) { |
| 325 return PathExists(FilePath::FromWStringHack(path)); |
| 326 } |
| 327 bool DirectoryExists(const std::wstring& path) { |
| 328 return DirectoryExists(FilePath::FromWStringHack(path)); |
| 329 } |
| 330 bool ContentsEqual(const std::wstring& filename1, |
| 331 const std::wstring& filename2) { |
| 332 return ContentsEqual(FilePath::FromWStringHack(filename1), |
| 333 FilePath::FromWStringHack(filename2)); |
| 334 } |
| 335 bool CreateDirectory(const std::wstring& full_path) { |
| 336 return CreateDirectory(FilePath::FromWStringHack(full_path)); |
| 337 } |
| 338 bool GetCurrentDirectory(std::wstring* path_str) { |
| 339 FilePath path; |
| 340 if (!GetCurrentDirectory(&path)) |
| 341 return false; |
| 342 *path_str = path.ToWStringHack(); |
| 343 return true; |
| 344 } |
| 345 bool GetTempDir(std::wstring* path_str) { |
| 346 FilePath path; |
| 347 if (!GetTempDir(&path)) |
| 348 return false; |
| 349 *path_str = path.ToWStringHack(); |
| 350 return true; |
| 351 } |
| 352 |
303 } // namespace | 353 } // namespace |
304 | 354 |
OLD | NEW |