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
11#include "base/basictypes.h"
12#include "base/strings/string_piece.h"
13#include "tools/gn/escape.h"
14#include "tools/gn/source_dir.h"
15
16class OutputFile;
17class SourceFile;
18
19// Writes file names to streams assuming a certain input directory and
20// escaping rules. This gives us a central place for managing this state.
21class PathOutput {
22 public:
23  // Controls whether writing directory names include the trailing slash.
24  // Often we don't want the trailing slash when writing out to a command line,
25  // especially on Windows where it's a backslash and might be interpreted as
26  // escaping the thing following it.
27  enum DirSlashEnding {
28    DIR_INCLUDE_LAST_SLASH,
29    DIR_NO_LAST_SLASH,
30  };
31
32  PathOutput(const SourceDir& current_dir,
33             EscapingMode escaping,
34             bool convert_slashes);
35  ~PathOutput();
36
37  // Read-only since inverse_current_dir_ is computed depending on this.
38  EscapingMode escaping_mode() const { return options_.mode; }
39
40  // When true, converts slashes to the system-type path separators (on
41  // Windows, this is a backslash, this is a NOP otherwise).
42  //
43  // Read-only since inverse_current_dir_ is computed depending on this.
44  bool convert_slashes_to_system() const { return options_.convert_slashes; }
45
46  // When the output escaping is ESCAPE_SHELL, the escaper will normally put
47  // quotes around suspect things. If this value is set to true, we'll disable
48  // the quoting feature. This means that in ESCAPE_SHELL mode, strings with
49  // spaces in them qon't be quoted. This mode is for when quoting is done at
50  // some higher-level. Defaults to false.
51  bool inhibit_quoting() const { return options_.inhibit_quoting; }
52  void set_inhibit_quoting(bool iq) { options_.inhibit_quoting = iq; }
53
54  void WriteFile(std::ostream& out, const SourceFile& file) const;
55  void WriteFile(std::ostream& out, const OutputFile& file) const;
56  void WriteDir(std::ostream& out,
57                const SourceDir& dir,
58                DirSlashEnding slash_ending) const;
59
60  // Backend for WriteFile and WriteDir. This appends the given file or
61  // directory string to the file.
62  void WritePathStr(std::ostream& out, const base::StringPiece& str) const;
63
64 private:
65  // Takes the given string and writes it out, appending to the inverse
66  // current dir. This assumes leading slashes have been trimmed.
67  void WriteSourceRelativeString(std::ostream& out,
68                                 const base::StringPiece& str) const;
69
70  SourceDir current_dir_;
71
72  // Uses system slashes if convert_slashes_to_system_.
73  std::string inverse_current_dir_;
74
75  // Since the inverse_current_dir_ depends on some of these, we don't expose
76  // this directly to modification.
77  EscapeOptions options_;
78};
79
80#endif  // TOOLS_GN_PATH_OUTPUT_H_
81