file_path.h revision 5e3f23d412006dc4db4e659864679f29341e113f
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"
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/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.
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetComponents(std::vector<FilePath::StringType>* components) const;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this FilePath is a strict parent of the |child|. Absolute
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and relative paths are accepted i.e. is /foo parent to /foo/bar and
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is foo parent to foo/bar. Does not convert paths to absolute, follow
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // symlinks or directory navigation (e.g. ".."). A path is *NOT* its own
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parent.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsParent(const FilePath& child) const;
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If IsParent(child) holds, appends to path (if non-NULL) the
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // relative path to child and returns true.  For example, if parent
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // holds "/Users/johndoe/Library/Application Support", child holds
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "/Users/johndoe/Library/Application Support/Google/Chrome/Default", and
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // *path holds "/Users/johndoe/Library/Caches", then after
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parent.AppendRelativePath(child, path) is called *path will hold
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "/Users/johndoe/Library/Caches/Google/Chrome/Default".  Otherwise,
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns false.
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AppendRelativePath(const FilePath& child, FilePath* path) const;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a FilePath corresponding to the directory containing the path
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // named by this object, stripping away the file component.  If this object
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only contains one component, returns a FilePath identifying
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kCurrentDirectory.  If this object already refers to the root directory,
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns a FilePath identifying the root directory.
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath DirName() const WARN_UNUSED_RESULT;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a FilePath corresponding to the last path component of this
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object, either a file or a directory.  If this object already refers to
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the root directory, returns a FilePath identifying the root directory;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this is the only situation in which BaseName will return an absolute path.
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath BaseName() const WARN_UNUSED_RESULT;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns ".jpg" for path "C:\pics\jojo.jpg", or an empty string if
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the file has no extension.  If non-empty, Extension() will always start
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with precisely one ".".  The following code should always work regardless
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of the value of path.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // new_path = path.RemoveExtension().value().append(path.Extension());
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ASSERT(new_path == path.value());
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: this is different from the original file_util implementation which
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned the extension without a leading "." ("jpg" instead of ".jpg")
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StringType Extension() const;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg"
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: this is slightly different from the similar file_util implementation
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which returned simply 'jojo'.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath RemoveExtension() const WARN_UNUSED_RESULT;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts |suffix| after the file name portion of |path| but before the
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // extension.  Returns "" if BaseName() == "." or "..".
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Examples:
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg"
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path == "jojo.jpg"         suffix == " (1)", returns "jojo (1).jpg"
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path == "C:\pics\jojo"     suffix == " (1)", returns "C:\pics\jojo (1)"
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)"
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath InsertBeforeExtension(
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const StringType& suffix) const WARN_UNUSED_RESULT;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath InsertBeforeExtensionASCII(
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const base::StringPiece& suffix) const WARN_UNUSED_RESULT;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds |extension| to |file_name|. Returns the current FilePath if
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |extension| is empty. Returns "" if BaseName() == "." or "..".
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath AddExtension(
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const StringType& extension) const WARN_UNUSED_RESULT;
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Replaces the extension of |file_name| with |extension|.  If |file_name|
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // does not have an extension, then |extension| is added.  If |extension| is
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // empty, then the extension is removed from |file_name|.
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns "" if BaseName() == "." or "..".
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath ReplaceExtension(
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const StringType& extension) const WARN_UNUSED_RESULT;
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the file path matches the specified extension. The test is
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // case insensitive. Don't forget the leading period if appropriate.
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool MatchesExtension(const StringType& extension) const;
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a FilePath by appending a separator and the supplied path
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // component to this object's path.  Append takes care to avoid adding
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // excessive separators if this object's path already ends with a separator.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this object's path is kCurrentDirectory, a new FilePath corresponding
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only to |component| is returned.  |component| must be a relative path;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it is an error to pass an absolute path.
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath Append(const StringType& component) const WARN_UNUSED_RESULT;
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT;
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Although Windows StringType is std::wstring, since the encoding it uses for
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // paths is well defined, it can handle ASCII path components as well.
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mac uses UTF8, and since ASCII is a subset of that, it works there as well.
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On Linux, although it can use any 8-bit encoding for paths, we assume that
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ASCII is a valid subset, regardless of the encoding, since many operating
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // system paths will always be ASCII.
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath AppendASCII(const base::StringPiece& component)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const WARN_UNUSED_RESULT;
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this FilePath contains an absolute path.  On Windows, an
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // absolute path begins with either a drive letter specification followed by
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a separator character, or with two separator characters.  On POSIX
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // platforms, an absolute path begins with a separator character.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsAbsolute() const;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns true if the patch ends with a path separator character.
292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool EndsWithSeparator() const WARN_UNUSED_RESULT;
293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns a copy of this FilePath that ends with a trailing separator. If
295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the input path is empty, an empty FilePath will be returned.
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  FilePath AsEndingWithSeparator() const WARN_UNUSED_RESULT;
297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a copy of this FilePath that does not end with a trailing
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // separator.
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath StripTrailingSeparators() const WARN_UNUSED_RESULT;
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this FilePath contains any attempt to reference a parent
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // directory (i.e. has a path component that is ".."
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ReferencesParent() const;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return a Unicode human-readable version of this path.
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Warning: you can *not*, in general, go from a display name back to a real
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path.  Only use this when displaying paths to users, not just when you
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // want to stuff a string16 into some other API.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string16 LossyDisplayName() const;
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the path as ASCII, or the empty string if the path is not ASCII.
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should only be used for cases where the FilePath is representing a
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // known-ASCII filename.
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string MaybeAsASCII() const;
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the path as UTF-8.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function is *unsafe* as there is no way to tell what encoding is
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // used in file names on POSIX systems other than Mac and Chrome OS,
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // although UTF-8 is practically used everywhere these days. To mitigate
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the encoding issue, this function internally calls
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SysNativeMBToWide() on POSIX systems other than Mac and Chrome OS,
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // per assumption that the current locale's encoding is used in file
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // names, but this isn't a perfect solution.
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Once it becomes safe to to stop caring about non-UTF-8 file names,
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the SysNativeMBToWide() hack will be removed from the code, along
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with "Unsafe" in the function name.
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string AsUTF8Unsafe() const;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Older Chromium code assumes that paths are always wstrings.
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function converts wstrings to FilePaths, and is
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // useful to smooth porting that old code to the FilePath API.
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It has "Hack" its name so people feel bad about using it.
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://code.google.com/p/chromium/issues/detail?id=24672
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If you are trying to be a good citizen and remove these, ask yourself:
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - Am I interacting with other Chrome code that deals with files?  Then
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   try to convert the API into using FilePath.
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - Am I interacting with OS-native calls?  Then use value() to get at an
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   OS-native string format.
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - Am I using well-known file names, like "config.ini"?  Then use the
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   ASCII functions (we require paths to always be supersets of ASCII).
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - Am I displaying a string to the user in some UI?  Then use the
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   LossyDisplayName() function, but keep in mind that you can't
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   ever use the result of that again as a path.
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static FilePath FromWStringHack(const std::wstring& wstring);
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a FilePath object from a path name in UTF-8. This function
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should only be used for cases where you are sure that the input
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // string is UTF-8.
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like AsUTF8Unsafe(), this function is unsafe. This function
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // internally calls SysWideToNativeMB() on POSIX systems other than Mac
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and Chrome OS, to mitigate the encoding issue. See the comment at
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AsUTF8Unsafe() for details.
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static FilePath FromUTF8Unsafe(const std::string& utf8);
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void WriteToPickle(Pickle* pickle) const;
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ReadFromPickle(PickleIterator* iter);
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Normalize all path separators to backslash on Windows
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (if FILE_PATH_USES_WIN_SEPARATORS is true), or do nothing on POSIX systems.
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FilePath NormalizePathSeparators() const;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compare two strings in the same way the file system does.
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that these always ignore case, even on file systems that are case-
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sensitive. If case-sensitive comparison is ever needed, add corresponding
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // methods here.
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The methods are written as a static method so that they can also be used
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on parts of a file path, e.g., just the extension.
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // CompareIgnoreCase() returns -1, 0 or 1 for less-than, equal-to and
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // greater-than respectively.
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int CompareIgnoreCase(const StringType& string1,
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const StringType& string2);
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool CompareEqualIgnoreCase(const StringType& string1,
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const StringType& string2) {
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return CompareIgnoreCase(string1, string2) == 0;
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool CompareLessIgnoreCase(const StringType& string1,
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    const StringType& string2) {
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return CompareIgnoreCase(string1, string2) < 0;
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the string in the special canonical decomposed form as defined for
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HFS, which is close to, but not quite, decomposition form D. See
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubtleties
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for further comments.
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the epmty string if the conversion failed.
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static StringType GetHFSDecomposedForm(const FilePath::StringType& string);
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Special UTF-8 version of FastUnicodeCompare. Cf:
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IMPORTANT: The input strings must be in the special HFS decomposed form!
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (cf. above GetHFSDecomposedForm method)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int HFSFastUnicodeCompare(const StringType& string1,
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const StringType& string2);
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Remove trailing separators from this object.  If the path is absolute, it
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will never be stripped any more than to refer to the absolute root
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // directory, so "////" will become "/", not "".  A leading pair of
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // separators is never stripped, to support alternate roots.  This is used to
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // support UNC paths on Windows.
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StripTrailingSeparatorsInternal();
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StringType path_;
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace base
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is required by googletest to print a readable output on test failures.
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT extern void PrintTo(const base::FilePath& path, std::ostream* out);
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Macros for string literal initialization of FilePath::CharType[], and for
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// using a FilePath::CharType[] in a printf-style format string.
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FILE_PATH_LITERAL(x) x
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PRFilePath "s"
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PRFilePathLiteral "%s"
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_WIN)
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FILE_PATH_LITERAL(x) L ## x
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PRFilePath "ls"
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PRFilePathLiteral L"%ls"
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_WIN
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Provide a hash function so that hash_sets and maps can contain FilePath
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// objects.
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace BASE_HASH_NAMESPACE {
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(COMPILER_GCC)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<>
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct hash<base::FilePath> {
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t operator()(const base::FilePath& f) const {
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return hash<base::FilePath::StringType>()(f.value());
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(COMPILER_MSVC)
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)inline size_t hash_value(const base::FilePath& f) {
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return hash_value(f.value());
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // COMPILER
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace BASE_HASH_NAMESPACE
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // BASE_FILES_FILE_PATH_H_
453