The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #include "generate_java.h" |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | #include "Type.h" |
Alexey Zaytsev | 0aa7fe6 | 2008-10-21 23:52:01 +0400 | [diff] [blame] | 3 | #include <string.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4 | #include <stdio.h> |
The Android Open Source Project | b798689 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 7 | |
| 8 | // ================================================= |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 9 | VariableFactory::VariableFactory(const string& base) |
| 10 | :m_base(base), |
| 11 | m_index(0) |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | Variable* |
| 16 | VariableFactory::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 | |
| 26 | Variable* |
| 27 | VariableFactory::Get(int index) |
| 28 | { |
| 29 | return m_vars[index]; |
| 30 | } |
| 31 | |
| 32 | // ================================================= |
Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame^] | 33 | string |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | gather_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 Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame^] | 51 | string |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 52 | append(const char* a, const char* b) |
| 53 | { |
| 54 | string s = a; |
| 55 | s += b; |
| 56 | return s; |
| 57 | } |
| 58 | |
Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame^] | 59 | // ================================================= |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | int |
| 61 | generate_java(const string& filename, const string& originalSrc, |
| 62 | interface_type* iface) |
| 63 | { |
Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame^] | 64 | 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | Document* document = new Document; |
| 74 | document->comment = ""; |
| 75 | if (iface->package) document->package = iface->package; |
| 76 | document->originalSrc = originalSrc; |
Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame^] | 77 | document->classes.push_back(cl); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | |
| 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 | |