| 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 // File utilities that use the ICU library go in this file. | 5 // File utilities that use the ICU library go in this file. |
| 6 | 6 |
| 7 #include "base/i18n/file_util_icu.h" | 7 #include "base/i18n/file_util_icu.h" |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
| 11 #include "base/i18n/string_compare.h" | 11 #include "base/i18n/string_compare.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "third_party/icu/source/common/unicode/uniset.h" | 19 #include "third_party/icu/source/common/unicode/uniset.h" |
| 20 #include "third_party/icu/source/i18n/unicode/coll.h" | 20 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 21 | 21 |
| 22 using base::string16; | 22 namespace base { |
| 23 namespace i18n { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 class IllegalCharacters { | 27 class IllegalCharacters { |
| 27 public: | 28 public: |
| 28 static IllegalCharacters* GetInstance() { | 29 static IllegalCharacters* GetInstance() { |
| 29 return Singleton<IllegalCharacters>::get(); | 30 return Singleton<IllegalCharacters>::get(); |
| 30 } | 31 } |
| 31 | 32 |
| 32 bool contains(UChar32 ucs4) { | 33 bool contains(UChar32 ucs4) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 set->add(0xFDD0, 0xFDEF); | 78 set->add(0xFDD0, 0xFDEF); |
| 78 for (int i = 0; i <= 0x10; ++i) { | 79 for (int i = 0; i <= 0x10; ++i) { |
| 79 int plane_base = 0x10000 * i; | 80 int plane_base = 0x10000 * i; |
| 80 set->add(plane_base + 0xFFFE, plane_base + 0xFFFF); | 81 set->add(plane_base + 0xFFFE, plane_base + 0xFFFF); |
| 81 } | 82 } |
| 82 set->freeze(); | 83 set->freeze(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 } // namespace | 86 } // namespace |
| 86 | 87 |
| 87 namespace file_util { | |
| 88 | |
| 89 bool IsFilenameLegal(const string16& file_name) { | 88 bool IsFilenameLegal(const string16& file_name) { |
| 90 return IllegalCharacters::GetInstance()->containsNone(file_name); | 89 return IllegalCharacters::GetInstance()->containsNone(file_name); |
| 91 } | 90 } |
| 92 | 91 |
| 93 void ReplaceIllegalCharactersInPath(base::FilePath::StringType* file_name, | 92 void ReplaceIllegalCharactersInPath(FilePath::StringType* file_name, |
| 94 char replace_char) { | 93 char replace_char) { |
| 95 DCHECK(file_name); | 94 DCHECK(file_name); |
| 96 | 95 |
| 97 DCHECK(!(IllegalCharacters::GetInstance()->contains(replace_char))); | 96 DCHECK(!(IllegalCharacters::GetInstance()->contains(replace_char))); |
| 98 | 97 |
| 99 // Remove leading and trailing whitespace. | 98 // Remove leading and trailing whitespace. |
| 100 base::TrimWhitespace(*file_name, base::TRIM_ALL, file_name); | 99 TrimWhitespace(*file_name, TRIM_ALL, file_name); |
| 101 | 100 |
| 102 IllegalCharacters* illegal = IllegalCharacters::GetInstance(); | 101 IllegalCharacters* illegal = IllegalCharacters::GetInstance(); |
| 103 int cursor = 0; // The ICU macros expect an int. | 102 int cursor = 0; // The ICU macros expect an int. |
| 104 while (cursor < static_cast<int>(file_name->size())) { | 103 while (cursor < static_cast<int>(file_name->size())) { |
| 105 int char_begin = cursor; | 104 int char_begin = cursor; |
| 106 uint32 code_point; | 105 uint32 code_point; |
| 107 #if defined(OS_MACOSX) | 106 #if defined(OS_MACOSX) |
| 108 // Mac uses UTF-8 encoding for filenames. | 107 // Mac uses UTF-8 encoding for filenames. |
| 109 U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()), | 108 U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()), |
| 110 code_point); | 109 code_point); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 126 if (illegal->contains(code_point)) { | 125 if (illegal->contains(code_point)) { |
| 127 file_name->replace(char_begin, cursor - char_begin, 1, replace_char); | 126 file_name->replace(char_begin, cursor - char_begin, 1, replace_char); |
| 128 // We just made the potentially multi-byte/word char into one that only | 127 // We just made the potentially multi-byte/word char into one that only |
| 129 // takes one byte/word, so need to adjust the cursor to point to the next | 128 // takes one byte/word, so need to adjust the cursor to point to the next |
| 130 // character again. | 129 // character again. |
| 131 cursor = char_begin + 1; | 130 cursor = char_begin + 1; |
| 132 } | 131 } |
| 133 } | 132 } |
| 134 } | 133 } |
| 135 | 134 |
| 136 bool LocaleAwareCompareFilenames(const base::FilePath& a, | 135 bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) { |
| 137 const base::FilePath& b) { | |
| 138 UErrorCode error_code = U_ZERO_ERROR; | 136 UErrorCode error_code = U_ZERO_ERROR; |
| 139 // Use the default collator. The default locale should have been properly | 137 // Use the default collator. The default locale should have been properly |
| 140 // set by the time this constructor is called. | 138 // set by the time this constructor is called. |
| 141 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error_code)); | 139 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error_code)); |
| 142 DCHECK(U_SUCCESS(error_code)); | 140 DCHECK(U_SUCCESS(error_code)); |
| 143 // Make it case-sensitive. | 141 // Make it case-sensitive. |
| 144 collator->setStrength(icu::Collator::TERTIARY); | 142 collator->setStrength(icu::Collator::TERTIARY); |
| 145 | 143 |
| 146 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
| 147 return base::i18n::CompareString16WithCollator(collator.get(), | 145 return CompareString16WithCollator(collator.get(), |
| 148 base::WideToUTF16(a.value()), base::WideToUTF16(b.value())) == UCOL_LESS; | 146 WideToUTF16(a.value()), WideToUTF16(b.value())) == UCOL_LESS; |
| 149 | 147 |
| 150 #elif defined(OS_POSIX) | 148 #elif defined(OS_POSIX) |
| 151 // On linux, the file system encoding is not defined. We assume | 149 // On linux, the file system encoding is not defined. We assume |
| 152 // SysNativeMBToWide takes care of it. | 150 // SysNativeMBToWide takes care of it. |
| 153 return base::i18n::CompareString16WithCollator( | 151 return CompareString16WithCollator( |
| 154 collator.get(), | 152 collator.get(), |
| 155 base::WideToUTF16(base::SysNativeMBToWide(a.value().c_str())), | 153 WideToUTF16(SysNativeMBToWide(a.value().c_str())), |
| 156 base::WideToUTF16(base::SysNativeMBToWide(b.value().c_str())) | 154 WideToUTF16(SysNativeMBToWide(b.value().c_str()))) == UCOL_LESS; |
| 157 ) == UCOL_LESS; | |
| 158 #else | 155 #else |
| 159 #error Not implemented on your system | 156 #error Not implemented on your system |
| 160 #endif | 157 #endif |
| 161 } | 158 } |
| 162 | 159 |
| 163 void NormalizeFileNameEncoding(base::FilePath* file_name) { | 160 void NormalizeFileNameEncoding(FilePath* file_name) { |
| 164 #if defined(OS_CHROMEOS) | 161 #if defined(OS_CHROMEOS) |
| 165 std::string normalized_str; | 162 std::string normalized_str; |
| 166 if (base::ConvertToUtf8AndNormalize(file_name->BaseName().value(), | 163 if (ConvertToUtf8AndNormalize(file_name->BaseName().value(), |
| 167 base::kCodepageUTF8, | 164 kCodepageUTF8, |
| 168 &normalized_str)) { | 165 &normalized_str)) { |
| 169 *file_name = file_name->DirName().Append(base::FilePath(normalized_str)); | 166 *file_name = file_name->DirName().Append(FilePath(normalized_str)); |
| 170 } | 167 } |
| 171 #endif | 168 #endif |
| 172 } | 169 } |
| 173 | 170 |
| 174 } // namespace | 171 } // namespace i18n |
| 172 } // namespace base |
| OLD | NEW |