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" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 DCHECK(!(illegal->DisallowedLeadingOrTrailing(replace_char))); | 108 DCHECK(!(illegal->DisallowedLeadingOrTrailing(replace_char))); |
109 | 109 |
110 int cursor = 0; // The ICU macros expect an int. | 110 int cursor = 0; // The ICU macros expect an int. |
111 while (cursor < static_cast<int>(file_name->size())) { | 111 while (cursor < static_cast<int>(file_name->size())) { |
112 int char_begin = cursor; | 112 int char_begin = cursor; |
113 uint32 code_point; | 113 uint32 code_point; |
114 #if defined(OS_MACOSX) | 114 #if defined(OS_MACOSX) |
115 // Mac uses UTF-8 encoding for filenames. | 115 // Mac uses UTF-8 encoding for filenames. |
116 U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()), | 116 U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()), |
117 code_point); | 117 code_point); |
118 #elif defined(OS_WIN) | |
119 // Windows uses UTF-16 encoding for filenames. | |
120 U16_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()), | |
121 code_point); | |
122 #elif defined(OS_POSIX) | 118 #elif defined(OS_POSIX) |
123 // Linux doesn't actually define an encoding. It basically allows anything | 119 // Linux doesn't actually define an encoding. It basically allows anything |
124 // except for a few special ASCII characters. | 120 // except for a few special ASCII characters. |
125 unsigned char cur_char = static_cast<unsigned char>((*file_name)[cursor++]); | 121 unsigned char cur_char = static_cast<unsigned char>((*file_name)[cursor++]); |
126 if (cur_char >= 0x80) | 122 if (cur_char >= 0x80) |
127 continue; | 123 continue; |
128 code_point = cur_char; | 124 code_point = cur_char; |
129 #else | 125 #else |
130 NOTREACHED(); | 126 NOTREACHED(); |
131 #endif | 127 #endif |
(...skipping 12 matching lines...) Expand all Loading... |
144 | 140 |
145 bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) { | 141 bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) { |
146 UErrorCode error_code = U_ZERO_ERROR; | 142 UErrorCode error_code = U_ZERO_ERROR; |
147 // Use the default collator. The default locale should have been properly | 143 // Use the default collator. The default locale should have been properly |
148 // set by the time this constructor is called. | 144 // set by the time this constructor is called. |
149 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error_code)); | 145 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error_code)); |
150 DCHECK(U_SUCCESS(error_code)); | 146 DCHECK(U_SUCCESS(error_code)); |
151 // Make it case-sensitive. | 147 // Make it case-sensitive. |
152 collator->setStrength(icu::Collator::TERTIARY); | 148 collator->setStrength(icu::Collator::TERTIARY); |
153 | 149 |
154 #if defined(OS_WIN) | 150 #if defined(OS_POSIX) |
155 return CompareString16WithCollator(*collator, WideToUTF16(a.value()), | |
156 WideToUTF16(b.value())) == UCOL_LESS; | |
157 | |
158 #elif defined(OS_POSIX) | |
159 // On linux, the file system encoding is not defined. We assume | 151 // On linux, the file system encoding is not defined. We assume |
160 // SysNativeMBToWide takes care of it. | 152 // SysNativeMBToWide takes care of it. |
161 return CompareString16WithCollator( | 153 return CompareString16WithCollator( |
162 *collator, WideToUTF16(SysNativeMBToWide(a.value().c_str())), | 154 *collator, WideToUTF16(SysNativeMBToWide(a.value().c_str())), |
163 WideToUTF16(SysNativeMBToWide(b.value().c_str()))) == UCOL_LESS; | 155 WideToUTF16(SysNativeMBToWide(b.value().c_str()))) == UCOL_LESS; |
164 #else | 156 #else |
165 #error Not implemented on your system | 157 #error Not implemented on your system |
166 #endif | 158 #endif |
167 } | 159 } |
168 | 160 |
169 void NormalizeFileNameEncoding(FilePath* file_name) { | 161 void NormalizeFileNameEncoding(FilePath* file_name) { |
170 #if defined(OS_CHROMEOS) | 162 #if defined(OS_CHROMEOS) |
171 std::string normalized_str; | 163 std::string normalized_str; |
172 if (ConvertToUtf8AndNormalize(file_name->BaseName().value(), | 164 if (ConvertToUtf8AndNormalize(file_name->BaseName().value(), |
173 kCodepageUTF8, | 165 kCodepageUTF8, |
174 &normalized_str)) { | 166 &normalized_str)) { |
175 *file_name = file_name->DirName().Append(FilePath(normalized_str)); | 167 *file_name = file_name->DirName().Append(FilePath(normalized_str)); |
176 } | 168 } |
177 #endif | 169 #endif |
178 } | 170 } |
179 | 171 |
180 } // namespace i18n | 172 } // namespace i18n |
181 } // namespace base | 173 } // namespace base |
OLD | NEW |