OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/file_path.h" |
| 6 |
| 7 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 8 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); |
| 9 #else // FILE_PATH_USES_WIN_SEPARATORS |
| 10 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); |
| 11 #endif // FILE_PATH_USES_WIN_SEPARATORS |
| 12 |
| 13 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); |
| 14 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); |
| 15 |
| 16 // Returns true if |character| is in kSeparators. |
| 17 static bool IsSeparator(FilePath::CharType character) { |
| 18 for (size_t i = 0; i < arraysize(FilePath::kSeparators) - 1; ++i) { |
| 19 if (character == FilePath::kSeparators[i]) { |
| 20 return true; |
| 21 } |
| 22 } |
| 23 |
| 24 return false; |
| 25 } |
| 26 |
| 27 // libgen's dirname and basename aren't guaranteed to be thread-safe and aren't |
| 28 // guaranteed to not modify their input strings, and in fact are implmeneted |
| 29 // differently in this regard on different platforms. Don't use them, but |
| 30 // adhere to their behavior. |
| 31 FilePath FilePath::DirName() const { |
| 32 FilePath new_path(path_); |
| 33 new_path.StripTrailingSeparators(); |
| 34 |
| 35 // The drive letter, if any, always needs to remain in the output. If there |
| 36 // is no drive letter, as will always be the case on platforms which do not |
| 37 // support drive letters, letter will be npos, or -1, so the comparisons and |
| 38 // resizes below using letter will still be valid. |
| 39 StringType::size_type letter = new_path.FindDriveLetter(); |
| 40 |
| 41 StringType::size_type last_separator = |
| 42 new_path.path_.find_last_of(kSeparators, StringType::npos, |
| 43 arraysize(kSeparators) - 1); |
| 44 if (last_separator == StringType::npos) { |
| 45 // path_ is in the current directory. |
| 46 new_path.path_.resize(letter + 1); |
| 47 } else if (last_separator == letter + 1) { |
| 48 // path_ is in the root directory. |
| 49 new_path.path_.resize(letter + 2); |
| 50 } else if (last_separator == letter + 2 && |
| 51 IsSeparator(new_path.path_[letter + 1])) { |
| 52 // path_ is in "//" (possibly with a drive letter); leave the double |
| 53 // separator intact indicating alternate root. |
| 54 new_path.path_.resize(letter + 3); |
| 55 } else if (last_separator != 0) { |
| 56 // path_ is somewhere else, trim the basename. |
| 57 new_path.path_.resize(last_separator); |
| 58 } |
| 59 |
| 60 new_path.StripTrailingSeparators(); |
| 61 if (!new_path.path_.length()) |
| 62 new_path.path_ = kCurrentDirectory; |
| 63 |
| 64 return new_path; |
| 65 } |
| 66 |
| 67 FilePath FilePath::BaseName() const { |
| 68 FilePath new_path(path_); |
| 69 new_path.StripTrailingSeparators(); |
| 70 |
| 71 // The drive letter, if any, is always stripped. |
| 72 StringType::size_type letter = new_path.FindDriveLetter(); |
| 73 if (letter != StringType::npos) { |
| 74 new_path.path_.erase(0, letter + 1); |
| 75 } |
| 76 |
| 77 // Keep everything after the final separator, but if the pathname is only |
| 78 // one character and it's a separator, leave it alone. |
| 79 StringType::size_type last_separator = |
| 80 new_path.path_.find_last_of(kSeparators, StringType::npos, |
| 81 arraysize(kSeparators) - 1); |
| 82 if (last_separator != StringType::npos && |
| 83 last_separator < new_path.path_.length() - 1) { |
| 84 new_path.path_.erase(0, last_separator + 1); |
| 85 } |
| 86 |
| 87 return new_path; |
| 88 } |
| 89 |
| 90 FilePath FilePath::Append(const FilePath::StringType& component) const { |
| 91 if (path_.compare(kCurrentDirectory) == 0) { |
| 92 // Append normally doesn't do any normalization, but as a special case, |
| 93 // when appending to kCurrentDirectory, just return a new path for the |
| 94 // component argument. Appending component to kCurrentDirectory would |
| 95 // serve no purpose other than needlessly lengthening the path, and |
| 96 // it's likely in practice to wind up with FilePath objects containing |
| 97 // only kCurrentDirectory when calling DirName on a single relative path |
| 98 // component. |
| 99 return FilePath(component); |
| 100 } |
| 101 |
| 102 FilePath new_path(path_); |
| 103 new_path.StripTrailingSeparators(); |
| 104 |
| 105 // Don't append a separator if the path is empty (indicating the current |
| 106 // directory) or if the path component is empty (indicating nothing to |
| 107 // append). |
| 108 if (component.length() > 0 && new_path.path_.length() > 0) { |
| 109 |
| 110 // Don't append a separator if the path still ends with a trailing |
| 111 // separator after stripping (indicating the root directory). |
| 112 if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) { |
| 113 |
| 114 // Don't append a separator if the path is just a drive letter. |
| 115 if (new_path.FindDriveLetter() + 1 != new_path.path_.length()) { |
| 116 new_path.path_.append(1, kSeparators[0]); |
| 117 } |
| 118 } |
| 119 } |
| 120 |
| 121 new_path.path_.append(component); |
| 122 return new_path; |
| 123 } |
| 124 |
| 125 FilePath::StringType::size_type FilePath::FindDriveLetter() const { |
| 126 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
| 127 // This is dependent on an ASCII-based character set, but that's a |
| 128 // reasonable assumption. iswalpha can be too inclusive here. |
| 129 if (path_.length() >= 2 && path_[1] == L':' && |
| 130 ((path_[0] >= L'A' && path_[0] <= L'Z') || |
| 131 (path_[0] >= L'a' && path_[0] <= L'z'))) { |
| 132 return 1; |
| 133 } |
| 134 return StringType::npos; |
| 135 #else // FILE_PATH_USES_DRIVE_LETTERS |
| 136 return StringType::npos; |
| 137 #endif // FILE_PATH_USES_DRIVE_LETTERS |
| 138 } |
| 139 |
| 140 bool FilePath::IsAbsolute() const { |
| 141 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
| 142 StringType::size_type letter = FindDriveLetter(); |
| 143 if (letter != StringType::npos) { |
| 144 // Look for a separator right after the drive specification. |
| 145 return path_.length() > letter + 1 && IsSeparator(path_[letter + 1]); |
| 146 } |
| 147 // Look for a pair of leading separators. |
| 148 return path_.length() > 1 && IsSeparator(path_[0]) && IsSeparator(path_[1]); |
| 149 #else // FILE_PATH_USES_DRIVE_LETTERS |
| 150 // Look for a separator in the first position. |
| 151 return path_.length() > 0 && IsSeparator(path_[0]); |
| 152 #endif // FILE_PATH_USES_DRIVE_LETTERS |
| 153 } |
| 154 |
| 155 void FilePath::StripTrailingSeparators() { |
| 156 // If there is no drive letter, start will be 1, which will prevent stripping |
| 157 // the leading separator if there is only one separator. If there is a drive |
| 158 // letter, start will be set appropriately to prevent stripping the first |
| 159 // separator following the drive letter, if a separator immediately follows |
| 160 // the drive letter. |
| 161 StringType::size_type start = FindDriveLetter() + 2; |
| 162 |
| 163 StringType::size_type last_stripped = StringType::npos; |
| 164 for (StringType::size_type pos = path_.length(); |
| 165 pos > start && IsSeparator(path_[pos - 1]); |
| 166 --pos) { |
| 167 // If the string only has two separators and they're at the beginning, |
| 168 // don't strip them, unless the string began with more than two separators. |
| 169 if (pos != start + 1 || last_stripped == start + 2 || |
| 170 !IsSeparator(path_[start - 1])) { |
| 171 path_.resize(pos - 1); |
| 172 last_stripped = pos; |
| 173 } |
| 174 } |
| 175 } |
OLD | NEW |