OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef TOOLS_GN_SOURCE_DIR_H_ |
| 6 #define TOOLS_GN_SOURCE_DIR_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 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" |
| 14 |
| 15 class SourceFile; |
| 16 |
| 17 // Represents a directory within the source tree. Source dirs begin and end in |
| 18 // slashes. |
| 19 // |
| 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". |
| 22 // |
| 23 // Two slashes at the beginning indicate a path relative to the source root. |
| 24 class SourceDir { |
| 25 public: |
| 26 SourceDir(); |
| 27 explicit SourceDir(const base::StringPiece& p); |
| 28 ~SourceDir(); |
| 29 |
| 30 // Resolves a file or dir name relative to this source directory. Will return |
| 31 // an empty SourceDir/File on error. Empty input is always an error (it's |
| 32 // possible we should say ResolveRelativeDir vs. an empty string should be |
| 33 // the source dir, but we require "." instead). |
| 34 SourceFile ResolveRelativeFile(const base::StringPiece& p) const; |
| 35 SourceDir ResolveRelativeDir(const base::StringPiece& p) const; |
| 36 |
| 37 // Resolves this source file relative to some given source root. Returns |
| 38 // an empty file path on error. |
| 39 base::FilePath Resolve(const base::FilePath& source_root) const; |
| 40 |
| 41 bool is_null() const { return value_.empty(); } |
| 42 const std::string& value() const { return value_; } |
| 43 |
| 44 // Returns true if this path starts with a "//" which indicates a path |
| 45 // from the source root. |
| 46 bool is_source_absolute() const { |
| 47 return value_.size() >= 2 && value_[0] == '/' && value_[1] == '/'; |
| 48 } |
| 49 |
| 50 // Returns true if this path starts with a single slash which indicates a |
| 51 // system-absolute path. |
| 52 bool is_system_absolute() const { |
| 53 return !is_source_absolute(); |
| 54 } |
| 55 |
| 56 // Returns a source-absolute path starting with only one slash at the |
| 57 // beginning (normally source-absolute paths start with two slashes to mark |
| 58 // them as such). This is normally used when concatenating directories |
| 59 // together. |
| 60 // |
| 61 // This function asserts that the directory is actually source-absolute. The |
| 62 // return value points into our buffer. |
| 63 base::StringPiece SourceAbsoluteWithOneSlash() const { |
| 64 CHECK(is_source_absolute()); |
| 65 return base::StringPiece(&value_[1], value_.size() - 1); |
| 66 } |
| 67 |
| 68 void SwapInValue(std::string* v); |
| 69 |
| 70 bool operator==(const SourceDir& other) const { |
| 71 return value_ == other.value_; |
| 72 } |
| 73 bool operator!=(const SourceDir& other) const { |
| 74 return !operator==(other); |
| 75 } |
| 76 bool operator<(const SourceDir& other) const { |
| 77 return value_ < other.value_; |
| 78 } |
| 79 |
| 80 private: |
| 81 friend class SourceFile; |
| 82 std::string value_; |
| 83 |
| 84 // Copy & assign supported. |
| 85 }; |
| 86 |
| 87 namespace BASE_HASH_NAMESPACE { |
| 88 |
| 89 #if defined(COMPILER_GCC) |
| 90 template<> struct hash<SourceDir> { |
| 91 std::size_t operator()(const SourceDir& v) const { |
| 92 hash<std::string> h; |
| 93 return h(v.value()); |
| 94 } |
| 95 }; |
| 96 #elif defined(COMPILER_MSVC) |
| 97 inline size_t hash_value(const SourceDir& v) { |
| 98 return hash_value(v.value()); |
| 99 } |
| 100 #endif // COMPILER... |
| 101 |
| 102 } // namespace BASE_HASH_NAMESPACE |
| 103 |
| 104 #endif // TOOLS_GN_SOURCE_DIR_H_ |
OLD | NEW |