blob: 9e57407e772ff8620d5bf5b9c191fd363bad5a1f [file] [log] [blame]
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001#include "generate_java.h"
The Android Open Source Project46c012c2008-10-21 07:00:00 -07002#include "Type.h"
Alexey Zaytsevccace322008-10-21 23:52:01 +04003#include <string.h>
The Android Open Source Project46c012c2008-10-21 07:00:00 -07004#include <stdio.h>
The Android Open Source Projectbcaa86d2009-01-09 17:51:23 -08005#include <stdlib.h>
6#include <string.h>
The Android Open Source Project46c012c2008-10-21 07:00:00 -07007
8// =================================================
The Android Open Source Project46c012c2008-10-21 07:00:00 -07009VariableFactory::VariableFactory(const string& base)
10 :m_base(base),
11 m_index(0)
12{
13}
14
15Variable*
16VariableFactory::Get(Type* type)
17{
18 char name[100];
19 sprintf(name, "%s%d", m_base.c_str(), m_index);
20 m_index++;
21 Variable* v = new Variable(type, name);
22 m_vars.push_back(v);
23 return v;
24}
25
26Variable*
27VariableFactory::Get(int index)
28{
29 return m_vars[index];
30}
31
32// =================================================
Joe Onoratoc596cfe2011-08-30 17:24:17 -070033string
The Android Open Source Project46c012c2008-10-21 07:00:00 -070034gather_comments(extra_text_type* extra)
35{
36 string s;
37 while (extra) {
38 if (extra->which == SHORT_COMMENT) {
39 s += extra->data;
40 }
41 else if (extra->which == LONG_COMMENT) {
42 s += "/*";
43 s += extra->data;
44 s += "*/";
45 }
46 extra = extra->next;
47 }
48 return s;
49}
50
Joe Onoratoc596cfe2011-08-30 17:24:17 -070051string
The Android Open Source Project46c012c2008-10-21 07:00:00 -070052append(const char* a, const char* b)
53{
54 string s = a;
55 s += b;
56 return s;
57}
58
Joe Onoratoc596cfe2011-08-30 17:24:17 -070059// =================================================
The Android Open Source Project46c012c2008-10-21 07:00:00 -070060int
61generate_java(const string& filename, const string& originalSrc,
62 interface_type* iface)
63{
Joe Onoratoc596cfe2011-08-30 17:24:17 -070064 Class* cl;
65
66 if (iface->document_item.item_type == INTERFACE_TYPE_BINDER) {
67 cl = generate_binder_interface_class(iface);
68 }
69 else if (iface->document_item.item_type == INTERFACE_TYPE_RPC) {
70 cl = generate_rpc_interface_class(iface);
71 }
72
The Android Open Source Project46c012c2008-10-21 07:00:00 -070073 Document* document = new Document;
74 document->comment = "";
75 if (iface->package) document->package = iface->package;
76 document->originalSrc = originalSrc;
Joe Onoratoc596cfe2011-08-30 17:24:17 -070077 document->classes.push_back(cl);
The Android Open Source Project46c012c2008-10-21 07:00:00 -070078
79// printf("outputting... filename=%s\n", filename.c_str());
80 FILE* to;
81 if (filename == "-") {
82 to = stdout;
83 } else {
84 /* open file in binary mode to ensure that the tool produces the
85 * same output on all platforms !!
86 */
87 to = fopen(filename.c_str(), "wb");
88 if (to == NULL) {
89 fprintf(stderr, "unable to open %s for write\n", filename.c_str());
90 return 1;
91 }
92 }
93
94 document->Write(to);
95
96 fclose(to);
97 return 0;
98}
99