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/file_path.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "unicode/uniset.h" | 14 #include "unicode/uniset.h" |
15 | 15 |
16 namespace file_util { | 16 namespace file_util { |
17 | 17 |
18 const wchar_t kExtensionSeparator = L'.'; | 18 const wchar_t kExtensionSeparator = L'.'; |
19 | 19 |
20 void PathComponents(const std::wstring& path, | 20 void PathComponents(const std::wstring& path, |
21 std::vector<std::wstring>* components) { | 21 std::vector<std::wstring>* components) { |
22 DCHECK(components != NULL); | 22 DCHECK(components != NULL); |
23 if (components == NULL) | 23 if (components == NULL) |
24 return; | 24 return; |
25 std::wstring::size_type start = 0; | 25 std::wstring::size_type start = 0; |
26 std::wstring::size_type end = path.find(kPathSeparator, start); | 26 std::wstring::size_type end = path.find(kPathSeparator, start); |
27 | 27 |
28 // Special case the "/" or "\" directory. On Windows with a drive letter, | 28 // Special case the "/" or "\" directory. On Windows with a drive letter, |
29 // this code path won't hit, but the right thing should still happen. | 29 // this code path won't hit, but the right thing should still happen. |
30 // "E:\foo" will turn into "E:","foo". | 30 // "E:\foo" will turn into "E:","foo". |
31 if (end == start) { | 31 if (end == start) { |
32 components->push_back(std::wstring(path, 0, 1)); | 32 components->push_back(std::wstring(path, 0, 1)); |
33 start = end + 1; | 33 start = end + 1; |
34 end = path.find(kPathSeparator, start); | 34 end = path.find(kPathSeparator, start); |
35 } | 35 } |
36 while (end != std::wstring::npos) { | 36 while (end != std::wstring::npos) { |
37 std::wstring component = std::wstring(path, start, end - start); | 37 std::wstring component = std::wstring(path, start, end - start); |
38 components->push_back(component); | 38 components->push_back(component); |
39 start = end + 1; | 39 start = end + 1; |
40 end = path.find(kPathSeparator, start); | 40 end = path.find(kPathSeparator, start); |
41 } | 41 } |
42 std::wstring component = std::wstring(path, start); | 42 std::wstring component = std::wstring(path, start); |
43 components->push_back(component); | 43 components->push_back(component); |
44 } | 44 } |
45 | 45 |
46 bool EndsWithSeparator(std::wstring* path) { | 46 bool EndsWithSeparator(const FilePath& file_path) { |
47 return EndsWithSeparator(*path); | 47 std::wstring path = file_path.ToWStringHack(); |
| 48 bool is_sep = (path.length() > 0 && |
| 49 path[path.length() - 1] == kPathSeparator); |
| 50 return is_sep; |
48 } | 51 } |
49 | 52 |
50 bool EndsWithSeparator(const std::wstring& path) { | 53 bool EnsureEndsWithSeparator(FilePath* path) { |
51 bool is_sep = (path.length() > 0 && | 54 if (!DirectoryExists(*path)) |
52 (path)[path.length() - 1] == kPathSeparator); | 55 return false; |
53 return is_sep; | 56 |
| 57 if (EndsWithSeparator(*path)) |
| 58 return true; |
| 59 |
| 60 FilePath::StringType& path_str = |
| 61 const_cast<FilePath::StringType&>(path->value()); |
| 62 path_str.append(&FilePath::kSeparators[0], 1); |
| 63 |
| 64 return true; |
54 } | 65 } |
55 | 66 |
56 void TrimTrailingSeparator(std::wstring* dir) { | 67 void TrimTrailingSeparator(std::wstring* dir) { |
57 while (dir->length() > 1 && EndsWithSeparator(dir)) | 68 while (dir->length() > 1 && EndsWithSeparator(dir)) |
58 dir->resize(dir->length() - 1); | 69 dir->resize(dir->length() - 1); |
59 } | 70 } |
60 | 71 |
61 void UpOneDirectory(std::wstring* dir) { | 72 void UpOneDirectory(std::wstring* dir) { |
62 TrimTrailingSeparator(dir); | 73 TrimTrailingSeparator(dir); |
63 | 74 |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 bool AbsolutePath(std::wstring* path_str) { | 319 bool AbsolutePath(std::wstring* path_str) { |
309 FilePath path(FilePath::FromWStringHack(*path_str)); | 320 FilePath path(FilePath::FromWStringHack(*path_str)); |
310 if (!AbsolutePath(&path)) | 321 if (!AbsolutePath(&path)) |
311 return false; | 322 return false; |
312 *path_str = path.ToWStringHack(); | 323 *path_str = path.ToWStringHack(); |
313 return true; | 324 return true; |
314 } | 325 } |
315 bool Delete(const std::wstring& path, bool recursive) { | 326 bool Delete(const std::wstring& path, bool recursive) { |
316 return Delete(FilePath::FromWStringHack(path), recursive); | 327 return Delete(FilePath::FromWStringHack(path), recursive); |
317 } | 328 } |
| 329 bool EndsWithSeparator(std::wstring* path) { |
| 330 return EndsWithSeparator(FilePath::FromWStringHack(*path)); |
| 331 } |
| 332 bool EndsWithSeparator(const std::wstring& path) { |
| 333 return EndsWithSeparator(FilePath::FromWStringHack(path)); |
| 334 } |
318 bool Move(const std::wstring& from_path, const std::wstring& to_path) { | 335 bool Move(const std::wstring& from_path, const std::wstring& to_path) { |
319 return Move(FilePath::FromWStringHack(from_path), | 336 return Move(FilePath::FromWStringHack(from_path), |
320 FilePath::FromWStringHack(to_path)); | 337 FilePath::FromWStringHack(to_path)); |
321 } | 338 } |
322 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { | 339 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { |
323 return CopyFile(FilePath::FromWStringHack(from_path), | 340 return CopyFile(FilePath::FromWStringHack(from_path), |
324 FilePath::FromWStringHack(to_path)); | 341 FilePath::FromWStringHack(to_path)); |
325 } | 342 } |
326 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, | 343 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, |
327 bool recursive) { | 344 bool recursive) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 } | 389 } |
373 FILE* OpenFile(const std::wstring& filename, const char* mode) { | 390 FILE* OpenFile(const std::wstring& filename, const char* mode) { |
374 return OpenFile(FilePath::FromWStringHack(filename), mode); | 391 return OpenFile(FilePath::FromWStringHack(filename), mode); |
375 } | 392 } |
376 bool SetCurrentDirectory(const std::wstring& directory) { | 393 bool SetCurrentDirectory(const std::wstring& directory) { |
377 return SetCurrentDirectory(FilePath::FromWStringHack(directory)); | 394 return SetCurrentDirectory(FilePath::FromWStringHack(directory)); |
378 } | 395 } |
379 | 396 |
380 } // namespace | 397 } // namespace |
381 | 398 |
OLD | NEW |