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