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/file_path.h" | 5 #include "base/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 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 if (Extension().empty()) | 364 if (Extension().empty()) |
365 return *this; | 365 return *this; |
366 | 366 |
367 const StringType::size_type dot = ExtensionSeparatorPosition(path_); | 367 const StringType::size_type dot = ExtensionSeparatorPosition(path_); |
368 if (dot == StringType::npos) | 368 if (dot == StringType::npos) |
369 return *this; | 369 return *this; |
370 | 370 |
371 return FilePath(path_.substr(0, dot)); | 371 return FilePath(path_.substr(0, dot)); |
372 } | 372 } |
373 | 373 |
| 374 bool FilePath::IsBaseEmptyOrSpecialCase() const { |
| 375 StringType base = BaseName().value(); |
| 376 if (base.empty()) |
| 377 return true; |
| 378 if (*(base.end() - 1) == kExtensionSeparator) { |
| 379 // Special case "." and ".." |
| 380 if (base == kCurrentDirectory || base == kParentDirectory) { |
| 381 return true; |
| 382 } |
| 383 } |
| 384 |
| 385 return false; |
| 386 } |
| 387 |
374 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const { | 388 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const { |
375 if (suffix.empty()) | 389 if (suffix.empty()) |
376 return FilePath(path_); | 390 return FilePath(path_); |
377 | 391 |
378 if (path_.empty()) | 392 if (path_.empty()) |
379 return FilePath(); | 393 return FilePath(); |
380 | 394 |
381 StringType base = BaseName().value(); | 395 if (IsBaseEmptyOrSpecialCase()) |
382 if (base.empty()) | |
383 return FilePath(); | 396 return FilePath(); |
384 if (*(base.end() - 1) == kExtensionSeparator) { | |
385 // Special case "." and ".." | |
386 if (base == kCurrentDirectory || base == kParentDirectory) { | |
387 return FilePath(); | |
388 } | |
389 } | |
390 | 397 |
391 StringType ext = Extension(); | 398 StringType ext = Extension(); |
392 StringType ret = RemoveExtension().value(); | 399 StringType ret = RemoveExtension().value(); |
393 ret.append(suffix); | 400 ret.append(suffix); |
394 ret.append(ext); | 401 ret.append(ext); |
395 return FilePath(ret); | 402 return FilePath(ret); |
396 } | 403 } |
397 | 404 |
398 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix) | 405 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix) |
399 const { | 406 const { |
400 DCHECK(IsStringASCII(suffix)); | 407 DCHECK(IsStringASCII(suffix)); |
401 #if defined(OS_WIN) | 408 #if defined(OS_WIN) |
402 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); | 409 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); |
403 #elif defined(OS_POSIX) | 410 #elif defined(OS_POSIX) |
404 return InsertBeforeExtension(suffix.as_string()); | 411 return InsertBeforeExtension(suffix.as_string()); |
405 #endif | 412 #endif |
406 } | 413 } |
407 | 414 |
| 415 FilePath FilePath::AddExtension(const StringType& extension) const { |
| 416 if (path_.empty()) |
| 417 return FilePath(); |
| 418 |
| 419 if (IsBaseEmptyOrSpecialCase()) |
| 420 return FilePath(); |
| 421 |
| 422 // If the new extension is "" or ".", then just return the current FilePath. |
| 423 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) |
| 424 return *this; |
| 425 |
| 426 StringType str = path_; |
| 427 if (extension[0] != kExtensionSeparator && |
| 428 *(str.end() - 1) != kExtensionSeparator) { |
| 429 str.append(1, kExtensionSeparator); |
| 430 } |
| 431 str.append(extension); |
| 432 return FilePath(str); |
| 433 } |
| 434 |
408 FilePath FilePath::ReplaceExtension(const StringType& extension) const { | 435 FilePath FilePath::ReplaceExtension(const StringType& extension) const { |
409 if (path_.empty()) | 436 if (path_.empty()) |
410 return FilePath(); | 437 return FilePath(); |
411 | 438 |
412 StringType base = BaseName().value(); | 439 if (IsBaseEmptyOrSpecialCase()) |
413 if (base.empty()) | |
414 return FilePath(); | 440 return FilePath(); |
415 if (*(base.end() - 1) == kExtensionSeparator) { | |
416 // Special case "." and ".." | |
417 if (base == kCurrentDirectory || base == kParentDirectory) { | |
418 return FilePath(); | |
419 } | |
420 } | |
421 | 441 |
422 FilePath no_ext = RemoveExtension(); | 442 FilePath no_ext = RemoveExtension(); |
423 // If the new extension is "" or ".", then just remove the current extension. | 443 // If the new extension is "" or ".", then just remove the current extension. |
424 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) | 444 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) |
425 return no_ext; | 445 return no_ext; |
426 | 446 |
427 StringType str = no_ext.value(); | 447 StringType str = no_ext.value(); |
428 if (extension[0] != kExtensionSeparator) | 448 if (extension[0] != kExtensionSeparator) |
429 str.append(1, kExtensionSeparator); | 449 str.append(1, kExtensionSeparator); |
430 str.append(extension); | 450 str.append(extension); |
(...skipping 783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1214 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 1234 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
1215 StringType copy = path_; | 1235 StringType copy = path_; |
1216 for (size_t i = 1; i < arraysize(kSeparators); ++i) { | 1236 for (size_t i = 1; i < arraysize(kSeparators); ++i) { |
1217 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); | 1237 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); |
1218 } | 1238 } |
1219 return FilePath(copy); | 1239 return FilePath(copy); |
1220 #else | 1240 #else |
1221 return *this; | 1241 return *this; |
1222 #endif | 1242 #endif |
1223 } | 1243 } |
OLD | NEW |