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 <windows.h> | 7 #include <windows.h> |
8 #include <shellapi.h> | 8 #include <shellapi.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #include <time.h> | 10 #include <time.h> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/scoped_handle.h" | 15 #include "base/scoped_handle.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/win_util.h" | 17 #include "base/win_util.h" |
18 | 18 |
19 namespace file_util { | 19 namespace file_util { |
20 | 20 |
21 const wchar_t kPathSeparator = L'\\'; | |
22 const wchar_t kExtensionSeparator = L'.'; | |
23 | |
24 void PathComponents(const std::wstring& path, | |
25 std::vector<std::wstring>* components) { | |
26 PathComponents(FilePath(path), components); | |
27 } | |
28 | |
29 std::wstring GetDirectoryFromPath(const std::wstring& path) { | 21 std::wstring GetDirectoryFromPath(const std::wstring& path) { |
30 wchar_t path_buffer[MAX_PATH]; | 22 wchar_t path_buffer[MAX_PATH]; |
31 wchar_t* file_ptr = NULL; | 23 wchar_t* file_ptr = NULL; |
32 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) | 24 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) |
33 return L""; | 25 return L""; |
34 | 26 |
35 std::wstring::size_type length = | 27 std::wstring::size_type length = |
36 file_ptr ? file_ptr - path_buffer : path.length(); | 28 file_ptr ? file_ptr - path_buffer : path.length(); |
37 std::wstring directory(path, 0, length); | 29 std::wstring directory(path, 0, length); |
38 TrimTrailingSeparator(&directory); | 30 TrimTrailingSeparator(&directory); |
39 return directory; | 31 return directory; |
40 } | 32 } |
41 | 33 |
42 bool AbsolutePath(FilePath* path) { | 34 bool AbsolutePath(FilePath* path) { |
43 wchar_t file_path_buf[MAX_PATH]; | 35 wchar_t file_path_buf[MAX_PATH]; |
44 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) | 36 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) |
45 return false; | 37 return false; |
46 *path = FilePath(file_path_buf); | 38 *path = FilePath(file_path_buf); |
47 return true; | 39 return true; |
48 } | 40 } |
49 | 41 |
50 void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix) { | |
51 DCHECK(path); | |
52 | |
53 const std::wstring::size_type last_dot = path->rfind(kExtensionSeparator); | |
54 const std::wstring::size_type last_sep = path->rfind(kPathSeparator); | |
55 | |
56 if (last_dot == std::wstring::npos || | |
57 (last_sep != std::wstring::npos && last_dot < last_sep)) { | |
58 // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". | |
59 // We should just append the suffix to the entire path. | |
60 path->append(suffix); | |
61 return; | |
62 } | |
63 | |
64 path->insert(last_dot, suffix); | |
65 } | |
66 | |
67 // Appends the extension to file adding a '.' if extension doesn't contain one. | |
68 // This does nothing if extension is empty or '.'. This is used internally by | |
69 // ReplaceExtension. | |
70 static void AppendExtension(const std::wstring& extension, | |
71 std::wstring* file) { | |
72 if (!extension.empty() && extension != L".") { | |
73 if (extension[0] != L'.') | |
74 file->append(L"."); | |
75 file->append(extension); | |
76 } | |
77 } | |
78 | |
79 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { | |
80 const std::wstring::size_type last_dot = file_name->rfind(L'.'); | |
81 if (last_dot == std::wstring::npos) { | |
82 // No extension, just append the supplied extension. | |
83 AppendExtension(extension, file_name); | |
84 return; | |
85 } | |
86 const std::wstring::size_type last_separator = | |
87 file_name->rfind(kPathSeparator); | |
88 if (last_separator != std::wstring::npos && last_dot < last_separator) { | |
89 // File name doesn't have extension, but one of the directories does; don't | |
90 // replace it, just append the supplied extension. For example | |
91 // 'c:\tmp.bar\foo'. | |
92 AppendExtension(extension, file_name); | |
93 return; | |
94 } | |
95 std::wstring result = file_name->substr(0, last_dot); | |
96 AppendExtension(extension, &result); | |
97 file_name->swap(result); | |
98 } | |
99 int CountFilesCreatedAfter(const std::wstring& path, | 42 int CountFilesCreatedAfter(const std::wstring& path, |
100 const FILETIME& comparison_time) { | 43 const FILETIME& comparison_time) { |
101 int file_count = 0; | 44 int file_count = 0; |
102 | 45 |
103 WIN32_FIND_DATA find_file_data; | 46 WIN32_FIND_DATA find_file_data; |
104 std::wstring filename_spec = path + L"\\*"; // All files in given dir | 47 std::wstring filename_spec = path + L"\\*"; // All files in given dir |
105 HANDLE find_handle = FindFirstFile(filename_spec.c_str(), &find_file_data); | 48 HANDLE find_handle = FindFirstFile(filename_spec.c_str(), &find_file_data); |
106 if (find_handle != INVALID_HANDLE_VALUE) { | 49 if (find_handle != INVALID_HANDLE_VALUE) { |
107 do { | 50 do { |
108 // Don't count current or parent directories. | 51 // Don't count current or parent directories. |
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 if (recursive_) { | 687 if (recursive_) { |
745 // If |cur_file| is a directory, and we are doing recursive searching, add | 688 // If |cur_file| is a directory, and we are doing recursive searching, add |
746 // it to pending_paths_ so we scan it after we finish scanning this | 689 // it to pending_paths_ so we scan it after we finish scanning this |
747 // directory. | 690 // directory. |
748 pending_paths_.push(cur_file); | 691 pending_paths_.push(cur_file); |
749 } | 692 } |
750 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); | 693 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); |
751 } | 694 } |
752 return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); | 695 return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); |
753 } | 696 } |
| 697 |
| 698 // Deprecated functions ---------------------------------------------------- |
| 699 |
| 700 void InsertBeforeExtension(std::wstring* path_str, |
| 701 const std::wstring& suffix) { |
| 702 FilePath path(*path_str); |
| 703 InsertBeforeExtension(&path, suffix); |
| 704 path_str->assign(path.value()); |
| 705 } |
| 706 void PathComponents(const std::wstring& path, |
| 707 std::vector<std::wstring>* components) { |
| 708 PathComponents(FilePath(path), components); |
| 709 } |
| 710 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { |
| 711 FilePath path(*file_name); |
| 712 ReplaceExtension(&path, extension); |
| 713 file_name->assign(path.value()); |
| 714 } |
754 } // namespace file_util | 715 } // namespace file_util |
OLD | NEW |