1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_I18N_FILE_UTIL_ICU_H_ 6 #define BASE_I18N_FILE_UTIL_ICU_H_ 7 8 // File utilities that use the ICU library go in this file. 9 10 #include <string> 11 12 #include "base/files/file_path.h" 13 #include "base/i18n/base_i18n_export.h" 14 15 namespace base { 16 namespace i18n { 17 18 // Returns true if file_name does not have any illegal character. The input 19 // param has the same restriction as that for ReplaceIllegalCharacters. 20 BASE_I18N_EXPORT bool IsFilenameLegal(const std::u16string& file_name); 21 22 // Replaces characters in |file_name| that are illegal for file names with 23 // |replace_char|. |file_name| must not be a full or relative path, but just the 24 // file name component (since slashes are considered illegal). Any leading or 25 // trailing whitespace or periods in |file_name| is also replaced with the 26 // |replace_char|, unless |replace_char| itself is a whitespace or period, in 27 // which case they are trimmed. 28 // 29 // Example: 30 // "bad:file*name?.txt" will be turned into "bad_file_name_.txt" when 31 // |replace_char| is '_'. 32 // 33 // If |replace_char| is a whitespace or period and |file_name| contains no legal 34 // characters, the result will be enclosed by '_'s. 35 // If |replace_char| is a whitespace or period and |file_name| contains no legal 36 // characters except a file extension, the result will have '_' prepended. 37 // 38 // Warning: Do not use this function as the sole means of sanitizing a filename. 39 // While the resulting filename itself would be legal, it doesn't necessarily 40 // mean that the file will behave safely. On Windows, certain reserved names 41 // refer to devices rather than files (E.g. LPT1), and some filenames could be 42 // interpreted as shell namespace extensions (E.g. Foo.{<GUID>}). 43 // 44 // On Windows, Chrome OS and Mac, the file system encoding is already known and 45 // parsed as UTF-8 and UTF-16 accordingly. 46 // On Linux, the file name will be parsed as UTF8. 47 // TODO(asanka): Move full filename sanitization logic here. 48 BASE_I18N_EXPORT void ReplaceIllegalCharactersInPath( 49 FilePath::StringType* file_name, 50 char replace_char); 51 52 // Compares two filenames using the current locale information. This can be 53 // used to sort directory listings. It behaves like "operator<" for use in 54 // std::sort. 55 BASE_I18N_EXPORT bool LocaleAwareCompareFilenames(const FilePath& a, 56 const FilePath& b); 57 58 // Calculates the canonical file-system representation of |file_name| base name. 59 // Modifies |file_name| in place. No-op if not on ChromeOS. 60 BASE_I18N_EXPORT void NormalizeFileNameEncoding(FilePath* file_name); 61 62 } // namespace i18n 63 } // namespace base 64 65 #endif // BASE_I18N_FILE_UTIL_ICU_H_ 66