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_FILE_H_ 6 #define TOOLS_GN_SOURCE_FILE_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 SourceDir; 16 17 // Represents a file within the source tree. Always begins in a slash, never 18 // ends in one. 19 class SourceFile { 20 public: 21 SourceFile(); 22 23 // Takes a known absolute source file. Always begins in a slash. 24 explicit SourceFile(const base::StringPiece& p); 25 26 ~SourceFile(); 27 28 bool is_null() const { return value_.empty(); } 29 const std::string& value() const { return value_; } 30 31 // Returns everythign after the last slash. 32 std::string GetName() const; 33 SourceDir GetDir() const; 34 35 // Resolves this source file relative to some given source root. Returns 36 // an empty file path on error. 37 base::FilePath Resolve(const base::FilePath& source_root) const; 38 39 // Returns true if this file starts with a "//" which indicates a path 40 // from the source root. 41 bool is_source_absolute() const { 42 return value_.size() >= 2 && value_[0] == '/' && value_[1] == '/'; 43 } 44 45 // Returns true if this file starts with a single slash which indicates a 46 // system-absolute path. 47 bool is_system_absolute() const { 48 return !is_source_absolute(); 49 } 50 51 // Returns a source-absolute path starting with only one slash at the 52 // beginning (normally source-absolute paths start with two slashes to mark 53 // them as such). This is normally used when concatenating names together. 54 // 55 // This function asserts that the file is actually source-absolute. The 56 // return value points into our buffer. 57 base::StringPiece SourceAbsoluteWithOneSlash() const { 58 CHECK(is_source_absolute()); 59 return base::StringPiece(&value_[1], value_.size() - 1); 60 } 61 62 bool operator==(const SourceFile& other) const { 63 return value_ == other.value_; 64 } 65 bool operator!=(const SourceFile& other) const { 66 return !operator==(other); 67 } 68 bool operator<(const SourceFile& other) const { 69 return value_ < other.value_; 70 } 71 72 private: 73 friend class SourceDir; 74 75 std::string value_; 76 77 // Copy & assign supported. 78 }; 79 80 namespace BASE_HASH_NAMESPACE { 81 82 #if defined(COMPILER_GCC) 83 template<> struct hash<SourceFile> { 84 std::size_t operator()(const SourceFile& v) const { 85 hash<std::string> h; 86 return h(v.value()); 87 } 88 }; 89 #elif defined(COMPILER_MSVC) 90 inline size_t hash_value(const SourceFile& v) { 91 return hash_value(v.value()); 92 } 93 #endif // COMPILER... 94 95 } // namespace BASE_HASH_NAMESPACE 96 97 #endif // TOOLS_GN_SOURCE_FILE_H_ 98