1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/file_path.h"
#include "base/logging.h"
// These includes are just for the *Hack functions, and should be removed
// when those functions are removed.
#include "base/string_piece.h"
#include "base/string_util.h"
#include "base/sys_string_conversions.h"
#if defined(FILE_PATH_USES_WIN_SEPARATORS)
const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/");
#else // FILE_PATH_USES_WIN_SEPARATORS
const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/");
#endif // FILE_PATH_USES_WIN_SEPARATORS
const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL(".");
const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL("..");
const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.');
namespace {
// If this FilePath contains a drive letter specification, returns the
// position of the last character of the drive letter specification,
// otherwise returns npos. This can only be true on Windows, when a pathname
// begins with a letter followed by a colon. On other platforms, this always
// returns npos.
FilePath::StringType::size_type FindDriveLetter(
const FilePath::StringType& path) {
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
// This is dependent on an ASCII-based character set, but that's a
// reasonable assumption. iswalpha can be too inclusive here.
if (path.length() >= 2 && path[1] == L':' &&
((path[0] >= L'A' && path[0] <= L'Z') ||
(path[0] >= L'a' && path[0] <= L'z'))) {
return 1;
}
#endif // FILE_PATH_USES_DRIVE_LETTERS
return FilePath::StringType::npos;
}
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
bool EqualDriveLetterCaseInsensitive(const FilePath::StringType a,
const FilePath::StringType b) {
size_t a_letter_pos = FindDriveLetter(a);
size_t b_letter_pos = FindDriveLetter(b);
if ((a_letter_pos == FilePath::StringType::npos) ||
(b_letter_pos == FilePath::StringType::npos))
return a == b;
FilePath::StringType a_letter(a.substr(0, a_letter_pos + 1));
FilePath::StringType b_letter(b.substr(0, b_letter_pos + 1));
if (!StartsWith(a_letter, b_letter, false))
return false;
FilePath::StringType a_rest(a.substr(a_letter_pos + 1));
FilePath::StringType b_rest(b.substr(b_letter_pos + 1));
return a_rest == b_rest;
}
#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
bool IsPathAbsolute(const FilePath::StringType& path) {
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
FilePath::StringType::size_type letter = FindDriveLetter(path);
if (letter != FilePath::StringType::npos) {
// Look for a separator right after the drive specification.
return path.length() > letter + 1 &&
FilePath::IsSeparator(path[letter + 1]);
}
// Look for a pair of leading separators.
return path.length() > 1 &&
FilePath::IsSeparator(path[0]) && FilePath::IsSeparator(path[1]);
#else // FILE_PATH_USES_DRIVE_LETTERS
// Look for a separator in the first position.
return path.length() > 0 && FilePath::IsSeparator(path[0]);
#endif // FILE_PATH_USES_DRIVE_LETTERS
}
bool AreAllSeparators(const FilePath::StringType& input) {
for (FilePath::StringType::const_iterator it = input.begin();
it != input.end(); ++it) {
if (!FilePath::IsSeparator(*it))
return false;
}
return true;
}
} // namespace
bool FilePath::IsSeparator(CharType character) {
for (size_t i = 0; i < arraysize(kSeparators) - 1; ++i) {
if (character == kSeparators[i]) {
return true;
}
}
return false;
}
void FilePath::GetComponents(std::vector<FilePath::StringType>* components)
const {
DCHECK(components);
if (!components)
return;
components->clear();
if (value().empty())
return;
std::vector<FilePath::StringType> ret_val;
FilePath current = *this;
FilePath base;
// Capture path components.
while (current != current.DirName()) {
base = current.BaseName();
if (!AreAllSeparators(base.value()))
ret_val.push_back(base.value());
current = current.DirName();
}
// Capture root, if any.
base = current.BaseName();
if (!base.value().empty() && base.value() != kCurrentDirectory)
ret_val.push_back(current.BaseName().value());
// Capture drive letter, if any.
FilePath dir = current.DirName();
StringType::size_type letter = FindDriveLetter(dir.value());
if (letter != FilePath::StringType::npos) {
ret_val.push_back(FilePath::StringType(dir.value(), 0, letter + 1));
}
*components = std::vector<FilePath::StringType>(ret_val.rbegin(),
ret_val.rend());
}
bool FilePath::operator==(const FilePath& that) const {
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
return EqualDriveLetterCaseInsensitive(this->path_, that.path_);
#else // defined(FILE_PATH_USES_DRIVE_LETTERS)
return path_ == that.path_;
#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
}
bool FilePath::operator!=(const FilePath& that) const {
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
return !EqualDriveLetterCaseInsensitive(this->path_, that.path_);
#else // defined(FILE_PATH_USES_DRIVE_LETTERS)
return path_ != that.path_;
#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
}
bool FilePath::IsParent(const FilePath& child) const {
std::vector<FilePath::StringType> parent_components;
std::vector<FilePath::StringType> child_components;
GetComponents(&parent_components);
child.GetComponents(&child_components);
if (parent_components.size() >= child_components.size())
return false;
if (parent_components.size() == 0)
return false;
std::vector<FilePath::StringType>::const_iterator parent_comp =
parent_components.begin();
std::vector<FilePath::StringType>::const_iterator child_comp =
child_components.begin();
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
// Windows can access case sensitive filesystems, so component
// comparisions must be case sensitive, but drive letters are
// never case sensitive.
if ((FindDriveLetter(*parent_comp) != FilePath::StringType::npos) &&
(FindDriveLetter(*child_comp) != FilePath::StringType::npos)) {
if (!StartsWith(*parent_comp, *child_comp, false))
return false;
++parent_comp;
++child_comp;
}
#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
while (parent_comp != parent_components.end()) {
if (*parent_comp != *child_comp)
return false;
++parent_comp;
++child_comp;
}
return true;
}
// libgen's dirname and basename aren't guaranteed to be thread-safe and aren't
// guaranteed to not modify their input strings, and in fact are implemented
// differently in this regard on different platforms. Don't use them, but
// adhere to their behavior.
FilePath FilePath::DirName() const {
FilePath new_path(path_);
new_path.StripTrailingSeparatorsInternal();
// The drive letter, if any, always needs to remain in the output. If there
// is no drive letter, as will always be the case on platforms which do not
// support drive letters, letter will be npos, or -1, so the comparisons and
// resizes below using letter will still be valid.
StringType::size_type letter = FindDriveLetter(new_path.path_);
StringType::size_type last_separator =
new_path.path_.find_last_of(kSeparators, StringType::npos,
arraysize(kSeparators) - 1);
if (last_separator == StringType::npos) {
// path_ is in the current directory.
new_path.path_.resize(letter + 1);
} else if (last_separator == letter + 1) {
// path_ is in the root directory.
new_path.path_.resize(letter + 2);
} else if (last_separator == letter + 2 &&
IsSeparator(new_path.path_[letter + 1])) {
// path_ is in "//" (possibly with a drive letter); leave the double
// separator intact indicating alternate root.
new_path.path_.resize(letter + 3);
} else if (last_separator != 0) {
// path_ is somewhere else, trim the basename.
new_path.path_.resize(last_separator);
}
new_path.StripTrailingSeparatorsInternal();
if (!new_path.path_.length())
new_path.path_ = kCurrentDirectory;
return new_path;
}
FilePath FilePath::BaseName() const {
FilePath new_path(path_);
new_path.StripTrailingSeparatorsInternal();
// The drive letter, if any, is always stripped.
StringType::size_type letter = FindDriveLetter(new_path.path_);
if (letter != StringType::npos) {
new_path.path_.erase(0, letter + 1);
}
// Keep everything after the final separator, but if the pathname is only
// one character and it's a separator, leave it alone.
StringType::size_type last_separator =
new_path.path_.find_last_of(kSeparators, StringType::npos,
arraysize(kSeparators) - 1);
if (last_separator != StringType::npos &&
last_separator < new_path.path_.length() - 1) {
new_path.path_.erase(0, last_separator + 1);
}
return new_path;
}
FilePath::StringType FilePath::Extension() const {
// BaseName() calls StripTrailingSeparators, so cases like /foo.baz/// work.
StringType base = BaseName().value();
// Special case "." and ".."
if (base == kCurrentDirectory || base == kParentDirectory)
return StringType();
const StringType::size_type last_dot = base.rfind(kExtensionSeparator);
if (last_dot == StringType::npos)
return StringType();
return StringType(base, last_dot);
}
FilePath FilePath::RemoveExtension() const {
StringType ext = Extension();
// It's important to check Extension() since that verifies that the
// kExtensionSeparator actually appeared in the last path component.
if (ext.empty())
return FilePath(path_);
// Since Extension() verified that the extension is in fact in the last path
// component, this substr will effectively strip trailing separators.
const StringType::size_type last_dot = path_.rfind(kExtensionSeparator);
return FilePath(path_.substr(0, last_dot));
}
FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
if (suffix.empty())
return FilePath(path_);
if (path_.empty())
return FilePath();
StringType base = BaseName().value();
if (base.empty())
return FilePath();
if (*(base.end() - 1) == kExtensionSeparator) {
// Special case "." and ".."
if (base == kCurrentDirectory || base == kParentDirectory) {
return FilePath();
}
}
StringType ext = Extension();
StringType ret = RemoveExtension().value();
ret.append(suffix);
ret.append(ext);
return FilePath(ret);
}
FilePath FilePath::InsertBeforeExtensionASCII(const StringPiece& suffix) const {
DCHECK(IsStringASCII(suffix));
#if defined(OS_WIN)
return InsertBeforeExtension(ASCIIToWide(suffix));
#elif defined(OS_POSIX)
return InsertBeforeExtension(suffix.as_string());
#endif
}
FilePath FilePath::ReplaceExtension(const StringType& extension) const {
if (path_.empty())
return FilePath();
StringType base = BaseName().value();
if (base.empty())
return FilePath();
if (*(base.end() - 1) == kExtensionSeparator) {
// Special case "." and ".."
if (base == kCurrentDirectory || base == kParentDirectory) {
return FilePath();
}
}
FilePath no_ext = RemoveExtension();
// If the new extension is "" or ".", then just remove the current extension.
if (extension.empty() || extension == StringType(1, kExtensionSeparator))
return no_ext;
StringType str = no_ext.value();
if (extension[0] != kExtensionSeparator)
str.append(1, kExtensionSeparator);
str.append(extension);
return FilePath(str);
}
bool FilePath::MatchesExtension(const StringType& extension) const {
FilePath::StringType current_extension = Extension();
if (current_extension.length() != extension.length())
return false;
return std::equal(extension.begin(),
extension.end(),
current_extension.begin(),
CaseInsensitiveCompare<FilePath::CharType>());
}
FilePath FilePath::Append(const StringType& component) const {
DCHECK(!IsPathAbsolute(component));
if (path_.compare(kCurrentDirectory) == 0) {
// Append normally doesn't do any normalization, but as a special case,
// when appending to kCurrentDirectory, just return a new path for the
// component argument. Appending component to kCurrentDirectory would
// serve no purpose other than needlessly lengthening the path, and
// it's likely in practice to wind up with FilePath objects containing
// only kCurrentDirectory when calling DirName on a single relative path
// component.
return FilePath(component);
}
FilePath new_path(path_);
new_path.StripTrailingSeparatorsInternal();
// Don't append a separator if the path is empty (indicating the current
// directory) or if the path component is empty (indicating nothing to
// append).
if (component.length() > 0 && new_path.path_.length() > 0) {
// Don't append a separator if the path still ends with a trailing
// separator after stripping (indicating the root directory).
if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) {
// Don't append a separator if the path is just a drive letter.
if (FindDriveLetter(new_path.path_) + 1 != new_path.path_.length()) {
new_path.path_.append(1, kSeparators[0]);
}
}
}
new_path.path_.append(component);
return new_path;
}
FilePath FilePath::Append(const FilePath& component) const {
return Append(component.value());
}
FilePath FilePath::AppendASCII(const StringPiece& component) const {
DCHECK(IsStringASCII(component));
#if defined(OS_WIN)
return Append(ASCIIToWide(component));
#elif defined(OS_POSIX)
return Append(component.as_string());
#endif
}
bool FilePath::IsAbsolute() const {
return IsPathAbsolute(path_);
}
#if defined(OS_POSIX)
// See file_path.h for a discussion of the encoding of paths on POSIX
// platforms. These *Hack() functions are not quite correct, but they're
// only temporary while we fix the remainder of the code.
// Remember to remove the #includes at the top when you remove these.
// static
FilePath FilePath::FromWStringHack(const std::wstring& wstring) {
return FilePath(base::SysWideToNativeMB(wstring));
}
std::wstring FilePath::ToWStringHack() const {
return base::SysNativeMBToWide(path_);
}
#elif defined(OS_WIN)
// static
FilePath FilePath::FromWStringHack(const std::wstring& wstring) {
return FilePath(wstring);
}
std::wstring FilePath::ToWStringHack() const {
return path_;
}
#endif
FilePath FilePath::StripTrailingSeparators() const {
FilePath new_path(path_);
new_path.StripTrailingSeparatorsInternal();
return new_path;
}
void FilePath::StripTrailingSeparatorsInternal() {
// If there is no drive letter, start will be 1, which will prevent stripping
// the leading separator if there is only one separator. If there is a drive
// letter, start will be set appropriately to prevent stripping the first
// separator following the drive letter, if a separator immediately follows
// the drive letter.
StringType::size_type start = FindDriveLetter(path_) + 2;
StringType::size_type last_stripped = StringType::npos;
for (StringType::size_type pos = path_.length();
pos > start && IsSeparator(path_[pos - 1]);
--pos) {
// If the string only has two separators and they're at the beginning,
// don't strip them, unless the string began with more than two separators.
if (pos != start + 1 || last_stripped == start + 2 ||
!IsSeparator(path_[start - 1])) {
path_.resize(pos - 1);
last_stripped = pos;
}
}
}
bool FilePath::ReferencesParent() const {
std::vector<FilePath::StringType> components;
GetComponents(&components);
std::vector<FilePath::StringType>::const_iterator it = components.begin();
for (; it != components.end(); ++it) {
const FilePath::StringType& component = *it;
if (component == kParentDirectory)
return true;
}
return false;
}
|