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 |
Mark Mentovai
2011/10/26 23:29:13
*
| |
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 |
Mark Mentovai
2011/10/26 23:29:13
*
| |
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 // Linux does not specify an encoding, but in practice, the locale's | 20 // Linux does not specify an encoding, but in practice, the locale's |
21 // character set may be used. | 21 // character set may be used. |
22 // | 22 // |
23 // For more arcane bits of path trivia, see below. | 23 // For more arcane bits of path trivia, see below. |
24 // | 24 // |
25 // FilePath objects are intended to be used anywhere paths are. An | 25 // FilePath objects are intended to be used anywhere paths are. An |
26 // application may pass FilePath objects around internally, masking the | 26 // application may pass FilePath objects around internally, masking the |
27 // underlying differences between systems, only differing in implementation | 27 // underlying differences between systems, only differing in implementation |
28 // where interfacing directly with the system. For example, a single | 28 // where interfacing directly with the system. For example, a single |
29 // OpenFile(const FilePath &) function may be made available, allowing all | 29 // OpenFile(const FilePath &) function may be made available, allowing all |
30 // callers to operate without regard to the underlying implementation. On | 30 // callers to operate without regard to the underlying implementation. On |
31 // POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might | 31 // POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might |
32 // wrap _wfopen_s, perhaps both by calling file_path.value().c_str(). This | 32 // wrap _wfopen_s, perhaps both by calling file_path.value().c_str(). This |
Mark Mentovai
2011/10/26 23:29:13
*
| |
33 // allows each platform to pass pathnames around without requiring conversions | 33 // allows each platform to pass pathnames around without requiring conversions |
34 // between encodings, which has an impact on performance, but more imporantly, | 34 // between encodings, which has an impact on performance, but more imporantly, |
35 // has an impact on correctness on platforms that do not have well-defined | 35 // has an impact on correctness on platforms that do not have well-defined |
36 // encodings for pathnames. | 36 // encodings for pathnames. |
37 // | 37 // |
38 // Several methods are available to perform common operations on a FilePath | 38 // Several methods are available to perform common operations on a FilePath |
39 // object, such as determining the parent directory (DirName), isolating the | 39 // object, such as determining the parent directory (DirName), isolating the |
40 // final path component (BaseName), and appending a relative pathname string | 40 // final path component (BaseName), and appending a relative pathname string |
41 // to an existing FilePath object (Append). These methods are highly | 41 // to an existing FilePath object (Append). These methods are highly |
42 // recommended over attempting to split and concatenate strings directly. | 42 // recommended over attempting to split and concatenate strings directly. |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 // excessive separators if this object's path already ends with a separator. | 256 // excessive separators if this object's path already ends with a separator. |
257 // If this object's path is kCurrentDirectory, a new FilePath corresponding | 257 // If this object's path is kCurrentDirectory, a new FilePath corresponding |
258 // only to |component| is returned. |component| must be a relative path; | 258 // only to |component| is returned. |component| must be a relative path; |
259 // it is an error to pass an absolute path. | 259 // it is an error to pass an absolute path. |
260 FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; | 260 FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; |
261 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; | 261 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; |
262 | 262 |
263 // Although Windows StringType is std::wstring, since the encoding it uses for | 263 // Although Windows StringType is std::wstring, since the encoding it uses for |
264 // paths is well defined, it can handle ASCII path components as well. | 264 // paths is well defined, it can handle ASCII path components as well. |
265 // Mac uses UTF8, and since ASCII is a subset of that, it works there as well. | 265 // Mac uses UTF8, and since ASCII is a subset of that, it works there as well. |
266 // On Linux, although it can use any 8-bit encoding for paths, we assume that | 266 // On Linux, although it can use any 8-bit encoding for paths, we assume that |
Mark Mentovai
2011/10/26 23:29:13
*
| |
267 // ASCII is a valid subset, regardless of the encoding, since many operating | 267 // ASCII is a valid subset, regardless of the encoding, since many operating |
268 // system paths will always be ASCII. | 268 // system paths will always be ASCII. |
269 FilePath AppendASCII(const base::StringPiece& component) | 269 FilePath AppendASCII(const base::StringPiece& component) |
270 const WARN_UNUSED_RESULT; | 270 const WARN_UNUSED_RESULT; |
271 | 271 |
272 // Returns true if this FilePath contains an absolute path. On Windows, an | 272 // Returns true if this FilePath contains an absolute path. On Windows, an |
273 // absolute path begins with either a drive letter specification followed by | 273 // absolute path begins with either a drive letter specification followed by |
274 // a separator character, or with two separator characters. On POSIX | 274 // a separator character, or with two separator characters. On POSIX |
275 // platforms, an absolute path begins with a separator character. | 275 // platforms, an absolute path begins with a separator character. |
276 bool IsAbsolute() const; | 276 bool IsAbsolute() const; |
(...skipping 10 matching lines...) Expand all Loading... | |
287 // Warning: you can *not*, in general, go from a display name back to a real | 287 // 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 | 288 // path. Only use this when displaying paths to users, not just when you |
289 // want to stuff a string16 into some other API. | 289 // want to stuff a string16 into some other API. |
290 string16 LossyDisplayName() const; | 290 string16 LossyDisplayName() const; |
291 | 291 |
292 // Return the path as ASCII, or the empty string if the path is not ASCII. | 292 // 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 | 293 // This should only be used for cases where the FilePath is representing a |
294 // known-ASCII filename. | 294 // known-ASCII filename. |
295 std::string MaybeAsASCII() const; | 295 std::string MaybeAsASCII() const; |
296 | 296 |
297 // Return the path as UTF-8. | |
298 std::string AsUTF8() const; | |
299 | |
297 // Older Chromium code assumes that paths are always wstrings. | 300 // Older Chromium code assumes that paths are always wstrings. |
298 // This function converts wstrings to FilePaths, and is | 301 // This function converts wstrings to FilePaths, and is |
299 // useful to smooth porting that old code to the FilePath API. | 302 // useful to smooth porting that old code to the FilePath API. |
300 // It has "Hack" its name so people feel bad about using it. | 303 // It has "Hack" its name so people feel bad about using it. |
301 // http://code.google.com/p/chromium/issues/detail?id=24672 | 304 // http://code.google.com/p/chromium/issues/detail?id=24672 |
302 // | 305 // |
303 // If you are trying to be a good citizen and remove these, ask yourself: | 306 // 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 | 307 // - Am I interacting with other Chrome code that deals with files? Then |
305 // try to convert the API into using FilePath. | 308 // try to convert the API into using FilePath. |
306 // - Am I interacting with OS-native calls? Then use value() to get at an | 309 // - Am I interacting with OS-native calls? Then use value() to get at an |
307 // OS-native string format. | 310 // OS-native string format. |
308 // - Am I using well-known file names, like "config.ini"? Then use the | 311 // - 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). | 312 // 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 | 313 // - 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 | 314 // LossyDisplayName() function, but keep in mind that you can't |
312 // ever use the result of that again as a path. | 315 // ever use the result of that again as a path. |
313 static FilePath FromWStringHack(const std::wstring& wstring); | 316 static FilePath FromWStringHack(const std::wstring& wstring); |
314 | 317 |
318 // Returns a FilePath object from a path name in UTF-8. | |
319 static FilePath FromUTF8(const std::string& utf8); | |
320 | |
315 void WriteToPickle(Pickle* pickle); | 321 void WriteToPickle(Pickle* pickle); |
316 bool ReadFromPickle(Pickle* pickle, void** iter); | 322 bool ReadFromPickle(Pickle* pickle, void** iter); |
317 | 323 |
318 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 324 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
319 // Normalize all path separators to backslash. | 325 // Normalize all path separators to backslash. |
320 FilePath NormalizeWindowsPathSeparators() const; | 326 FilePath NormalizeWindowsPathSeparators() const; |
321 #endif | 327 #endif |
322 | 328 |
323 // Compare two strings in the same way the file system does. | 329 // 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- | 330 // 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 | 400 |
395 inline size_t hash_value(const FilePath& f) { | 401 inline size_t hash_value(const FilePath& f) { |
396 return hash_value(f.value()); | 402 return hash_value(f.value()); |
397 } | 403 } |
398 | 404 |
399 #endif // COMPILER | 405 #endif // COMPILER |
400 | 406 |
401 } // namespace BASE_HASH_NAMESPACE | 407 } // namespace BASE_HASH_NAMESPACE |
402 | 408 |
403 #endif // BASE_FILE_PATH_H_ | 409 #endif // BASE_FILE_PATH_H_ |
OLD | NEW |