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_
10 #include "base/containers/hash_tables.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/strings/string_piece.h"
17 // Represents a directory within the source tree. Source dirs begin and end in
20 // If there is one slash at the beginning, it will mean a system-absolute file
21 // path. On Windows, absolute system paths will be of the form "/C:/foo/bar".
23 // Two slashes at the beginning indicate a path relative to the source root.
26 enum SwapIn
{ SWAP_IN
};
29 explicit SourceDir(const base::StringPiece
& p
);
30 // Swaps the given string in without copies. The given string will be empty
32 SourceDir(SwapIn
, std::string
* s
);
35 // Resolves a file or dir name relative to this source directory. Will return
36 // an empty SourceDir/File on error. Empty input is always an error (it's
37 // possible we should say ResolveRelativeDir vs. an empty string should be
38 // the source dir, but we require "." instead).
40 // If source_root is supplied, these functions will additionally handle the
41 // case where the input is a system-absolute but still inside the source
42 // tree. This is the case for some external tools.
43 SourceFile
ResolveRelativeFile(
44 const base::StringPiece
& p
,
45 const base::StringPiece
& source_root
= base::StringPiece()) const;
46 SourceDir
ResolveRelativeDir(
47 const base::StringPiece
& p
,
48 const base::StringPiece
& source_root
= base::StringPiece()) const;
50 // Resolves this source file relative to some given source root. Returns
51 // an empty file path on error.
52 base::FilePath
Resolve(const base::FilePath
& source_root
) const;
54 bool is_null() const { return value_
.empty(); }
55 const std::string
& value() const { return value_
; }
57 // Returns true if this path starts with a "//" which indicates a path
58 // from the source root.
59 bool is_source_absolute() const {
60 return value_
.size() >= 2 && value_
[0] == '/' && value_
[1] == '/';
63 // Returns true if this path starts with a single slash which indicates a
64 // system-absolute path.
65 bool is_system_absolute() const {
66 return !is_source_absolute();
69 // Returns a source-absolute path starting with only one slash at the
70 // beginning (normally source-absolute paths start with two slashes to mark
71 // them as such). This is normally used when concatenating directories
74 // This function asserts that the directory is actually source-absolute. The
75 // return value points into our buffer.
76 base::StringPiece
SourceAbsoluteWithOneSlash() const {
77 CHECK(is_source_absolute());
78 return base::StringPiece(&value_
[1], value_
.size() - 1);
81 void SwapValue(std::string
* v
);
83 bool operator==(const SourceDir
& other
) const {
84 return value_
== other
.value_
;
86 bool operator!=(const SourceDir
& other
) const {
87 return !operator==(other
);
89 bool operator<(const SourceDir
& other
) const {
90 return value_
< other
.value_
;
94 friend class SourceFile
;
97 // Copy & assign supported.
100 namespace BASE_HASH_NAMESPACE
{
102 #if defined(COMPILER_GCC)
103 template<> struct hash
<SourceDir
> {
104 std::size_t operator()(const SourceDir
& v
) const {
109 #elif defined(COMPILER_MSVC)
110 inline size_t hash_value(const SourceDir
& v
) {
111 return hash_value(v
.value());
113 #endif // COMPILER...
115 } // namespace BASE_HASH_NAMESPACE
117 #endif // TOOLS_GN_SOURCE_DIR_H_