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