blob: e2ef36210914030ed6804b12d320e461fe46d784 [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
19#include <stdarg.h>
20
21#include <base/stringprintf.h>
22
23namespace android {
24namespace aidl {
25
26namespace {
27
28class StringCodeWriter : public CodeWriter {
29 public:
30 StringCodeWriter(std::string* output_buffer) : output_(output_buffer) {}
31
32 bool Write(const char* format, ...) override {
33 va_list ap;
34 va_start(ap, format);
35 android::base::StringAppendV(output_, format, ap);
36 va_end(ap);
37 return true;
38 }
39
40 private:
41 std::string* output_;
42}; // class StringCodeWriter
43
44class FileCodeWriter : public CodeWriter {
45 public:
46 FileCodeWriter(FILE* output_file) : output_(output_file) {}
47 ~FileCodeWriter() {
48 fclose(output_);
49 }
50
51 bool Write(const char* format, ...) override {
52 bool success;
53 va_list ap;
54 va_start(ap, format);
55 success = vfprintf(output_, format, ap) >= 0;
56 va_end(ap);
57 return success;
58 }
59
60 private:
61 FILE* output_;
62}; // class StringCodeWriter
63
64} // namespace
65
66CodeWriterPtr get_file_writer(FILE* output_file) {
67 return CodeWriterPtr(new FileCodeWriter(output_file));
68}
69
70CodeWriterPtr get_string_writer(std::string* output_buffer) {
71 return CodeWriterPtr(new StringCodeWriter(output_buffer));
72}
73
74} // namespace aidl
75} // namespace android