Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 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 | */ |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 16 | #include "code_writer.h" |
| 17 | |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 18 | #include <stdarg.h> |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 19 | #include <fstream> |
Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 20 | #include <iostream> |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 21 | #include <sstream> |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 22 | #include <vector> |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 23 | |
Steven Moreland | 201a316 | 2019-03-06 12:04:46 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace aidl { |
| 29 | |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 30 | CodeWriter::CodeWriter(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {} |
| 31 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 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 | |
| 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; |
Chih-Hung Hsieh | 1e45eef | 2018-12-12 14:45:29 -0800 | [diff] [blame] | 65 | for (const auto& line : lines) { |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 66 | indented.append(ApplyIndent(line)); |
| 67 | } |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 68 | |
| 69 | (*ostream_) << indented; |
| 70 | return !ostream_->fail(); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 71 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 72 | |
Steven Moreland | 201a316 | 2019-03-06 12:04:46 -0800 | [diff] [blame] | 73 | void CodeWriter::Indent() { |
| 74 | indent_level_++; |
| 75 | } |
| 76 | void CodeWriter::Dedent() { |
| 77 | CHECK(indent_level_ > 0); |
| 78 | |
| 79 | indent_level_--; |
| 80 | } |
| 81 | |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 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(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 87 | } |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 88 | return true; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jiyong Park | 22b4ecd | 2018-07-23 23:34:42 +0900 | [diff] [blame] | 91 | CodeWriter& CodeWriter::operator<<(const char* s) { |
Steven Moreland | d3721d6 | 2019-12-09 13:04:16 -0800 | [diff] [blame] | 92 | Write("%s", s); |
Jiyong Park | 22b4ecd | 2018-07-23 23:34:42 +0900 | [diff] [blame] | 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | CodeWriter& CodeWriter::operator<<(const std::string& str) { |
Steven Moreland | d3721d6 | 2019-12-09 13:04:16 -0800 | [diff] [blame] | 97 | Write("%s", str.c_str()); |
Jiyong Park | 22b4ecd | 2018-07-23 23:34:42 +0900 | [diff] [blame] | 98 | return *this; |
| 99 | } |
| 100 | |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 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 | |
| 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) {} |
Yi Kong | de13891 | 2019-03-30 01:38:17 -0700 | [diff] [blame] | 120 | ~StringCodeWriter() override { Close(); } |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 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)); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // namespace aidl |
| 135 | } // namespace android |