file_path.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FilePath is a container for pathnames stored in a platform's native string
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// type, providing containers for manipulation in according with the
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platform's conventions for pathnames.  It supports the following path
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// types:
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                   POSIX            Windows
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                   ---------------  ----------------------------------
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Fundamental type  char[]           wchar_t[]
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Encoding          unspecified*     UTF-16
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Separator         /                \, tolerant of /
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Drive letters     no               case-insensitive A-Z followed by :
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Alternate root    // (surprise!)   \\, for UNC paths
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * The encoding need not be specified on POSIX systems, although some
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   POSIX-compliant systems do specify an encoding.  Mac OS X uses UTF-8.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   Chrome OS also uses UTF-8.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   Linux does not specify an encoding, but in practice, the locale's
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   character set may be used.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For more arcane bits of path trivia, see below.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FilePath objects are intended to be used anywhere paths are.  An
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// application may pass FilePath objects around internally, masking the
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// underlying differences between systems, only differing in implementation
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// where interfacing directly with the system.  For example, a single
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OpenFile(const FilePath &) function may be made available, allowing all
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// callers to operate without regard to the underlying implementation.  On
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// wrap _wfopen_s, perhaps both by calling file_path.value().c_str().  This
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// allows each platform to pass pathnames around without requiring conversions
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// between encodings, which has an impact on performance, but more imporantly,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// has an impact on correctness on platforms that do not have well-defined
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// encodings for pathnames.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Several methods are available to perform common operations on a FilePath
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// object, such as determining the parent directory (DirName), isolating the
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// final path component (BaseName), and appending a relative pathname string
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to an existing FilePath object (Append).  These methods are highly
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// recommended over attempting to split and concatenate strings directly.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These methods are based purely on string manipulation and knowledge of
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platform-specific pathname conventions, and do not consult the filesystem
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// at all, making them safe to use without fear of blocking on I/O operations.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These methods do not function as mutators but instead return distinct
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// instances of FilePath objects, and are therefore safe to use on const
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// objects.  The objects themselves are safe to share between threads.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// To aid in initialization of FilePath objects from string literals, a
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FILE_PATH_LITERAL macro is provided, which accounts for the difference
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// between char[]-based pathnames on POSIX systems and wchar_t[]-based
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// pathnames on Windows.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Paths can't contain NULs as a precaution agaist premature truncation.
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Because a FilePath object should not be instantiated at the global scope,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// instead, use a FilePath::CharType[] and initialize it with
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FILE_PATH_LITERAL.  At runtime, a FilePath object can be created from the
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// character array.  Example:
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// | const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("log.txt");
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// | void Function() {
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |   FilePath log_file_path(kLogFileName);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |   [...]
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// | }
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WARNING: FilePaths should ALWAYS be displayed with LTR directionality, even
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// when the UI language is RTL. This means you always need to pass filepaths
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// through base::i18n::WrapPathWithLTRFormatting() before displaying it in the
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// RTL UI.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is a very common source of bugs, please try to keep this in mind.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ARCANE BITS OF PATH TRIVIA
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  - A double leading slash is actually part of the POSIX standard.  Systems
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    are allowed to treat // as an alternate root, as Windows does for UNC
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    (network share) paths.  Most POSIX systems don't do anything special
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    with two leading slashes, but FilePath handles this case properly
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    in case it ever comes across such a system.  FilePath needs this support
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    for Windows UNC paths, anyway.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    References:
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    The Open Group Base Specifications Issue 7, sections 3.266 ("Pathname")
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    and 4.12 ("Pathname Resolution"), available at:
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_266
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_12
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  - Windows treats c:\\ the same way it treats \\.  This was intended to
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    allow older applications that require drive letters to support UNC paths
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    like \\server\share\path, by permitting c:\\server\share\path as an
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    equivalent.  Since the OS treats these paths specially, FilePath needs
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    to do the same.  Since Windows can use either / or \ as the separator,
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    FilePath treats c://, c:\\, //, and \\ all equivalently.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    Reference:
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    The Old New Thing, "Why is a drive letter permitted in front of UNC
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    paths (sometimes)?", available at:
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    http://blogs.msdn.com/oldnewthing/archive/2005/11/22/495740.aspx
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef BASE_FILES_FILE_PATH_H_
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define BASE_FILES_FILE_PATH_H_
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stddef.h>
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h"
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h"
1125e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string16.h"
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h"  // For implicit conversions.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Windows-style drive letter support and pathname separator characters can be
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// enabled and disabled independently, to aid testing.  These #defines are
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// here so that the same setting can be used in both the implementation and
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the unit test.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FILE_PATH_USES_DRIVE_LETTERS
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FILE_PATH_USES_WIN_SEPARATORS
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_WIN
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Pickle;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PickleIterator;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// An abstraction to isolate users from the differences between native
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// pathnames on different platforms.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT FilePath {
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On most platforms, native pathnames are char arrays, and the encoding
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // may or may not be specified.  On Mac OS X, native pathnames are encoded
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in UTF-8.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::string StringType;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_WIN)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On Windows, for Unicode-aware applications, native pathnames are wchar_t
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // arrays encoded in UTF-16.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::wstring StringType;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_WIN
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef StringType::value_type CharType;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Null-terminated array of separators used to separate components in
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // hierarchical paths.  Each character in this array is a valid separator,
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // but kSeparators[0] is treated as the canonical separator and will be used
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when composing pathnames.
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const CharType kSeparators[];
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // arraysize(kSeparators).
15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  static const size_t kSeparatorsLength;
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A special path component meaning "this directory."
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const CharType kCurrentDirectory[];
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A special path component meaning "the parent directory."
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const CharType kParentDirectory[];
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The character used to identify a file extension.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const CharType kExtensionSeparator;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath();
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath(const FilePath& that);
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FilePath(const StringType& path);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~FilePath();
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath& operator=(const FilePath& that);
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator==(const FilePath& that) const;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator!=(const FilePath& that) const;
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Required for some STL containers and operations
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<(const FilePath& that) const {
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return path_ < that.path_;
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const StringType& value() const { return path_; }
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool empty() const { return path_.empty(); }
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void clear() { path_.clear(); }
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if |character| is in kSeparators.
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool IsSeparator(CharType character);
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a vector of all of the components of the provided path. It is
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // equivalent to calling DirName().value() on the path's root component,
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and BaseName().value() on each child component.
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // To make sure this is lossless so we can differentiate absolute and
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // relative paths, the root slash will be included even though no other
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // slashes will be. The precise behavior is:
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Posix:  "/foo/bar"  ->  [ "/", "foo", "bar" ]
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Windows:  "C:\foo\bar"  ->  [ "C:", "\\", "foo", "bar" ]
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetComponents(std::vector<FilePath::StringType>* components) const;
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this FilePath is a strict parent of the |child|. Absolute
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and relative paths are accepted i.e. is /foo parent to /foo/bar and
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is foo parent to foo/bar. Does not convert paths to absolute, follow
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // symlinks or directory navigation (e.g. ".."). A path is *NOT* its own
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parent.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsParent(const FilePath& child) const;
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If IsParent(child) holds, appends to path (if non-NULL) the
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // relative path to child and returns true.  For example, if parent
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // holds "/Users/johndoe/Library/Application Support", child holds
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "/Users/johndoe/Library/Application Support/Google/Chrome/Default", and
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // *path holds "/Users/johndoe/Library/Caches", then after
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parent.AppendRelativePath(child, path) is called *path will hold
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "/Users/johndoe/Library/Caches/Google/Chrome/Default".  Otherwise,
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns false.
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AppendRelativePath(const FilePath& child, FilePath* path) const;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a FilePath corresponding to the directory containing the path
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // named by this object, stripping away the file component.  If this object
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only contains one component, returns a FilePath identifying
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kCurrentDirectory.  If this object already refers to the root directory,
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns a FilePath identifying the root directory.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath DirName() const WARN_UNUSED_RESULT;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a FilePath corresponding to the last path component of this
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object, either a file or a directory.  If this object already refers to
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the root directory, returns a FilePath identifying the root directory;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this is the only situation in which BaseName will return an absolute path.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath BaseName() const WARN_UNUSED_RESULT;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns ".jpg" for path "C:\pics\jojo.jpg", or an empty string if
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the file has no extension.  If non-empty, Extension() will always start
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with precisely one ".".  The following code should always work regardless
234a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // of the value of path.  For common double-extensions like .tar.gz and
235a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // .user.js, this method returns the combined extension.  For a single
236a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // component, use FinalExtension().
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // new_path = path.RemoveExtension().value().append(path.Extension());
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ASSERT(new_path == path.value());
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: this is different from the original file_util implementation which
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned the extension without a leading "." ("jpg" instead of ".jpg")
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StringType Extension() const;
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
243a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Returns the path's file extension, as in Extension(), but will
244a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // never return a double extension.
245a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  //
246a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // TODO(davidben): Check all our extension-sensitive code to see if
247a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // we can rename this to Extension() and the other to something like
248a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // LongExtension(), defaulting to short extensions and leaving the
249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // long "extensions" to logic like base::GetUniquePathNumber().
250a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  StringType FinalExtension() const;
251a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg"
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: this is slightly different from the similar file_util implementation
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which returned simply 'jojo'.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath RemoveExtension() const WARN_UNUSED_RESULT;
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
257a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Removes the path's file extension, as in RemoveExtension(), but
258a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // ignores double extensions.
259a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  FilePath RemoveFinalExtension() const WARN_UNUSED_RESULT;
260a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts |suffix| after the file name portion of |path| but before the
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // extension.  Returns "" if BaseName() == "." or "..".
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Examples:
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg"
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path == "jojo.jpg"         suffix == " (1)", returns "jojo (1).jpg"
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path == "C:\pics\jojo"     suffix == " (1)", returns "C:\pics\jojo (1)"
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)"
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath InsertBeforeExtension(
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const StringType& suffix) const WARN_UNUSED_RESULT;
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath InsertBeforeExtensionASCII(
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const base::StringPiece& suffix) const WARN_UNUSED_RESULT;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds |extension| to |file_name|. Returns the current FilePath if
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |extension| is empty. Returns "" if BaseName() == "." or "..".
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath AddExtension(
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const StringType& extension) const WARN_UNUSED_RESULT;
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Replaces the extension of |file_name| with |extension|.  If |file_name|
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // does not have an extension, then |extension| is added.  If |extension| is
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // empty, then the extension is removed from |file_name|.
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns "" if BaseName() == "." or "..".
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath ReplaceExtension(
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const StringType& extension) const WARN_UNUSED_RESULT;
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the file path matches the specified extension. The test is
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // case insensitive. Don't forget the leading period if appropriate.
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool MatchesExtension(const StringType& extension) const;
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a FilePath by appending a separator and the supplied path
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // component to this object's path.  Append takes care to avoid adding
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // excessive separators if this object's path already ends with a separator.
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this object's path is kCurrentDirectory, a new FilePath corresponding
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only to |component| is returned.  |component| must be a relative path;
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it is an error to pass an absolute path.
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath Append(const StringType& component) const WARN_UNUSED_RESULT;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Although Windows StringType is std::wstring, since the encoding it uses for
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // paths is well defined, it can handle ASCII path components as well.
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mac uses UTF8, and since ASCII is a subset of that, it works there as well.
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On Linux, although it can use any 8-bit encoding for paths, we assume that
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ASCII is a valid subset, regardless of the encoding, since many operating
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // system paths will always be ASCII.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath AppendASCII(const base::StringPiece& component)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const WARN_UNUSED_RESULT;
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this FilePath contains an absolute path.  On Windows, an
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // absolute path begins with either a drive letter specification followed by
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a separator character, or with two separator characters.  On POSIX
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // platforms, an absolute path begins with a separator character.
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsAbsolute() const;
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns true if the patch ends with a path separator character.
314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool EndsWithSeparator() const WARN_UNUSED_RESULT;
315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns a copy of this FilePath that ends with a trailing separator. If
317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the input path is empty, an empty FilePath will be returned.
318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  FilePath AsEndingWithSeparator() const WARN_UNUSED_RESULT;
319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a copy of this FilePath that does not end with a trailing
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // separator.
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath StripTrailingSeparators() const WARN_UNUSED_RESULT;
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this FilePath contains any attempt to reference a parent
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // directory (i.e. has a path component that is ".."
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ReferencesParent() const;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return a Unicode human-readable version of this path.
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Warning: you can *not*, in general, go from a display name back to a real
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path.  Only use this when displaying paths to users, not just when you
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // want to stuff a string16 into some other API.
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string16 LossyDisplayName() const;
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the path as ASCII, or the empty string if the path is not ASCII.
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should only be used for cases where the FilePath is representing a
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // known-ASCII filename.
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string MaybeAsASCII() const;
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the path as UTF-8.
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function is *unsafe* as there is no way to tell what encoding is
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // used in file names on POSIX systems other than Mac and Chrome OS,
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // although UTF-8 is practically used everywhere these days. To mitigate
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the encoding issue, this function internally calls
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SysNativeMBToWide() on POSIX systems other than Mac and Chrome OS,
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // per assumption that the current locale's encoding is used in file
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // names, but this isn't a perfect solution.
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Once it becomes safe to to stop caring about non-UTF-8 file names,
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the SysNativeMBToWide() hack will be removed from the code, along
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with "Unsafe" in the function name.
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string AsUTF8Unsafe() const;
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
354eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Similar to AsUTF8Unsafe, but returns UTF-16 instead.
355eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  string16 AsUTF16Unsafe() const;
356eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a FilePath object from a path name in UTF-8. This function
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should only be used for cases where you are sure that the input
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // string is UTF-8.
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like AsUTF8Unsafe(), this function is unsafe. This function
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // internally calls SysWideToNativeMB() on POSIX systems other than Mac
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and Chrome OS, to mitigate the encoding issue. See the comment at
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AsUTF8Unsafe() for details.
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static FilePath FromUTF8Unsafe(const std::string& utf8);
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
367eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Similar to FromUTF8Unsafe, but accepts UTF-16 instead.
368eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  static FilePath FromUTF16Unsafe(const string16& utf16);
369eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void WriteToPickle(Pickle* pickle) const;
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ReadFromPickle(PickleIterator* iter);
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Normalize all path separators to backslash on Windows
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (if FILE_PATH_USES_WIN_SEPARATORS is true), or do nothing on POSIX systems.
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath NormalizePathSeparators() const;
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compare two strings in the same way the file system does.
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that these always ignore case, even on file systems that are case-
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sensitive. If case-sensitive comparison is ever needed, add corresponding
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // methods here.
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The methods are written as a static method so that they can also be used
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on parts of a file path, e.g., just the extension.
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // CompareIgnoreCase() returns -1, 0 or 1 for less-than, equal-to and
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // greater-than respectively.
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int CompareIgnoreCase(const StringType& string1,
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const StringType& string2);
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool CompareEqualIgnoreCase(const StringType& string1,
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const StringType& string2) {
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return CompareIgnoreCase(string1, string2) == 0;
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool CompareLessIgnoreCase(const StringType& string1,
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    const StringType& string2) {
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return CompareIgnoreCase(string1, string2) < 0;
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the string in the special canonical decomposed form as defined for
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HFS, which is close to, but not quite, decomposition form D. See
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubtleties
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for further comments.
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the epmty string if the conversion failed.
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static StringType GetHFSDecomposedForm(const FilePath::StringType& string);
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Special UTF-8 version of FastUnicodeCompare. Cf:
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IMPORTANT: The input strings must be in the special HFS decomposed form!
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (cf. above GetHFSDecomposedForm method)
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int HFSFastUnicodeCompare(const StringType& string1,
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const StringType& string2);
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
412f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if defined(OS_ANDROID)
413f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // On android, file selection dialog can return a file with content uri
414f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // scheme(starting with content://). Content uri needs to be opened with
415f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // ContentResolver to guarantee that the app has appropriate permissions
416f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // to access it.
417f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if the path is a content uri, or false otherwise.
418f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool IsContentUri() const;
419f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif
420f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Remove trailing separators from this object.  If the path is absolute, it
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will never be stripped any more than to refer to the absolute root
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // directory, so "////" will become "/", not "".  A leading pair of
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // separators is never stripped, to support alternate roots.  This is used to
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // support UNC paths on Windows.
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StripTrailingSeparatorsInternal();
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StringType path_;
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace base
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is required by googletest to print a readable output on test failures.
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT extern void PrintTo(const base::FilePath& path, std::ostream* out);
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Macros for string literal initialization of FilePath::CharType[], and for
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// using a FilePath::CharType[] in a printf-style format string.
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FILE_PATH_LITERAL(x) x
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PRFilePath "s"
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PRFilePathLiteral "%s"
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_WIN)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FILE_PATH_LITERAL(x) L ## x
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PRFilePath "ls"
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PRFilePathLiteral L"%ls"
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_WIN
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Provide a hash function so that hash_sets and maps can contain FilePath
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// objects.
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace BASE_HASH_NAMESPACE {
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(COMPILER_GCC)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<>
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct hash<base::FilePath> {
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t operator()(const base::FilePath& f) const {
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return hash<base::FilePath::StringType>()(f.value());
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(COMPILER_MSVC)
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)inline size_t hash_value(const base::FilePath& f) {
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return hash_value(f.value());
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // COMPILER
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace BASE_HASH_NAMESPACE
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // BASE_FILES_FILE_PATH_H_
472