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_INPUT_FILE_H_ 6 #define TOOLS_GN_INPUT_FILE_H_ 7 8 #include <string> 9 10 #include "base/files/file_path.h" 11 #include "base/logging.h" 12 #include "gn/source_dir.h" 13 #include "gn/source_file.h" 14 15 class InputFile { 16 public: 17 explicit InputFile(const SourceFile& name); 18 19 ~InputFile(); 20 21 // The virtual name passed into the constructor. This does not take into 22 // account whether the file was loaded from the secondary source tree (see 23 // BuildSettings secondary_source_path). name()24 const SourceFile& name() const { return name_; } 25 26 // The directory is just a cached version of name()->GetDir() but we get this 27 // a lot so computing it once up front saves a bunch of work. dir()28 const SourceDir& dir() const { return dir_; } 29 30 // The physical name tells the actual name on disk, if there is one. physical_name()31 const base::FilePath& physical_name() const { return physical_name_; } 32 33 // The friendly name can be set to override the name() in cases where there 34 // is no name (like SetContents is used instead) or if the name doesn't 35 // make sense. This will be displayed in error messages. friendly_name()36 const std::string& friendly_name() const { return friendly_name_; } set_friendly_name(const std::string & f)37 void set_friendly_name(const std::string& f) { friendly_name_ = f; } 38 contents()39 const std::string& contents() const { 40 DCHECK(contents_loaded_); 41 return contents_; 42 } 43 44 // For testing and in cases where this input doesn't actually refer to 45 // "a file". 46 void SetContents(const std::string& c); 47 48 // Loads the given file synchronously, returning true on success. This 49 bool Load(const base::FilePath& system_path); 50 51 private: 52 SourceFile name_; 53 SourceDir dir_; 54 55 base::FilePath physical_name_; 56 std::string friendly_name_; 57 58 bool contents_loaded_ = false; 59 std::string contents_; 60 61 InputFile(const InputFile&) = delete; 62 InputFile& operator=(const InputFile&) = delete; 63 }; 64 65 #endif // TOOLS_GN_INPUT_FILE_H_ 66