blob: 5626d3dd7a779476afd4dee9676bbfa40536a631 [file] [log] [blame]
Adam Lesinskiffa16862014-01-23 18:17:42 -08001#include "generate_java.h"
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07002
Adam Lesinskiffa16862014-01-23 18:17:42 -08003#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07007#include "Type.h"
8#include "code_writer.h"
9
10namespace android {
11namespace aidl {
12
Adam Lesinskiffa16862014-01-23 18:17:42 -080013// =================================================
14VariableFactory::VariableFactory(const string& base)
15 :m_base(base),
16 m_index(0)
17{
18}
19
20Variable*
21VariableFactory::Get(Type* type)
22{
23 char name[100];
24 sprintf(name, "%s%d", m_base.c_str(), m_index);
25 m_index++;
26 Variable* v = new Variable(type, name);
27 m_vars.push_back(v);
28 return v;
29}
30
31Variable*
32VariableFactory::Get(int index)
33{
34 return m_vars[index];
35}
36
37// =================================================
38string
39gather_comments(extra_text_type* extra)
40{
41 string s;
42 while (extra) {
43 if (extra->which == SHORT_COMMENT) {
44 s += extra->data;
45 }
46 else if (extra->which == LONG_COMMENT) {
47 s += "/*";
48 s += extra->data;
49 s += "*/";
50 }
51 extra = extra->next;
52 }
53 return s;
54}
55
56string
57append(const char* a, const char* b)
58{
59 string s = a;
60 s += b;
61 return s;
62}
63
64// =================================================
65int
66generate_java(const string& filename, const string& originalSrc,
67 interface_type* iface)
68{
69 Class* cl;
70
71 if (iface->document_item.item_type == INTERFACE_TYPE_BINDER) {
72 cl = generate_binder_interface_class(iface);
73 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080074
75 Document* document = new Document;
76 document->comment = "";
77 if (iface->package) document->package = iface->package;
78 document->originalSrc = originalSrc;
79 document->classes.push_back(cl);
80
81// printf("outputting... filename=%s\n", filename.c_str());
82 FILE* to;
83 if (filename == "-") {
84 to = stdout;
85 } else {
86 /* open file in binary mode to ensure that the tool produces the
87 * same output on all platforms !!
88 */
89 to = fopen(filename.c_str(), "wb");
90 if (to == NULL) {
91 fprintf(stderr, "unable to open %s for write\n", filename.c_str());
92 return 1;
93 }
94 }
95
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070096 CodeWriterPtr code_writer = get_file_writer(to);
97 document->Write(code_writer.get());
Adam Lesinskiffa16862014-01-23 18:17:42 -080098
Adam Lesinskiffa16862014-01-23 18:17:42 -080099 return 0;
100}
101
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700102} // namespace android
103} // namespace aidl