blob: 7425c39ba2707bc96cb8f2f90d96c74dc9291dc6 [file] [log] [blame]
/*
* Copyright (C) 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "code_writer.h"
#include <iostream>
#include <stdarg.h>
#include <base/stringprintf.h>
using std::cerr;
using std::endl;
namespace android {
namespace aidl {
namespace {
class StringCodeWriter : public CodeWriter {
public:
StringCodeWriter(std::string* output_buffer) : output_(output_buffer) {}
bool Write(const char* format, ...) override {
va_list ap;
va_start(ap, format);
android::base::StringAppendV(output_, format, ap);
va_end(ap);
return true;
}
private:
std::string* output_;
}; // class StringCodeWriter
class FileCodeWriter : public CodeWriter {
public:
FileCodeWriter(FILE* output_file, bool close_on_destruction)
: output_(output_file),
close_on_destruction_(close_on_destruction) {}
~FileCodeWriter() {
if (close_on_destruction_) {
fclose(output_);
}
}
bool Write(const char* format, ...) override {
bool success;
va_list ap;
va_start(ap, format);
success = vfprintf(output_, format, ap) >= 0;
va_end(ap);
return success;
}
private:
FILE* output_;
bool close_on_destruction_;
}; // class StringCodeWriter
} // namespace
CodeWriterPtr GetFileWriter(const std::string& output_file) {
CodeWriterPtr result;
FILE* to = nullptr;
bool close_on_destruction = true;
if (output_file == "-") {
to = stdout;
close_on_destruction = false;
} else {
// open file in binary mode to ensure that the tool produces the
// same output on all platforms !!
to = fopen(output_file.c_str(), "wb");
}
if (to != nullptr) {
result.reset(new FileCodeWriter(to, close_on_destruction));
} else {
cerr << "unable to open " << output_file << " for write" << endl;
}
return result;
}
CodeWriterPtr GetStringWriter(std::string* output_buffer) {
return CodeWriterPtr(new StringCodeWriter(output_buffer));
}
} // namespace aidl
} // namespace android