blob: e75d7a5f6e1d04a2000c1755b3a27b970106448d [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
Elliott Hughes0a620672015-12-04 13:53:18 -080024#include <android-base/stringprintf.h>
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070025
26namespace android {
27namespace aidl {
28
Jiyong Parkb78e15b2018-07-04 20:31:03 +090029CodeWriter::CodeWriter(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {}
30
Jiyong Parka755dc72018-06-29 13:52:24 +090031std::string CodeWriter::ApplyIndent(const std::string& str) {
32 std::string output;
33 if (!start_of_line_ || str == "\n") {
34 output = str;
35 } else {
36 output = std::string(indent_level_ * 2, ' ') + str;
37 }
38 start_of_line_ = !output.empty() && output.back() == '\n';
39 return output;
40}
41
42bool CodeWriter::Write(const char* format, ...) {
43 va_list ap;
44 va_start(ap, format);
45 std::string formatted;
46 android::base::StringAppendV(&formatted, format, ap);
47 va_end(ap);
48
49 // extract lines. empty line is preserved.
50 std::vector<std::string> lines;
51 size_t pos = 0;
52 while (pos < formatted.size()) {
53 size_t line_end = formatted.find('\n', pos);
54 if (line_end != std::string::npos) {
55 lines.push_back(formatted.substr(pos, (line_end - pos) + 1));
56 pos = line_end + 1;
57 } else {
58 lines.push_back(formatted.substr(pos));
59 break;
60 }
61 }
62
63 std::string indented;
64 for (auto line : lines) {
65 indented.append(ApplyIndent(line));
66 }
Jiyong Parkb78e15b2018-07-04 20:31:03 +090067
68 (*ostream_) << indented;
69 return !ostream_->fail();
Jiyong Parka755dc72018-06-29 13:52:24 +090070}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070071
Jiyong Parkb78e15b2018-07-04 20:31:03 +090072bool CodeWriter::Close() {
73 if (ostream_.get()->rdbuf() != std::cout.rdbuf()) {
74 // if the steam is for file (not stdout), do the close.
75 static_cast<std::fstream*>(ostream_.get())->close();
76 return !ostream_->fail();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070077 }
Jiyong Parkb78e15b2018-07-04 20:31:03 +090078 return true;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070079}
80
Jiyong Parkb78e15b2018-07-04 20:31:03 +090081CodeWriterPtr CodeWriter::ForFile(const std::string& filename) {
82 std::unique_ptr<std::ostream> stream;
83 if (filename == "-") {
84 stream = std::unique_ptr<std::ostream>(new std::ostream(std::cout.rdbuf()));
85 } else {
86 stream = std::unique_ptr<std::ostream>(
87 new std::fstream(filename, std::fstream::out | std::fstream::binary));
88 }
89 return CodeWriterPtr(new CodeWriter(std::move(stream)));
90}
91
92CodeWriterPtr CodeWriter::ForString(std::string* buf) {
93 // This class is defined inside this static function of CodeWriter
94 // in order to have access to private constructor and private member
95 // ostream_.
96 class StringCodeWriter : public CodeWriter {
97 public:
98 StringCodeWriter(std::string* buf)
99 : CodeWriter(std::unique_ptr<std::ostream>(new std::stringstream())), buf_(buf) {}
100 ~StringCodeWriter() { Close(); }
101 bool Close() override {
102 // extract whats written to the stringstream to the external buffer.
103 // we are sure that ostream_ is indeed stringstream.
104 *buf_ = static_cast<std::stringstream*>(ostream_.get())->str();
105 return true;
106 }
107
108 private:
109 std::string* buf_;
110 };
111 return CodeWriterPtr(new StringCodeWriter(buf));
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700112}
113
114} // namespace aidl
115} // namespace android