OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #endif | 24 #endif |
25 | 25 |
26 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
27 #include <windows.h> | 27 #include <windows.h> |
28 #elif defined(OS_MACOSX) | 28 #elif defined(OS_MACOSX) |
29 #include <CoreFoundation/CoreFoundation.h> | 29 #include <CoreFoundation/CoreFoundation.h> |
30 #endif | 30 #endif |
31 | 31 |
32 namespace base { | 32 namespace base { |
33 | 33 |
34 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
35 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); | |
36 #else // FILE_PATH_USES_WIN_SEPARATORS | |
37 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); | |
38 #endif // FILE_PATH_USES_WIN_SEPARATORS | |
39 | |
40 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); | |
41 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); | |
42 | |
43 const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.'); | |
44 | |
45 typedef FilePath::StringType StringType; | 34 typedef FilePath::StringType StringType; |
46 | 35 |
47 namespace { | 36 namespace { |
48 | 37 |
49 const char* kCommonDoubleExtensionSuffixes[] = { "gz", "z", "bz2" }; | 38 const char* kCommonDoubleExtensionSuffixes[] = { "gz", "z", "bz2" }; |
50 const char* kCommonDoubleExtensions[] = { "user.js" }; | 39 const char* kCommonDoubleExtensions[] = { "user.js" }; |
51 | 40 |
52 const FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0'); | 41 const FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0'); |
53 | 42 |
54 // If this FilePath contains a drive letter specification, returns the | 43 // If this FilePath contains a drive letter specification, returns the |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 path.rfind(FilePath::kExtensionSeparator); | 121 path.rfind(FilePath::kExtensionSeparator); |
133 | 122 |
134 // No extension, or the extension is the whole filename. | 123 // No extension, or the extension is the whole filename. |
135 if (last_dot == StringType::npos || last_dot == 0U) | 124 if (last_dot == StringType::npos || last_dot == 0U) |
136 return last_dot; | 125 return last_dot; |
137 | 126 |
138 const StringType::size_type penultimate_dot = | 127 const StringType::size_type penultimate_dot = |
139 path.rfind(FilePath::kExtensionSeparator, last_dot - 1); | 128 path.rfind(FilePath::kExtensionSeparator, last_dot - 1); |
140 const StringType::size_type last_separator = | 129 const StringType::size_type last_separator = |
141 path.find_last_of(FilePath::kSeparators, last_dot - 1, | 130 path.find_last_of(FilePath::kSeparators, last_dot - 1, |
142 arraysize(FilePath::kSeparators) - 1); | 131 FilePath::kSeparatorsLength - 1); |
143 | 132 |
144 if (penultimate_dot == StringType::npos || | 133 if (penultimate_dot == StringType::npos || |
145 (last_separator != StringType::npos && | 134 (last_separator != StringType::npos && |
146 penultimate_dot < last_separator)) { | 135 penultimate_dot < last_separator)) { |
147 return last_dot; | 136 return last_dot; |
148 } | 137 } |
149 | 138 |
150 for (size_t i = 0; i < arraysize(kCommonDoubleExtensions); ++i) { | 139 for (size_t i = 0; i < arraysize(kCommonDoubleExtensions); ++i) { |
151 StringType extension(path, penultimate_dot + 1); | 140 StringType extension(path, penultimate_dot + 1); |
152 if (LowerCaseEqualsASCII(extension, kCommonDoubleExtensions[i])) | 141 if (LowerCaseEqualsASCII(extension, kCommonDoubleExtensions[i])) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 bool FilePath::operator!=(const FilePath& that) const { | 199 bool FilePath::operator!=(const FilePath& that) const { |
211 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | 200 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
212 return !EqualDriveLetterCaseInsensitive(this->path_, that.path_); | 201 return !EqualDriveLetterCaseInsensitive(this->path_, that.path_); |
213 #else // defined(FILE_PATH_USES_DRIVE_LETTERS) | 202 #else // defined(FILE_PATH_USES_DRIVE_LETTERS) |
214 return path_ != that.path_; | 203 return path_ != that.path_; |
215 #endif // defined(FILE_PATH_USES_DRIVE_LETTERS) | 204 #endif // defined(FILE_PATH_USES_DRIVE_LETTERS) |
216 } | 205 } |
217 | 206 |
218 // static | 207 // static |
219 bool FilePath::IsSeparator(CharType character) { | 208 bool FilePath::IsSeparator(CharType character) { |
220 for (size_t i = 0; i < arraysize(kSeparators) - 1; ++i) { | 209 for (size_t i = 0; i < kSeparatorsLength - 1; ++i) { |
221 if (character == kSeparators[i]) { | 210 if (character == kSeparators[i]) { |
222 return true; | 211 return true; |
223 } | 212 } |
224 } | 213 } |
225 | 214 |
226 return false; | 215 return false; |
227 } | 216 } |
228 | 217 |
229 void FilePath::GetComponents(std::vector<StringType>* components) const { | 218 void FilePath::GetComponents(std::vector<StringType>* components) const { |
230 DCHECK(components); | 219 DCHECK(components); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 new_path.StripTrailingSeparatorsInternal(); | 307 new_path.StripTrailingSeparatorsInternal(); |
319 | 308 |
320 // The drive letter, if any, always needs to remain in the output. If there | 309 // The drive letter, if any, always needs to remain in the output. If there |
321 // is no drive letter, as will always be the case on platforms which do not | 310 // is no drive letter, as will always be the case on platforms which do not |
322 // support drive letters, letter will be npos, or -1, so the comparisons and | 311 // support drive letters, letter will be npos, or -1, so the comparisons and |
323 // resizes below using letter will still be valid. | 312 // resizes below using letter will still be valid. |
324 StringType::size_type letter = FindDriveLetter(new_path.path_); | 313 StringType::size_type letter = FindDriveLetter(new_path.path_); |
325 | 314 |
326 StringType::size_type last_separator = | 315 StringType::size_type last_separator = |
327 new_path.path_.find_last_of(kSeparators, StringType::npos, | 316 new_path.path_.find_last_of(kSeparators, StringType::npos, |
328 arraysize(kSeparators) - 1); | 317 kSeparatorsLength - 1); |
329 if (last_separator == StringType::npos) { | 318 if (last_separator == StringType::npos) { |
330 // path_ is in the current directory. | 319 // path_ is in the current directory. |
331 new_path.path_.resize(letter + 1); | 320 new_path.path_.resize(letter + 1); |
332 } else if (last_separator == letter + 1) { | 321 } else if (last_separator == letter + 1) { |
333 // path_ is in the root directory. | 322 // path_ is in the root directory. |
334 new_path.path_.resize(letter + 2); | 323 new_path.path_.resize(letter + 2); |
335 } else if (last_separator == letter + 2 && | 324 } else if (last_separator == letter + 2 && |
336 IsSeparator(new_path.path_[letter + 1])) { | 325 IsSeparator(new_path.path_[letter + 1])) { |
337 // path_ is in "//" (possibly with a drive letter); leave the double | 326 // path_ is in "//" (possibly with a drive letter); leave the double |
338 // separator intact indicating alternate root. | 327 // separator intact indicating alternate root. |
(...skipping 17 matching lines...) Expand all Loading... |
356 // The drive letter, if any, is always stripped. | 345 // The drive letter, if any, is always stripped. |
357 StringType::size_type letter = FindDriveLetter(new_path.path_); | 346 StringType::size_type letter = FindDriveLetter(new_path.path_); |
358 if (letter != StringType::npos) { | 347 if (letter != StringType::npos) { |
359 new_path.path_.erase(0, letter + 1); | 348 new_path.path_.erase(0, letter + 1); |
360 } | 349 } |
361 | 350 |
362 // Keep everything after the final separator, but if the pathname is only | 351 // Keep everything after the final separator, but if the pathname is only |
363 // one character and it's a separator, leave it alone. | 352 // one character and it's a separator, leave it alone. |
364 StringType::size_type last_separator = | 353 StringType::size_type last_separator = |
365 new_path.path_.find_last_of(kSeparators, StringType::npos, | 354 new_path.path_.find_last_of(kSeparators, StringType::npos, |
366 arraysize(kSeparators) - 1); | 355 kSeparatorsLength - 1); |
367 if (last_separator != StringType::npos && | 356 if (last_separator != StringType::npos && |
368 last_separator < new_path.path_.length() - 1) { | 357 last_separator < new_path.path_.length() - 1) { |
369 new_path.path_.erase(0, last_separator + 1); | 358 new_path.path_.erase(0, last_separator + 1); |
370 } | 359 } |
371 | 360 |
372 return new_path; | 361 return new_path; |
373 } | 362 } |
374 | 363 |
375 StringType FilePath::Extension() const { | 364 StringType FilePath::Extension() const { |
376 FilePath base(BaseName()); | 365 FilePath base(BaseName()); |
(...skipping 885 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1262 !IsSeparator(path_[start - 1])) { | 1251 !IsSeparator(path_[start - 1])) { |
1263 path_.resize(pos - 1); | 1252 path_.resize(pos - 1); |
1264 last_stripped = pos; | 1253 last_stripped = pos; |
1265 } | 1254 } |
1266 } | 1255 } |
1267 } | 1256 } |
1268 | 1257 |
1269 FilePath FilePath::NormalizePathSeparators() const { | 1258 FilePath FilePath::NormalizePathSeparators() const { |
1270 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 1259 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
1271 StringType copy = path_; | 1260 StringType copy = path_; |
1272 for (size_t i = 1; i < arraysize(kSeparators); ++i) { | 1261 for (size_t i = 1; i < kSeparatorsLength; ++i) { |
1273 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); | 1262 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); |
1274 } | 1263 } |
1275 return FilePath(copy); | 1264 return FilePath(copy); |
1276 #else | 1265 #else |
1277 return *this; | 1266 return *this; |
1278 #endif | 1267 #endif |
1279 } | 1268 } |
1280 | 1269 |
1281 } // namespace base | 1270 } // namespace base |
1282 | 1271 |
1283 void PrintTo(const base::FilePath& path, std::ostream* out) { | 1272 void PrintTo(const base::FilePath& path, std::ostream* out) { |
1284 *out << path.value(); | 1273 *out << path.value(); |
1285 } | 1274 } |
OLD | NEW |