aboutsummaryrefslogtreecommitdiff
path: root/code_writer.cpp
blob: 8dd59da8de8d48e3c782933041982b34bbe62ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2015, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "code_writer.h"

#include "logging.h"

#include <stdarg.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>

namespace android {
namespace aidl {

CodeWriter::CodeWriter(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {}

std::string CodeWriter::ApplyIndent(const std::string& str) {
  std::string output;
  if (!start_of_line_ || str == "\n") {
    output = str;
  } else {
    output = std::string(indent_level_ * 2, ' ') + str;
  }
  start_of_line_ = !output.empty() && output.back() == '\n';
  return output;
}

bool CodeWriter::WriteString(const std::string& str) {
  // extract lines. empty line is preserved.
  std::vector<std::string> lines;
  size_t pos = 0;
  while (pos < str.size()) {
    size_t line_end = str.find('\n', pos);
    if (line_end != std::string::npos) {
      lines.push_back(str.substr(pos, (line_end - pos) + 1));
      pos = line_end + 1;
    } else {
      lines.push_back(str.substr(pos));
      break;
    }
  }

  std::string indented;
  for (const auto& line : lines) {
    indented.append(ApplyIndent(line));
  }

  (*ostream_) << indented;
  return !ostream_->fail();
}

void CodeWriter::Indent() {
  indent_level_++;
}
void CodeWriter::Dedent() {
  AIDL_FATAL_IF(indent_level_ <= 0, "Mismatched dedent");

  indent_level_--;
}

bool CodeWriter::Close() {
  if (ostream_.get()->rdbuf() != std::cout.rdbuf()) {
    // if the steam is for file (not stdout), do the close.
    static_cast<std::fstream*>(ostream_.get())->close();
    return !ostream_->fail();
  }
  return true;
}

CodeWriter& CodeWriter::operator<<(const char* s) {
  Write("%s", s);
  return *this;
}

CodeWriter& CodeWriter::operator<<(const std::string& str) {
  Write("%s", str.c_str());
  return *this;
}

CodeWriterPtr CodeWriter::ForFile(const std::string& filename) {
  std::unique_ptr<std::ostream> stream;
  if (filename == "-") {
    stream = std::unique_ptr<std::ostream>(new std::ostream(std::cout.rdbuf()));
  } else {
    stream = std::unique_ptr<std::ostream>(
        new std::fstream(filename, std::fstream::out | std::fstream::binary));
  }
  return CodeWriterPtr(new CodeWriter(std::move(stream)));
}

CodeWriterPtr CodeWriter::ForString(std::string* buf) {
  // This class is defined inside this static function of CodeWriter
  // in order to have access to private constructor and private member
  // ostream_.
  class StringCodeWriter : public CodeWriter {
   public:
    StringCodeWriter(std::string* buf)
        : CodeWriter(std::unique_ptr<std::ostream>(new std::stringstream())), buf_(buf) {}
    ~StringCodeWriter() override { Close(); }
    bool Close() override {
      // extract whats written to the stringstream to the external buffer.
      // we are sure that ostream_ is indeed stringstream.
      *buf_ = static_cast<std::stringstream*>(ostream_.get())->str();
      return true;
    }

   private:
    std::string* buf_;
  };
  return CodeWriterPtr(new StringCodeWriter(buf));
}

std::string QuotedEscape(const std::string& str) {
  std::string result;
  result += '"';
  static const std::unordered_map<char, std::string> escape = {
      {'"', "\\\""}, {'\\', "\\\\"}, {'\n', "\\n"}, {'\r', "\\r"}, {'\t', "\\t"}, {'\v', "\\v"},
  };
  for (auto c : str) {
    auto it = escape.find(c);
    if (it != escape.end()) {
      result += it->second;
    } else {
      result += c;
    }
  }
  result += '"';
  return result;
}

}  // namespace aidl
}  // namespace android