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.
5 #ifndef TOOLS_GN_SOURCE_DIR_H_
6 #define TOOLS_GN_SOURCE_DIR_H_
11 #include "base/containers/hash_tables.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/strings/string_piece.h"
20 // Represents a directory within the source tree. Source dirs begin and end in
23 // If there is one slash at the beginning, it will mean a system-absolute file
24 // path. On Windows, absolute system paths will be of the form "/C:/foo/bar".
26 // Two slashes at the beginning indicate a path relative to the source root.
29 enum SwapIn
{ SWAP_IN
};
32 explicit SourceDir(const base::StringPiece
& p
);
33 // Swaps the given string in without copies. The given string will be empty
35 SourceDir(SwapIn
, std::string
* s
);
38 // Resolves a file or dir name relative to this source directory. Will return
39 // an empty SourceDir/File on error and set the give *err pointer (required).
40 // Empty input is always an error.
42 // If source_root is supplied, these functions will additionally handle the
43 // case where the input is a system-absolute but still inside the source
44 // tree. This is the case for some external tools.
45 SourceFile
ResolveRelativeFile(
48 const base::StringPiece
& source_root
= base::StringPiece()) const;
49 SourceDir
ResolveRelativeDir(
52 const base::StringPiece
& source_root
= base::StringPiece()) const;
54 // Like ResolveRelativeDir but takes a separate value (which gets blamed)
55 // and string to use (in cases where a substring has been extracted from the
56 // value, as with label resolution).
57 SourceDir
ResolveRelativeDir(
58 const Value
& blame_but_dont_use
,
59 const base::StringPiece
& p
,
61 const base::StringPiece
& source_root
= base::StringPiece()) const;
63 // Resolves this source file relative to some given source root. Returns
64 // an empty file path on error.
65 base::FilePath
Resolve(const base::FilePath
& source_root
) const;
67 bool is_null() const { return value_
.empty(); }
68 const std::string
& value() const { return value_
; }
70 // Returns true if this path starts with a "//" which indicates a path
71 // from the source root.
72 bool is_source_absolute() const {
73 return value_
.size() >= 2 && value_
[0] == '/' && value_
[1] == '/';
76 // Returns true if this path starts with a single slash which indicates a
77 // system-absolute path.
78 bool is_system_absolute() const {
79 return !is_source_absolute();
82 // Returns a source-absolute path starting with only one slash at the
83 // beginning (normally source-absolute paths start with two slashes to mark
84 // them as such). This is normally used when concatenating directories
87 // This function asserts that the directory is actually source-absolute. The
88 // return value points into our buffer.
89 base::StringPiece
SourceAbsoluteWithOneSlash() const {
90 CHECK(is_source_absolute());
91 return base::StringPiece(&value_
[1], value_
.size() - 1);
94 void SwapValue(std::string
* v
);
96 bool operator==(const SourceDir
& other
) const {
97 return value_
== other
.value_
;
99 bool operator!=(const SourceDir
& other
) const {
100 return !operator==(other
);
102 bool operator<(const SourceDir
& other
) const {
103 return value_
< other
.value_
;
106 void swap(SourceDir
& other
) {
107 value_
.swap(other
.value_
);
111 friend class SourceFile
;
114 // Copy & assign supported.
117 namespace BASE_HASH_NAMESPACE
{
119 template<> struct hash
<SourceDir
> {
120 std::size_t operator()(const SourceDir
& v
) const {
126 } // namespace BASE_HASH_NAMESPACE
128 inline void swap(SourceDir
& lhs
, SourceDir
& rhs
) {
132 #endif // TOOLS_GN_SOURCE_DIR_H_