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