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 #include "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 // These includes are just for the *Hack functions, and should be removed | 9 // These includes are just for the *Hack functions, and should be removed |
10 // when those functions are removed. | 10 // when those functions are removed. |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 new_path.path_.resize(last_separator); | 106 new_path.path_.resize(last_separator); |
107 } | 107 } |
108 | 108 |
109 new_path.StripTrailingSeparatorsInternal(); | 109 new_path.StripTrailingSeparatorsInternal(); |
110 if (!new_path.path_.length()) | 110 if (!new_path.path_.length()) |
111 new_path.path_ = kCurrentDirectory; | 111 new_path.path_ = kCurrentDirectory; |
112 | 112 |
113 return new_path; | 113 return new_path; |
114 } | 114 } |
115 | 115 |
116 FilePath::StringType FilePath::BaseName() const { | 116 FilePath FilePath::BaseName() const { |
117 FilePath new_path(path_); | 117 FilePath new_path(path_); |
118 new_path.StripTrailingSeparatorsInternal(); | 118 new_path.StripTrailingSeparatorsInternal(); |
119 | 119 |
120 // The drive letter, if any, is always stripped. | 120 // The drive letter, if any, is always stripped. |
121 StringType::size_type letter = FindDriveLetter(new_path.path_); | 121 StringType::size_type letter = FindDriveLetter(new_path.path_); |
122 if (letter != StringType::npos) { | 122 if (letter != StringType::npos) { |
123 new_path.path_.erase(0, letter + 1); | 123 new_path.path_.erase(0, letter + 1); |
124 } | 124 } |
125 | 125 |
126 // Keep everything after the final separator, but if the pathname is only | 126 // Keep everything after the final separator, but if the pathname is only |
127 // one character and it's a separator, leave it alone. | 127 // one character and it's a separator, leave it alone. |
128 StringType::size_type last_separator = | 128 StringType::size_type last_separator = |
129 new_path.path_.find_last_of(kSeparators, StringType::npos, | 129 new_path.path_.find_last_of(kSeparators, StringType::npos, |
130 arraysize(kSeparators) - 1); | 130 arraysize(kSeparators) - 1); |
131 if (last_separator != StringType::npos && | 131 if (last_separator != StringType::npos && |
132 last_separator < new_path.path_.length() - 1) { | 132 last_separator < new_path.path_.length() - 1) { |
133 new_path.path_.erase(0, last_separator + 1); | 133 new_path.path_.erase(0, last_separator + 1); |
134 } | 134 } |
135 | 135 |
136 return new_path.path_; | 136 return new_path; |
137 } | 137 } |
138 | 138 |
139 FilePath FilePath::Append(const FilePath::StringType& component) const { | 139 FilePath FilePath::Append(const FilePath::StringType& component) const { |
140 DCHECK(!IsPathAbsolute(component)); | 140 DCHECK(!IsPathAbsolute(component)); |
141 if (path_.compare(kCurrentDirectory) == 0) { | 141 if (path_.compare(kCurrentDirectory) == 0) { |
142 // Append normally doesn't do any normalization, but as a special case, | 142 // Append normally doesn't do any normalization, but as a special case, |
143 // when appending to kCurrentDirectory, just return a new path for the | 143 // when appending to kCurrentDirectory, just return a new path for the |
144 // component argument. Appending component to kCurrentDirectory would | 144 // component argument. Appending component to kCurrentDirectory would |
145 // serve no purpose other than needlessly lengthening the path, and | 145 // serve no purpose other than needlessly lengthening the path, and |
146 // it's likely in practice to wind up with FilePath objects containing | 146 // it's likely in practice to wind up with FilePath objects containing |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 --pos) { | 249 --pos) { |
250 // If the string only has two separators and they're at the beginning, | 250 // If the string only has two separators and they're at the beginning, |
251 // don't strip them, unless the string began with more than two separators. | 251 // don't strip them, unless the string began with more than two separators. |
252 if (pos != start + 1 || last_stripped == start + 2 || | 252 if (pos != start + 1 || last_stripped == start + 2 || |
253 !IsSeparator(path_[start - 1])) { | 253 !IsSeparator(path_[start - 1])) { |
254 path_.resize(pos - 1); | 254 path_.resize(pos - 1); |
255 last_stripped = pos; | 255 last_stripped = pos; |
256 } | 256 } |
257 } | 257 } |
258 } | 258 } |
OLD | NEW |