blob: 7425c39ba2707bc96cb8f2f90d96c74dc9291dc6 [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 */
16
17#include "code_writer.h"
18
Christopher Wiley413e0422015-09-18 10:54:51 -070019#include <iostream>
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070020#include <stdarg.h>
21
22#include <base/stringprintf.h>
23
Christopher Wiley413e0422015-09-18 10:54:51 -070024using std::cerr;
25using std::endl;
26
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070027namespace android {
28namespace aidl {
29
30namespace {
31
32class 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
48class FileCodeWriter : public CodeWriter {
49 public:
Christopher Wiley413e0422015-09-18 10:54:51 -070050 FileCodeWriter(FILE* output_file, bool close_on_destruction)
51 : output_(output_file),
52 close_on_destruction_(close_on_destruction) {}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070053 ~FileCodeWriter() {
Christopher Wiley413e0422015-09-18 10:54:51 -070054 if (close_on_destruction_) {
55 fclose(output_);
56 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070057 }
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 Wiley413e0422015-09-18 10:54:51 -070070 bool close_on_destruction_;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070071}; // class StringCodeWriter
72
73} // namespace
74
Christopher Wiley413e0422015-09-18 10:54:51 -070075CodeWriterPtr 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 Wileyfdeb0f42015-09-11 15:38:22 -070095}
96
Christopher Wiley413e0422015-09-18 10:54:51 -070097CodeWriterPtr GetStringWriter(std::string* output_buffer) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070098 return CodeWriterPtr(new StringCodeWriter(output_buffer));
99}
100
101} // namespace aidl
102} // namespace android