| 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 | */ |
| 16 | |
| 17 | #include "code_writer.h" |
| 18 | |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 19 | #include <iostream> |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | |
| 22 | #include <base/stringprintf.h> |
| 23 | |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 24 | using std::cerr; |
| 25 | using std::endl; |
| 26 | |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace aidl { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | class StringCodeWriter : public CodeWriter { |
| 33 | public: |
| 34 | StringCodeWriter(std::string* output_buffer) : output_(output_buffer) {} |
| 35 | |
| 36 | bool Write(const char* format, ...) override { |
| 37 | va_list ap; |
| 38 | va_start(ap, format); |
| 39 | android::base::StringAppendV(output_, format, ap); |
| 40 | va_end(ap); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | std::string* output_; |
| 46 | }; // class StringCodeWriter |
| 47 | |
| 48 | class FileCodeWriter : public CodeWriter { |
| 49 | public: |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 50 | FileCodeWriter(FILE* output_file, bool close_on_destruction) |
| 51 | : output_(output_file), |
| 52 | close_on_destruction_(close_on_destruction) {} |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 53 | ~FileCodeWriter() { |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 54 | if (close_on_destruction_) { |
| 55 | fclose(output_); |
| 56 | } |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool Write(const char* format, ...) override { |
| 60 | bool success; |
| 61 | va_list ap; |
| 62 | va_start(ap, format); |
| 63 | success = vfprintf(output_, format, ap) >= 0; |
| 64 | va_end(ap); |
| 65 | return success; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | FILE* output_; |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 70 | bool close_on_destruction_; |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 71 | }; // class StringCodeWriter |
| 72 | |
| 73 | } // namespace |
| 74 | |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 75 | CodeWriterPtr GetFileWriter(const std::string& output_file) { |
| 76 | CodeWriterPtr result; |
| 77 | FILE* to = nullptr; |
| 78 | bool close_on_destruction = true; |
| 79 | if (output_file == "-") { |
| 80 | to = stdout; |
| 81 | close_on_destruction = false; |
| 82 | } else { |
| 83 | // open file in binary mode to ensure that the tool produces the |
| 84 | // same output on all platforms !! |
| 85 | to = fopen(output_file.c_str(), "wb"); |
| 86 | } |
| 87 | |
| 88 | if (to != nullptr) { |
| 89 | result.reset(new FileCodeWriter(to, close_on_destruction)); |
| 90 | } else { |
| 91 | cerr << "unable to open " << output_file << " for write" << endl; |
| 92 | } |
| 93 | |
| 94 | return result; |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 97 | CodeWriterPtr GetStringWriter(std::string* output_buffer) { |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 98 | return CodeWriterPtr(new StringCodeWriter(output_buffer)); |
| 99 | } |
| 100 | |
| 101 | } // namespace aidl |
| 102 | } // namespace android |