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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 // but kSeparators[0] is treated as the canonical separator and will be used | 102 // but kSeparators[0] is treated as the canonical separator and will be used |
103 // when composing pathnames. | 103 // when composing pathnames. |
104 static const CharType kSeparators[]; | 104 static const CharType kSeparators[]; |
105 | 105 |
106 // A special path component meaning "this directory." | 106 // A special path component meaning "this directory." |
107 static const CharType kCurrentDirectory[]; | 107 static const CharType kCurrentDirectory[]; |
108 | 108 |
109 // A special path component meaning "the parent directory." | 109 // A special path component meaning "the parent directory." |
110 static const CharType kParentDirectory[]; | 110 static const CharType kParentDirectory[]; |
111 | 111 |
| 112 // The character used to identify a file extension. |
| 113 static const CharType kExtensionSeparator; |
| 114 |
112 FilePath() {} | 115 FilePath() {} |
113 FilePath(const FilePath& that) : path_(that.path_) {} | 116 FilePath(const FilePath& that) : path_(that.path_) {} |
114 explicit FilePath(const StringType& path) : path_(path) {} | 117 explicit FilePath(const StringType& path) : path_(path) {} |
115 | 118 |
116 FilePath& operator=(const FilePath& that) { | 119 FilePath& operator=(const FilePath& that) { |
117 path_ = that.path_; | 120 path_ = that.path_; |
118 return *this; | 121 return *this; |
119 } | 122 } |
120 | 123 |
121 bool operator==(const FilePath& that) const { | 124 bool operator==(const FilePath& that) const { |
(...skipping 18 matching lines...) Expand all Loading... |
140 // kCurrentDirectory. If this object already refers to the root directory, | 143 // kCurrentDirectory. If this object already refers to the root directory, |
141 // returns a FilePath identifying the root directory. | 144 // returns a FilePath identifying the root directory. |
142 FilePath DirName() const; | 145 FilePath DirName() const; |
143 | 146 |
144 // Returns a FilePath corresponding to the last path component of this | 147 // Returns a FilePath corresponding to the last path component of this |
145 // object, either a file or a directory. If this object already refers to | 148 // object, either a file or a directory. If this object already refers to |
146 // the root directory, returns a FilePath identifying the root directory; | 149 // the root directory, returns a FilePath identifying the root directory; |
147 // this is the only situation in which BaseName will return an absolute path. | 150 // this is the only situation in which BaseName will return an absolute path. |
148 FilePath BaseName() const; | 151 FilePath BaseName() const; |
149 | 152 |
| 153 // Returns ".jpg" for path "C:\pics\jojo.jpg", or an empty string if |
| 154 // the file has no extension. If non-empty, Extension() will always start |
| 155 // with precisely one ".". The following code should always work regardless |
| 156 // of the value of path. |
| 157 // new_path = path.RemoveExtension().value().append(path.Extension()); |
| 158 // ASSERT(new_path == path.value()); |
| 159 // NOTE: this is different from the original file_util implementation which |
| 160 // returned the extension without a leading "." ("jpg" instead of ".jpg") |
| 161 StringType Extension() const; |
| 162 |
| 163 // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg" |
| 164 // NOTE: this is slightly different from the similar file_util implementation |
| 165 // which returned simply 'jojo'. |
| 166 FilePath RemoveExtension() const; |
| 167 |
| 168 // Inserts |suffix| after the file name portion of |path| but before the |
| 169 // extension. Returns "" if BaseName() == "." or "..". |
| 170 // Examples: |
| 171 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" |
| 172 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" |
| 173 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" |
| 174 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" |
| 175 FilePath InsertBeforeExtension(const StringType& suffix) const; |
| 176 |
| 177 // Replaces the extension of |file_name| with |extension|. If |file_name| |
| 178 // does not have an extension, them |extension| is added. If |extension| is |
| 179 // empty, then the extension is removed from |file_name|. |
| 180 // Returns "" if BaseName() == "." or "..". |
| 181 FilePath ReplaceExtension(const StringType& extension) const; |
| 182 |
150 // Returns a FilePath by appending a separator and the supplied path | 183 // Returns a FilePath by appending a separator and the supplied path |
151 // component to this object's path. Append takes care to avoid adding | 184 // component to this object's path. Append takes care to avoid adding |
152 // excessive separators if this object's path already ends with a separator. | 185 // excessive separators if this object's path already ends with a separator. |
153 // If this object's path is kCurrentDirectory, a new FilePath corresponding | 186 // If this object's path is kCurrentDirectory, a new FilePath corresponding |
154 // only to |component| is returned. |component| must be a relative path; | 187 // only to |component| is returned. |component| must be a relative path; |
155 // it is an error to pass an absolute path. | 188 // it is an error to pass an absolute path. |
156 FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; | 189 FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; |
157 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; | 190 FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; |
158 | 191 |
159 // Returns true if this FilePath contains an absolute path. On Windows, an | 192 // Returns true if this FilePath contains an absolute path. On Windows, an |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 namespace stdext { | 252 namespace stdext { |
220 | 253 |
221 inline size_t hash_value(const FilePath& f) { | 254 inline size_t hash_value(const FilePath& f) { |
222 return hash_value(f.value()); | 255 return hash_value(f.value()); |
223 } | 256 } |
224 | 257 |
225 } // namespace stdext | 258 } // namespace stdext |
226 #endif // COMPILER | 259 #endif // COMPILER |
227 | 260 |
228 #endif // BASE_FILE_PATH_H_ | 261 #endif // BASE_FILE_PATH_H_ |
OLD | NEW |