• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PATH_OUTPUT_H_
6 #define TOOLS_GN_PATH_OUTPUT_H_
7 
8 #include <iosfwd>
9 #include <string>
10 #include <string_view>
11 
12 #include "base/macros.h"
13 #include "gn/escape.h"
14 #include "gn/source_dir.h"
15 #include "gn/unique_vector.h"
16 
17 class OutputFile;
18 class SourceFile;
19 
20 namespace base {
21 class FilePath;
22 }
23 
24 // Writes file names to streams assuming a certain input directory and
25 // escaping rules. This gives us a central place for managing this state.
26 class PathOutput {
27  public:
28   // Controls whether writing directory names include the trailing slash.
29   // Often we don't want the trailing slash when writing out to a command line,
30   // especially on Windows where it's a backslash and might be interpreted as
31   // escaping the thing following it.
32   enum DirSlashEnding {
33     DIR_INCLUDE_LAST_SLASH,
34     DIR_NO_LAST_SLASH,
35   };
36 
37   PathOutput(const SourceDir& current_dir,
38              const std::string_view& source_root,
39              EscapingMode escaping);
40   ~PathOutput();
41 
42   // Read-only since inverse_current_dir_ is computed depending on this.
escaping_mode()43   EscapingMode escaping_mode() const { return options_.mode; }
44 
current_dir()45   const SourceDir& current_dir() const { return current_dir_; }
46 
47   // Getter/setters for flags inside the escape options.
inhibit_quoting()48   bool inhibit_quoting() const { return options_.inhibit_quoting; }
set_inhibit_quoting(bool iq)49   void set_inhibit_quoting(bool iq) { options_.inhibit_quoting = iq; }
set_escape_platform(EscapingPlatform p)50   void set_escape_platform(EscapingPlatform p) { options_.platform = p; }
51 
52   void WriteFile(std::ostream& out, const SourceFile& file) const;
53   void WriteFile(std::ostream& out, const OutputFile& file) const;
54   void WriteFile(std::ostream& out, const base::FilePath& file) const;
55 
56   // Writes the given OutputFiles with spaces separating them. This will also
57   // write an initial space before the first item.
58   void WriteFiles(std::ostream& out,
59                   const std::vector<OutputFile>& files) const;
60   void WriteFiles(std::ostream& out,
61                   const UniqueVector<OutputFile>& files) const;
62 
63   // This variant assumes the dir ends in a trailing slash or is empty.
64   void WriteDir(std::ostream& out,
65                 const SourceDir& dir,
66                 DirSlashEnding slash_ending) const;
67 
68   void WriteDir(std::ostream& out,
69                 const OutputFile& file,
70                 DirSlashEnding slash_ending) const;
71 
72   // Backend for WriteFile and WriteDir. This appends the given file or
73   // directory string to the file.
74   void WritePathStr(std::ostream& out, const std::string_view& str) const;
75 
76  private:
77   // Takes the given string and writes it out, appending to the inverse
78   // current dir. This assumes leading slashes have been trimmed.
79   void WriteSourceRelativeString(std::ostream& out,
80                                  const std::string_view& str) const;
81 
82   SourceDir current_dir_;
83 
84   // Uses system slashes if convert_slashes_to_system_.
85   std::string inverse_current_dir_;
86 
87   // Since the inverse_current_dir_ depends on some of these, we don't expose
88   // this directly to modification.
89   EscapeOptions options_;
90 };
91 
92 #endif  // TOOLS_GN_PATH_OUTPUT_H_
93