Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // FilePath is a container for pathnames stored in a platform's native string |
michael@0 | 6 | // type, providing containers for manipulation in according with the |
michael@0 | 7 | // platform's conventions for pathnames. It supports the following path |
michael@0 | 8 | // types: |
michael@0 | 9 | // |
michael@0 | 10 | // POSIX Windows |
michael@0 | 11 | // --------------- ---------------------------------- |
michael@0 | 12 | // Fundamental type char[] wchar_t[] |
michael@0 | 13 | // Encoding unspecified* UTF-16 |
michael@0 | 14 | // Separator / \, tolerant of / |
michael@0 | 15 | // Drive letters no case-insensitive A-Z followed by : |
michael@0 | 16 | // Alternate root // (surprise!) \\, for UNC paths |
michael@0 | 17 | // |
michael@0 | 18 | // * The encoding need not be specified on POSIX systems, although some |
michael@0 | 19 | // POSIX-compliant systems do specify an encoding. Mac OS X uses UTF-8. |
michael@0 | 20 | // Linux does not specify an encoding, but in practice, the locale's |
michael@0 | 21 | // character set may be used. |
michael@0 | 22 | // |
michael@0 | 23 | // FilePath objects are intended to be used anywhere paths are. An |
michael@0 | 24 | // application may pass FilePath objects around internally, masking the |
michael@0 | 25 | // underlying differences between systems, only differing in implementation |
michael@0 | 26 | // where interfacing directly with the system. For example, a single |
michael@0 | 27 | // OpenFile(const FilePath &) function may be made available, allowing all |
michael@0 | 28 | // callers to operate without regard to the underlying implementation. On |
michael@0 | 29 | // POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might |
michael@0 | 30 | // wrap _wfopen_s, perhaps both by calling file_path.value().c_str(). This |
michael@0 | 31 | // allows each platform to pass pathnames around without requiring conversions |
michael@0 | 32 | // between encodings, which has an impact on performance, but more imporantly, |
michael@0 | 33 | // has an impact on correctness on platforms that do not have well-defined |
michael@0 | 34 | // encodings for pathnames. |
michael@0 | 35 | // |
michael@0 | 36 | // Several methods are available to perform common operations on a FilePath |
michael@0 | 37 | // object, such as determining the parent directory (DirName), isolating the |
michael@0 | 38 | // final path component (BaseName), and appending a relative pathname string |
michael@0 | 39 | // to an existing FilePath object (Append). These methods are highly |
michael@0 | 40 | // recommended over attempting to split and concatenate strings directly. |
michael@0 | 41 | // These methods are based purely on string manipulation and knowledge of |
michael@0 | 42 | // platform-specific pathname conventions, and do not consult the filesystem |
michael@0 | 43 | // at all, making them safe to use without fear of blocking on I/O operations. |
michael@0 | 44 | // These methods do not function as mutators but instead return distinct |
michael@0 | 45 | // instances of FilePath objects, and are therefore safe to use on const |
michael@0 | 46 | // objects. The objects themselves are safe to share between threads. |
michael@0 | 47 | // |
michael@0 | 48 | // To aid in initialization of FilePath objects from string literals, a |
michael@0 | 49 | // FILE_PATH_LITERAL macro is provided, which accounts for the difference |
michael@0 | 50 | // between char[]-based pathnames on POSIX systems and wchar_t[]-based |
michael@0 | 51 | // pathnames on Windows. |
michael@0 | 52 | // |
michael@0 | 53 | // Because a FilePath object should not be instantiated at the global scope, |
michael@0 | 54 | // instead, use a FilePath::CharType[] and initialize it with |
michael@0 | 55 | // FILE_PATH_LITERAL. At runtime, a FilePath object can be created from the |
michael@0 | 56 | // character array. Example: |
michael@0 | 57 | // |
michael@0 | 58 | // | const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("log.txt"); |
michael@0 | 59 | // | |
michael@0 | 60 | // | void Function() { |
michael@0 | 61 | // | FilePath log_file_path(kLogFileName); |
michael@0 | 62 | // | [...] |
michael@0 | 63 | // | } |
michael@0 | 64 | |
michael@0 | 65 | #ifndef BASE_FILE_PATH_H_ |
michael@0 | 66 | #define BASE_FILE_PATH_H_ |
michael@0 | 67 | |
michael@0 | 68 | #include <string> |
michael@0 | 69 | |
michael@0 | 70 | #include "base/basictypes.h" |
michael@0 | 71 | #include "base/compiler_specific.h" |
michael@0 | 72 | #include "base/hash_tables.h" |
michael@0 | 73 | |
michael@0 | 74 | // Windows-style drive letter support and pathname separator characters can be |
michael@0 | 75 | // enabled and disabled independently, to aid testing. These #defines are |
michael@0 | 76 | // here so that the same setting can be used in both the implementation and |
michael@0 | 77 | // in the unit test. |
michael@0 | 78 | #if defined(OS_WIN) |
michael@0 | 79 | #define FILE_PATH_USES_DRIVE_LETTERS |
michael@0 | 80 | #define FILE_PATH_USES_WIN_SEPARATORS |
michael@0 | 81 | #endif // OS_WIN |
michael@0 | 82 | |
michael@0 | 83 | // An abstraction to isolate users from the differences between native |
michael@0 | 84 | // pathnames on different platforms. |
michael@0 | 85 | class FilePath { |
michael@0 | 86 | public: |
michael@0 | 87 | #if defined(OS_POSIX) |
michael@0 | 88 | // On most platforms, native pathnames are char arrays, and the encoding |
michael@0 | 89 | // may or may not be specified. On Mac OS X, native pathnames are encoded |
michael@0 | 90 | // in UTF-8. |
michael@0 | 91 | typedef std::string StringType; |
michael@0 | 92 | #elif defined(OS_WIN) |
michael@0 | 93 | // On Windows, for Unicode-aware applications, native pathnames are wchar_t |
michael@0 | 94 | // arrays encoded in UTF-16. |
michael@0 | 95 | typedef std::wstring StringType; |
michael@0 | 96 | #endif // OS_WIN |
michael@0 | 97 | |
michael@0 | 98 | typedef StringType::value_type CharType; |
michael@0 | 99 | |
michael@0 | 100 | // Null-terminated array of separators used to separate components in |
michael@0 | 101 | // hierarchical paths. Each character in this array is a valid separator, |
michael@0 | 102 | // but kSeparators[0] is treated as the canonical separator and will be used |
michael@0 | 103 | // when composing pathnames. |
michael@0 | 104 | static const CharType kSeparators[]; |
michael@0 | 105 | |
michael@0 | 106 | // A special path component meaning "this directory." |
michael@0 | 107 | static const CharType kCurrentDirectory[]; |
michael@0 | 108 | |
michael@0 | 109 | // A special path component meaning "the parent directory." |
michael@0 | 110 | static const CharType kParentDirectory[]; |
michael@0 | 111 | |
michael@0 | 112 | // The character used to identify a file extension. |
michael@0 | 113 | static const CharType kExtensionSeparator; |
michael@0 | 114 | |
michael@0 | 115 | FilePath() {} |
michael@0 | 116 | FilePath(const FilePath& that) : path_(that.path_) {} |
michael@0 | 117 | explicit FilePath(const StringType& path) : path_(path) {} |
michael@0 | 118 | |
michael@0 | 119 | FilePath& operator=(const FilePath& that) { |
michael@0 | 120 | path_ = that.path_; |
michael@0 | 121 | return *this; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | bool operator==(const FilePath& that) const { |
michael@0 | 125 | return path_ == that.path_; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | bool operator!=(const FilePath& that) const { |
michael@0 | 129 | return path_ != that.path_; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // Required for some STL containers and operations |
michael@0 | 133 | bool operator<(const FilePath& that) const { |
michael@0 | 134 | return path_ < that.path_; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | const StringType& value() const { return path_; } |
michael@0 | 138 | |
michael@0 | 139 | bool empty() const { return path_.empty(); } |
michael@0 | 140 | |
michael@0 | 141 | // Returns true if |character| is in kSeparators. |
michael@0 | 142 | static bool IsSeparator(CharType character); |
michael@0 | 143 | |
michael@0 | 144 | // Returns a FilePath corresponding to the directory containing the path |
michael@0 | 145 | // named by this object, stripping away the file component. If this object |
michael@0 | 146 | // only contains one component, returns a FilePath identifying |
michael@0 | 147 | // kCurrentDirectory. If this object already refers to the root directory, |
michael@0 | 148 | // returns a FilePath identifying the root directory. |
michael@0 | 149 | FilePath DirName() const; |
michael@0 | 150 | |
michael@0 | 151 | // Returns a FilePath corresponding to the last path component of this |
michael@0 | 152 | // object, either a file or a directory. If this object already refers to |
michael@0 | 153 | // the root directory, returns a FilePath identifying the root directory; |
michael@0 | 154 | // this is the only situation in which BaseName will return an absolute path. |
michael@0 | 155 | FilePath BaseName() const; |
michael@0 | 156 | |
michael@0 | 157 | // Returns ".jpg" for path "C:\pics\jojo.jpg", or an empty string if |
michael@0 | 158 | // the file has no extension. If non-empty, Extension() will always start |
michael@0 | 159 | // with precisely one ".". The following code should always work regardless |
michael@0 | 160 | // of the value of path. |
michael@0 | 161 | // new_path = path.RemoveExtension().value().append(path.Extension()); |
michael@0 | 162 | // ASSERT(new_path == path.value()); |
michael@0 | 163 | // NOTE: this is different from the original file_util implementation which |
michael@0 | 164 | // returned the extension without a leading "." ("jpg" instead of ".jpg") |
michael@0 | 165 | StringType Extension() const; |
michael@0 | 166 | |
michael@0 | 167 | // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg" |
michael@0 | 168 | // NOTE: this is slightly different from the similar file_util implementation |
michael@0 | 169 | // which returned simply 'jojo'. |
michael@0 | 170 | FilePath RemoveExtension() const; |
michael@0 | 171 | |
michael@0 | 172 | // Inserts |suffix| after the file name portion of |path| but before the |
michael@0 | 173 | // extension. Returns "" if BaseName() == "." or "..". |
michael@0 | 174 | // Examples: |
michael@0 | 175 | // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" |
michael@0 | 176 | // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" |
michael@0 | 177 | // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" |
michael@0 | 178 | // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" |
michael@0 | 179 | FilePath InsertBeforeExtension(const StringType& suffix) const; |
michael@0 | 180 | |
michael@0 | 181 | // Replaces the extension of |file_name| with |extension|. If |file_name| |
michael@0 | 182 | // does not have an extension, them |extension| is added. If |extension| is |
michael@0 | 183 | // empty, then the extension is removed from |file_name|. |
michael@0 | 184 | // Returns "" if BaseName() == "." or "..". |
michael@0 | 185 | FilePath ReplaceExtension(const StringType& extension) const; |
michael@0 | 186 | |
michael@0 | 187 | // Returns a FilePath by appending a separator and the supplied path |
michael@0 | 188 | // component to this object's path. Append takes care to avoid adding |
michael@0 | 189 | // excessive separators if this object's path already ends with a separator. |
michael@0 | 190 | // If this object's path is kCurrentDirectory, a new FilePath corresponding |
michael@0 | 191 | // only to |component| is returned. |component| must be a relative path; |
michael@0 | 192 | // it is an error to pass an absolute path. |
michael@0 | 193 | FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; |
michael@0 | 194 | FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; |
michael@0 | 195 | |
michael@0 | 196 | // Although Windows StringType is std::wstring, since the encoding it uses for |
michael@0 | 197 | // paths is well defined, it can handle ASCII path components as well. |
michael@0 | 198 | // Mac uses UTF8, and since ASCII is a subset of that, it works there as well. |
michael@0 | 199 | // On Linux, although it can use any 8-bit encoding for paths, we assume that |
michael@0 | 200 | // ASCII is a valid subset, regardless of the encoding, since many operating |
michael@0 | 201 | // system paths will always be ASCII. |
michael@0 | 202 | FilePath AppendASCII(const std::string& component) const WARN_UNUSED_RESULT; |
michael@0 | 203 | |
michael@0 | 204 | // Returns true if this FilePath contains an absolute path. On Windows, an |
michael@0 | 205 | // absolute path begins with either a drive letter specification followed by |
michael@0 | 206 | // a separator character, or with two separator characters. On POSIX |
michael@0 | 207 | // platforms, an absolute path begins with a separator character. |
michael@0 | 208 | bool IsAbsolute() const; |
michael@0 | 209 | |
michael@0 | 210 | // Returns a copy of this FilePath that does not end with a trailing |
michael@0 | 211 | // separator. |
michael@0 | 212 | FilePath StripTrailingSeparators() const; |
michael@0 | 213 | |
michael@0 | 214 | // Calls open on given ifstream instance |
michael@0 | 215 | void OpenInputStream(std::ifstream &stream) const; |
michael@0 | 216 | |
michael@0 | 217 | // Older Chromium code assumes that paths are always wstrings. |
michael@0 | 218 | // This function converts a wstring to a FilePath, and is useful to smooth |
michael@0 | 219 | // porting that old code to the FilePath API. |
michael@0 | 220 | // It has "Hack" in its name so people feel bad about using it. |
michael@0 | 221 | // TODO(port): remove these functions. |
michael@0 | 222 | static FilePath FromWStringHack(const std::wstring& wstring); |
michael@0 | 223 | |
michael@0 | 224 | // Older Chromium code assumes that paths are always wstrings. |
michael@0 | 225 | // This function produces a wstring from a FilePath, and is useful to smooth |
michael@0 | 226 | // porting that old code to the FilePath API. |
michael@0 | 227 | // It has "Hack" in its name so people feel bad about using it. |
michael@0 | 228 | // TODO(port): remove these functions. |
michael@0 | 229 | std::wstring ToWStringHack() const; |
michael@0 | 230 | |
michael@0 | 231 | private: |
michael@0 | 232 | // Remove trailing separators from this object. If the path is absolute, it |
michael@0 | 233 | // will never be stripped any more than to refer to the absolute root |
michael@0 | 234 | // directory, so "////" will become "/", not "". A leading pair of |
michael@0 | 235 | // separators is never stripped, to support alternate roots. This is used to |
michael@0 | 236 | // support UNC paths on Windows. |
michael@0 | 237 | void StripTrailingSeparatorsInternal(); |
michael@0 | 238 | |
michael@0 | 239 | StringType path_; |
michael@0 | 240 | }; |
michael@0 | 241 | |
michael@0 | 242 | // Macros for string literal initialization of FilePath::CharType[]. |
michael@0 | 243 | #if defined(OS_POSIX) |
michael@0 | 244 | #define FILE_PATH_LITERAL(x) x |
michael@0 | 245 | #elif defined(OS_WIN) |
michael@0 | 246 | #define FILE_PATH_LITERAL(x) L ## x |
michael@0 | 247 | #endif // OS_WIN |
michael@0 | 248 | |
michael@0 | 249 | // Implement hash function so that we can use FilePaths in hashsets and maps. |
michael@0 | 250 | #if defined(COMPILER_GCC) && !defined(ANDROID) |
michael@0 | 251 | namespace __gnu_cxx { |
michael@0 | 252 | |
michael@0 | 253 | template<> |
michael@0 | 254 | struct hash<FilePath> { |
michael@0 | 255 | size_t operator()(const FilePath& f) const { |
michael@0 | 256 | return hash<FilePath::StringType>()(f.value()); |
michael@0 | 257 | } |
michael@0 | 258 | }; |
michael@0 | 259 | |
michael@0 | 260 | } // namespace __gnu_cxx |
michael@0 | 261 | #elif defined(COMPILER_MSVC) |
michael@0 | 262 | namespace stdext { |
michael@0 | 263 | |
michael@0 | 264 | inline size_t hash_value(const FilePath& f) { |
michael@0 | 265 | return hash_value(f.value()); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | } // namespace stdext |
michael@0 | 269 | #endif // COMPILER |
michael@0 | 270 | |
michael@0 | 271 | #endif // BASE_FILE_PATH_H_ |