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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 (last_separator == StringType::npos || | 152 (last_separator == StringType::npos || |
153 penultimate_dot > last_separator) && | 153 penultimate_dot > last_separator) && |
154 last_dot - penultimate_dot <= 5U && | 154 last_dot - penultimate_dot <= 5U && |
155 last_dot - penultimate_dot > 1U) { | 155 last_dot - penultimate_dot > 1U) { |
156 return penultimate_dot; | 156 return penultimate_dot; |
157 } | 157 } |
158 | 158 |
159 return last_dot; | 159 return last_dot; |
160 } | 160 } |
161 | 161 |
162 // Returns true if path is "", ".", or "..". | |
163 bool IsEmptyOrSpecialCase(const StringType& path) { | |
164 // Special cases "", ".", and ".." | |
165 if (path.empty() || path == FilePath::kCurrentDirectory || | |
166 path == FilePath::kParentDirectory) { | |
167 return true; | |
168 } | |
169 | |
170 return false; | |
171 } | |
172 | |
162 } // namespace | 173 } // namespace |
163 | 174 |
164 FilePath::FilePath() { | 175 FilePath::FilePath() { |
165 } | 176 } |
166 | 177 |
167 FilePath::FilePath(const FilePath& that) : path_(that.path_) { | 178 FilePath::FilePath(const FilePath& that) : path_(that.path_) { |
168 } | 179 } |
169 | 180 |
170 FilePath::FilePath(const StringType& path) : path_(path) { | 181 FilePath::FilePath(const StringType& path) : path_(path) { |
171 } | 182 } |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 return FilePath(path_.substr(0, dot)); | 382 return FilePath(path_.substr(0, dot)); |
372 } | 383 } |
373 | 384 |
374 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const { | 385 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const { |
375 if (suffix.empty()) | 386 if (suffix.empty()) |
376 return FilePath(path_); | 387 return FilePath(path_); |
377 | 388 |
378 if (path_.empty()) | 389 if (path_.empty()) |
379 return FilePath(); | 390 return FilePath(); |
380 | 391 |
381 StringType base = BaseName().value(); | 392 if (IsEmptyOrSpecialCase(BaseName().value())) |
382 if (base.empty()) | |
383 return FilePath(); | 393 return FilePath(); |
384 if (*(base.end() - 1) == kExtensionSeparator) { | |
385 // Special case "." and ".." | |
386 if (base == kCurrentDirectory || base == kParentDirectory) { | |
387 return FilePath(); | |
388 } | |
389 } | |
390 | 394 |
391 StringType ext = Extension(); | 395 StringType ext = Extension(); |
392 StringType ret = RemoveExtension().value(); | 396 StringType ret = RemoveExtension().value(); |
393 ret.append(suffix); | 397 ret.append(suffix); |
394 ret.append(ext); | 398 ret.append(ext); |
395 return FilePath(ret); | 399 return FilePath(ret); |
396 } | 400 } |
397 | 401 |
398 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix) | 402 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix) |
399 const { | 403 const { |
400 DCHECK(IsStringASCII(suffix)); | 404 DCHECK(IsStringASCII(suffix)); |
401 #if defined(OS_WIN) | 405 #if defined(OS_WIN) |
402 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); | 406 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); |
403 #elif defined(OS_POSIX) | 407 #elif defined(OS_POSIX) |
404 return InsertBeforeExtension(suffix.as_string()); | 408 return InsertBeforeExtension(suffix.as_string()); |
405 #endif | 409 #endif |
406 } | 410 } |
407 | 411 |
412 FilePath FilePath::AddExtension(const StringType& extension) const { | |
413 if (path_.empty()) | |
Mark Mentovai
2012/04/21 17:02:00
This check is redundant because IsEmptyOrSpecialCa
| |
414 return FilePath(); | |
415 | |
416 if (IsEmptyOrSpecialCase(BaseName().value())) | |
417 return FilePath(); | |
418 | |
419 // If the new extension is "" or ".", then just return the current FilePath. | |
420 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) | |
421 return *this; | |
422 | |
423 StringType str = path_; | |
424 if (extension[0] != kExtensionSeparator && | |
425 *(str.end() - 1) != kExtensionSeparator) { | |
426 str.append(1, kExtensionSeparator); | |
427 } | |
428 str.append(extension); | |
429 return FilePath(str); | |
430 } | |
431 | |
408 FilePath FilePath::ReplaceExtension(const StringType& extension) const { | 432 FilePath FilePath::ReplaceExtension(const StringType& extension) const { |
409 if (path_.empty()) | 433 if (path_.empty()) |
Mark Mentovai
2012/04/21 17:02:00
Same question applies here.
| |
410 return FilePath(); | 434 return FilePath(); |
411 | 435 |
412 StringType base = BaseName().value(); | 436 if (IsEmptyOrSpecialCase(BaseName().value())) |
413 if (base.empty()) | |
414 return FilePath(); | 437 return FilePath(); |
415 if (*(base.end() - 1) == kExtensionSeparator) { | |
416 // Special case "." and ".." | |
417 if (base == kCurrentDirectory || base == kParentDirectory) { | |
418 return FilePath(); | |
419 } | |
420 } | |
421 | 438 |
422 FilePath no_ext = RemoveExtension(); | 439 FilePath no_ext = RemoveExtension(); |
423 // If the new extension is "" or ".", then just remove the current extension. | 440 // If the new extension is "" or ".", then just remove the current extension. |
424 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) | 441 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) |
425 return no_ext; | 442 return no_ext; |
426 | 443 |
427 StringType str = no_ext.value(); | 444 StringType str = no_ext.value(); |
428 if (extension[0] != kExtensionSeparator) | 445 if (extension[0] != kExtensionSeparator) |
429 str.append(1, kExtensionSeparator); | 446 str.append(1, kExtensionSeparator); |
430 str.append(extension); | 447 str.append(extension); |
(...skipping 783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1214 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 1231 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
1215 StringType copy = path_; | 1232 StringType copy = path_; |
1216 for (size_t i = 1; i < arraysize(kSeparators); ++i) { | 1233 for (size_t i = 1; i < arraysize(kSeparators); ++i) { |
1217 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); | 1234 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); |
1218 } | 1235 } |
1219 return FilePath(copy); | 1236 return FilePath(copy); |
1220 #else | 1237 #else |
1221 return *this; | 1238 return *this; |
1222 #endif | 1239 #endif |
1223 } | 1240 } |
OLD | NEW |