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
));
26 SourceFile::SourceFile() {
29 SourceFile::SourceFile(const base::StringPiece
& p
)
30 : value_(p
.data(), p
.size()) {
31 DCHECK(!value_
.empty());
32 AssertValueSourceFileString(value_
);
35 SourceFile::SourceFile(SwapIn
, std::string
* value
) {
37 DCHECK(!value_
.empty());
38 AssertValueSourceFileString(value_
);
41 SourceFile::~SourceFile() {
44 std::string
SourceFile::GetName() const {
48 DCHECK(value_
.find('/') != std::string::npos
);
49 size_t last_slash
= value_
.rfind('/');
50 return std::string(&value_
[last_slash
+ 1],
51 value_
.size() - last_slash
- 1);
54 SourceDir
SourceFile::GetDir() const {
58 DCHECK(value_
.find('/') != std::string::npos
);
59 size_t last_slash
= value_
.rfind('/');
60 return SourceDir(base::StringPiece(&value_
[0], last_slash
+ 1));
63 base::FilePath
SourceFile::Resolve(const base::FilePath
& source_root
) const {
65 return base::FilePath();
67 std::string converted
;
68 if (is_system_absolute()) {
69 if (value_
.size() > 2 && value_
[2] == ':') {
70 // Windows path, strip the leading slash.
71 converted
.assign(&value_
[1], value_
.size() - 1);
73 converted
.assign(value_
);
75 return base::FilePath(UTF8ToFilePath(converted
));
78 converted
.assign(&value_
[2], value_
.size() - 2);
79 if (source_root
.empty())
80 return UTF8ToFilePath(converted
).NormalizePathSeparatorsTo('/');
81 return source_root
.Append(UTF8ToFilePath(converted
))
82 .NormalizePathSeparatorsTo('/');