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 <stdarg.h>
19 #include <fstream>
20 #include <iostream>
21 #include <sstream>
22 #include <vector>
23
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
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
Write(const char * format,...)43 bool CodeWriter::Write(const char* format, ...) {
44 va_list ap;
45 va_start(ap, format);
46 std::string formatted;
47 android::base::StringAppendV(&formatted, format, ap);
48 va_end(ap);
49
50 // extract lines. empty line is preserved.
51 std::vector<std::string> lines;
52 size_t pos = 0;
53 while (pos < formatted.size()) {
54 size_t line_end = formatted.find('\n', pos);
55 if (line_end != std::string::npos) {
56 lines.push_back(formatted.substr(pos, (line_end - pos) + 1));
57 pos = line_end + 1;
58 } else {
59 lines.push_back(formatted.substr(pos));
60 break;
61 }
62 }
63
64 std::string indented;
65 for (const auto& line : lines) {
66 indented.append(ApplyIndent(line));
67 }
68
69 (*ostream_) << indented;
70 return !ostream_->fail();
71 }
72
Indent()73 void CodeWriter::Indent() {
74 indent_level_++;
75 }
Dedent()76 void CodeWriter::Dedent() {
77 CHECK(indent_level_ > 0);
78
79 indent_level_--;
80 }
81
Close()82 bool CodeWriter::Close() {
83 if (ostream_.get()->rdbuf() != std::cout.rdbuf()) {
84 // if the steam is for file (not stdout), do the close.
85 static_cast<std::fstream*>(ostream_.get())->close();
86 return !ostream_->fail();
87 }
88 return true;
89 }
90
operator <<(const char * s)91 CodeWriter& CodeWriter::operator<<(const char* s) {
92 Write("%s", s);
93 return *this;
94 }
95
operator <<(const std::string & str)96 CodeWriter& CodeWriter::operator<<(const std::string& str) {
97 Write("%s", str.c_str());
98 return *this;
99 }
100
ForFile(const std::string & filename)101 CodeWriterPtr CodeWriter::ForFile(const std::string& filename) {
102 std::unique_ptr<std::ostream> stream;
103 if (filename == "-") {
104 stream = std::unique_ptr<std::ostream>(new std::ostream(std::cout.rdbuf()));
105 } else {
106 stream = std::unique_ptr<std::ostream>(
107 new std::fstream(filename, std::fstream::out | std::fstream::binary));
108 }
109 return CodeWriterPtr(new CodeWriter(std::move(stream)));
110 }
111
ForString(std::string * buf)112 CodeWriterPtr CodeWriter::ForString(std::string* buf) {
113 // This class is defined inside this static function of CodeWriter
114 // in order to have access to private constructor and private member
115 // ostream_.
116 class StringCodeWriter : public CodeWriter {
117 public:
118 StringCodeWriter(std::string* buf)
119 : CodeWriter(std::unique_ptr<std::ostream>(new std::stringstream())), buf_(buf) {}
120 ~StringCodeWriter() override { Close(); }
121 bool Close() override {
122 // extract whats written to the stringstream to the external buffer.
123 // we are sure that ostream_ is indeed stringstream.
124 *buf_ = static_cast<std::stringstream*>(ostream_.get())->str();
125 return true;
126 }
127
128 private:
129 std::string* buf_;
130 };
131 return CodeWriterPtr(new StringCodeWriter(buf));
132 }
133
134 } // namespace aidl
135 } // namespace android
136