OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // FilePath is a container for pathnames stored in a platform's native string | 5 // FilePath is a container for pathnames stored in a platform's native string |
6 // type, providing containers for manipulation in according with the | 6 // type, providing containers for manipulation in according with the |
7 // platform's conventions for pathnames. It supports the following path | 7 // platform's conventions for pathnames. It supports the following path |
8 // types: | 8 // types: |
9 // | 9 // |
10 // POSIX Windows | 10 // POSIX Windows |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // | void Function() { | 60 // | void Function() { |
61 // | FilePath log_file_path(kLogFileName); | 61 // | FilePath log_file_path(kLogFileName); |
62 // | [...] | 62 // | [...] |
63 // | } | 63 // | } |
64 | 64 |
65 #ifndef BASE_FILE_PATH_H_ | 65 #ifndef BASE_FILE_PATH_H_ |
66 #define BASE_FILE_PATH_H_ | 66 #define BASE_FILE_PATH_H_ |
67 | 67 |
68 #include <string> | 68 #include <string> |
69 | 69 |
| 70 #include "base/basictypes.h" |
70 #include "base/compiler_specific.h" | 71 #include "base/compiler_specific.h" |
71 #include "base/basictypes.h" | |
72 | 72 |
73 // Windows-style drive letter support and pathname separator characters can be | 73 // Windows-style drive letter support and pathname separator characters can be |
74 // enabled and disabled independently, to aid testing. These #defines are | 74 // enabled and disabled independently, to aid testing. These #defines are |
75 // here so that the same setting can be used in both the implementation and | 75 // here so that the same setting can be used in both the implementation and |
76 // in the unit test. | 76 // in the unit test. |
77 #if defined(OS_WIN) | 77 #if defined(OS_WIN) |
78 #define FILE_PATH_USES_DRIVE_LETTERS | 78 #define FILE_PATH_USES_DRIVE_LETTERS |
79 #define FILE_PATH_USES_WIN_SEPARATORS | 79 #define FILE_PATH_USES_WIN_SEPARATORS |
80 #endif // OS_WIN | 80 #endif // OS_WIN |
81 | 81 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 114 |
115 FilePath& operator=(const FilePath& that) { | 115 FilePath& operator=(const FilePath& that) { |
116 path_ = that.path_; | 116 path_ = that.path_; |
117 return *this; | 117 return *this; |
118 } | 118 } |
119 | 119 |
120 bool operator==(const FilePath& that) const { | 120 bool operator==(const FilePath& that) const { |
121 return path_ == that.path_; | 121 return path_ == that.path_; |
122 } | 122 } |
123 | 123 |
| 124 // Required for some STL containers and operations |
| 125 bool operator<(const FilePath& that) const { |
| 126 return path_ < that.path_; |
| 127 } |
| 128 |
124 const StringType& value() const { return path_; } | 129 const StringType& value() const { return path_; } |
125 | 130 |
126 // Returns true if |character| is in kSeparators. | 131 // Returns true if |character| is in kSeparators. |
127 static bool IsSeparator(CharType character); | 132 static bool IsSeparator(CharType character); |
128 | 133 |
129 // Returns a FilePath corresponding to the directory containing the path | 134 // Returns a FilePath corresponding to the directory containing the path |
130 // named by this object, stripping away the file component. If this object | 135 // named by this object, stripping away the file component. If this object |
131 // only contains one component, returns a FilePath identifying | 136 // only contains one component, returns a FilePath identifying |
132 // kCurrentDirectory. If this object already refers to the root directory, | 137 // kCurrentDirectory. If this object already refers to the root directory, |
133 // returns a FilePath identifying the root directory. | 138 // returns a FilePath identifying the root directory. |
(...skipping 13 matching lines...) Expand all Loading... |
147 // it is an error to pass an absolute path. | 152 // it is an error to pass an absolute path. |
148 FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; | 153 FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; |
149 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; | 154 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; |
150 | 155 |
151 // Returns true if this FilePath contains an absolute path. On Windows, an | 156 // Returns true if this FilePath contains an absolute path. On Windows, an |
152 // absolute path begins with either a drive letter specification followed by | 157 // absolute path begins with either a drive letter specification followed by |
153 // a separator character, or with two separator characters. On POSIX | 158 // a separator character, or with two separator characters. On POSIX |
154 // platforms, an absolute path begins with a separator character. | 159 // platforms, an absolute path begins with a separator character. |
155 bool IsAbsolute() const; | 160 bool IsAbsolute() const; |
156 | 161 |
| 162 // Returns a copy of this FilePath that does not end with a trailing |
| 163 // separator. |
| 164 FilePath StripTrailingSeparators() const; |
| 165 |
157 // Older Chromium code assumes that paths are always wstrings. | 166 // Older Chromium code assumes that paths are always wstrings. |
158 // This function converts a wstring to a FilePath, and is useful to smooth | 167 // This function converts a wstring to a FilePath, and is useful to smooth |
159 // porting that old code to the FilePath API. | 168 // porting that old code to the FilePath API. |
160 // It has "Hack" in its name so people feel bad about using it. | 169 // It has "Hack" in its name so people feel bad about using it. |
161 // TODO(port): remove these functions. | 170 // TODO(port): remove these functions. |
162 static FilePath FromWStringHack(const std::wstring& wstring); | 171 static FilePath FromWStringHack(const std::wstring& wstring); |
163 | 172 |
164 // Older Chromium code assumes that paths are always wstrings. | 173 // Older Chromium code assumes that paths are always wstrings. |
165 // This function produces a wstring from a FilePath, and is useful to smooth | 174 // This function produces a wstring from a FilePath, and is useful to smooth |
166 // porting that old code to the FilePath API. | 175 // porting that old code to the FilePath API. |
167 // It has "Hack" in its name so people feel bad about using it. | 176 // It has "Hack" in its name so people feel bad about using it. |
168 // TODO(port): remove these functions. | 177 // TODO(port): remove these functions. |
169 std::wstring ToWStringHack() const; | 178 std::wstring ToWStringHack() const; |
170 | 179 |
171 private: | 180 private: |
172 // Remove trailing separators from this object. If the path is absolute, it | 181 // Remove trailing separators from this object. If the path is absolute, it |
173 // will never be stripped any more than to refer to the absolute root | 182 // will never be stripped any more than to refer to the absolute root |
174 // directory, so "////" will become "/", not "". A leading pair of | 183 // directory, so "////" will become "/", not "". A leading pair of |
175 // separators is never stripped, to support alternate roots. This is used to | 184 // separators is never stripped, to support alternate roots. This is used to |
176 // support UNC paths on Windows. | 185 // support UNC paths on Windows. |
177 void StripTrailingSeparators(); | 186 void StripTrailingSeparatorsInternal(); |
178 | 187 |
179 StringType path_; | 188 StringType path_; |
180 }; | 189 }; |
181 | 190 |
182 // Macros for string literal initialization of FilePath::CharType[]. | 191 // Macros for string literal initialization of FilePath::CharType[]. |
183 #if defined(OS_POSIX) | 192 #if defined(OS_POSIX) |
184 #define FILE_PATH_LITERAL(x) x | 193 #define FILE_PATH_LITERAL(x) x |
185 #elif defined(OS_WIN) | 194 #elif defined(OS_WIN) |
186 #define FILE_PATH_LITERAL(x) L ## x | 195 #define FILE_PATH_LITERAL(x) L ## x |
187 #endif // OS_WIN | 196 #endif // OS_WIN |
188 | 197 |
189 #endif // BASE_FILE_PATH_H_ | 198 #endif // BASE_FILE_PATH_H_ |
OLD | NEW |