| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_path.h" | 5 #include "base/file_path.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "base/mac/scoped_cftyperef.h" | 22 #include "base/mac/scoped_cftyperef.h" |
| 23 #include "base/third_party/icu/icu_utf.h" | 23 #include "base/third_party/icu/icu_utf.h" |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| 27 #include <windows.h> | 27 #include <windows.h> |
| 28 #elif defined(OS_MACOSX) | 28 #elif defined(OS_MACOSX) |
| 29 #include <CoreFoundation/CoreFoundation.h> | 29 #include <CoreFoundation/CoreFoundation.h> |
| 30 #endif | 30 #endif |
| 31 | 31 |
| 32 namespace base { |
| 33 |
| 32 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 34 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 33 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); | 35 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); |
| 34 #else // FILE_PATH_USES_WIN_SEPARATORS | 36 #else // FILE_PATH_USES_WIN_SEPARATORS |
| 35 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); | 37 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); |
| 36 #endif // FILE_PATH_USES_WIN_SEPARATORS | 38 #endif // FILE_PATH_USES_WIN_SEPARATORS |
| 37 | 39 |
| 38 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); | 40 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); |
| 39 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); | 41 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); |
| 40 | 42 |
| 41 const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.'); | 43 const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.'); |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 if (IsEmptyOrSpecialCase(BaseName().value())) | 399 if (IsEmptyOrSpecialCase(BaseName().value())) |
| 398 return FilePath(); | 400 return FilePath(); |
| 399 | 401 |
| 400 StringType ext = Extension(); | 402 StringType ext = Extension(); |
| 401 StringType ret = RemoveExtension().value(); | 403 StringType ret = RemoveExtension().value(); |
| 402 ret.append(suffix); | 404 ret.append(suffix); |
| 403 ret.append(ext); | 405 ret.append(ext); |
| 404 return FilePath(ret); | 406 return FilePath(ret); |
| 405 } | 407 } |
| 406 | 408 |
| 407 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix) | 409 FilePath FilePath::InsertBeforeExtensionASCII(const StringPiece& suffix) |
| 408 const { | 410 const { |
| 409 DCHECK(IsStringASCII(suffix)); | 411 DCHECK(IsStringASCII(suffix)); |
| 410 #if defined(OS_WIN) | 412 #if defined(OS_WIN) |
| 411 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); | 413 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); |
| 412 #elif defined(OS_POSIX) | 414 #elif defined(OS_POSIX) |
| 413 return InsertBeforeExtension(suffix.as_string()); | 415 return InsertBeforeExtension(suffix.as_string()); |
| 414 #endif | 416 #endif |
| 415 } | 417 } |
| 416 | 418 |
| 417 FilePath FilePath::AddExtension(const StringType& extension) const { | 419 FilePath FilePath::AddExtension(const StringType& extension) const { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 } | 501 } |
| 500 | 502 |
| 501 new_path.path_.append(*appended); | 503 new_path.path_.append(*appended); |
| 502 return new_path; | 504 return new_path; |
| 503 } | 505 } |
| 504 | 506 |
| 505 FilePath FilePath::Append(const FilePath& component) const { | 507 FilePath FilePath::Append(const FilePath& component) const { |
| 506 return Append(component.value()); | 508 return Append(component.value()); |
| 507 } | 509 } |
| 508 | 510 |
| 509 FilePath FilePath::AppendASCII(const base::StringPiece& component) const { | 511 FilePath FilePath::AppendASCII(const StringPiece& component) const { |
| 510 DCHECK(IsStringASCII(component)); | 512 DCHECK(IsStringASCII(component)); |
| 511 #if defined(OS_WIN) | 513 #if defined(OS_WIN) |
| 512 return Append(ASCIIToUTF16(component.as_string())); | 514 return Append(ASCIIToUTF16(component.as_string())); |
| 513 #elif defined(OS_POSIX) | 515 #elif defined(OS_POSIX) |
| 514 return Append(component.as_string()); | 516 return Append(component.as_string()); |
| 515 #endif | 517 #endif |
| 516 } | 518 } |
| 517 | 519 |
| 518 bool FilePath::IsAbsolute() const { | 520 bool FilePath::IsAbsolute() const { |
| 519 return IsPathAbsolute(path_); | 521 return IsPathAbsolute(path_); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 537 return true; | 539 return true; |
| 538 } | 540 } |
| 539 return false; | 541 return false; |
| 540 } | 542 } |
| 541 | 543 |
| 542 #if defined(OS_POSIX) | 544 #if defined(OS_POSIX) |
| 543 // See file_path.h for a discussion of the encoding of paths on POSIX | 545 // See file_path.h for a discussion of the encoding of paths on POSIX |
| 544 // platforms. These encoding conversion functions are not quite correct. | 546 // platforms. These encoding conversion functions are not quite correct. |
| 545 | 547 |
| 546 string16 FilePath::LossyDisplayName() const { | 548 string16 FilePath::LossyDisplayName() const { |
| 547 return WideToUTF16(base::SysNativeMBToWide(path_)); | 549 return WideToUTF16(SysNativeMBToWide(path_)); |
| 548 } | 550 } |
| 549 | 551 |
| 550 std::string FilePath::MaybeAsASCII() const { | 552 std::string FilePath::MaybeAsASCII() const { |
| 551 if (IsStringASCII(path_)) | 553 if (IsStringASCII(path_)) |
| 552 return path_; | 554 return path_; |
| 553 return ""; | 555 return ""; |
| 554 } | 556 } |
| 555 | 557 |
| 556 std::string FilePath::AsUTF8Unsafe() const { | 558 std::string FilePath::AsUTF8Unsafe() const { |
| 557 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) | 559 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 558 return value(); | 560 return value(); |
| 559 #else | 561 #else |
| 560 return WideToUTF8(base::SysNativeMBToWide(value())); | 562 return WideToUTF8(SysNativeMBToWide(value())); |
| 561 #endif | 563 #endif |
| 562 } | 564 } |
| 563 | 565 |
| 564 // The *Hack functions are temporary while we fix the remainder of the code. | 566 // The *Hack functions are temporary while we fix the remainder of the code. |
| 565 // Remember to remove the #includes at the top when you remove these. | 567 // Remember to remove the #includes at the top when you remove these. |
| 566 | 568 |
| 567 // static | 569 // static |
| 568 FilePath FilePath::FromWStringHack(const std::wstring& wstring) { | 570 FilePath FilePath::FromWStringHack(const std::wstring& wstring) { |
| 569 return FilePath(base::SysWideToNativeMB(wstring)); | 571 return FilePath(SysWideToNativeMB(wstring)); |
| 570 } | 572 } |
| 571 | 573 |
| 572 // static | 574 // static |
| 573 FilePath FilePath::FromUTF8Unsafe(const std::string& utf8) { | 575 FilePath FilePath::FromUTF8Unsafe(const std::string& utf8) { |
| 574 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) | 576 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 575 return FilePath(utf8); | 577 return FilePath(utf8); |
| 576 #else | 578 #else |
| 577 return FilePath(base::SysWideToNativeMB(UTF8ToWide(utf8))); | 579 return FilePath(SysWideToNativeMB(UTF8ToWide(utf8))); |
| 578 #endif | 580 #endif |
| 579 } | 581 } |
| 580 | 582 |
| 581 #elif defined(OS_WIN) | 583 #elif defined(OS_WIN) |
| 582 string16 FilePath::LossyDisplayName() const { | 584 string16 FilePath::LossyDisplayName() const { |
| 583 return path_; | 585 return path_; |
| 584 } | 586 } |
| 585 | 587 |
| 586 std::string FilePath::MaybeAsASCII() const { | 588 std::string FilePath::MaybeAsASCII() const { |
| 587 if (IsStringASCII(path_)) | 589 if (IsStringASCII(path_)) |
| (...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 return (codepoint1 < codepoint2) ? -1 : 1; | 1124 return (codepoint1 < codepoint2) ? -1 : 1; |
| 1123 if (codepoint1 == 0) { | 1125 if (codepoint1 == 0) { |
| 1124 DCHECK_EQ(index1, length1); | 1126 DCHECK_EQ(index1, length1); |
| 1125 DCHECK_EQ(index2, length2); | 1127 DCHECK_EQ(index2, length2); |
| 1126 return 0; | 1128 return 0; |
| 1127 } | 1129 } |
| 1128 } | 1130 } |
| 1129 } | 1131 } |
| 1130 | 1132 |
| 1131 StringType FilePath::GetHFSDecomposedForm(const StringType& string) { | 1133 StringType FilePath::GetHFSDecomposedForm(const StringType& string) { |
| 1132 base::mac::ScopedCFTypeRef<CFStringRef> cfstring( | 1134 mac::ScopedCFTypeRef<CFStringRef> cfstring( |
| 1133 CFStringCreateWithBytesNoCopy( | 1135 CFStringCreateWithBytesNoCopy( |
| 1134 NULL, | 1136 NULL, |
| 1135 reinterpret_cast<const UInt8*>(string.c_str()), | 1137 reinterpret_cast<const UInt8*>(string.c_str()), |
| 1136 string.length(), | 1138 string.length(), |
| 1137 kCFStringEncodingUTF8, | 1139 kCFStringEncodingUTF8, |
| 1138 false, | 1140 false, |
| 1139 kCFAllocatorNull)); | 1141 kCFAllocatorNull)); |
| 1140 // Query the maximum length needed to store the result. In most cases this | 1142 // Query the maximum length needed to store the result. In most cases this |
| 1141 // will overestimate the required space. The return value also already | 1143 // will overestimate the required space. The return value also already |
| 1142 // includes the space needed for a terminating 0. | 1144 // includes the space needed for a terminating 0. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1169 return string2.empty() ? 0 : -1; | 1171 return string2.empty() ? 0 : -1; |
| 1170 if (string2.empty()) | 1172 if (string2.empty()) |
| 1171 return 1; | 1173 return 1; |
| 1172 | 1174 |
| 1173 StringType hfs1 = GetHFSDecomposedForm(string1); | 1175 StringType hfs1 = GetHFSDecomposedForm(string1); |
| 1174 StringType hfs2 = GetHFSDecomposedForm(string2); | 1176 StringType hfs2 = GetHFSDecomposedForm(string2); |
| 1175 | 1177 |
| 1176 // GetHFSDecomposedForm() returns an empty string in an error case. | 1178 // GetHFSDecomposedForm() returns an empty string in an error case. |
| 1177 if (hfs1.empty() || hfs2.empty()) { | 1179 if (hfs1.empty() || hfs2.empty()) { |
| 1178 NOTREACHED(); | 1180 NOTREACHED(); |
| 1179 base::mac::ScopedCFTypeRef<CFStringRef> cfstring1( | 1181 mac::ScopedCFTypeRef<CFStringRef> cfstring1( |
| 1180 CFStringCreateWithBytesNoCopy( | 1182 CFStringCreateWithBytesNoCopy( |
| 1181 NULL, | 1183 NULL, |
| 1182 reinterpret_cast<const UInt8*>(string1.c_str()), | 1184 reinterpret_cast<const UInt8*>(string1.c_str()), |
| 1183 string1.length(), | 1185 string1.length(), |
| 1184 kCFStringEncodingUTF8, | 1186 kCFStringEncodingUTF8, |
| 1185 false, | 1187 false, |
| 1186 kCFAllocatorNull)); | 1188 kCFAllocatorNull)); |
| 1187 base::mac::ScopedCFTypeRef<CFStringRef> cfstring2( | 1189 mac::ScopedCFTypeRef<CFStringRef> cfstring2( |
| 1188 CFStringCreateWithBytesNoCopy( | 1190 CFStringCreateWithBytesNoCopy( |
| 1189 NULL, | 1191 NULL, |
| 1190 reinterpret_cast<const UInt8*>(string2.c_str()), | 1192 reinterpret_cast<const UInt8*>(string2.c_str()), |
| 1191 string2.length(), | 1193 string2.length(), |
| 1192 kCFStringEncodingUTF8, | 1194 kCFStringEncodingUTF8, |
| 1193 false, | 1195 false, |
| 1194 kCFAllocatorNull)); | 1196 kCFAllocatorNull)); |
| 1195 return CFStringCompare(cfstring1, | 1197 return CFStringCompare(cfstring1, |
| 1196 cfstring2, | 1198 cfstring2, |
| 1197 kCFCompareCaseInsensitive); | 1199 kCFCompareCaseInsensitive); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1244 StringType copy = path_; | 1246 StringType copy = path_; |
| 1245 for (size_t i = 1; i < arraysize(kSeparators); ++i) { | 1247 for (size_t i = 1; i < arraysize(kSeparators); ++i) { |
| 1246 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); | 1248 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); |
| 1247 } | 1249 } |
| 1248 return FilePath(copy); | 1250 return FilePath(copy); |
| 1249 #else | 1251 #else |
| 1250 return *this; | 1252 return *this; |
| 1251 #endif | 1253 #endif |
| 1252 } | 1254 } |
| 1253 | 1255 |
| 1254 void PrintTo(const FilePath& path, std::ostream* out) { | 1256 } // namespace base |
| 1257 |
| 1258 void PrintTo(const base::FilePath& path, std::ostream* out) { |
| 1255 *out << path.value(); | 1259 *out << path.value(); |
| 1256 } | 1260 } |
| OLD | NEW |