| 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 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
| 17 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
| 18 | 18 |
| 19 namespace { |
| 20 |
| 21 const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); |
| 22 |
| 23 } |
| 24 |
| 19 namespace file_util { | 25 namespace file_util { |
| 20 | 26 |
| 21 void PathComponents(const FilePath& path, | 27 void PathComponents(const FilePath& path, |
| 22 std::vector<FilePath::StringType>* components) { | 28 std::vector<FilePath::StringType>* components) { |
| 23 DCHECK(components); | 29 DCHECK(components); |
| 24 if (!components) | 30 if (!components) |
| 25 return; | 31 return; |
| 26 | 32 |
| 27 FilePath::StringType path_str = path.value(); | 33 FilePath::StringType path_str = path.value(); |
| 28 FilePath::StringType::size_type start = 0; | 34 FilePath::StringType::size_type start = 0; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 path_str.append(&FilePath::kSeparators[0], 1); | 72 path_str.append(&FilePath::kSeparators[0], 1); |
| 67 | 73 |
| 68 return true; | 74 return true; |
| 69 } | 75 } |
| 70 | 76 |
| 71 void TrimTrailingSeparator(std::wstring* dir) { | 77 void TrimTrailingSeparator(std::wstring* dir) { |
| 72 while (dir->length() > 1 && EndsWithSeparator(dir)) | 78 while (dir->length() > 1 && EndsWithSeparator(dir)) |
| 73 dir->resize(dir->length() - 1); | 79 dir->resize(dir->length() - 1); |
| 74 } | 80 } |
| 75 | 81 |
| 76 std::wstring GetFilenameFromPath(const std::wstring& path) { | |
| 77 // TODO(erikkay): fix this - it's not using kPathSeparator, but win unit test | |
| 78 // are exercising '/' as a path separator as well. | |
| 79 std::wstring::size_type pos = path.find_last_of(L"\\/"); | |
| 80 return std::wstring(path, pos == std::wstring::npos ? 0 : pos + 1); | |
| 81 } | |
| 82 | |
| 83 std::wstring GetFileExtensionFromPath(const std::wstring& path) { | 82 std::wstring GetFileExtensionFromPath(const std::wstring& path) { |
| 84 std::wstring file_name = GetFilenameFromPath(path); | 83 std::wstring file_name = GetFilenameFromPath(path); |
| 85 std::wstring::size_type last_dot = file_name.rfind(L'.'); | 84 std::wstring::size_type last_dot = file_name.rfind(L'.'); |
| 86 return std::wstring(last_dot == std::wstring::npos ? | 85 return std::wstring(last_dot == std::wstring::npos ? |
| 87 L"" : | 86 L"" : |
| 88 file_name, last_dot+1); | 87 file_name, last_dot+1); |
| 89 } | 88 } |
| 90 | 89 |
| 91 std::wstring GetFilenameWithoutExtensionFromPath(const std::wstring& path) { | 90 std::wstring GetFilenameWithoutExtensionFromPath(const std::wstring& path) { |
| 92 std::wstring file_name = GetFilenameFromPath(path); | 91 std::wstring file_name = GetFilenameFromPath(path); |
| 93 std::wstring::size_type last_dot = file_name.rfind(L'.'); | 92 std::wstring::size_type last_dot = file_name.rfind(L'.'); |
| 94 return file_name.substr(0, last_dot); | 93 return file_name.substr(0, last_dot); |
| 95 } | 94 } |
| 96 | 95 |
| 96 void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { |
| 97 FilePath::StringType& value = |
| 98 const_cast<FilePath::StringType&>(path->value()); |
| 99 |
| 100 const FilePath::StringType::size_type last_dot = |
| 101 value.rfind(kExtensionSeparator); |
| 102 const FilePath::StringType::size_type last_separator = |
| 103 value.find_last_of(FilePath::StringType(FilePath::kSeparators)); |
| 104 |
| 105 if (last_dot == FilePath::StringType::npos || |
| 106 (last_separator != std::wstring::npos && last_dot < last_separator)) { |
| 107 // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". |
| 108 // We should just append the suffix to the entire path. |
| 109 value.append(suffix); |
| 110 return; |
| 111 } |
| 112 |
| 113 value.insert(last_dot, suffix); |
| 114 } |
| 115 |
| 116 void ReplaceExtension(FilePath* path, const FilePath::StringType& extension) { |
| 117 FilePath::StringType clean_extension; |
| 118 // If the new extension is "" or ".", then we will just remove the current |
| 119 // extension. |
| 120 if (!extension.empty() && |
| 121 extension != FilePath::StringType(&kExtensionSeparator, 1)) { |
| 122 if (extension[0] != kExtensionSeparator) |
| 123 clean_extension.append(&kExtensionSeparator, 1); |
| 124 clean_extension.append(extension); |
| 125 } |
| 126 |
| 127 FilePath::StringType& value = |
| 128 const_cast<FilePath::StringType&>(path->value()); |
| 129 const FilePath::StringType::size_type last_dot = |
| 130 value.rfind(kExtensionSeparator); |
| 131 const FilePath::StringType::size_type last_separator = |
| 132 value.find_last_of(FilePath::StringType(FilePath::kSeparators)); |
| 133 |
| 134 // Erase the current extension, if any. |
| 135 if ((last_dot > last_separator || |
| 136 last_separator == FilePath::StringType::npos) && |
| 137 last_dot != FilePath::StringType::npos) |
| 138 value.erase(last_dot); |
| 139 |
| 140 value.append(clean_extension); |
| 141 } |
| 142 |
| 97 void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char) { | 143 void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char) { |
| 98 DCHECK(file_name); | 144 DCHECK(file_name); |
| 99 | 145 |
| 100 // Control characters, formatting characters, non-characters, and | 146 // Control characters, formatting characters, non-characters, and |
| 101 // some printable ASCII characters regarded as dangerous ('"*/:<>?\\'). | 147 // some printable ASCII characters regarded as dangerous ('"*/:<>?\\'). |
| 102 // See http://blogs.msdn.com/michkap/archive/2006/11/03/941420.aspx | 148 // See http://blogs.msdn.com/michkap/archive/2006/11/03/941420.aspx |
| 103 // and http://msdn2.microsoft.com/en-us/library/Aa365247.aspx | 149 // and http://msdn2.microsoft.com/en-us/library/Aa365247.aspx |
| 104 // TODO(jungshik): Revisit the set. ZWJ and ZWNJ are excluded because they | 150 // TODO(jungshik): Revisit the set. ZWJ and ZWNJ are excluded because they |
| 105 // are legitimate in Arabic and some S/SE Asian scripts. However, when used | 151 // are legitimate in Arabic and some S/SE Asian scripts. However, when used |
| 106 // elsewhere, they can be confusing/problematic. | 152 // elsewhere, they can be confusing/problematic. |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 bool GetCurrentDirectory(std::wstring* path_str) { | 334 bool GetCurrentDirectory(std::wstring* path_str) { |
| 289 FilePath path; | 335 FilePath path; |
| 290 if (!GetCurrentDirectory(&path)) | 336 if (!GetCurrentDirectory(&path)) |
| 291 return false; | 337 return false; |
| 292 *path_str = path.ToWStringHack(); | 338 *path_str = path.ToWStringHack(); |
| 293 return true; | 339 return true; |
| 294 } | 340 } |
| 295 bool GetFileInfo(const std::wstring& file_path, FileInfo* results) { | 341 bool GetFileInfo(const std::wstring& file_path, FileInfo* results) { |
| 296 return GetFileInfo(FilePath::FromWStringHack(file_path), results); | 342 return GetFileInfo(FilePath::FromWStringHack(file_path), results); |
| 297 } | 343 } |
| 344 std::wstring GetFilenameFromPath(const std::wstring& path) { |
| 345 if (path.empty() || EndsWithSeparator(path)) |
| 346 return std::wstring(); |
| 347 |
| 348 return FilePath::FromWStringHack(path).BaseName().ToWStringHack(); |
| 349 } |
| 298 bool GetFileSize(const std::wstring& file_path, int64* file_size) { | 350 bool GetFileSize(const std::wstring& file_path, int64* file_size) { |
| 299 return GetFileSize(FilePath::FromWStringHack(file_path), file_size); | 351 return GetFileSize(FilePath::FromWStringHack(file_path), file_size); |
| 300 } | 352 } |
| 301 bool GetTempDir(std::wstring* path_str) { | 353 bool GetTempDir(std::wstring* path_str) { |
| 302 FilePath path; | 354 FilePath path; |
| 303 if (!GetTempDir(&path)) | 355 if (!GetTempDir(&path)) |
| 304 return false; | 356 return false; |
| 305 *path_str = path.ToWStringHack(); | 357 *path_str = path.ToWStringHack(); |
| 306 return true; | 358 return true; |
| 307 } | 359 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 338 FilePath directory = path.DirName(); | 390 FilePath directory = path.DirName(); |
| 339 // If there is no separator, we will get back kCurrentDirectory. | 391 // If there is no separator, we will get back kCurrentDirectory. |
| 340 // In this case, clear dir. | 392 // In this case, clear dir. |
| 341 if (directory == path || directory.value() == FilePath::kCurrentDirectory) | 393 if (directory == path || directory.value() == FilePath::kCurrentDirectory) |
| 342 dir->clear(); | 394 dir->clear(); |
| 343 else | 395 else |
| 344 *dir = directory.ToWStringHack(); | 396 *dir = directory.ToWStringHack(); |
| 345 } | 397 } |
| 346 } // namespace | 398 } // namespace |
| 347 | 399 |
| OLD | NEW |