1d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// found in the LICENSE file.
4d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
5d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#ifndef TOOLS_GN_PATH_OUTPUT_H_
6d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#define TOOLS_GN_PATH_OUTPUT_H_
7d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
8d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include <iosfwd>
9d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include <string>
10d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
11d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "base/basictypes.h"
12d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "base/strings/string_piece.h"
13d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/escape.h"
14d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/source_dir.h"
15d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
16d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass OutputFile;
17d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass SourceFile;
18d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace base {
204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class FilePath;
214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
23d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Writes file names to streams assuming a certain input directory and
24d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// escaping rules. This gives us a central place for managing this state.
25d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass PathOutput {
26d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch public:
27d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Controls whether writing directory names include the trailing slash.
28d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Often we don't want the trailing slash when writing out to a command line,
29d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // especially on Windows where it's a backslash and might be interpreted as
30d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // escaping the thing following it.
31d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  enum DirSlashEnding {
32d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    DIR_INCLUDE_LAST_SLASH,
33d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    DIR_NO_LAST_SLASH,
34d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  };
35d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  PathOutput(const SourceDir& current_dir, EscapingMode escaping);
37d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  ~PathOutput();
38d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
39d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Read-only since inverse_current_dir_ is computed depending on this.
40d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  EscapingMode escaping_mode() const { return options_.mode; }
41d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const SourceDir& current_dir() const { return current_dir_; }
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Getter/setters for flags inside the escape options.
45d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  bool inhibit_quoting() const { return options_.inhibit_quoting; }
46d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  void set_inhibit_quoting(bool iq) { options_.inhibit_quoting = iq; }
4746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void set_escape_platform(EscapingPlatform p) { options_.platform = p; }
48d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
49d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  void WriteFile(std::ostream& out, const SourceFile& file) const;
50d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  void WriteFile(std::ostream& out, const OutputFile& file) const;
514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  void WriteFile(std::ostream& out, const base::FilePath& file) const;
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
5303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Writes the given OutputFiles with spaces separating them. This will also
5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // write an initial space before the first item.
5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  void WriteFiles(std::ostream& out,
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                  const std::vector<OutputFile>& files) const;
5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // This variant assumes the dir ends in a trailing slash or is empty.
59d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  void WriteDir(std::ostream& out,
60d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch                const SourceDir& dir,
61d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch                DirSlashEnding slash_ending) const;
62d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void WriteDir(std::ostream& out,
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                const OutputFile& file,
655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                DirSlashEnding slash_ending) const;
665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
67d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Backend for WriteFile and WriteDir. This appends the given file or
68d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // directory string to the file.
69d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  void WritePathStr(std::ostream& out, const base::StringPiece& str) const;
70d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
71d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch private:
72d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Takes the given string and writes it out, appending to the inverse
73d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // current dir. This assumes leading slashes have been trimmed.
74d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  void WriteSourceRelativeString(std::ostream& out,
75d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch                                 const base::StringPiece& str) const;
76d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
77d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  SourceDir current_dir_;
78d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
79d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Uses system slashes if convert_slashes_to_system_.
80d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  std::string inverse_current_dir_;
81d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
82d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Since the inverse_current_dir_ depends on some of these, we don't expose
83d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // this directly to modification.
84d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  EscapeOptions options_;
85d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch};
86d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
87d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#endif  // TOOLS_GN_PATH_OUTPUT_H_
88