summaryrefslogtreecommitdiff
path: root/chromium/tools/gn/source_file.cc
blob: 62f8fb11a45c33fa2cc41618f392c0b35efb9a13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/gn/source_file.h"

#include "base/logging.h"
#include "build/build_config.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/source_dir.h"

SourceFile::SourceFile() {
}

SourceFile::SourceFile(const base::StringPiece& p)
    : value_(p.data(), p.size()) {
  DCHECK(!value_.empty());
  DCHECK(value_[0] == '/');
  DCHECK(!EndsWithSlash(value_));
}

SourceFile::~SourceFile() {
}

std::string SourceFile::GetName() const {
  if (is_null())
    return std::string();

  DCHECK(value_.find('/') != std::string::npos);
  size_t last_slash = value_.rfind('/');
  return std::string(&value_[last_slash + 1],
                     value_.size() - last_slash - 1);
}

SourceDir SourceFile::GetDir() const {
  if (is_null())
    return SourceDir();

  DCHECK(value_.find('/') != std::string::npos);
  size_t last_slash = value_.rfind('/');
  return SourceDir(base::StringPiece(&value_[0], last_slash + 1));
}

base::FilePath SourceFile::Resolve(const base::FilePath& source_root) const {
  if (is_null())
    return base::FilePath();

  std::string converted;
#if defined(OS_WIN)
  if (is_system_absolute()) {
    converted.assign(&value_[1], value_.size() - 1);
    DCHECK(converted.size() > 2 && converted[1] == ':')
        << "Expecting Windows absolute file path with a drive letter: "
        << value_;
    return base::FilePath(UTF8ToFilePath(converted));
  }

  converted.assign(&value_[2], value_.size() - 2);
  ConvertPathToSystem(&converted);
  return source_root.Append(UTF8ToFilePath(converted));
#else
  if (is_system_absolute())
    return base::FilePath(value_);
  converted.assign(&value_[2], value_.size() - 2);
  return source_root.Append(converted);
#endif
}