OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 #include <stddef.h> | 105 #include <stddef.h> |
106 | 106 |
107 #include <iosfwd> | 107 #include <iosfwd> |
108 #include <string> | 108 #include <string> |
109 #include <vector> | 109 #include <vector> |
110 | 110 |
111 #include "base/base_export.h" | 111 #include "base/base_export.h" |
112 #include "base/compiler_specific.h" | 112 #include "base/compiler_specific.h" |
113 #include "base/containers/hash_tables.h" | 113 #include "base/containers/hash_tables.h" |
114 #include "base/strings/string16.h" | 114 #include "base/strings/string16.h" |
115 #include "base/strings/string_piece.h" // For implicit conversions. | 115 #include "base/strings/string_piece.h" |
116 #include "build/build_config.h" | 116 #include "build/build_config.h" |
117 | 117 |
118 // Windows-style drive letter support and pathname separator characters can be | 118 // Windows-style drive letter support and pathname separator characters can be |
119 // enabled and disabled independently, to aid testing. These #defines are | 119 // enabled and disabled independently, to aid testing. These #defines are |
120 // here so that the same setting can be used in both the implementation and | 120 // here so that the same setting can be used in both the implementation and |
121 // in the unit test. | 121 // in the unit test. |
122 #if defined(OS_WIN) | 122 #if defined(OS_WIN) |
123 #define FILE_PATH_USES_DRIVE_LETTERS | 123 #define FILE_PATH_USES_DRIVE_LETTERS |
124 #define FILE_PATH_USES_WIN_SEPARATORS | 124 #define FILE_PATH_USES_WIN_SEPARATORS |
125 #endif // OS_WIN | 125 #endif // OS_WIN |
(...skipping 11 matching lines...) Expand all Loading... |
137 // On most platforms, native pathnames are char arrays, and the encoding | 137 // On most platforms, native pathnames are char arrays, and the encoding |
138 // may or may not be specified. On Mac OS X, native pathnames are encoded | 138 // may or may not be specified. On Mac OS X, native pathnames are encoded |
139 // in UTF-8. | 139 // in UTF-8. |
140 typedef std::string StringType; | 140 typedef std::string StringType; |
141 #elif defined(OS_WIN) | 141 #elif defined(OS_WIN) |
142 // On Windows, for Unicode-aware applications, native pathnames are wchar_t | 142 // On Windows, for Unicode-aware applications, native pathnames are wchar_t |
143 // arrays encoded in UTF-16. | 143 // arrays encoded in UTF-16. |
144 typedef std::wstring StringType; | 144 typedef std::wstring StringType; |
145 #endif // OS_WIN | 145 #endif // OS_WIN |
146 | 146 |
| 147 typedef BasicStringPiece<StringType> StringPieceType; |
147 typedef StringType::value_type CharType; | 148 typedef StringType::value_type CharType; |
148 | 149 |
149 // Null-terminated array of separators used to separate components in | 150 // Null-terminated array of separators used to separate components in |
150 // hierarchical paths. Each character in this array is a valid separator, | 151 // hierarchical paths. Each character in this array is a valid separator, |
151 // but kSeparators[0] is treated as the canonical separator and will be used | 152 // but kSeparators[0] is treated as the canonical separator and will be used |
152 // when composing pathnames. | 153 // when composing pathnames. |
153 static const CharType kSeparators[]; | 154 static const CharType kSeparators[]; |
154 | 155 |
155 // arraysize(kSeparators). | 156 // arraysize(kSeparators). |
156 static const size_t kSeparatorsLength; | 157 static const size_t kSeparatorsLength; |
157 | 158 |
158 // A special path component meaning "this directory." | 159 // A special path component meaning "this directory." |
159 static const CharType kCurrentDirectory[]; | 160 static const CharType kCurrentDirectory[]; |
160 | 161 |
161 // A special path component meaning "the parent directory." | 162 // A special path component meaning "the parent directory." |
162 static const CharType kParentDirectory[]; | 163 static const CharType kParentDirectory[]; |
163 | 164 |
164 // The character used to identify a file extension. | 165 // The character used to identify a file extension. |
165 static const CharType kExtensionSeparator; | 166 static const CharType kExtensionSeparator; |
166 | 167 |
167 FilePath(); | 168 FilePath(); |
168 FilePath(const FilePath& that); | 169 FilePath(const FilePath& that); |
169 explicit FilePath(const StringType& path); | 170 explicit FilePath(StringPieceType path); |
170 ~FilePath(); | 171 ~FilePath(); |
171 FilePath& operator=(const FilePath& that); | 172 FilePath& operator=(const FilePath& that); |
172 | 173 |
173 bool operator==(const FilePath& that) const; | 174 bool operator==(const FilePath& that) const; |
174 | 175 |
175 bool operator!=(const FilePath& that) const; | 176 bool operator!=(const FilePath& that) const; |
176 | 177 |
177 // Required for some STL containers and operations | 178 // Required for some STL containers and operations |
178 bool operator<(const FilePath& that) const { | 179 bool operator<(const FilePath& that) const { |
179 return path_ < that.path_; | 180 return path_ < that.path_; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 // ignores double extensions. | 261 // ignores double extensions. |
261 FilePath RemoveFinalExtension() const WARN_UNUSED_RESULT; | 262 FilePath RemoveFinalExtension() const WARN_UNUSED_RESULT; |
262 | 263 |
263 // Inserts |suffix| after the file name portion of |path| but before the | 264 // Inserts |suffix| after the file name portion of |path| but before the |
264 // extension. Returns "" if BaseName() == "." or "..". | 265 // extension. Returns "" if BaseName() == "." or "..". |
265 // Examples: | 266 // Examples: |
266 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" | 267 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" |
267 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" | 268 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" |
268 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" | 269 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" |
269 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" | 270 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" |
270 FilePath InsertBeforeExtension( | 271 FilePath InsertBeforeExtension(StringPieceType suffix) const |
271 const StringType& suffix) const WARN_UNUSED_RESULT; | 272 WARN_UNUSED_RESULT; |
272 FilePath InsertBeforeExtensionASCII( | 273 FilePath InsertBeforeExtensionASCII(StringPiece suffix) const |
273 const base::StringPiece& suffix) const WARN_UNUSED_RESULT; | 274 WARN_UNUSED_RESULT; |
274 | 275 |
275 // Adds |extension| to |file_name|. Returns the current FilePath if | 276 // Adds |extension| to |file_name|. Returns the current FilePath if |
276 // |extension| is empty. Returns "" if BaseName() == "." or "..". | 277 // |extension| is empty. Returns "" if BaseName() == "." or "..". |
277 FilePath AddExtension( | 278 FilePath AddExtension(StringPieceType extension) const WARN_UNUSED_RESULT; |
278 const StringType& extension) const WARN_UNUSED_RESULT; | |
279 | 279 |
280 // Replaces the extension of |file_name| with |extension|. If |file_name| | 280 // Replaces the extension of |file_name| with |extension|. If |file_name| |
281 // does not have an extension, then |extension| is added. If |extension| is | 281 // does not have an extension, then |extension| is added. If |extension| is |
282 // empty, then the extension is removed from |file_name|. | 282 // empty, then the extension is removed from |file_name|. |
283 // Returns "" if BaseName() == "." or "..". | 283 // Returns "" if BaseName() == "." or "..". |
284 FilePath ReplaceExtension( | 284 FilePath ReplaceExtension(StringPieceType extension) const WARN_UNUSED_RESULT; |
285 const StringType& extension) const WARN_UNUSED_RESULT; | |
286 | 285 |
287 // Returns true if the file path matches the specified extension. The test is | 286 // Returns true if the file path matches the specified extension. The test is |
288 // case insensitive. Don't forget the leading period if appropriate. | 287 // case insensitive. Don't forget the leading period if appropriate. |
289 bool MatchesExtension(const StringType& extension) const; | 288 bool MatchesExtension(StringPieceType extension) const; |
290 | 289 |
291 // Returns a FilePath by appending a separator and the supplied path | 290 // Returns a FilePath by appending a separator and the supplied path |
292 // component to this object's path. Append takes care to avoid adding | 291 // component to this object's path. Append takes care to avoid adding |
293 // excessive separators if this object's path already ends with a separator. | 292 // excessive separators if this object's path already ends with a separator. |
294 // If this object's path is kCurrentDirectory, a new FilePath corresponding | 293 // If this object's path is kCurrentDirectory, a new FilePath corresponding |
295 // only to |component| is returned. |component| must be a relative path; | 294 // only to |component| is returned. |component| must be a relative path; |
296 // it is an error to pass an absolute path. | 295 // it is an error to pass an absolute path. |
297 FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; | 296 FilePath Append(StringPieceType component) const WARN_UNUSED_RESULT; |
298 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; | 297 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; |
299 | 298 |
300 // Although Windows StringType is std::wstring, since the encoding it uses for | 299 // Although Windows StringType is std::wstring, since the encoding it uses for |
301 // paths is well defined, it can handle ASCII path components as well. | 300 // paths is well defined, it can handle ASCII path components as well. |
302 // Mac uses UTF8, and since ASCII is a subset of that, it works there as well. | 301 // Mac uses UTF8, and since ASCII is a subset of that, it works there as well. |
303 // On Linux, although it can use any 8-bit encoding for paths, we assume that | 302 // On Linux, although it can use any 8-bit encoding for paths, we assume that |
304 // ASCII is a valid subset, regardless of the encoding, since many operating | 303 // ASCII is a valid subset, regardless of the encoding, since many operating |
305 // system paths will always be ASCII. | 304 // system paths will always be ASCII. |
306 FilePath AppendASCII(const base::StringPiece& component) | 305 FilePath AppendASCII(StringPiece component) const WARN_UNUSED_RESULT; |
307 const WARN_UNUSED_RESULT; | |
308 | 306 |
309 // Returns true if this FilePath contains an absolute path. On Windows, an | 307 // Returns true if this FilePath contains an absolute path. On Windows, an |
310 // absolute path begins with either a drive letter specification followed by | 308 // absolute path begins with either a drive letter specification followed by |
311 // a separator character, or with two separator characters. On POSIX | 309 // a separator character, or with two separator characters. On POSIX |
312 // platforms, an absolute path begins with a separator character. | 310 // platforms, an absolute path begins with a separator character. |
313 bool IsAbsolute() const; | 311 bool IsAbsolute() const; |
314 | 312 |
315 // Returns true if the patch ends with a path separator character. | 313 // Returns true if the patch ends with a path separator character. |
316 bool EndsWithSeparator() const WARN_UNUSED_RESULT; | 314 bool EndsWithSeparator() const WARN_UNUSED_RESULT; |
317 | 315 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 FilePath NormalizePathSeparatorsTo(CharType separator) const; | 379 FilePath NormalizePathSeparatorsTo(CharType separator) const; |
382 | 380 |
383 // Compare two strings in the same way the file system does. | 381 // Compare two strings in the same way the file system does. |
384 // Note that these always ignore case, even on file systems that are case- | 382 // Note that these always ignore case, even on file systems that are case- |
385 // sensitive. If case-sensitive comparison is ever needed, add corresponding | 383 // sensitive. If case-sensitive comparison is ever needed, add corresponding |
386 // methods here. | 384 // methods here. |
387 // The methods are written as a static method so that they can also be used | 385 // The methods are written as a static method so that they can also be used |
388 // on parts of a file path, e.g., just the extension. | 386 // on parts of a file path, e.g., just the extension. |
389 // CompareIgnoreCase() returns -1, 0 or 1 for less-than, equal-to and | 387 // CompareIgnoreCase() returns -1, 0 or 1 for less-than, equal-to and |
390 // greater-than respectively. | 388 // greater-than respectively. |
391 static int CompareIgnoreCase(const StringType& string1, | 389 static int CompareIgnoreCase(StringPieceType string1, |
392 const StringType& string2); | 390 StringPieceType string2); |
393 static bool CompareEqualIgnoreCase(const StringType& string1, | 391 static bool CompareEqualIgnoreCase(StringPieceType string1, |
394 const StringType& string2) { | 392 StringPieceType string2) { |
395 return CompareIgnoreCase(string1, string2) == 0; | 393 return CompareIgnoreCase(string1, string2) == 0; |
396 } | 394 } |
397 static bool CompareLessIgnoreCase(const StringType& string1, | 395 static bool CompareLessIgnoreCase(StringPieceType string1, |
398 const StringType& string2) { | 396 StringPieceType string2) { |
399 return CompareIgnoreCase(string1, string2) < 0; | 397 return CompareIgnoreCase(string1, string2) < 0; |
400 } | 398 } |
401 | 399 |
402 #if defined(OS_MACOSX) | 400 #if defined(OS_MACOSX) |
403 // Returns the string in the special canonical decomposed form as defined for | 401 // Returns the string in the special canonical decomposed form as defined for |
404 // HFS, which is close to, but not quite, decomposition form D. See | 402 // HFS, which is close to, but not quite, decomposition form D. See |
405 // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubt
leties | 403 // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubt
leties |
406 // for further comments. | 404 // for further comments. |
407 // Returns the epmty string if the conversion failed. | 405 // Returns the epmty string if the conversion failed. |
408 static StringType GetHFSDecomposedForm(const FilePath::StringType& string); | 406 static StringType GetHFSDecomposedForm(StringPieceType string); |
409 | 407 |
410 // Special UTF-8 version of FastUnicodeCompare. Cf: | 408 // Special UTF-8 version of FastUnicodeCompare. Cf: |
411 // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringCompa
risonAlgorithm | 409 // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringCompa
risonAlgorithm |
412 // IMPORTANT: The input strings must be in the special HFS decomposed form! | 410 // IMPORTANT: The input strings must be in the special HFS decomposed form! |
413 // (cf. above GetHFSDecomposedForm method) | 411 // (cf. above GetHFSDecomposedForm method) |
414 static int HFSFastUnicodeCompare(const StringType& string1, | 412 static int HFSFastUnicodeCompare(StringPieceType string1, |
415 const StringType& string2); | 413 StringPieceType string2); |
416 #endif | 414 #endif |
417 | 415 |
418 #if defined(OS_ANDROID) | 416 #if defined(OS_ANDROID) |
419 // On android, file selection dialog can return a file with content uri | 417 // On android, file selection dialog can return a file with content uri |
420 // scheme(starting with content://). Content uri needs to be opened with | 418 // scheme(starting with content://). Content uri needs to be opened with |
421 // ContentResolver to guarantee that the app has appropriate permissions | 419 // ContentResolver to guarantee that the app has appropriate permissions |
422 // to access it. | 420 // to access it. |
423 // Returns true if the path is a content uri, or false otherwise. | 421 // Returns true if the path is a content uri, or false otherwise. |
424 bool IsContentUri() const; | 422 bool IsContentUri() const; |
425 #endif | 423 #endif |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 template<> | 458 template<> |
461 struct hash<base::FilePath> { | 459 struct hash<base::FilePath> { |
462 size_t operator()(const base::FilePath& f) const { | 460 size_t operator()(const base::FilePath& f) const { |
463 return hash<base::FilePath::StringType>()(f.value()); | 461 return hash<base::FilePath::StringType>()(f.value()); |
464 } | 462 } |
465 }; | 463 }; |
466 | 464 |
467 } // namespace BASE_HASH_NAMESPACE | 465 } // namespace BASE_HASH_NAMESPACE |
468 | 466 |
469 #endif // BASE_FILES_FILE_PATH_H_ | 467 #endif // BASE_FILES_FILE_PATH_H_ |
OLD | NEW |