blob: 15093406da6cd0c35a062691a6d8e7b64e63fd98 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001#include "generate_java.h"
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07002#include "Type.h"
Alexey Zaytsev0aa7fe62008-10-21 23:52:01 +04003#include <string.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004#include <stdio.h>
The Android Open Source Projectb7986892009-01-09 17:51:23 -08005#include <stdlib.h>
6#include <string.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07007
8// =================================================
The Android Open Source Project54b6cfa2008-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 Onoratofdfe2ff2011-08-30 17:24:17 -070033string
The Android Open Source Project54b6cfa2008-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 Onoratofdfe2ff2011-08-30 17:24:17 -070051string
The Android Open Source Project54b6cfa2008-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 Onoratofdfe2ff2011-08-30 17:24:17 -070059// =================================================
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070060int
61generate_java(const string& filename, const string& originalSrc,
62 interface_type* iface)
63{
Joe Onoratofdfe2ff2011-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 }
Joe Onoratofdfe2ff2011-08-30 17:24:17 -070069
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070070 Document* document = new Document;
71 document->comment = "";
72 if (iface->package) document->package = iface->package;
73 document->originalSrc = originalSrc;
Joe Onoratofdfe2ff2011-08-30 17:24:17 -070074 document->classes.push_back(cl);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070075
76// printf("outputting... filename=%s\n", filename.c_str());
77 FILE* to;
78 if (filename == "-") {
79 to = stdout;
80 } else {
81 /* open file in binary mode to ensure that the tool produces the
82 * same output on all platforms !!
83 */
84 to = fopen(filename.c_str(), "wb");
85 if (to == NULL) {
86 fprintf(stderr, "unable to open %s for write\n", filename.c_str());
87 return 1;
88 }
89 }
90
91 document->Write(to);
92
93 fclose(to);
94 return 0;
95}
96