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 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace aidl { |
| 28 | |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame^] | 29 | CodeWriter::CodeWriter(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {} |
| 30 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 31 | std::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 | |
| 42 | bool 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 Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame^] | 67 | |
| 68 | (*ostream_) << indented; |
| 69 | return !ostream_->fail(); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 70 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 71 | |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame^] | 72 | bool 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 77 | } |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame^] | 78 | return true; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame^] | 81 | CodeWriterPtr 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 | |
| 92 | CodeWriterPtr 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | } // namespace aidl |
| 115 | } // namespace android |