| 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'\\'; | 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 } |
| 22 | 28 |
| 23 std::wstring GetDirectoryFromPath(const std::wstring& path) { | 29 std::wstring GetDirectoryFromPath(const std::wstring& path) { |
| 24 wchar_t path_buffer[MAX_PATH]; | 30 wchar_t path_buffer[MAX_PATH]; |
| 25 wchar_t* file_ptr = NULL; | 31 wchar_t* file_ptr = NULL; |
| 26 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) | 32 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) |
| 27 return L""; | 33 return L""; |
| 28 | 34 |
| 29 std::wstring::size_type length = | 35 std::wstring::size_type length = |
| 30 file_ptr ? file_ptr - path_buffer : path.length(); | 36 file_ptr ? file_ptr - path_buffer : path.length(); |
| 31 std::wstring directory(path, 0, length); | 37 std::wstring directory(path, 0, length); |
| 32 TrimTrailingSeparator(&directory); | 38 TrimTrailingSeparator(&directory); |
| 33 return directory; | 39 return directory; |
| 34 } | 40 } |
| 35 | 41 |
| 36 bool AbsolutePath(FilePath* path) { | 42 bool AbsolutePath(FilePath* path) { |
| 37 wchar_t file_path_buf[MAX_PATH]; | 43 wchar_t file_path_buf[MAX_PATH]; |
| 38 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) | 44 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) |
| 39 return false; | 45 return false; |
| 40 *path = FilePath(file_path_buf); | 46 *path = FilePath(file_path_buf); |
| 41 return true; | 47 return true; |
| 42 } | 48 } |
| 43 | 49 |
| 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 } |
| 44 int CountFilesCreatedAfter(const std::wstring& path, | 99 int CountFilesCreatedAfter(const std::wstring& path, |
| 45 const FILETIME& comparison_time) { | 100 const FILETIME& comparison_time) { |
| 46 int file_count = 0; | 101 int file_count = 0; |
| 47 | 102 |
| 48 WIN32_FIND_DATA find_file_data; | 103 WIN32_FIND_DATA find_file_data; |
| 49 std::wstring filename_spec = path + L"\\*"; // All files in given dir | 104 std::wstring filename_spec = path + L"\\*"; // All files in given dir |
| 50 HANDLE find_handle = FindFirstFile(filename_spec.c_str(), &find_file_data); | 105 HANDLE find_handle = FindFirstFile(filename_spec.c_str(), &find_file_data); |
| 51 if (find_handle != INVALID_HANDLE_VALUE) { | 106 if (find_handle != INVALID_HANDLE_VALUE) { |
| 52 do { | 107 do { |
| 53 // Don't count current or parent directories. | 108 // Don't count current or parent directories. |
| (...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 // it to pending_paths_ so we scan it after we finish scanning this | 746 // it to pending_paths_ so we scan it after we finish scanning this |
| 692 // directory. | 747 // directory. |
| 693 pending_paths_.push(cur_file); | 748 pending_paths_.push(cur_file); |
| 694 } | 749 } |
| 695 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); | 750 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); |
| 696 } | 751 } |
| 697 return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); | 752 return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); |
| 698 } | 753 } |
| 699 | 754 |
| 700 } // namespace file_util | 755 } // namespace file_util |
| OLD | NEW |