blob: 773a598fa9c03466b1f498a1dd27958977f7f687 [file] [log] [blame]
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001/*
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 */
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070016#include "code_writer.h"
17
Steven Moreland21780812020-09-11 01:29:45 +000018#include "logging.h"
19
Jiyong Parkb78e15b2018-07-04 20:31:03 +090020#include <stdarg.h>
Jiyong Parka755dc72018-06-29 13:52:24 +090021#include <fstream>
Christopher Wiley413e0422015-09-18 10:54:51 -070022#include <iostream>
Jiyong Parkb78e15b2018-07-04 20:31:03 +090023#include <sstream>
Jooyung Han6ec07c52021-01-11 16:21:53 +090024#include <unordered_map>
Jiyong Parka755dc72018-06-29 13:52:24 +090025#include <vector>
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070026
Elliott Hughes0a620672015-12-04 13:53:18 -080027#include <android-base/stringprintf.h>
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070028
29namespace android {
30namespace aidl {
31
Jiyong Parkb78e15b2018-07-04 20:31:03 +090032CodeWriter::CodeWriter(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {}
33
Jiyong Parka755dc72018-06-29 13:52:24 +090034std::string CodeWriter::ApplyIndent(const std::string& str) {
35 std::string output;
36 if (!start_of_line_ || str == "\n") {
37 output = str;
38 } else {
39 output = std::string(indent_level_ * 2, ' ') + str;
40 }
41 start_of_line_ = !output.empty() && output.back() == '\n';
42 return output;
43}
44
45bool CodeWriter::Write(const char* format, ...) {
46 va_list ap;
47 va_start(ap, format);
48 std::string formatted;
49 android::base::StringAppendV(&formatted, format, ap);
50 va_end(ap);
51
52 // extract lines. empty line is preserved.
53 std::vector<std::string> lines;
54 size_t pos = 0;
55 while (pos < formatted.size()) {
56 size_t line_end = formatted.find('\n', pos);
57 if (line_end != std::string::npos) {
58 lines.push_back(formatted.substr(pos, (line_end - pos) + 1));
59 pos = line_end + 1;
60 } else {
61 lines.push_back(formatted.substr(pos));
62 break;
63 }
64 }
65
66 std::string indented;
Chih-Hung Hsieh1e45eef2018-12-12 14:45:29 -080067 for (const auto& line : lines) {
Jiyong Parka755dc72018-06-29 13:52:24 +090068 indented.append(ApplyIndent(line));
69 }
Jiyong Parkb78e15b2018-07-04 20:31:03 +090070
71 (*ostream_) << indented;
72 return !ostream_->fail();
Jiyong Parka755dc72018-06-29 13:52:24 +090073}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070074
Steven Moreland201a3162019-03-06 12:04:46 -080075void CodeWriter::Indent() {
76 indent_level_++;
77}
78void CodeWriter::Dedent() {
Steven Moreland21780812020-09-11 01:29:45 +000079 AIDL_FATAL_IF(indent_level_ <= 0, "Mismatched dedent");
Steven Moreland201a3162019-03-06 12:04:46 -080080
81 indent_level_--;
82}
83
Jiyong Parkb78e15b2018-07-04 20:31:03 +090084bool CodeWriter::Close() {
85 if (ostream_.get()->rdbuf() != std::cout.rdbuf()) {
86 // if the steam is for file (not stdout), do the close.
87 static_cast<std::fstream*>(ostream_.get())->close();
88 return !ostream_->fail();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070089 }
Jiyong Parkb78e15b2018-07-04 20:31:03 +090090 return true;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070091}
92
Jiyong Park22b4ecd2018-07-23 23:34:42 +090093CodeWriter& CodeWriter::operator<<(const char* s) {
Steven Morelandd3721d62019-12-09 13:04:16 -080094 Write("%s", s);
Jiyong Park22b4ecd2018-07-23 23:34:42 +090095 return *this;
96}
97
98CodeWriter& CodeWriter::operator<<(const std::string& str) {
Steven Morelandd3721d62019-12-09 13:04:16 -080099 Write("%s", str.c_str());
Jiyong Park22b4ecd2018-07-23 23:34:42 +0900100 return *this;
101}
102
Jiyong Parkb78e15b2018-07-04 20:31:03 +0900103CodeWriterPtr CodeWriter::ForFile(const std::string& filename) {
104 std::unique_ptr<std::ostream> stream;
105 if (filename == "-") {
106 stream = std::unique_ptr<std::ostream>(new std::ostream(std::cout.rdbuf()));
107 } else {
108 stream = std::unique_ptr<std::ostream>(
109 new std::fstream(filename, std::fstream::out | std::fstream::binary));
110 }
111 return CodeWriterPtr(new CodeWriter(std::move(stream)));
112}
113
114CodeWriterPtr CodeWriter::ForString(std::string* buf) {
115 // This class is defined inside this static function of CodeWriter
116 // in order to have access to private constructor and private member
117 // ostream_.
118 class StringCodeWriter : public CodeWriter {
119 public:
120 StringCodeWriter(std::string* buf)
121 : CodeWriter(std::unique_ptr<std::ostream>(new std::stringstream())), buf_(buf) {}
Yi Kongde138912019-03-30 01:38:17 -0700122 ~StringCodeWriter() override { Close(); }
Jiyong Parkb78e15b2018-07-04 20:31:03 +0900123 bool Close() override {
124 // extract whats written to the stringstream to the external buffer.
125 // we are sure that ostream_ is indeed stringstream.
126 *buf_ = static_cast<std::stringstream*>(ostream_.get())->str();
127 return true;
128 }
129
130 private:
131 std::string* buf_;
132 };
133 return CodeWriterPtr(new StringCodeWriter(buf));
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700134}
135
Jooyung Han6ec07c52021-01-11 16:21:53 +0900136std::string QuotedEscape(const std::string& str) {
137 std::string result;
138 result += '"';
139 static const std::unordered_map<char, std::string> escape = {
140 {'"', "\\\""}, {'\\', "\\\\"}, {'\n', "\\n"}, {'\r', "\\r"}, {'\t', "\\t"}, {'\v', "\\v"},
141 };
142 for (auto c : str) {
143 auto it = escape.find(c);
144 if (it != escape.end()) {
145 result += it->second;
146 } else {
147 result += c;
148 }
149 }
150 result += '"';
151 return result;
152}
153
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700154} // namespace aidl
155} // namespace android