1d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// found in the LICENSE file.
4d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
5d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/source_file.h"
6d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
7d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "base/logging.h"
8d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "build/build_config.h"
9d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/filesystem_utils.h"
10d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/source_dir.h"
11d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
12d3868032626d59662ff73b372b5d584c1d144c53Ben MurdochSourceFile::SourceFile() {
13d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}
14d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
15d3868032626d59662ff73b372b5d584c1d144c53Ben MurdochSourceFile::SourceFile(const base::StringPiece& p)
16d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    : value_(p.data(), p.size()) {
17d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  DCHECK(!value_.empty());
18d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  DCHECK(value_[0] == '/');
19d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  DCHECK(!EndsWithSlash(value_));
20d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}
21d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)SourceFile::SourceFile(SwapIn, std::string* value) {
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  value_.swap(*value);
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(!value_.empty());
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(value_[0] == '/');
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(!EndsWithSlash(value_));
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
29d3868032626d59662ff73b372b5d584c1d144c53Ben MurdochSourceFile::~SourceFile() {
30d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}
31d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
32d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochstd::string SourceFile::GetName() const {
33d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  if (is_null())
34d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    return std::string();
35d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
36d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  DCHECK(value_.find('/') != std::string::npos);
37d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  size_t last_slash = value_.rfind('/');
38d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  return std::string(&value_[last_slash + 1],
39d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch                     value_.size() - last_slash - 1);
40d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}
41d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
42d3868032626d59662ff73b372b5d584c1d144c53Ben MurdochSourceDir SourceFile::GetDir() const {
43d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  if (is_null())
44d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    return SourceDir();
45d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
46d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  DCHECK(value_.find('/') != std::string::npos);
47d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  size_t last_slash = value_.rfind('/');
48d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  return SourceDir(base::StringPiece(&value_[0], last_slash + 1));
49d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}
50d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
51d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochbase::FilePath SourceFile::Resolve(const base::FilePath& source_root) const {
52d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  if (is_null())
53d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    return base::FilePath();
54d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
55d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  std::string converted;
56d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  if (is_system_absolute()) {
574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (value_.size() > 2 && value_[2] == ':') {
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Windows path, strip the leading slash.
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      converted.assign(&value_[1], value_.size() - 1);
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    } else {
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      converted.assign(value_);
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
63d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    return base::FilePath(UTF8ToFilePath(converted));
64d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  }
65d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
66d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  converted.assign(&value_[2], value_.size() - 2);
67c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return source_root.Append(UTF8ToFilePath(converted))
68c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      .NormalizePathSeparatorsTo('/');
69d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}
70