blob: fba2f98ab692674f56e35d07d40ff370ff78f94f [file] [log] [blame]
brettw@chromium.org59eef1f2013-02-24 14:40:52 +09001// 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
3// found in the LICENSE file.
4
5// FilePath is a container for pathnames stored in a platform's native string
6// type, providing containers for manipulation in according with the
7// platform's conventions for pathnames. It supports the following path
8// types:
9//
10// POSIX Windows
11// --------------- ----------------------------------
12// Fundamental type char[] wchar_t[]
13// Encoding unspecified* UTF-16
14// Separator / \, tolerant of /
15// Drive letters no case-insensitive A-Z followed by :
16// Alternate root // (surprise!) \\, for UNC paths
17//
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.
20// Chrome OS also uses UTF-8.
21// Linux does not specify an encoding, but in practice, the locale's
22// character set may be used.
23//
24// For more arcane bits of path trivia, see below.
25//
26// FilePath objects are intended to be used anywhere paths are. An
27// application may pass FilePath objects around internally, masking the
28// underlying differences between systems, only differing in implementation
29// where interfacing directly with the system. For example, a single
30// OpenFile(const FilePath &) function may be made available, allowing all
31// callers to operate without regard to the underlying implementation. On
32// POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might
33// wrap _wfopen_s, perhaps both by calling file_path.value().c_str(). This
34// allows each platform to pass pathnames around without requiring conversions
35// between encodings, which has an impact on performance, but more imporantly,
36// has an impact on correctness on platforms that do not have well-defined
37// encodings for pathnames.
38//
39// Several methods are available to perform common operations on a FilePath
40// object, such as determining the parent directory (DirName), isolating the
41// final path component (BaseName), and appending a relative pathname string
42// to an existing FilePath object (Append). These methods are highly
43// recommended over attempting to split and concatenate strings directly.
44// These methods are based purely on string manipulation and knowledge of
45// platform-specific pathname conventions, and do not consult the filesystem
46// at all, making them safe to use without fear of blocking on I/O operations.
47// These methods do not function as mutators but instead return distinct
48// instances of FilePath objects, and are therefore safe to use on const
49// objects. The objects themselves are safe to share between threads.
50//
51// To aid in initialization of FilePath objects from string literals, a
52// FILE_PATH_LITERAL macro is provided, which accounts for the difference
53// between char[]-based pathnames on POSIX systems and wchar_t[]-based
54// pathnames on Windows.
55//
dougk2e9fe272015-02-13 11:47:52 +090056// As a precaution against premature truncation, paths can't contain NULs.
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090057//
58// Because a FilePath object should not be instantiated at the global scope,
59// instead, use a FilePath::CharType[] and initialize it with
60// FILE_PATH_LITERAL. At runtime, a FilePath object can be created from the
61// character array. Example:
62//
63// | const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("log.txt");
64// |
65// | void Function() {
66// | FilePath log_file_path(kLogFileName);
67// | [...]
68// | }
69//
70// WARNING: FilePaths should ALWAYS be displayed with LTR directionality, even
71// when the UI language is RTL. This means you always need to pass filepaths
72// through base::i18n::WrapPathWithLTRFormatting() before displaying it in the
73// RTL UI.
74//
75// This is a very common source of bugs, please try to keep this in mind.
76//
77// ARCANE BITS OF PATH TRIVIA
78//
79// - A double leading slash is actually part of the POSIX standard. Systems
80// are allowed to treat // as an alternate root, as Windows does for UNC
81// (network share) paths. Most POSIX systems don't do anything special
82// with two leading slashes, but FilePath handles this case properly
83// in case it ever comes across such a system. FilePath needs this support
84// for Windows UNC paths, anyway.
85// References:
dougk2e9fe272015-02-13 11:47:52 +090086// The Open Group Base Specifications Issue 7, sections 3.267 ("Pathname")
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090087// and 4.12 ("Pathname Resolution"), available at:
dougk2e9fe272015-02-13 11:47:52 +090088// http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_267
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090089// http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_12
90//
91// - Windows treats c:\\ the same way it treats \\. This was intended to
92// allow older applications that require drive letters to support UNC paths
93// like \\server\share\path, by permitting c:\\server\share\path as an
94// equivalent. Since the OS treats these paths specially, FilePath needs
95// to do the same. Since Windows can use either / or \ as the separator,
96// FilePath treats c://, c:\\, //, and \\ all equivalently.
97// Reference:
98// The Old New Thing, "Why is a drive letter permitted in front of UNC
99// paths (sometimes)?", available at:
100// http://blogs.msdn.com/oldnewthing/archive/2005/11/22/495740.aspx
101
102#ifndef BASE_FILES_FILE_PATH_H_
103#define BASE_FILES_FILE_PATH_H_
104
105#include <stddef.h>
mgiuca36c0d632015-05-28 19:11:33 +0900106
107#include <iosfwd>
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900108#include <string>
109#include <vector>
110
111#include "base/base_export.h"
pkastingee4b1122014-11-21 10:37:22 +0900112#include "base/compiler_specific.h"
brettw@chromium.org30230a82013-06-12 02:52:44 +0900113#include "base/containers/hash_tables.h"
avi@chromium.org94bd5732013-06-11 22:36:37 +0900114#include "base/strings/string16.h"
brettwe1daa972015-06-16 14:52:47 +0900115#include "base/strings/string_piece.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900116#include "build/build_config.h"
117
118// Windows-style drive letter support and pathname separator characters can be
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
121// in the unit test.
122#if defined(OS_WIN)
123#define FILE_PATH_USES_DRIVE_LETTERS
124#define FILE_PATH_USES_WIN_SEPARATORS
125#endif // OS_WIN
126
brucedawsond7e5dfd2015-10-07 04:22:00 +0900127// To print path names portably use PRIsFP (based on PRIuS and friends from
128// C99 and format_macros.h) like this:
129// base::StringPrintf("Path is %" PRIsFP ".\n", path.value().c_str());
130#if defined(OS_POSIX)
131#define PRIsFP "s"
132#elif defined(OS_WIN)
133#define PRIsFP "ls"
134#endif // OS_WIN
135
brettwa4879472015-06-02 16:02:47 +0900136namespace base {
137
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900138class Pickle;
139class PickleIterator;
140
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900141// An abstraction to isolate users from the differences between native
142// pathnames on different platforms.
143class BASE_EXPORT FilePath {
144 public:
145#if defined(OS_POSIX)
146 // On most platforms, native pathnames are char arrays, and the encoding
147 // may or may not be specified. On Mac OS X, native pathnames are encoded
148 // in UTF-8.
149 typedef std::string StringType;
150#elif defined(OS_WIN)
151 // On Windows, for Unicode-aware applications, native pathnames are wchar_t
152 // arrays encoded in UTF-16.
153 typedef std::wstring StringType;
154#endif // OS_WIN
155
brettwe1daa972015-06-16 14:52:47 +0900156 typedef BasicStringPiece<StringType> StringPieceType;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900157 typedef StringType::value_type CharType;
158
159 // Null-terminated array of separators used to separate components in
160 // hierarchical paths. Each character in this array is a valid separator,
161 // but kSeparators[0] is treated as the canonical separator and will be used
162 // when composing pathnames.
163 static const CharType kSeparators[];
164
scottmg@chromium.orgdef6ed82013-05-21 18:44:02 +0900165 // arraysize(kSeparators).
166 static const size_t kSeparatorsLength;
167
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900168 // A special path component meaning "this directory."
169 static const CharType kCurrentDirectory[];
170
171 // A special path component meaning "the parent directory."
172 static const CharType kParentDirectory[];
173
174 // The character used to identify a file extension.
175 static const CharType kExtensionSeparator;
176
177 FilePath();
178 FilePath(const FilePath& that);
brettwe1daa972015-06-16 14:52:47 +0900179 explicit FilePath(StringPieceType path);
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900180 ~FilePath();
181 FilePath& operator=(const FilePath& that);
182
183 bool operator==(const FilePath& that) const;
184
185 bool operator!=(const FilePath& that) const;
186
187 // Required for some STL containers and operations
188 bool operator<(const FilePath& that) const {
189 return path_ < that.path_;
190 }
191
192 const StringType& value() const { return path_; }
193
194 bool empty() const { return path_.empty(); }
195
196 void clear() { path_.clear(); }
197
198 // Returns true if |character| is in kSeparators.
199 static bool IsSeparator(CharType character);
200
201 // Returns a vector of all of the components of the provided path. It is
202 // equivalent to calling DirName().value() on the path's root component,
203 // and BaseName().value() on each child component.
brettw@chromium.orgb80e1ef2014-02-20 04:11:17 +0900204 //
205 // To make sure this is lossless so we can differentiate absolute and
206 // relative paths, the root slash will be included even though no other
207 // slashes will be. The precise behavior is:
208 //
209 // Posix: "/foo/bar" -> [ "/", "foo", "bar" ]
210 // Windows: "C:\foo\bar" -> [ "C:", "\\", "foo", "bar" ]
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900211 void GetComponents(std::vector<FilePath::StringType>* components) const;
212
213 // Returns true if this FilePath is a strict parent of the |child|. Absolute
214 // and relative paths are accepted i.e. is /foo parent to /foo/bar and
215 // is foo parent to foo/bar. Does not convert paths to absolute, follow
216 // symlinks or directory navigation (e.g. ".."). A path is *NOT* its own
217 // parent.
218 bool IsParent(const FilePath& child) const;
219
220 // If IsParent(child) holds, appends to path (if non-NULL) the
221 // relative path to child and returns true. For example, if parent
222 // holds "/Users/johndoe/Library/Application Support", child holds
223 // "/Users/johndoe/Library/Application Support/Google/Chrome/Default", and
224 // *path holds "/Users/johndoe/Library/Caches", then after
225 // parent.AppendRelativePath(child, path) is called *path will hold
226 // "/Users/johndoe/Library/Caches/Google/Chrome/Default". Otherwise,
227 // returns false.
228 bool AppendRelativePath(const FilePath& child, FilePath* path) const;
229
230 // Returns a FilePath corresponding to the directory containing the path
231 // named by this object, stripping away the file component. If this object
232 // only contains one component, returns a FilePath identifying
233 // kCurrentDirectory. If this object already refers to the root directory,
234 // returns a FilePath identifying the root directory.
235 FilePath DirName() const WARN_UNUSED_RESULT;
236
237 // Returns a FilePath corresponding to the last path component of this
238 // object, either a file or a directory. If this object already refers to
239 // the root directory, returns a FilePath identifying the root directory;
240 // this is the only situation in which BaseName will return an absolute path.
241 FilePath BaseName() const WARN_UNUSED_RESULT;
242
243 // Returns ".jpg" for path "C:\pics\jojo.jpg", or an empty string if
244 // the file has no extension. If non-empty, Extension() will always start
245 // with precisely one ".". The following code should always work regardless
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900246 // of the value of path. For common double-extensions like .tar.gz and
247 // .user.js, this method returns the combined extension. For a single
248 // component, use FinalExtension().
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900249 // new_path = path.RemoveExtension().value().append(path.Extension());
250 // ASSERT(new_path == path.value());
251 // NOTE: this is different from the original file_util implementation which
252 // returned the extension without a leading "." ("jpg" instead of ".jpg")
scottmg624fe102015-01-21 04:15:54 +0900253 StringType Extension() const WARN_UNUSED_RESULT;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900254
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900255 // Returns the path's file extension, as in Extension(), but will
256 // never return a double extension.
257 //
258 // TODO(davidben): Check all our extension-sensitive code to see if
259 // we can rename this to Extension() and the other to something like
260 // LongExtension(), defaulting to short extensions and leaving the
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900261 // long "extensions" to logic like base::GetUniquePathNumber().
scottmg624fe102015-01-21 04:15:54 +0900262 StringType FinalExtension() const WARN_UNUSED_RESULT;
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900263
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900264 // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg"
265 // NOTE: this is slightly different from the similar file_util implementation
266 // which returned simply 'jojo'.
267 FilePath RemoveExtension() const WARN_UNUSED_RESULT;
268
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900269 // Removes the path's file extension, as in RemoveExtension(), but
270 // ignores double extensions.
271 FilePath RemoveFinalExtension() const WARN_UNUSED_RESULT;
272
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900273 // Inserts |suffix| after the file name portion of |path| but before the
274 // extension. Returns "" if BaseName() == "." or "..".
275 // Examples:
276 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg"
277 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg"
278 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)"
279 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)"
280 FilePath InsertBeforeExtension(
brettwe1daa972015-06-16 14:52:47 +0900281 StringPieceType suffix) const WARN_UNUSED_RESULT;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900282 FilePath InsertBeforeExtensionASCII(
brettwe1daa972015-06-16 14:52:47 +0900283 StringPiece suffix) const WARN_UNUSED_RESULT;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900284
285 // Adds |extension| to |file_name|. Returns the current FilePath if
286 // |extension| is empty. Returns "" if BaseName() == "." or "..".
brettwe1daa972015-06-16 14:52:47 +0900287 FilePath AddExtension(StringPieceType extension) const WARN_UNUSED_RESULT;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900288
289 // Replaces the extension of |file_name| with |extension|. If |file_name|
290 // does not have an extension, then |extension| is added. If |extension| is
291 // empty, then the extension is removed from |file_name|.
292 // Returns "" if BaseName() == "." or "..".
brettwe1daa972015-06-16 14:52:47 +0900293 FilePath ReplaceExtension(StringPieceType extension) const WARN_UNUSED_RESULT;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900294
295 // Returns true if the file path matches the specified extension. The test is
296 // case insensitive. Don't forget the leading period if appropriate.
brettwe1daa972015-06-16 14:52:47 +0900297 bool MatchesExtension(StringPieceType extension) const;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900298
299 // Returns a FilePath by appending a separator and the supplied path
300 // component to this object's path. Append takes care to avoid adding
301 // excessive separators if this object's path already ends with a separator.
302 // If this object's path is kCurrentDirectory, a new FilePath corresponding
303 // only to |component| is returned. |component| must be a relative path;
304 // it is an error to pass an absolute path.
brettwe1daa972015-06-16 14:52:47 +0900305 FilePath Append(StringPieceType component) const WARN_UNUSED_RESULT;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900306 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT;
307
308 // Although Windows StringType is std::wstring, since the encoding it uses for
309 // paths is well defined, it can handle ASCII path components as well.
310 // Mac uses UTF8, and since ASCII is a subset of that, it works there as well.
311 // On Linux, although it can use any 8-bit encoding for paths, we assume that
312 // ASCII is a valid subset, regardless of the encoding, since many operating
313 // system paths will always be ASCII.
brettwe1daa972015-06-16 14:52:47 +0900314 FilePath AppendASCII(StringPiece component) const WARN_UNUSED_RESULT;
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900315
316 // Returns true if this FilePath contains an absolute path. On Windows, an
317 // absolute path begins with either a drive letter specification followed by
318 // a separator character, or with two separator characters. On POSIX
319 // platforms, an absolute path begins with a separator character.
320 bool IsAbsolute() const;
321
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900322 // Returns true if the patch ends with a path separator character.
323 bool EndsWithSeparator() const WARN_UNUSED_RESULT;
324
325 // Returns a copy of this FilePath that ends with a trailing separator. If
326 // the input path is empty, an empty FilePath will be returned.
327 FilePath AsEndingWithSeparator() const WARN_UNUSED_RESULT;
328
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900329 // Returns a copy of this FilePath that does not end with a trailing
330 // separator.
331 FilePath StripTrailingSeparators() const WARN_UNUSED_RESULT;
332
tnagel@chromium.orge2352822014-05-02 01:52:07 +0900333 // Returns true if this FilePath contains an attempt to reference a parent
334 // directory (e.g. has a path component that is "..").
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900335 bool ReferencesParent() const;
336
337 // Return a Unicode human-readable version of this path.
338 // Warning: you can *not*, in general, go from a display name back to a real
339 // path. Only use this when displaying paths to users, not just when you
340 // want to stuff a string16 into some other API.
341 string16 LossyDisplayName() const;
342
343 // Return the path as ASCII, or the empty string if the path is not ASCII.
344 // This should only be used for cases where the FilePath is representing a
345 // known-ASCII filename.
346 std::string MaybeAsASCII() const;
347
348 // Return the path as UTF-8.
349 //
350 // This function is *unsafe* as there is no way to tell what encoding is
351 // used in file names on POSIX systems other than Mac and Chrome OS,
352 // although UTF-8 is practically used everywhere these days. To mitigate
353 // the encoding issue, this function internally calls
354 // SysNativeMBToWide() on POSIX systems other than Mac and Chrome OS,
355 // per assumption that the current locale's encoding is used in file
356 // names, but this isn't a perfect solution.
357 //
358 // Once it becomes safe to to stop caring about non-UTF-8 file names,
359 // the SysNativeMBToWide() hack will be removed from the code, along
360 // with "Unsafe" in the function name.
361 std::string AsUTF8Unsafe() const;
362
darin@chromium.org1aead732013-06-21 06:29:54 +0900363 // Similar to AsUTF8Unsafe, but returns UTF-16 instead.
364 string16 AsUTF16Unsafe() const;
365
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900366 // Returns a FilePath object from a path name in UTF-8. This function
367 // should only be used for cases where you are sure that the input
368 // string is UTF-8.
369 //
370 // Like AsUTF8Unsafe(), this function is unsafe. This function
371 // internally calls SysWideToNativeMB() on POSIX systems other than Mac
372 // and Chrome OS, to mitigate the encoding issue. See the comment at
373 // AsUTF8Unsafe() for details.
374 static FilePath FromUTF8Unsafe(const std::string& utf8);
375
darin@chromium.org1aead732013-06-21 06:29:54 +0900376 // Similar to FromUTF8Unsafe, but accepts UTF-16 instead.
377 static FilePath FromUTF16Unsafe(const string16& utf16);
378
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900379 void WriteToPickle(Pickle* pickle) const;
380 bool ReadFromPickle(PickleIterator* iter);
381
382 // Normalize all path separators to backslash on Windows
383 // (if FILE_PATH_USES_WIN_SEPARATORS is true), or do nothing on POSIX systems.
384 FilePath NormalizePathSeparators() const;
385
scottmg@chromium.orgb58eb012014-04-03 16:45:42 +0900386 // Normalize all path separattors to given type on Windows
387 // (if FILE_PATH_USES_WIN_SEPARATORS is true), or do nothing on POSIX systems.
388 FilePath NormalizePathSeparatorsTo(CharType separator) const;
389
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900390 // Compare two strings in the same way the file system does.
391 // Note that these always ignore case, even on file systems that are case-
392 // sensitive. If case-sensitive comparison is ever needed, add corresponding
393 // methods here.
394 // The methods are written as a static method so that they can also be used
395 // on parts of a file path, e.g., just the extension.
396 // CompareIgnoreCase() returns -1, 0 or 1 for less-than, equal-to and
397 // greater-than respectively.
brettwe1daa972015-06-16 14:52:47 +0900398 static int CompareIgnoreCase(StringPieceType string1,
399 StringPieceType string2);
400 static bool CompareEqualIgnoreCase(StringPieceType string1,
401 StringPieceType string2) {
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900402 return CompareIgnoreCase(string1, string2) == 0;
403 }
brettwe1daa972015-06-16 14:52:47 +0900404 static bool CompareLessIgnoreCase(StringPieceType string1,
405 StringPieceType string2) {
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900406 return CompareIgnoreCase(string1, string2) < 0;
407 }
408
409#if defined(OS_MACOSX)
410 // Returns the string in the special canonical decomposed form as defined for
411 // HFS, which is close to, but not quite, decomposition form D. See
412 // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubtleties
413 // for further comments.
414 // Returns the epmty string if the conversion failed.
brettwe1daa972015-06-16 14:52:47 +0900415 static StringType GetHFSDecomposedForm(StringPieceType string);
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900416
417 // Special UTF-8 version of FastUnicodeCompare. Cf:
418 // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
419 // IMPORTANT: The input strings must be in the special HFS decomposed form!
420 // (cf. above GetHFSDecomposedForm method)
brettwe1daa972015-06-16 14:52:47 +0900421 static int HFSFastUnicodeCompare(StringPieceType string1,
422 StringPieceType string2);
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900423#endif
424
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +0900425#if defined(OS_ANDROID)
426 // On android, file selection dialog can return a file with content uri
427 // scheme(starting with content://). Content uri needs to be opened with
428 // ContentResolver to guarantee that the app has appropriate permissions
429 // to access it.
430 // Returns true if the path is a content uri, or false otherwise.
431 bool IsContentUri() const;
432#endif
433
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900434 private:
435 // Remove trailing separators from this object. If the path is absolute, it
436 // will never be stripped any more than to refer to the absolute root
437 // directory, so "////" will become "/", not "". A leading pair of
438 // separators is never stripped, to support alternate roots. This is used to
439 // support UNC paths on Windows.
440 void StripTrailingSeparatorsInternal();
441
442 StringType path_;
443};
444
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900445// This is required by googletest to print a readable output on test failures.
mgiuca36c0d632015-05-28 19:11:33 +0900446// This is declared here for use in gtest-based unit tests but is defined in
447// the test_support_base target. Depend on that to use this in your unit test.
448// This should not be used in production code - call ToString() instead.
449void PrintTo(const FilePath& path, std::ostream* out);
450
451} // namespace base
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900452
453// Macros for string literal initialization of FilePath::CharType[], and for
454// using a FilePath::CharType[] in a printf-style format string.
455#if defined(OS_POSIX)
456#define FILE_PATH_LITERAL(x) x
457#define PRFilePath "s"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900458#elif defined(OS_WIN)
459#define FILE_PATH_LITERAL(x) L ## x
460#define PRFilePath "ls"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900461#endif // OS_WIN
462
463// Provide a hash function so that hash_sets and maps can contain FilePath
464// objects.
465namespace BASE_HASH_NAMESPACE {
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900466
467template<>
468struct hash<base::FilePath> {
469 size_t operator()(const base::FilePath& f) const {
470 return hash<base::FilePath::StringType>()(f.value());
471 }
472};
473
brettw@chromium.org59eef1f2013-02-24 14:40:52 +0900474} // namespace BASE_HASH_NAMESPACE
475
476#endif // BASE_FILES_FILE_PATH_H_