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"
18 // Represents a directory within the source tree. Source dirs begin and end in
21 // If there is one slash at the beginning, it will mean a system-absolute file
22 // path. On Windows, absolute system paths will be of the form "/C:/foo/bar".
24 // Two slashes at the beginning indicate a path relative to the source root.
27 enum SwapIn
{ SWAP_IN
};
30 explicit SourceDir(const base::StringPiece
& p
);
31 // Swaps the given string in without copies. The given string will be empty
33 SourceDir(SwapIn
, std::string
* s
);
36 // Resolves a file or dir name relative to this source directory. Will return
37 // an empty SourceDir/File on error. Empty input is always an error (it's
38 // possible we should say ResolveRelativeDir vs. an empty string should be
39 // the source dir, but we require "." instead).
41 // If source_root is supplied, these functions will additionally handle the
42 // case where the input is a system-absolute but still inside the source
43 // tree. This is the case for some external tools.
44 SourceFile
ResolveRelativeFile(
45 const base::StringPiece
& p
,
46 const base::StringPiece
& source_root
= base::StringPiece()) const;
47 SourceDir
ResolveRelativeDir(
48 const base::StringPiece
& p
,
49 const base::StringPiece
& source_root
= base::StringPiece()) const;
51 // Resolves this source file relative to some given source root. Returns
52 // an empty file path on error.
53 base::FilePath
Resolve(const base::FilePath
& source_root
) const;
55 bool is_null() const { return value_
.empty(); }
56 const std::string
& value() const { return value_
; }
58 // Returns true if this path starts with a "//" which indicates a path
59 // from the source root.
60 bool is_source_absolute() const {
61 return value_
.size() >= 2 && value_
[0] == '/' && value_
[1] == '/';
64 // Returns true if this path starts with a single slash which indicates a
65 // system-absolute path.
66 bool is_system_absolute() const {
67 return !is_source_absolute();
70 // Returns a source-absolute path starting with only one slash at the
71 // beginning (normally source-absolute paths start with two slashes to mark
72 // them as such). This is normally used when concatenating directories
75 // This function asserts that the directory is actually source-absolute. The
76 // return value points into our buffer.
77 base::StringPiece
SourceAbsoluteWithOneSlash() const {
78 CHECK(is_source_absolute());
79 return base::StringPiece(&value_
[1], value_
.size() - 1);
82 void SwapValue(std::string
* v
);
84 bool operator==(const SourceDir
& other
) const {
85 return value_
== other
.value_
;
87 bool operator!=(const SourceDir
& other
) const {
88 return !operator==(other
);
90 bool operator<(const SourceDir
& other
) const {
91 return value_
< other
.value_
;
94 void swap(SourceDir
& other
) {
95 value_
.swap(other
.value_
);
99 friend class SourceFile
;
102 // Copy & assign supported.
105 namespace BASE_HASH_NAMESPACE
{
107 #if defined(COMPILER_GCC)
108 template<> struct hash
<SourceDir
> {
109 std::size_t operator()(const SourceDir
& v
) const {
114 #elif defined(COMPILER_MSVC)
115 inline size_t hash_value(const SourceDir
& v
) {
116 return hash_value(v
.value());
118 #endif // COMPILER...
120 } // namespace BASE_HASH_NAMESPACE
122 inline void swap(SourceDir
& lhs
, SourceDir
& rhs
) {
126 #endif // TOOLS_GN_SOURCE_DIR_H_