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/files/file_path.h" | 5 #include "base/files/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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/pickle.h" | 12 #include "base/pickle.h" |
13 | 13 |
14 // These includes are just for the *Hack functions, and should be removed | 14 // These includes are just for the *Hack functions, and should be removed |
15 // when those functions are removed. | 15 // when those functions are removed. |
16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 | 20 |
21 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
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_MACOSX) |
27 #include <windows.h> | |
28 #elif defined(OS_MACOSX) | |
29 #include <CoreFoundation/CoreFoundation.h> | 27 #include <CoreFoundation/CoreFoundation.h> |
30 #endif | 28 #endif |
31 | 29 |
32 namespace base { | 30 namespace base { |
33 | 31 |
34 typedef FilePath::StringType StringType; | 32 typedef FilePath::StringType StringType; |
35 | 33 |
36 namespace { | 34 namespace { |
37 | 35 |
38 const char* const kCommonDoubleExtensionSuffixes[] = { "gz", "z", "bz2", "bz" }; | 36 const char* const kCommonDoubleExtensionSuffixes[] = { "gz", "z", "bz2", "bz" }; |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 StringType ext = Extension(); | 412 StringType ext = Extension(); |
415 StringType ret = RemoveExtension().value(); | 413 StringType ret = RemoveExtension().value(); |
416 ret.append(suffix); | 414 ret.append(suffix); |
417 ret.append(ext); | 415 ret.append(ext); |
418 return FilePath(ret); | 416 return FilePath(ret); |
419 } | 417 } |
420 | 418 |
421 FilePath FilePath::InsertBeforeExtensionASCII(const StringPiece& suffix) | 419 FilePath FilePath::InsertBeforeExtensionASCII(const StringPiece& suffix) |
422 const { | 420 const { |
423 DCHECK(IsStringASCII(suffix)); | 421 DCHECK(IsStringASCII(suffix)); |
424 #if defined(OS_WIN) | |
425 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); | |
426 #elif defined(OS_POSIX) | |
427 return InsertBeforeExtension(suffix.as_string()); | 422 return InsertBeforeExtension(suffix.as_string()); |
428 #endif | |
429 } | 423 } |
430 | 424 |
431 FilePath FilePath::AddExtension(const StringType& extension) const { | 425 FilePath FilePath::AddExtension(const StringType& extension) const { |
432 if (IsEmptyOrSpecialCase(BaseName().value())) | 426 if (IsEmptyOrSpecialCase(BaseName().value())) |
433 return FilePath(); | 427 return FilePath(); |
434 | 428 |
435 // If the new extension is "" or ".", then just return the current FilePath. | 429 // If the new extension is "" or ".", then just return the current FilePath. |
436 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) | 430 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) |
437 return *this; | 431 return *this; |
438 | 432 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 new_path.path_.append(*appended); | 509 new_path.path_.append(*appended); |
516 return new_path; | 510 return new_path; |
517 } | 511 } |
518 | 512 |
519 FilePath FilePath::Append(const FilePath& component) const { | 513 FilePath FilePath::Append(const FilePath& component) const { |
520 return Append(component.value()); | 514 return Append(component.value()); |
521 } | 515 } |
522 | 516 |
523 FilePath FilePath::AppendASCII(const StringPiece& component) const { | 517 FilePath FilePath::AppendASCII(const StringPiece& component) const { |
524 DCHECK(base::IsStringASCII(component)); | 518 DCHECK(base::IsStringASCII(component)); |
525 #if defined(OS_WIN) | |
526 return Append(ASCIIToUTF16(component.as_string())); | |
527 #elif defined(OS_POSIX) | |
528 return Append(component.as_string()); | 519 return Append(component.as_string()); |
529 #endif | |
530 } | 520 } |
531 | 521 |
532 bool FilePath::IsAbsolute() const { | 522 bool FilePath::IsAbsolute() const { |
533 return IsPathAbsolute(path_); | 523 return IsPathAbsolute(path_); |
534 } | 524 } |
535 | 525 |
536 bool FilePath::EndsWithSeparator() const { | 526 bool FilePath::EndsWithSeparator() const { |
537 if (empty()) | 527 if (empty()) |
538 return false; | 528 return false; |
539 return IsSeparator(path_[path_.size() - 1]); | 529 return IsSeparator(path_[path_.size() - 1]); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 | 609 |
620 // static | 610 // static |
621 FilePath FilePath::FromUTF16Unsafe(const string16& utf16) { | 611 FilePath FilePath::FromUTF16Unsafe(const string16& utf16) { |
622 #if defined(SYSTEM_NATIVE_UTF8) | 612 #if defined(SYSTEM_NATIVE_UTF8) |
623 return FilePath(UTF16ToUTF8(utf16)); | 613 return FilePath(UTF16ToUTF8(utf16)); |
624 #else | 614 #else |
625 return FilePath(SysWideToNativeMB(UTF16ToWide(utf16))); | 615 return FilePath(SysWideToNativeMB(UTF16ToWide(utf16))); |
626 #endif | 616 #endif |
627 } | 617 } |
628 | 618 |
629 #elif defined(OS_WIN) | |
630 string16 FilePath::LossyDisplayName() const { | |
631 return path_; | |
632 } | |
633 | |
634 std::string FilePath::MaybeAsASCII() const { | |
635 if (base::IsStringASCII(path_)) | |
636 return UTF16ToASCII(path_); | |
637 return std::string(); | |
638 } | |
639 | |
640 std::string FilePath::AsUTF8Unsafe() const { | |
641 return WideToUTF8(value()); | |
642 } | |
643 | |
644 string16 FilePath::AsUTF16Unsafe() const { | |
645 return value(); | |
646 } | |
647 | |
648 // static | |
649 FilePath FilePath::FromUTF8Unsafe(const std::string& utf8) { | |
650 return FilePath(UTF8ToWide(utf8)); | |
651 } | |
652 | |
653 // static | |
654 FilePath FilePath::FromUTF16Unsafe(const string16& utf16) { | |
655 return FilePath(utf16); | |
656 } | |
657 #endif | 619 #endif |
658 | 620 |
659 void FilePath::WriteToPickle(Pickle* pickle) const { | 621 void FilePath::WriteToPickle(Pickle* pickle) const { |
660 #if defined(OS_WIN) | |
661 pickle->WriteString16(path_); | |
662 #else | |
663 pickle->WriteString(path_); | 622 pickle->WriteString(path_); |
664 #endif | |
665 } | 623 } |
666 | 624 |
667 bool FilePath::ReadFromPickle(PickleIterator* iter) { | 625 bool FilePath::ReadFromPickle(PickleIterator* iter) { |
668 #if defined(OS_WIN) | |
669 if (!iter->ReadString16(&path_)) | |
670 return false; | |
671 #else | |
672 if (!iter->ReadString(&path_)) | 626 if (!iter->ReadString(&path_)) |
673 return false; | 627 return false; |
674 #endif | |
675 | 628 |
676 if (path_.find(kStringTerminator) != StringType::npos) | 629 if (path_.find(kStringTerminator) != StringType::npos) |
677 return false; | 630 return false; |
678 | 631 |
679 return true; | 632 return true; |
680 } | 633 } |
681 | 634 |
682 #if defined(OS_WIN) | 635 #if defined(OS_MACOSX) |
683 // Windows specific implementation of file string comparisons | |
684 | |
685 int FilePath::CompareIgnoreCase(const StringType& string1, | |
686 const StringType& string2) { | |
687 // Perform character-wise upper case comparison rather than using the | |
688 // fully Unicode-aware CompareString(). For details see: | |
689 // http://blogs.msdn.com/michkap/archive/2005/10/17/481600.aspx | |
690 StringType::const_iterator i1 = string1.begin(); | |
691 StringType::const_iterator i2 = string2.begin(); | |
692 StringType::const_iterator string1end = string1.end(); | |
693 StringType::const_iterator string2end = string2.end(); | |
694 for ( ; i1 != string1end && i2 != string2end; ++i1, ++i2) { | |
695 wchar_t c1 = | |
696 (wchar_t)LOWORD(::CharUpperW((LPWSTR)(DWORD_PTR)MAKELONG(*i1, 0))); | |
697 wchar_t c2 = | |
698 (wchar_t)LOWORD(::CharUpperW((LPWSTR)(DWORD_PTR)MAKELONG(*i2, 0))); | |
699 if (c1 < c2) | |
700 return -1; | |
701 if (c1 > c2) | |
702 return 1; | |
703 } | |
704 if (i1 != string1end) | |
705 return 1; | |
706 if (i2 != string2end) | |
707 return -1; | |
708 return 0; | |
709 } | |
710 | |
711 #elif defined(OS_MACOSX) | |
712 // Mac OS X specific implementation of file string comparisons | 636 // Mac OS X specific implementation of file string comparisons |
713 | 637 |
714 // cf. http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSu
btleties | 638 // cf. http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSu
btleties |
715 // | 639 // |
716 // "When using CreateTextEncoding to create a text encoding, you should set | 640 // "When using CreateTextEncoding to create a text encoding, you should set |
717 // the TextEncodingBase to kTextEncodingUnicodeV2_0, set the | 641 // the TextEncodingBase to kTextEncodingUnicodeV2_0, set the |
718 // TextEncodingVariant to kUnicodeCanonicalDecompVariant, and set the | 642 // TextEncodingVariant to kUnicodeCanonicalDecompVariant, and set the |
719 // TextEncodingFormat to kUnicode16BitFormat. Using these values ensures that | 643 // TextEncodingFormat to kUnicode16BitFormat. Using these values ensures that |
720 // the Unicode will be in the same form as on an HFS Plus volume, even as the | 644 // the Unicode will be in the same form as on an HFS Plus volume, even as the |
721 // Unicode standard evolves." | 645 // Unicode standard evolves." |
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1311 #endif | 1235 #endif |
1312 } | 1236 } |
1313 | 1237 |
1314 #if defined(OS_ANDROID) | 1238 #if defined(OS_ANDROID) |
1315 bool FilePath::IsContentUri() const { | 1239 bool FilePath::IsContentUri() const { |
1316 return StartsWithASCII(path_, "content://", false /*case_sensitive*/); | 1240 return StartsWithASCII(path_, "content://", false /*case_sensitive*/); |
1317 } | 1241 } |
1318 #endif | 1242 #endif |
1319 | 1243 |
1320 } // namespace base | 1244 } // namespace base |
OLD | NEW |