security/sandbox/chromium/base/files/file_path.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // FilePath is a container for pathnames stored in a platform's native string
michael@0 6 // type, providing containers for manipulation in according with the
michael@0 7 // platform's conventions for pathnames. It supports the following path
michael@0 8 // types:
michael@0 9 //
michael@0 10 // POSIX Windows
michael@0 11 // --------------- ----------------------------------
michael@0 12 // Fundamental type char[] wchar_t[]
michael@0 13 // Encoding unspecified* UTF-16
michael@0 14 // Separator / \, tolerant of /
michael@0 15 // Drive letters no case-insensitive A-Z followed by :
michael@0 16 // Alternate root // (surprise!) \\, for UNC paths
michael@0 17 //
michael@0 18 // * The encoding need not be specified on POSIX systems, although some
michael@0 19 // POSIX-compliant systems do specify an encoding. Mac OS X uses UTF-8.
michael@0 20 // Chrome OS also uses UTF-8.
michael@0 21 // Linux does not specify an encoding, but in practice, the locale's
michael@0 22 // character set may be used.
michael@0 23 //
michael@0 24 // For more arcane bits of path trivia, see below.
michael@0 25 //
michael@0 26 // FilePath objects are intended to be used anywhere paths are. An
michael@0 27 // application may pass FilePath objects around internally, masking the
michael@0 28 // underlying differences between systems, only differing in implementation
michael@0 29 // where interfacing directly with the system. For example, a single
michael@0 30 // OpenFile(const FilePath &) function may be made available, allowing all
michael@0 31 // callers to operate without regard to the underlying implementation. On
michael@0 32 // POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might
michael@0 33 // wrap _wfopen_s, perhaps both by calling file_path.value().c_str(). This
michael@0 34 // allows each platform to pass pathnames around without requiring conversions
michael@0 35 // between encodings, which has an impact on performance, but more imporantly,
michael@0 36 // has an impact on correctness on platforms that do not have well-defined
michael@0 37 // encodings for pathnames.
michael@0 38 //
michael@0 39 // Several methods are available to perform common operations on a FilePath
michael@0 40 // object, such as determining the parent directory (DirName), isolating the
michael@0 41 // final path component (BaseName), and appending a relative pathname string
michael@0 42 // to an existing FilePath object (Append). These methods are highly
michael@0 43 // recommended over attempting to split and concatenate strings directly.
michael@0 44 // These methods are based purely on string manipulation and knowledge of
michael@0 45 // platform-specific pathname conventions, and do not consult the filesystem
michael@0 46 // at all, making them safe to use without fear of blocking on I/O operations.
michael@0 47 // These methods do not function as mutators but instead return distinct
michael@0 48 // instances of FilePath objects, and are therefore safe to use on const
michael@0 49 // objects. The objects themselves are safe to share between threads.
michael@0 50 //
michael@0 51 // To aid in initialization of FilePath objects from string literals, a
michael@0 52 // FILE_PATH_LITERAL macro is provided, which accounts for the difference
michael@0 53 // between char[]-based pathnames on POSIX systems and wchar_t[]-based
michael@0 54 // pathnames on Windows.
michael@0 55 //
michael@0 56 // Paths can't contain NULs as a precaution agaist premature truncation.
michael@0 57 //
michael@0 58 // Because a FilePath object should not be instantiated at the global scope,
michael@0 59 // instead, use a FilePath::CharType[] and initialize it with
michael@0 60 // FILE_PATH_LITERAL. At runtime, a FilePath object can be created from the
michael@0 61 // character array. Example:
michael@0 62 //
michael@0 63 // | const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("log.txt");
michael@0 64 // |
michael@0 65 // | void Function() {
michael@0 66 // | FilePath log_file_path(kLogFileName);
michael@0 67 // | [...]
michael@0 68 // | }
michael@0 69 //
michael@0 70 // WARNING: FilePaths should ALWAYS be displayed with LTR directionality, even
michael@0 71 // when the UI language is RTL. This means you always need to pass filepaths
michael@0 72 // through base::i18n::WrapPathWithLTRFormatting() before displaying it in the
michael@0 73 // RTL UI.
michael@0 74 //
michael@0 75 // This is a very common source of bugs, please try to keep this in mind.
michael@0 76 //
michael@0 77 // ARCANE BITS OF PATH TRIVIA
michael@0 78 //
michael@0 79 // - A double leading slash is actually part of the POSIX standard. Systems
michael@0 80 // are allowed to treat // as an alternate root, as Windows does for UNC
michael@0 81 // (network share) paths. Most POSIX systems don't do anything special
michael@0 82 // with two leading slashes, but FilePath handles this case properly
michael@0 83 // in case it ever comes across such a system. FilePath needs this support
michael@0 84 // for Windows UNC paths, anyway.
michael@0 85 // References:
michael@0 86 // The Open Group Base Specifications Issue 7, sections 3.266 ("Pathname")
michael@0 87 // and 4.12 ("Pathname Resolution"), available at:
michael@0 88 // http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_266
michael@0 89 // http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_12
michael@0 90 //
michael@0 91 // - Windows treats c:\\ the same way it treats \\. This was intended to
michael@0 92 // allow older applications that require drive letters to support UNC paths
michael@0 93 // like \\server\share\path, by permitting c:\\server\share\path as an
michael@0 94 // equivalent. Since the OS treats these paths specially, FilePath needs
michael@0 95 // to do the same. Since Windows can use either / or \ as the separator,
michael@0 96 // FilePath treats c://, c:\\, //, and \\ all equivalently.
michael@0 97 // Reference:
michael@0 98 // The Old New Thing, "Why is a drive letter permitted in front of UNC
michael@0 99 // paths (sometimes)?", available at:
michael@0 100 // http://blogs.msdn.com/oldnewthing/archive/2005/11/22/495740.aspx
michael@0 101
michael@0 102 #ifndef BASE_FILES_FILE_PATH_H_
michael@0 103 #define BASE_FILES_FILE_PATH_H_
michael@0 104
michael@0 105 #include <stddef.h>
michael@0 106 #include <string>
michael@0 107 #include <vector>
michael@0 108
michael@0 109 #include "base/base_export.h"
michael@0 110 #include "base/compiler_specific.h"
michael@0 111 #include "base/containers/hash_tables.h"
michael@0 112 #include "base/strings/string16.h"
michael@0 113 #include "base/strings/string_piece.h" // For implicit conversions.
michael@0 114 #include "build/build_config.h"
michael@0 115
michael@0 116 // Windows-style drive letter support and pathname separator characters can be
michael@0 117 // enabled and disabled independently, to aid testing. These #defines are
michael@0 118 // here so that the same setting can be used in both the implementation and
michael@0 119 // in the unit test.
michael@0 120 #if defined(OS_WIN)
michael@0 121 #define FILE_PATH_USES_DRIVE_LETTERS
michael@0 122 #define FILE_PATH_USES_WIN_SEPARATORS
michael@0 123 #endif // OS_WIN
michael@0 124
michael@0 125 class Pickle;
michael@0 126 class PickleIterator;
michael@0 127
michael@0 128 namespace base {
michael@0 129
michael@0 130 // An abstraction to isolate users from the differences between native
michael@0 131 // pathnames on different platforms.
michael@0 132 class BASE_EXPORT FilePath {
michael@0 133 public:
michael@0 134 #if defined(OS_POSIX)
michael@0 135 // On most platforms, native pathnames are char arrays, and the encoding
michael@0 136 // may or may not be specified. On Mac OS X, native pathnames are encoded
michael@0 137 // in UTF-8.
michael@0 138 typedef std::string StringType;
michael@0 139 #elif defined(OS_WIN)
michael@0 140 // On Windows, for Unicode-aware applications, native pathnames are wchar_t
michael@0 141 // arrays encoded in UTF-16.
michael@0 142 typedef std::wstring StringType;
michael@0 143 #endif // OS_WIN
michael@0 144
michael@0 145 typedef StringType::value_type CharType;
michael@0 146
michael@0 147 // Null-terminated array of separators used to separate components in
michael@0 148 // hierarchical paths. Each character in this array is a valid separator,
michael@0 149 // but kSeparators[0] is treated as the canonical separator and will be used
michael@0 150 // when composing pathnames.
michael@0 151 static const CharType kSeparators[];
michael@0 152
michael@0 153 // arraysize(kSeparators).
michael@0 154 static const size_t kSeparatorsLength;
michael@0 155
michael@0 156 // A special path component meaning "this directory."
michael@0 157 static const CharType kCurrentDirectory[];
michael@0 158
michael@0 159 // A special path component meaning "the parent directory."
michael@0 160 static const CharType kParentDirectory[];
michael@0 161
michael@0 162 // The character used to identify a file extension.
michael@0 163 static const CharType kExtensionSeparator;
michael@0 164
michael@0 165 FilePath();
michael@0 166 FilePath(const FilePath& that);
michael@0 167 explicit FilePath(const StringType& path);
michael@0 168 ~FilePath();
michael@0 169 FilePath& operator=(const FilePath& that);
michael@0 170
michael@0 171 bool operator==(const FilePath& that) const;
michael@0 172
michael@0 173 bool operator!=(const FilePath& that) const;
michael@0 174
michael@0 175 // Required for some STL containers and operations
michael@0 176 bool operator<(const FilePath& that) const {
michael@0 177 return path_ < that.path_;
michael@0 178 }
michael@0 179
michael@0 180 const StringType& value() const { return path_; }
michael@0 181
michael@0 182 bool empty() const { return path_.empty(); }
michael@0 183
michael@0 184 void clear() { path_.clear(); }
michael@0 185
michael@0 186 // Returns true if |character| is in kSeparators.
michael@0 187 static bool IsSeparator(CharType character);
michael@0 188
michael@0 189 // Returns a vector of all of the components of the provided path. It is
michael@0 190 // equivalent to calling DirName().value() on the path's root component,
michael@0 191 // and BaseName().value() on each child component.
michael@0 192 void GetComponents(std::vector<FilePath::StringType>* components) const;
michael@0 193
michael@0 194 // Returns true if this FilePath is a strict parent of the |child|. Absolute
michael@0 195 // and relative paths are accepted i.e. is /foo parent to /foo/bar and
michael@0 196 // is foo parent to foo/bar. Does not convert paths to absolute, follow
michael@0 197 // symlinks or directory navigation (e.g. ".."). A path is *NOT* its own
michael@0 198 // parent.
michael@0 199 bool IsParent(const FilePath& child) const;
michael@0 200
michael@0 201 // If IsParent(child) holds, appends to path (if non-NULL) the
michael@0 202 // relative path to child and returns true. For example, if parent
michael@0 203 // holds "/Users/johndoe/Library/Application Support", child holds
michael@0 204 // "/Users/johndoe/Library/Application Support/Google/Chrome/Default", and
michael@0 205 // *path holds "/Users/johndoe/Library/Caches", then after
michael@0 206 // parent.AppendRelativePath(child, path) is called *path will hold
michael@0 207 // "/Users/johndoe/Library/Caches/Google/Chrome/Default". Otherwise,
michael@0 208 // returns false.
michael@0 209 bool AppendRelativePath(const FilePath& child, FilePath* path) const;
michael@0 210
michael@0 211 // Returns a FilePath corresponding to the directory containing the path
michael@0 212 // named by this object, stripping away the file component. If this object
michael@0 213 // only contains one component, returns a FilePath identifying
michael@0 214 // kCurrentDirectory. If this object already refers to the root directory,
michael@0 215 // returns a FilePath identifying the root directory.
michael@0 216 FilePath DirName() const WARN_UNUSED_RESULT;
michael@0 217
michael@0 218 // Returns a FilePath corresponding to the last path component of this
michael@0 219 // object, either a file or a directory. If this object already refers to
michael@0 220 // the root directory, returns a FilePath identifying the root directory;
michael@0 221 // this is the only situation in which BaseName will return an absolute path.
michael@0 222 FilePath BaseName() const WARN_UNUSED_RESULT;
michael@0 223
michael@0 224 // Returns ".jpg" for path "C:\pics\jojo.jpg", or an empty string if
michael@0 225 // the file has no extension. If non-empty, Extension() will always start
michael@0 226 // with precisely one ".". The following code should always work regardless
michael@0 227 // of the value of path.
michael@0 228 // new_path = path.RemoveExtension().value().append(path.Extension());
michael@0 229 // ASSERT(new_path == path.value());
michael@0 230 // NOTE: this is different from the original file_util implementation which
michael@0 231 // returned the extension without a leading "." ("jpg" instead of ".jpg")
michael@0 232 StringType Extension() const;
michael@0 233
michael@0 234 // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg"
michael@0 235 // NOTE: this is slightly different from the similar file_util implementation
michael@0 236 // which returned simply 'jojo'.
michael@0 237 FilePath RemoveExtension() const WARN_UNUSED_RESULT;
michael@0 238
michael@0 239 // Inserts |suffix| after the file name portion of |path| but before the
michael@0 240 // extension. Returns "" if BaseName() == "." or "..".
michael@0 241 // Examples:
michael@0 242 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg"
michael@0 243 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg"
michael@0 244 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)"
michael@0 245 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)"
michael@0 246 FilePath InsertBeforeExtension(
michael@0 247 const StringType& suffix) const WARN_UNUSED_RESULT;
michael@0 248 FilePath InsertBeforeExtensionASCII(
michael@0 249 const base::StringPiece& suffix) const WARN_UNUSED_RESULT;
michael@0 250
michael@0 251 // Adds |extension| to |file_name|. Returns the current FilePath if
michael@0 252 // |extension| is empty. Returns "" if BaseName() == "." or "..".
michael@0 253 FilePath AddExtension(
michael@0 254 const StringType& extension) const WARN_UNUSED_RESULT;
michael@0 255
michael@0 256 // Replaces the extension of |file_name| with |extension|. If |file_name|
michael@0 257 // does not have an extension, then |extension| is added. If |extension| is
michael@0 258 // empty, then the extension is removed from |file_name|.
michael@0 259 // Returns "" if BaseName() == "." or "..".
michael@0 260 FilePath ReplaceExtension(
michael@0 261 const StringType& extension) const WARN_UNUSED_RESULT;
michael@0 262
michael@0 263 // Returns true if the file path matches the specified extension. The test is
michael@0 264 // case insensitive. Don't forget the leading period if appropriate.
michael@0 265 bool MatchesExtension(const StringType& extension) const;
michael@0 266
michael@0 267 // Returns a FilePath by appending a separator and the supplied path
michael@0 268 // component to this object's path. Append takes care to avoid adding
michael@0 269 // excessive separators if this object's path already ends with a separator.
michael@0 270 // If this object's path is kCurrentDirectory, a new FilePath corresponding
michael@0 271 // only to |component| is returned. |component| must be a relative path;
michael@0 272 // it is an error to pass an absolute path.
michael@0 273 FilePath Append(const StringType& component) const WARN_UNUSED_RESULT;
michael@0 274 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT;
michael@0 275
michael@0 276 // Although Windows StringType is std::wstring, since the encoding it uses for
michael@0 277 // paths is well defined, it can handle ASCII path components as well.
michael@0 278 // Mac uses UTF8, and since ASCII is a subset of that, it works there as well.
michael@0 279 // On Linux, although it can use any 8-bit encoding for paths, we assume that
michael@0 280 // ASCII is a valid subset, regardless of the encoding, since many operating
michael@0 281 // system paths will always be ASCII.
michael@0 282 FilePath AppendASCII(const base::StringPiece& component)
michael@0 283 const WARN_UNUSED_RESULT;
michael@0 284
michael@0 285 // Returns true if this FilePath contains an absolute path. On Windows, an
michael@0 286 // absolute path begins with either a drive letter specification followed by
michael@0 287 // a separator character, or with two separator characters. On POSIX
michael@0 288 // platforms, an absolute path begins with a separator character.
michael@0 289 bool IsAbsolute() const;
michael@0 290
michael@0 291 // Returns true if the patch ends with a path separator character.
michael@0 292 bool EndsWithSeparator() const WARN_UNUSED_RESULT;
michael@0 293
michael@0 294 // Returns a copy of this FilePath that ends with a trailing separator. If
michael@0 295 // the input path is empty, an empty FilePath will be returned.
michael@0 296 FilePath AsEndingWithSeparator() const WARN_UNUSED_RESULT;
michael@0 297
michael@0 298 // Returns a copy of this FilePath that does not end with a trailing
michael@0 299 // separator.
michael@0 300 FilePath StripTrailingSeparators() const WARN_UNUSED_RESULT;
michael@0 301
michael@0 302 // Returns true if this FilePath contains any attempt to reference a parent
michael@0 303 // directory (i.e. has a path component that is ".."
michael@0 304 bool ReferencesParent() const;
michael@0 305
michael@0 306 // Return a Unicode human-readable version of this path.
michael@0 307 // Warning: you can *not*, in general, go from a display name back to a real
michael@0 308 // path. Only use this when displaying paths to users, not just when you
michael@0 309 // want to stuff a string16 into some other API.
michael@0 310 string16 LossyDisplayName() const;
michael@0 311
michael@0 312 // Return the path as ASCII, or the empty string if the path is not ASCII.
michael@0 313 // This should only be used for cases where the FilePath is representing a
michael@0 314 // known-ASCII filename.
michael@0 315 std::string MaybeAsASCII() const;
michael@0 316
michael@0 317 // Return the path as UTF-8.
michael@0 318 //
michael@0 319 // This function is *unsafe* as there is no way to tell what encoding is
michael@0 320 // used in file names on POSIX systems other than Mac and Chrome OS,
michael@0 321 // although UTF-8 is practically used everywhere these days. To mitigate
michael@0 322 // the encoding issue, this function internally calls
michael@0 323 // SysNativeMBToWide() on POSIX systems other than Mac and Chrome OS,
michael@0 324 // per assumption that the current locale's encoding is used in file
michael@0 325 // names, but this isn't a perfect solution.
michael@0 326 //
michael@0 327 // Once it becomes safe to to stop caring about non-UTF-8 file names,
michael@0 328 // the SysNativeMBToWide() hack will be removed from the code, along
michael@0 329 // with "Unsafe" in the function name.
michael@0 330 std::string AsUTF8Unsafe() const;
michael@0 331
michael@0 332 // Similar to AsUTF8Unsafe, but returns UTF-16 instead.
michael@0 333 string16 AsUTF16Unsafe() const;
michael@0 334
michael@0 335 // Older Chromium code assumes that paths are always wstrings.
michael@0 336 // This function converts wstrings to FilePaths, and is
michael@0 337 // useful to smooth porting that old code to the FilePath API.
michael@0 338 // It has "Hack" its name so people feel bad about using it.
michael@0 339 // http://code.google.com/p/chromium/issues/detail?id=24672
michael@0 340 //
michael@0 341 // If you are trying to be a good citizen and remove these, ask yourself:
michael@0 342 // - Am I interacting with other Chrome code that deals with files? Then
michael@0 343 // try to convert the API into using FilePath.
michael@0 344 // - Am I interacting with OS-native calls? Then use value() to get at an
michael@0 345 // OS-native string format.
michael@0 346 // - Am I using well-known file names, like "config.ini"? Then use the
michael@0 347 // ASCII functions (we require paths to always be supersets of ASCII).
michael@0 348 // - Am I displaying a string to the user in some UI? Then use the
michael@0 349 // LossyDisplayName() function, but keep in mind that you can't
michael@0 350 // ever use the result of that again as a path.
michael@0 351 static FilePath FromWStringHack(const std::wstring& wstring);
michael@0 352
michael@0 353 // Returns a FilePath object from a path name in UTF-8. This function
michael@0 354 // should only be used for cases where you are sure that the input
michael@0 355 // string is UTF-8.
michael@0 356 //
michael@0 357 // Like AsUTF8Unsafe(), this function is unsafe. This function
michael@0 358 // internally calls SysWideToNativeMB() on POSIX systems other than Mac
michael@0 359 // and Chrome OS, to mitigate the encoding issue. See the comment at
michael@0 360 // AsUTF8Unsafe() for details.
michael@0 361 static FilePath FromUTF8Unsafe(const std::string& utf8);
michael@0 362
michael@0 363 // Similar to FromUTF8Unsafe, but accepts UTF-16 instead.
michael@0 364 static FilePath FromUTF16Unsafe(const string16& utf16);
michael@0 365
michael@0 366 void WriteToPickle(Pickle* pickle) const;
michael@0 367 bool ReadFromPickle(PickleIterator* iter);
michael@0 368
michael@0 369 // Normalize all path separators to backslash on Windows
michael@0 370 // (if FILE_PATH_USES_WIN_SEPARATORS is true), or do nothing on POSIX systems.
michael@0 371 FilePath NormalizePathSeparators() const;
michael@0 372
michael@0 373 // Compare two strings in the same way the file system does.
michael@0 374 // Note that these always ignore case, even on file systems that are case-
michael@0 375 // sensitive. If case-sensitive comparison is ever needed, add corresponding
michael@0 376 // methods here.
michael@0 377 // The methods are written as a static method so that they can also be used
michael@0 378 // on parts of a file path, e.g., just the extension.
michael@0 379 // CompareIgnoreCase() returns -1, 0 or 1 for less-than, equal-to and
michael@0 380 // greater-than respectively.
michael@0 381 static int CompareIgnoreCase(const StringType& string1,
michael@0 382 const StringType& string2);
michael@0 383 static bool CompareEqualIgnoreCase(const StringType& string1,
michael@0 384 const StringType& string2) {
michael@0 385 return CompareIgnoreCase(string1, string2) == 0;
michael@0 386 }
michael@0 387 static bool CompareLessIgnoreCase(const StringType& string1,
michael@0 388 const StringType& string2) {
michael@0 389 return CompareIgnoreCase(string1, string2) < 0;
michael@0 390 }
michael@0 391
michael@0 392 #if defined(OS_MACOSX)
michael@0 393 // Returns the string in the special canonical decomposed form as defined for
michael@0 394 // HFS, which is close to, but not quite, decomposition form D. See
michael@0 395 // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubtleties
michael@0 396 // for further comments.
michael@0 397 // Returns the epmty string if the conversion failed.
michael@0 398 static StringType GetHFSDecomposedForm(const FilePath::StringType& string);
michael@0 399
michael@0 400 // Special UTF-8 version of FastUnicodeCompare. Cf:
michael@0 401 // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
michael@0 402 // IMPORTANT: The input strings must be in the special HFS decomposed form!
michael@0 403 // (cf. above GetHFSDecomposedForm method)
michael@0 404 static int HFSFastUnicodeCompare(const StringType& string1,
michael@0 405 const StringType& string2);
michael@0 406 #endif
michael@0 407
michael@0 408 private:
michael@0 409 // Remove trailing separators from this object. If the path is absolute, it
michael@0 410 // will never be stripped any more than to refer to the absolute root
michael@0 411 // directory, so "////" will become "/", not "". A leading pair of
michael@0 412 // separators is never stripped, to support alternate roots. This is used to
michael@0 413 // support UNC paths on Windows.
michael@0 414 void StripTrailingSeparatorsInternal();
michael@0 415
michael@0 416 StringType path_;
michael@0 417 };
michael@0 418
michael@0 419 } // namespace base
michael@0 420
michael@0 421 // This is required by googletest to print a readable output on test failures.
michael@0 422 BASE_EXPORT extern void PrintTo(const base::FilePath& path, std::ostream* out);
michael@0 423
michael@0 424 // Macros for string literal initialization of FilePath::CharType[], and for
michael@0 425 // using a FilePath::CharType[] in a printf-style format string.
michael@0 426 #if defined(OS_POSIX)
michael@0 427 #define FILE_PATH_LITERAL(x) x
michael@0 428 #define PRFilePath "s"
michael@0 429 #define PRFilePathLiteral "%s"
michael@0 430 #elif defined(OS_WIN)
michael@0 431 #define FILE_PATH_LITERAL(x) L ## x
michael@0 432 #define PRFilePath "ls"
michael@0 433 #define PRFilePathLiteral L"%ls"
michael@0 434 #endif // OS_WIN
michael@0 435
michael@0 436 // Provide a hash function so that hash_sets and maps can contain FilePath
michael@0 437 // objects.
michael@0 438 namespace BASE_HASH_NAMESPACE {
michael@0 439 #if defined(COMPILER_GCC)
michael@0 440
michael@0 441 template<>
michael@0 442 struct hash<base::FilePath> {
michael@0 443 size_t operator()(const base::FilePath& f) const {
michael@0 444 return hash<base::FilePath::StringType>()(f.value());
michael@0 445 }
michael@0 446 };
michael@0 447
michael@0 448 #elif defined(COMPILER_MSVC)
michael@0 449
michael@0 450 inline size_t hash_value(const base::FilePath& f) {
michael@0 451 return hash_value(f.value());
michael@0 452 }
michael@0 453
michael@0 454 #endif // COMPILER
michael@0 455
michael@0 456 } // namespace BASE_HASH_NAMESPACE
michael@0 457
michael@0 458 #endif // BASE_FILES_FILE_PATH_H_

mercurial