Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: base/file_path.cc

Issue 14827: Implement FilePath::Contains() (Closed)
Patch Set: Make it work on mac and linux Created 12 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/file_path.h ('k') | base/file_path_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/logging.h" 7 #include "base/logging.h"
7 8
8 // 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
9 // when those functions are removed. 10 // when those functions are removed.
10 #include "base/string_piece.h" 11 #include "base/string_piece.h"
12 #include "base/string_util.h"
11 #include "base/sys_string_conversions.h" 13 #include "base/sys_string_conversions.h"
12 14
13 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 15 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
14 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); 16 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/");
15 #else // FILE_PATH_USES_WIN_SEPARATORS 17 #else // FILE_PATH_USES_WIN_SEPARATORS
16 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); 18 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/");
17 #endif // FILE_PATH_USES_WIN_SEPARATORS 19 #endif // FILE_PATH_USES_WIN_SEPARATORS
18 20
19 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); 21 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL(".");
20 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); 22 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL("..");
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 } 173 }
172 174
173 FilePath FilePath::Append(const FilePath& component) const { 175 FilePath FilePath::Append(const FilePath& component) const {
174 return Append(component.value()); 176 return Append(component.value());
175 } 177 }
176 178
177 bool FilePath::IsAbsolute() const { 179 bool FilePath::IsAbsolute() const {
178 return IsPathAbsolute(path_); 180 return IsPathAbsolute(path_);
179 } 181 }
180 182
183 bool FilePath::Contains(const FilePath &other) const {
184 FilePath parent = FilePath(*this);
185 FilePath child = FilePath(other);
186
187 if (!file_util::AbsolutePath(&parent) || !file_util::AbsolutePath(&child))
188 return false;
189
190 #if defined(OS_WIN)
191 // file_util::AbsolutePath() does not flatten case on Windows, so we must do
192 // a case-insensitive compare.
193 if (!StartsWith(child.value(), parent.value(), false))
194 #else
195 if (!StartsWithASCII(child.value(), parent.value(), true))
196 #endif
197 return false;
198
199 // file_util::AbsolutePath() normalizes '/' to '\', so we only need to check
200 // kSeparators[0].
201 if (child.value().length() <= parent.value().length() ||
202 child.value()[parent.value().length()] != kSeparators[0])
203 return false;
204
205 return true;
206 }
207
181 #if defined(OS_POSIX) 208 #if defined(OS_POSIX)
182 // See file_path.h for a discussion of the encoding of paths on POSIX 209 // See file_path.h for a discussion of the encoding of paths on POSIX
183 // platforms. These *Hack() functions are not quite correct, but they're 210 // platforms. These *Hack() functions are not quite correct, but they're
184 // only temporary while we fix the remainder of the code. 211 // only temporary while we fix the remainder of the code.
185 // Remember to remove the #includes at the top when you remove these. 212 // Remember to remove the #includes at the top when you remove these.
186 213
187 // static 214 // static
188 FilePath FilePath::FromWStringHack(const std::wstring& wstring) { 215 FilePath FilePath::FromWStringHack(const std::wstring& wstring) {
189 return FilePath(base::SysWideToNativeMB(wstring)); 216 return FilePath(base::SysWideToNativeMB(wstring));
190 } 217 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 --pos) { 249 --pos) {
223 // 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,
224 // 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.
225 if (pos != start + 1 || last_stripped == start + 2 || 252 if (pos != start + 1 || last_stripped == start + 2 ||
226 !IsSeparator(path_[start - 1])) { 253 !IsSeparator(path_[start - 1])) {
227 path_.resize(pos - 1); 254 path_.resize(pos - 1);
228 last_stripped = pos; 255 last_stripped = pos;
229 } 256 }
230 } 257 }
231 } 258 }
OLDNEW
« no previous file with comments | « base/file_path.h ('k') | base/file_path_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698