OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/source_dir.h" | 5 #include "tools/gn/source_dir.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "tools/gn/filesystem_utils.h" | 8 #include "tools/gn/filesystem_utils.h" |
9 #include "tools/gn/source_file.h" | 9 #include "tools/gn/source_file.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 void AssertValueSourceDirString(const std::string& s) { | 13 void AssertValueSourceDirString(const std::string& s) { |
14 DCHECK(!s.empty()); | 14 if (!s.empty()) { |
15 DCHECK(s[0] == '/'); | 15 DCHECK(s[0] == '/'); |
16 DCHECK(EndsWithSlash(s)); | 16 DCHECK(EndsWithSlash(s)); |
| 17 } |
17 } | 18 } |
18 | 19 |
19 } // namespace | 20 } // namespace |
20 | 21 |
21 SourceDir::SourceDir() { | 22 SourceDir::SourceDir() { |
22 } | 23 } |
23 | 24 |
24 SourceDir::SourceDir(const base::StringPiece& p) | 25 SourceDir::SourceDir(const base::StringPiece& p) |
25 : value_(p.data(), p.size()) { | 26 : value_(p.data(), p.size()) { |
26 if (!EndsWithSlash(value_)) | 27 if (!EndsWithSlash(value_)) |
27 value_.push_back('/'); | 28 value_.push_back('/'); |
28 AssertValueSourceDirString(value_); | 29 AssertValueSourceDirString(value_); |
29 } | 30 } |
30 | 31 |
| 32 SourceDir::SourceDir(SwapIn, std::string* s) { |
| 33 value_.swap(*s); |
| 34 if (!EndsWithSlash(value_)) |
| 35 value_.push_back('/'); |
| 36 AssertValueSourceDirString(value_); |
| 37 } |
| 38 |
31 SourceDir::~SourceDir() { | 39 SourceDir::~SourceDir() { |
32 } | 40 } |
33 | 41 |
34 SourceFile SourceDir::ResolveRelativeFile( | 42 SourceFile SourceDir::ResolveRelativeFile( |
35 const base::StringPiece& p, | 43 const base::StringPiece& p, |
36 const base::StringPiece& source_root) const { | 44 const base::StringPiece& source_root) const { |
37 SourceFile ret; | 45 SourceFile ret; |
38 | 46 |
39 // It's an error to resolve an empty string or one that is a directory | 47 // It's an error to resolve an empty string or one that is a directory |
40 // (indicated by a trailing slash) because this is the function that expects | 48 // (indicated by a trailing slash) because this is the function that expects |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 ConvertPathToSystem(&converted); | 133 ConvertPathToSystem(&converted); |
126 return base::FilePath(UTF8ToFilePath(converted)); | 134 return base::FilePath(UTF8ToFilePath(converted)); |
127 } | 135 } |
128 | 136 |
129 // String the double-leading slash for source-relative paths. | 137 // String the double-leading slash for source-relative paths. |
130 converted.assign(&value_[2], value_.size() - 2); | 138 converted.assign(&value_[2], value_.size() - 2); |
131 ConvertPathToSystem(&converted); | 139 ConvertPathToSystem(&converted); |
132 return source_root.Append(UTF8ToFilePath(converted)); | 140 return source_root.Append(UTF8ToFilePath(converted)); |
133 } | 141 } |
134 | 142 |
135 void SourceDir::SwapInValue(std::string* v) { | 143 void SourceDir::SwapValue(std::string* v) { |
136 value_.swap(*v); | 144 value_.swap(*v); |
137 AssertValueSourceDirString(value_); | 145 AssertValueSourceDirString(value_); |
138 } | 146 } |
OLD | NEW |