| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 public: | 23 public: |
| 24 bool contains(UChar32 ucs4) { | 24 bool contains(UChar32 ucs4) { |
| 25 return !!set->contains(ucs4); | 25 return !!set->contains(ucs4); |
| 26 } | 26 } |
| 27 | 27 |
| 28 bool containsNone(const string16 &s) { | 28 bool containsNone(const string16 &s) { |
| 29 return !!set->containsNone(icu::UnicodeString(s.c_str(), s.size())); | 29 return !!set->containsNone(icu::UnicodeString(s.c_str(), s.size())); |
| 30 } | 30 } |
| 31 | 31 |
| 32 private: | 32 private: |
| 33 friend class Singleton<IllegalCharacters>; | |
| 34 friend struct DefaultSingletonTraits<IllegalCharacters>; | 33 friend struct DefaultSingletonTraits<IllegalCharacters>; |
| 35 | 34 |
| 36 IllegalCharacters(); | 35 IllegalCharacters(); |
| 37 ~IllegalCharacters() { } | 36 ~IllegalCharacters() { } |
| 38 | 37 |
| 39 scoped_ptr<icu::UnicodeSet> set; | 38 scoped_ptr<icu::UnicodeSet> set; |
| 40 | 39 |
| 41 DISALLOW_COPY_AND_ASSIGN(IllegalCharacters); | 40 DISALLOW_COPY_AND_ASSIGN(IllegalCharacters); |
| 42 }; | 41 }; |
| 43 | 42 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 friend struct DefaultSingletonTraits<LocaleAwareComparator>; | 115 friend struct DefaultSingletonTraits<LocaleAwareComparator>; |
| 117 | 116 |
| 118 DISALLOW_COPY_AND_ASSIGN(LocaleAwareComparator); | 117 DISALLOW_COPY_AND_ASSIGN(LocaleAwareComparator); |
| 119 }; | 118 }; |
| 120 | 119 |
| 121 } // namespace | 120 } // namespace |
| 122 | 121 |
| 123 namespace file_util { | 122 namespace file_util { |
| 124 | 123 |
| 125 bool IsFilenameLegal(const string16& file_name) { | 124 bool IsFilenameLegal(const string16& file_name) { |
| 126 return Singleton<IllegalCharacters>()->containsNone(file_name); | 125 return LeakySingleton<IllegalCharacters>()->containsNone(file_name); |
| 127 } | 126 } |
| 128 | 127 |
| 129 void ReplaceIllegalCharactersInPath(FilePath::StringType* file_name, | 128 void ReplaceIllegalCharactersInPath(FilePath::StringType* file_name, |
| 130 char replace_char) { | 129 char replace_char) { |
| 131 DCHECK(file_name); | 130 DCHECK(file_name); |
| 132 | 131 |
| 133 DCHECK(!(Singleton<IllegalCharacters>()->contains(replace_char))); | 132 DCHECK(!(LeakySingleton<IllegalCharacters>()->contains(replace_char))); |
| 134 | 133 |
| 135 // Remove leading and trailing whitespace. | 134 // Remove leading and trailing whitespace. |
| 136 TrimWhitespace(*file_name, TRIM_ALL, file_name); | 135 TrimWhitespace(*file_name, TRIM_ALL, file_name); |
| 137 | 136 |
| 138 IllegalCharacters* illegal = Singleton<IllegalCharacters>::get(); | 137 IllegalCharacters* illegal = LeakySingleton<IllegalCharacters>::get(); |
| 139 int cursor = 0; // The ICU macros expect an int. | 138 int cursor = 0; // The ICU macros expect an int. |
| 140 while (cursor < static_cast<int>(file_name->size())) { | 139 while (cursor < static_cast<int>(file_name->size())) { |
| 141 int char_begin = cursor; | 140 int char_begin = cursor; |
| 142 uint32 code_point; | 141 uint32 code_point; |
| 143 #if defined(OS_MACOSX) | 142 #if defined(OS_MACOSX) |
| 144 // Mac uses UTF-8 encoding for filenames. | 143 // Mac uses UTF-8 encoding for filenames. |
| 145 U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()), | 144 U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()), |
| 146 code_point); | 145 code_point); |
| 147 #elif defined(OS_WIN) | 146 #elif defined(OS_WIN) |
| 148 // Windows uses UTF-16 encoding for filenames. | 147 // Windows uses UTF-16 encoding for filenames. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 164 // We just made the potentially multi-byte/word char into one that only | 163 // We just made the potentially multi-byte/word char into one that only |
| 165 // takes one byte/word, so need to adjust the cursor to point to the next | 164 // takes one byte/word, so need to adjust the cursor to point to the next |
| 166 // character again. | 165 // character again. |
| 167 cursor = char_begin + 1; | 166 cursor = char_begin + 1; |
| 168 } | 167 } |
| 169 } | 168 } |
| 170 } | 169 } |
| 171 | 170 |
| 172 bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) { | 171 bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) { |
| 173 #if defined(OS_WIN) | 172 #if defined(OS_WIN) |
| 174 return Singleton<LocaleAwareComparator>()->Compare(a.value().c_str(), | 173 return LeakySingleton<LocaleAwareComparator>()->Compare( |
| 175 b.value().c_str()) < 0; | 174 a.value().c_str(), b.value().c_str()) < 0; |
| 176 | 175 |
| 177 #elif defined(OS_POSIX) | 176 #elif defined(OS_POSIX) |
| 178 // On linux, the file system encoding is not defined. We assume | 177 // On linux, the file system encoding is not defined. We assume |
| 179 // SysNativeMBToWide takes care of it. | 178 // SysNativeMBToWide takes care of it. |
| 180 // | 179 // |
| 181 // ICU's collator can take strings in OS native encoding. But we convert the | 180 // ICU's collator can take strings in OS native encoding. But we convert the |
| 182 // strings to UTF-16 ourselves to ensure conversion consistency. | 181 // strings to UTF-16 ourselves to ensure conversion consistency. |
| 183 // TODO(yuzo): Perhaps we should define SysNativeMBToUTF16? | 182 // TODO(yuzo): Perhaps we should define SysNativeMBToUTF16? |
| 184 return Singleton<LocaleAwareComparator>()->Compare( | 183 return LeakySingleton<LocaleAwareComparator>()->Compare( |
| 185 WideToUTF16(base::SysNativeMBToWide(a.value().c_str())), | 184 WideToUTF16(base::SysNativeMBToWide(a.value().c_str())), |
| 186 WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))) < 0; | 185 WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))) < 0; |
| 187 #else | 186 #else |
| 188 #error Not implemented on your system | 187 #error Not implemented on your system |
| 189 #endif | 188 #endif |
| 190 } | 189 } |
| 191 | 190 |
| 192 } // namespace | 191 } // namespace |
| OLD | NEW |