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