summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Wiley <wiley@google.com>2015-09-18 10:54:51 -0700
committerChristopher Wiley <wiley@google.com>2015-09-19 10:46:31 -0700
commit413e0426eebdb41d8e9344bd2a6a0ef79369de25 (patch)
treefe7a506a380a61cb5dfbb0dd7ce063de42562e09
parent88924d60d8758c87ad48e6d227839eb354cf369e (diff)
downloadandroid_system_tools_aidl-413e0426eebdb41d8e9344bd2a6a0ef79369de25.tar.gz
android_system_tools_aidl-413e0426eebdb41d8e9344bd2a6a0ef79369de25.tar.bz2
android_system_tools_aidl-413e0426eebdb41d8e9344bd2a6a0ef79369de25.zip
Move logic to write to stdout into CodeWriter
This will allow us to reuse it when we write C++ code. I think this will mostly come in handy for debugging. While here, rename the helper functions in accordance with the style guide. Test: unittests Bug: 24220933 Change-Id: I269eb4a0dda10588c2ae77b7c8f2b00e50e70047
-rw-r--r--ast_cpp_unittest.cpp2
-rw-r--r--ast_java_unittest.cpp2
-rw-r--r--code_writer.cpp37
-rw-r--r--code_writer.h6
-rw-r--r--generate_java.cpp17
5 files changed, 37 insertions, 27 deletions
diff --git a/ast_cpp_unittest.cpp b/ast_cpp_unittest.cpp
index ce1981d..0b2d707 100644
--- a/ast_cpp_unittest.cpp
+++ b/ast_cpp_unittest.cpp
@@ -79,7 +79,7 @@ TEST(AstCppTests, GeneratesHeader) {
}}
};
string actual_output;
- CodeWriterPtr writer = get_string_writer(&actual_output);
+ CodeWriterPtr writer = GetStringWriter(&actual_output);
cpp_header.Write(writer.get());
EXPECT_EQ(string(kExpectedHeaderOutput), actual_output);
}
diff --git a/ast_java_unittest.cpp b/ast_java_unittest.cpp
index 2d90462..31dfb5a 100644
--- a/ast_java_unittest.cpp
+++ b/ast_java_unittest.cpp
@@ -46,7 +46,7 @@ TEST(AstJavaTests, GeneratesClass) {
a_class.extends = &extend_type;
string actual_output;
- CodeWriterPtr writer = get_string_writer(&actual_output);
+ CodeWriterPtr writer = GetStringWriter(&actual_output);
a_class.Write(writer.get());
EXPECT_EQ(string(kExpectedClassOutput), actual_output);
}
diff --git a/code_writer.cpp b/code_writer.cpp
index e2ef362..7425c39 100644
--- a/code_writer.cpp
+++ b/code_writer.cpp
@@ -16,10 +16,14 @@
#include "code_writer.h"
+#include <iostream>
#include <stdarg.h>
#include <base/stringprintf.h>
+using std::cerr;
+using std::endl;
+
namespace android {
namespace aidl {
@@ -43,9 +47,13 @@ class StringCodeWriter : public CodeWriter {
class FileCodeWriter : public CodeWriter {
public:
- FileCodeWriter(FILE* output_file) : output_(output_file) {}
+ FileCodeWriter(FILE* output_file, bool close_on_destruction)
+ : output_(output_file),
+ close_on_destruction_(close_on_destruction) {}
~FileCodeWriter() {
- fclose(output_);
+ if (close_on_destruction_) {
+ fclose(output_);
+ }
}
bool Write(const char* format, ...) override {
@@ -59,15 +67,34 @@ class FileCodeWriter : public CodeWriter {
private:
FILE* output_;
+ bool close_on_destruction_;
}; // class StringCodeWriter
} // namespace
-CodeWriterPtr get_file_writer(FILE* output_file) {
- return CodeWriterPtr(new FileCodeWriter(output_file));
+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 get_string_writer(std::string* output_buffer) {
+CodeWriterPtr GetStringWriter(std::string* output_buffer) {
return CodeWriterPtr(new StringCodeWriter(output_buffer));
}
diff --git a/code_writer.h b/code_writer.h
index bc617c0..0519d74 100644
--- a/code_writer.h
+++ b/code_writer.h
@@ -38,14 +38,12 @@ class CodeWriter {
using CodeWriterPtr = std::unique_ptr<CodeWriter>;
// Get a CodeWriter that writes to |output_file|.
-// The CodeWriter will take ownership of |output_file| and close it
-// upon destruction.
-CodeWriterPtr get_file_writer(FILE* output_file);
+CodeWriterPtr GetFileWriter(const std::string& output_file);
// Get a CodeWriter that writes to a string buffer.
// Caller retains ownership of the buffer.
// The buffer must outlive the CodeWriter.
-CodeWriterPtr get_string_writer(std::string* output_buffer);
+CodeWriterPtr GetStringWriter(std::string* output_buffer);
} // namespace aidl
} // namespace android
diff --git a/generate_java.cpp b/generate_java.cpp
index 5626d3d..868783c 100644
--- a/generate_java.cpp
+++ b/generate_java.cpp
@@ -78,22 +78,7 @@ generate_java(const string& filename, const string& originalSrc,
document->originalSrc = originalSrc;
document->classes.push_back(cl);
-// printf("outputting... filename=%s\n", filename.c_str());
- FILE* to;
- if (filename == "-") {
- to = stdout;
- } else {
- /* open file in binary mode to ensure that the tool produces the
- * same output on all platforms !!
- */
- to = fopen(filename.c_str(), "wb");
- if (to == NULL) {
- fprintf(stderr, "unable to open %s for write\n", filename.c_str());
- return 1;
- }
- }
-
- CodeWriterPtr code_writer = get_file_writer(to);
+ CodeWriterPtr code_writer = GetFileWriter(filename);
document->Write(code_writer.get());
return 0;