Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | #include "generate_java.h" |
| 2 | #include "Type.h" |
| 3 | #include <string.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | // ================================================= |
| 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 | // ================================================= |
| 33 | string |
| 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 | |
| 51 | string |
| 52 | append(const char* a, const char* b) |
| 53 | { |
| 54 | string s = a; |
| 55 | s += b; |
| 56 | return s; |
| 57 | } |
| 58 | |
| 59 | // ================================================= |
| 60 | int |
| 61 | generate_java(const string& filename, const string& originalSrc, |
| 62 | interface_type* iface) |
| 63 | { |
| 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 | |
| 73 | Document* document = new Document; |
| 74 | document->comment = ""; |
| 75 | if (iface->package) document->package = iface->package; |
| 76 | document->originalSrc = originalSrc; |
| 77 | document->classes.push_back(cl); |
| 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 | |