Home | History | Annotate | Download | only in gn
      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   enum SwapIn { SWAP_IN };
     27 
     28   SourceDir();
     29   explicit SourceDir(const base::StringPiece& p);
     30   // Swaps the given string in without copies. The given string will be empty
     31   // after this call.
     32   SourceDir(SwapIn, std::string* s);
     33   ~SourceDir();
     34 
     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).
     39   //
     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;
     49 
     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;
     53 
     54   bool is_null() const { return value_.empty(); }
     55   const std::string& value() const { return value_; }
     56 
     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] == '/';
     61   }
     62 
     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();
     67   }
     68 
     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
     72   // together.
     73   //
     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);
     79   }
     80 
     81   void SwapValue(std::string* v);
     82 
     83   bool operator==(const SourceDir& other) const {
     84     return value_ == other.value_;
     85   }
     86   bool operator!=(const SourceDir& other) const {
     87     return !operator==(other);
     88   }
     89   bool operator<(const SourceDir& other) const {
     90     return value_ < other.value_;
     91   }
     92 
     93  private:
     94   friend class SourceFile;
     95   std::string value_;
     96 
     97   // Copy & assign supported.
     98 };
     99 
    100 namespace BASE_HASH_NAMESPACE {
    101 
    102 #if defined(COMPILER_GCC)
    103 template<> struct hash<SourceDir> {
    104   std::size_t operator()(const SourceDir& v) const {
    105     hash<std::string> h;
    106     return h(v.value());
    107   }
    108 };
    109 #elif defined(COMPILER_MSVC)
    110 inline size_t hash_value(const SourceDir& v) {
    111   return hash_value(v.value());
    112 }
    113 #endif  // COMPILER...
    114 
    115 }  // namespace BASE_HASH_NAMESPACE
    116 
    117 #endif  // TOOLS_GN_SOURCE_DIR_H_
    118