OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // FilePath is a container for pathnames stored in a platform's native string | 5 // FilePath is a container for pathnames stored in a platform's native string |
6 // type, providing containers for manipulation in according with the | 6 // type, providing containers for manipulation in according with the |
7 // platform's conventions for pathnames. It supports the following path | 7 // platform's conventions for pathnames. It supports the following path |
8 // types: | 8 // types: |
9 // | 9 // |
10 // POSIX Windows | 10 // POSIX Windows |
11 // --------------- ---------------------------------- | 11 // --------------- ---------------------------------- |
12 // Fundamental type char[] wchar_t[] | 12 // Fundamental type char[] wchar_t[] |
13 // Encoding unspecified* UTF-16 | 13 // Encoding unspecified* UTF-16 |
14 // Separator / \, tolerant of / | 14 // Separator / \, tolerant of / |
15 // Drive letters no case-insensitive A-Z followed by : | 15 // Drive letters no case-insensitive A-Z followed by : |
16 // Alternate root // (surprise!) \\, for UNC paths | 16 // Alternate root // (surprise!) \\, for UNC paths |
17 // | 17 // |
18 // * The encoding need not be specified on POSIX systems, although some | 18 // * The encoding need not be specified on POSIX systems, although some |
19 // POSIX-compliant systems do specify an encoding. Mac OS X uses UTF-8. | 19 // POSIX-compliant systems do specify an encoding. Mac OS X uses UTF-8. |
20 // Chrome OS also uses UTF-8. | |
20 // Linux does not specify an encoding, but in practice, the locale's | 21 // Linux does not specify an encoding, but in practice, the locale's |
21 // character set may be used. | 22 // character set may be used. |
22 // | 23 // |
23 // For more arcane bits of path trivia, see below. | 24 // For more arcane bits of path trivia, see below. |
24 // | 25 // |
25 // FilePath objects are intended to be used anywhere paths are. An | 26 // FilePath objects are intended to be used anywhere paths are. An |
26 // application may pass FilePath objects around internally, masking the | 27 // application may pass FilePath objects around internally, masking the |
27 // underlying differences between systems, only differing in implementation | 28 // underlying differences between systems, only differing in implementation |
28 // where interfacing directly with the system. For example, a single | 29 // where interfacing directly with the system. For example, a single |
29 // OpenFile(const FilePath &) function may be made available, allowing all | 30 // OpenFile(const FilePath &) function may be made available, allowing all |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 // Warning: you can *not*, in general, go from a display name back to a real | 288 // Warning: you can *not*, in general, go from a display name back to a real |
288 // path. Only use this when displaying paths to users, not just when you | 289 // path. Only use this when displaying paths to users, not just when you |
289 // want to stuff a string16 into some other API. | 290 // want to stuff a string16 into some other API. |
290 string16 LossyDisplayName() const; | 291 string16 LossyDisplayName() const; |
291 | 292 |
292 // Return the path as ASCII, or the empty string if the path is not ASCII. | 293 // Return the path as ASCII, or the empty string if the path is not ASCII. |
293 // This should only be used for cases where the FilePath is representing a | 294 // This should only be used for cases where the FilePath is representing a |
294 // known-ASCII filename. | 295 // known-ASCII filename. |
295 std::string MaybeAsASCII() const; | 296 std::string MaybeAsASCII() const; |
296 | 297 |
298 // Return the path as UTF-8. | |
299 // | |
300 // This function is *unsafe* as we cannot tell what encoding is used in | |
Mark Mentovai
2011/11/01 21:30:46
Please avoid using “we” in comments. Line 307 and
satorux1
2011/11/01 21:49:55
Done.
| |
301 // file names on POSIX systems other than Mac and Chrome OS, although | |
302 // UTF-8 is practically used everywhere these days. To mitigate the | |
303 // encoding issue, this function internally calls SysNativeMBToWide() on | |
304 // POSIX per assumption that the current locale's encoding is used in | |
Mark Mentovai
2011/11/01 21:30:46
SysNativeMBToWide is unnecessary (and in some case
satorux1
2011/11/01 21:49:55
Done.
| |
305 // file names, but this isn't a perfect solution. | |
306 // | |
307 // Once we are confident enough to stop caring about non-UTF-8 file | |
308 // names, we'll get rid of the SysNativeMBToWide() hack and and remove | |
309 // "Unsafe" from the function name. | |
310 std::string AsUTF8Unsafe() const; | |
311 | |
297 // Older Chromium code assumes that paths are always wstrings. | 312 // Older Chromium code assumes that paths are always wstrings. |
298 // This function converts wstrings to FilePaths, and is | 313 // This function converts wstrings to FilePaths, and is |
299 // useful to smooth porting that old code to the FilePath API. | 314 // useful to smooth porting that old code to the FilePath API. |
300 // It has "Hack" its name so people feel bad about using it. | 315 // It has "Hack" its name so people feel bad about using it. |
301 // http://code.google.com/p/chromium/issues/detail?id=24672 | 316 // http://code.google.com/p/chromium/issues/detail?id=24672 |
302 // | 317 // |
303 // If you are trying to be a good citizen and remove these, ask yourself: | 318 // If you are trying to be a good citizen and remove these, ask yourself: |
304 // - Am I interacting with other Chrome code that deals with files? Then | 319 // - Am I interacting with other Chrome code that deals with files? Then |
305 // try to convert the API into using FilePath. | 320 // try to convert the API into using FilePath. |
306 // - Am I interacting with OS-native calls? Then use value() to get at an | 321 // - Am I interacting with OS-native calls? Then use value() to get at an |
307 // OS-native string format. | 322 // OS-native string format. |
308 // - Am I using well-known file names, like "config.ini"? Then use the | 323 // - Am I using well-known file names, like "config.ini"? Then use the |
309 // ASCII functions (we require paths to always be supersets of ASCII). | 324 // ASCII functions (we require paths to always be supersets of ASCII). |
310 // - Am I displaying a string to the user in some UI? Then use the | 325 // - Am I displaying a string to the user in some UI? Then use the |
311 // LossyDisplayName() function, but keep in mind that you can't | 326 // LossyDisplayName() function, but keep in mind that you can't |
312 // ever use the result of that again as a path. | 327 // ever use the result of that again as a path. |
313 static FilePath FromWStringHack(const std::wstring& wstring); | 328 static FilePath FromWStringHack(const std::wstring& wstring); |
314 | 329 |
330 // Returns a FilePath object from a path name in UTF-8. This function | |
331 // should only be used for cases where you are sure that the input | |
332 // string is UTF-8. | |
333 // | |
334 // Like AsUTF8Unsafe(), this function is unsafe. This function | |
335 // internally calls SysWideToNativeMB() on POSIX to mitigate the | |
336 // encoding issue. See the comment at AsUTF8Unsafe() for details. | |
337 static FilePath FromUTF8Unsafe(const std::string& utf8); | |
338 | |
315 void WriteToPickle(Pickle* pickle); | 339 void WriteToPickle(Pickle* pickle); |
316 bool ReadFromPickle(Pickle* pickle, void** iter); | 340 bool ReadFromPickle(Pickle* pickle, void** iter); |
317 | 341 |
318 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 342 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
319 // Normalize all path separators to backslash. | 343 // Normalize all path separators to backslash. |
320 FilePath NormalizeWindowsPathSeparators() const; | 344 FilePath NormalizeWindowsPathSeparators() const; |
321 #endif | 345 #endif |
322 | 346 |
323 // Compare two strings in the same way the file system does. | 347 // Compare two strings in the same way the file system does. |
324 // Note that these always ignore case, even on file systems that are case- | 348 // Note that these always ignore case, even on file systems that are case- |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 | 418 |
395 inline size_t hash_value(const FilePath& f) { | 419 inline size_t hash_value(const FilePath& f) { |
396 return hash_value(f.value()); | 420 return hash_value(f.value()); |
397 } | 421 } |
398 | 422 |
399 #endif // COMPILER | 423 #endif // COMPILER |
400 | 424 |
401 } // namespace BASE_HASH_NAMESPACE | 425 } // namespace BASE_HASH_NAMESPACE |
402 | 426 |
403 #endif // BASE_FILE_PATH_H_ | 427 #endif // BASE_FILE_PATH_H_ |
OLD | NEW |