1 // Copyright (c) 2013 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.
5 #include "tools/gn/source_file.h"
7 #include "base/logging.h"
8 #include "build/build_config.h"
9 #include "tools/gn/filesystem_utils.h"
10 #include "tools/gn/source_dir.h"
14 void AssertValueSourceFileString(const std::string
& s
) {
17 (s
.size() > 2 && s
[0] != '/' && s
[1] == ':' && IsSlash(s
[2])));
21 DCHECK(!EndsWithSlash(s
)) << s
;
26 SourceFile::SourceFile() {
29 SourceFile::SourceFile(const base::StringPiece
& p
)
30 : value_(p
.data(), p
.size()) {
31 DCHECK(!value_
.empty());
32 AssertValueSourceFileString(value_
);
33 NormalizePath(&value_
);
36 SourceFile::SourceFile(SwapIn
, std::string
* value
) {
38 DCHECK(!value_
.empty());
39 AssertValueSourceFileString(value_
);
40 NormalizePath(&value_
);
43 SourceFile::~SourceFile() {
46 std::string
SourceFile::GetName() const {
50 DCHECK(value_
.find('/') != std::string::npos
);
51 size_t last_slash
= value_
.rfind('/');
52 return std::string(&value_
[last_slash
+ 1],
53 value_
.size() - last_slash
- 1);
56 SourceDir
SourceFile::GetDir() const {
60 DCHECK(value_
.find('/') != std::string::npos
);
61 size_t last_slash
= value_
.rfind('/');
62 return SourceDir(base::StringPiece(&value_
[0], last_slash
+ 1));
65 base::FilePath
SourceFile::Resolve(const base::FilePath
& source_root
) const {
67 return base::FilePath();
69 std::string converted
;
70 if (is_system_absolute()) {
71 if (value_
.size() > 2 && value_
[2] == ':') {
72 // Windows path, strip the leading slash.
73 converted
.assign(&value_
[1], value_
.size() - 1);
75 converted
.assign(value_
);
77 return base::FilePath(UTF8ToFilePath(converted
));
80 converted
.assign(&value_
[2], value_
.size() - 2);
81 if (source_root
.empty())
82 return UTF8ToFilePath(converted
).NormalizePathSeparatorsTo('/');
83 return source_root
.Append(UTF8ToFilePath(converted
))
84 .NormalizePathSeparatorsTo('/');