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 <android-base/strings.h>
21 
22 #include <stdarg.h>
23 #include <fstream>
24 #include <iostream>
25 #include <sstream>
26 #include <unordered_map>
27 #include <vector>
28 
29 using android::base::StringReplace;
30 
31 namespace android {
32 namespace aidl {
33 
CodeWriter(std::unique_ptr<std::ostream> ostream)34 CodeWriter::CodeWriter(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {}
35 
ApplyIndent(const std::string & str)36 std::string CodeWriter::ApplyIndent(const std::string& str) {
37   std::string output;
38   if (!start_of_line_ || str == "\n") {
39     output = str;
40   } else {
41     output = std::string(indent_level_ * 2, ' ') + str;
42   }
43   start_of_line_ = !output.empty() && output.back() == '\n';
44   return output;
45 }
46 
WriteString(const std::string & str)47 bool CodeWriter::WriteString(const std::string& str) {
48   // extract lines. empty line is preserved.
49   std::vector<std::string> lines;
50   size_t pos = 0;
51   while (pos < str.size()) {
52     size_t line_end = str.find('\n', pos);
53     if (line_end != std::string::npos) {
54       lines.push_back(str.substr(pos, (line_end - pos) + 1));
55       pos = line_end + 1;
56     } else {
57       lines.push_back(str.substr(pos));
58       break;
59     }
60   }
61 
62   std::string indented;
63   for (const auto& line : lines) {
64     indented.append(ApplyIndent(line));
65   }
66 
67   (*ostream_) << indented;
68   return !ostream_->fail();
69 }
70 
Indent()71 void CodeWriter::Indent() {
72   indent_level_++;
73 }
Dedent()74 void CodeWriter::Dedent() {
75   AIDL_FATAL_IF(indent_level_ <= 0, "Mismatched dedent");
76 
77   indent_level_--;
78 }
79 
Close()80 bool CodeWriter::Close() {
81   if (ostream_.get()->rdbuf() != std::cout.rdbuf()) {
82     // if the steam is for file (not stdout), do the close.
83     static_cast<std::fstream*>(ostream_.get())->close();
84     return !ostream_->fail();
85   }
86   return true;
87 }
88 
operator <<(const char * s)89 CodeWriter& CodeWriter::operator<<(const char* s) {
90   Write("%s", s);
91   return *this;
92 }
93 
operator <<(const std::string & str)94 CodeWriter& CodeWriter::operator<<(const std::string& str) {
95   Write("%s", str.c_str());
96   return *this;
97 }
98 
ForFile(const std::string & filename)99 CodeWriterPtr CodeWriter::ForFile(const std::string& filename) {
100   std::unique_ptr<std::ostream> stream;
101   if (filename == "-") {
102     stream = std::unique_ptr<std::ostream>(new std::ostream(std::cout.rdbuf()));
103   } else {
104     stream = std::unique_ptr<std::ostream>(
105         new std::fstream(filename, std::fstream::out | std::fstream::binary));
106   }
107   return CodeWriterPtr(new CodeWriter(std::move(stream)));
108 }
109 
ForString(std::string * buf)110 CodeWriterPtr CodeWriter::ForString(std::string* buf) {
111   // This class is defined inside this static function of CodeWriter
112   // in order to have access to private constructor and private member
113   // ostream_.
114   class StringCodeWriter : public CodeWriter {
115    public:
116     StringCodeWriter(std::string* buf)
117         : CodeWriter(std::unique_ptr<std::ostream>(new std::stringstream())), buf_(buf) {}
118     ~StringCodeWriter() override { Close(); }
119     bool Close() override {
120       // extract whats written to the stringstream to the external buffer.
121       // we are sure that ostream_ is indeed stringstream.
122       *buf_ = static_cast<std::stringstream*>(ostream_.get())->str();
123       return true;
124     }
125 
126    private:
127     std::string* buf_;
128   };
129   return CodeWriterPtr(new StringCodeWriter(buf));
130 }
131 
QuotedEscape(const std::string & str)132 std::string QuotedEscape(const std::string& str) {
133   std::string result;
134   result += '"';
135   static const std::unordered_map<char, std::string> escape = {
136       {'"', "\\\""}, {'\\', "\\\\"}, {'\n', "\\n"}, {'\r', "\\r"}, {'\t', "\\t"}, {'\v', "\\v"},
137   };
138   for (auto c : str) {
139     auto it = escape.find(c);
140     if (it != escape.end()) {
141       result += it->second;
142     } else {
143       result += c;
144     }
145   }
146   result += '"';
147   return result;
148 }
149 
MultilineCommentEscape(const std::string & str)150 std::string MultilineCommentEscape(const std::string& str) {
151   std::string res;
152   res = StringReplace(str, "\\", "\\\\", true /* all */);
153   res = StringReplace(res, "*/", "* /", true /* all */);
154   return res;
155 }
156 
157 }  // namespace aidl
158 }  // namespace android
159