source_file.cc revision d3868032626d59662ff73b372b5d584c1d144c53
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
12SourceFile::SourceFile() {
13}
14
15SourceFile::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
22SourceFile::~SourceFile() {
23}
24
25std::string SourceFile::GetName() const {
26  if (is_null())
27    return std::string();
28
29  DCHECK(value_.find('/') != std::string::npos);
30  size_t last_slash = value_.rfind('/');
31  return std::string(&value_[last_slash + 1],
32                     value_.size() - last_slash - 1);
33}
34
35SourceDir SourceFile::GetDir() const {
36  if (is_null())
37    return SourceDir();
38
39  DCHECK(value_.find('/') != std::string::npos);
40  size_t last_slash = value_.rfind('/');
41  return SourceDir(base::StringPiece(&value_[0], last_slash + 1));
42}
43
44base::FilePath SourceFile::Resolve(const base::FilePath& source_root) const {
45  if (is_null())
46    return base::FilePath();
47
48  std::string converted;
49#if defined(OS_WIN)
50  if (is_system_absolute()) {
51    converted.assign(&value_[1], value_.size() - 1);
52    DCHECK(converted.size() > 2 && converted[1] == ':')
53        << "Expecting Windows absolute file path with a drive letter: "
54        << value_;
55    return base::FilePath(UTF8ToFilePath(converted));
56  }
57
58  converted.assign(&value_[2], value_.size() - 2);
59  ConvertPathToSystem(&converted);
60  return root_path_.Append(UTF8ToFilePath(converted));
61#else
62  if (is_system_absolute())
63    return base::FilePath(value_);
64  converted.assign(&value_[2], value_.size() - 2);
65  return source_root.Append(converted);
66#endif
67}
68