1 /*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "code_writer.h"
17
18 #include "logging.h"
19
20 #include <stdarg.h>
21 #include <fstream>
22 #include <iostream>
23 #include <sstream>
24 #include <unordered_map>
25 #include <vector>
26
27 namespace android {
28 namespace aidl {
29
CodeWriter(std::unique_ptr<std::ostream> ostream)30 CodeWriter::CodeWriter(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {}
31
ApplyIndent(const std::string & str)32 std::string CodeWriter::ApplyIndent(const std::string& str) {
33 std::string output;
34 if (!start_of_line_ || str == "\n") {
35 output = str;
36 } else {
37 output = std::string(indent_level_ * 2, ' ') + str;
38 }
39 start_of_line_ = !output.empty() && output.back() == '\n';
40 return output;
41 }
42
WriteString(const std::string & str)43 bool CodeWriter::WriteString(const std::string& str) {
44 // extract lines. empty line is preserved.
45 std::vector<std::string> lines;
46 size_t pos = 0;
47 while (pos < str.size()) {
48 size_t line_end = str.find('\n', pos);
49 if (line_end != std::string::npos) {
50 lines.push_back(str.substr(pos, (line_end - pos) + 1));
51 pos = line_end + 1;
52 } else {
53 lines.push_back(str.substr(pos));
54 break;
55 }
56 }
57
58 std::string indented;
59 for (const auto& line : lines) {
60 indented.append(ApplyIndent(line));
61 }
62
63 (*ostream_) << indented;
64 return !ostream_->fail();
65 }
66
Indent()67 void CodeWriter::Indent() {
68 indent_level_++;
69 }
Dedent()70 void CodeWriter::Dedent() {
71 AIDL_FATAL_IF(indent_level_ <= 0, "Mismatched dedent");
72
73 indent_level_--;
74 }
75
Close()76 bool CodeWriter::Close() {
77 if (ostream_.get()->rdbuf() != std::cout.rdbuf()) {
78 // if the steam is for file (not stdout), do the close.
79 static_cast<std::fstream*>(ostream_.get())->close();
80 return !ostream_->fail();
81 }
82 return true;
83 }
84
operator <<(const char * s)85 CodeWriter& CodeWriter::operator<<(const char* s) {
86 Write("%s", s);
87 return *this;
88 }
89
operator <<(const std::string & str)90 CodeWriter& CodeWriter::operator<<(const std::string& str) {
91 Write("%s", str.c_str());
92 return *this;
93 }
94
ForFile(const std::string & filename)95 CodeWriterPtr CodeWriter::ForFile(const std::string& filename) {
96 std::unique_ptr<std::ostream> stream;
97 if (filename == "-") {
98 stream = std::unique_ptr<std::ostream>(new std::ostream(std::cout.rdbuf()));
99 } else {
100 stream = std::unique_ptr<std::ostream>(
101 new std::fstream(filename, std::fstream::out | std::fstream::binary));
102 }
103 return CodeWriterPtr(new CodeWriter(std::move(stream)));
104 }
105
ForString(std::string * buf)106 CodeWriterPtr CodeWriter::ForString(std::string* buf) {
107 // This class is defined inside this static function of CodeWriter
108 // in order to have access to private constructor and private member
109 // ostream_.
110 class StringCodeWriter : public CodeWriter {
111 public:
112 StringCodeWriter(std::string* buf)
113 : CodeWriter(std::unique_ptr<std::ostream>(new std::stringstream())), buf_(buf) {}
114 ~StringCodeWriter() override { Close(); }
115 bool Close() override {
116 // extract whats written to the stringstream to the external buffer.
117 // we are sure that ostream_ is indeed stringstream.
118 *buf_ = static_cast<std::stringstream*>(ostream_.get())->str();
119 return true;
120 }
121
122 private:
123 std::string* buf_;
124 };
125 return CodeWriterPtr(new StringCodeWriter(buf));
126 }
127
QuotedEscape(const std::string & str)128 std::string QuotedEscape(const std::string& str) {
129 std::string result;
130 result += '"';
131 static const std::unordered_map<char, std::string> escape = {
132 {'"', "\\\""}, {'\\', "\\\\"}, {'\n', "\\n"}, {'\r', "\\r"}, {'\t', "\\t"}, {'\v', "\\v"},
133 };
134 for (auto c : str) {
135 auto it = escape.find(c);
136 if (it != escape.end()) {
137 result += it->second;
138 } else {
139 result += c;
140 }
141 }
142 result += '"';
143 return result;
144 }
145
146 } // namespace aidl
147 } // namespace android
148