Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / tools / gn / source_file.cc
blob71d8592c1aac614b26b7c919283f369c83217624
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"
12 namespace {
14 void AssertValueSourceFileString(const std::string& s) {
15 #if defined(OS_WIN)
16 DCHECK(s[0] == '/' ||
17 (s.size() > 2 && s[0] != '/' && s[1] == ':' && IsSlash(s[2])));
18 #else
19 DCHECK(s[0] == '/');
20 #endif
21 DCHECK(!EndsWithSlash(s));
24 } // namespace
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) {
36 value_.swap(*value);
37 DCHECK(!value_.empty());
38 AssertValueSourceFileString(value_);
41 SourceFile::~SourceFile() {
44 std::string SourceFile::GetName() const {
45 if (is_null())
46 return std::string();
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 {
55 if (is_null())
56 return SourceDir();
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 {
64 if (is_null())
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);
72 } else {
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('/');