blob: 096c89556e6d352e67598067ba5486d176676841 [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
Jiyong Parkb78e15b2018-07-04 20:31:03 +090018#include <stdarg.h>
Jiyong Parka755dc72018-06-29 13:52:24 +090019#include <fstream>
Christopher Wiley413e0422015-09-18 10:54:51 -070020#include <iostream>
Jiyong Parkb78e15b2018-07-04 20:31:03 +090021#include <sstream>
Jiyong Parka755dc72018-06-29 13:52:24 +090022#include <vector>
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070023
Steven Moreland201a3162019-03-06 12:04:46 -080024#include <android-base/logging.h>
Elliott Hughes0a620672015-12-04 13:53:18 -080025#include <android-base/stringprintf.h>
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070026
27namespace android {
28namespace aidl {
29
Jiyong Parkb78e15b2018-07-04 20:31:03 +090030CodeWriter::CodeWriter(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {}
31
Jiyong Parka755dc72018-06-29 13:52:24 +090032std::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
43bool 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;
Chih-Hung Hsieh1e45eef2018-12-12 14:45:29 -080065 for (const auto& line : lines) {
Jiyong Parka755dc72018-06-29 13:52:24 +090066 indented.append(ApplyIndent(line));
67 }
Jiyong Parkb78e15b2018-07-04 20:31:03 +090068
69 (*ostream_) << indented;
70 return !ostream_->fail();
Jiyong Parka755dc72018-06-29 13:52:24 +090071}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070072
Steven Moreland201a3162019-03-06 12:04:46 -080073void CodeWriter::Indent() {
74 indent_level_++;
75}
76void CodeWriter::Dedent() {
77 CHECK(indent_level_ > 0);
78
79 indent_level_--;
80}
81
Jiyong Parkb78e15b2018-07-04 20:31:03 +090082bool 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();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070087 }
Jiyong Parkb78e15b2018-07-04 20:31:03 +090088 return true;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070089}
90
Jiyong Park22b4ecd2018-07-23 23:34:42 +090091CodeWriter& CodeWriter::operator<<(const char* s) {
Steven Morelandd3721d62019-12-09 13:04:16 -080092 Write("%s", s);
Jiyong Park22b4ecd2018-07-23 23:34:42 +090093 return *this;
94}
95
96CodeWriter& CodeWriter::operator<<(const std::string& str) {
Steven Morelandd3721d62019-12-09 13:04:16 -080097 Write("%s", str.c_str());
Jiyong Park22b4ecd2018-07-23 23:34:42 +090098 return *this;
99}
100
Jiyong Parkb78e15b2018-07-04 20:31:03 +0900101CodeWriterPtr 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
112CodeWriterPtr 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) {}
Yi Kongde138912019-03-30 01:38:17 -0700120 ~StringCodeWriter() override { Close(); }
Jiyong Parkb78e15b2018-07-04 20:31:03 +0900121 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));
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700132}
133
134} // namespace aidl
135} // namespace android