• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "tools/gn/source_file.h"
6 
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"
11 
SourceFile()12 SourceFile::SourceFile() {
13 }
14 
SourceFile(const base::StringPiece & p)15 SourceFile::SourceFile(const base::StringPiece& p)
16     : value_(p.data(), p.size()) {
17   DCHECK(!value_.empty());
18   DCHECK(value_[0] == '/');
19   DCHECK(!EndsWithSlash(value_));
20 }
21 
SourceFile(SwapIn,std::string * value)22 SourceFile::SourceFile(SwapIn, std::string* value) {
23   value_.swap(*value);
24   DCHECK(!value_.empty());
25   DCHECK(value_[0] == '/');
26   DCHECK(!EndsWithSlash(value_));
27 }
28 
~SourceFile()29 SourceFile::~SourceFile() {
30 }
31 
GetName() const32 std::string SourceFile::GetName() const {
33   if (is_null())
34     return std::string();
35 
36   DCHECK(value_.find('/') != std::string::npos);
37   size_t last_slash = value_.rfind('/');
38   return std::string(&value_[last_slash + 1],
39                      value_.size() - last_slash - 1);
40 }
41 
GetDir() const42 SourceDir SourceFile::GetDir() const {
43   if (is_null())
44     return SourceDir();
45 
46   DCHECK(value_.find('/') != std::string::npos);
47   size_t last_slash = value_.rfind('/');
48   return SourceDir(base::StringPiece(&value_[0], last_slash + 1));
49 }
50 
Resolve(const base::FilePath & source_root) const51 base::FilePath SourceFile::Resolve(const base::FilePath& source_root) const {
52   if (is_null())
53     return base::FilePath();
54 
55   std::string converted;
56   if (is_system_absolute()) {
57     if (value_.size() > 2 && value_[2] == ':') {
58       // Windows path, strip the leading slash.
59       converted.assign(&value_[1], value_.size() - 1);
60     } else {
61       converted.assign(value_);
62     }
63     return base::FilePath(UTF8ToFilePath(converted));
64   }
65 
66   converted.assign(&value_[2], value_.size() - 2);
67   return source_root.Append(UTF8ToFilePath(converted))
68       .NormalizePathSeparatorsTo('/');
69 }
70