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