X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=wrappergen%2Fwrite_jna.c;h=228520625e2cccc5428581bddbe3d4fc6cb0656b;hb=2d4a842e50718fa66d8d13aac18871c090de6b31;hp=add8d4b139220d978554190138e0e4bae7e38c7a;hpb=85bc20f10d096f390a5b5f81e97b7ca1efe94500;p=cparser diff --git a/wrappergen/write_jna.c b/wrappergen/write_jna.c index add8d4b..2285206 100644 --- a/wrappergen/write_jna.c +++ b/wrappergen/write_jna.c @@ -1,6 +1,6 @@ /* * This file is part of cparser. - * Copyright (C) 2007-2008 Matthias Braun + * Copyright (C) 2007-2009 Matthias Braun * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -28,6 +28,7 @@ #include "type_t.h" #include "entity_t.h" #include "type.h" +#include "printer.h" #include "adt/error.h" #include @@ -39,7 +40,11 @@ static void write_type(type_t *type); static bool is_system_header(const char *fname) { - return strncmp(fname, "/usr/include", 12) == 0; + if (strncmp(fname, "/usr/include", 12) == 0) + return true; + if (fname == builtin_source_position.input_name) + return true; + return false; } static const char *fix_builtin_names(const char *name) @@ -72,8 +77,8 @@ static const char *get_atomic_type_string(const atomic_type_kind_t type) case ATOMIC_TYPE_USHORT: return "short"; case ATOMIC_TYPE_INT: return "int"; case ATOMIC_TYPE_UINT: return "int"; - case ATOMIC_TYPE_LONG: return "NativeLong"; - case ATOMIC_TYPE_ULONG: return "NativeLong"; + case ATOMIC_TYPE_LONG: return "com.sun.jna.NativeLong"; + case ATOMIC_TYPE_ULONG: return "com.sun.jna.NativeLong"; case ATOMIC_TYPE_LONGLONG: return "long"; case ATOMIC_TYPE_ULONGLONG: return "long"; case ATOMIC_TYPE_FLOAT: return "float"; @@ -96,6 +101,11 @@ static void write_pointer_type(const pointer_type_t *type) fputs("String", out); return; } + if (is_type_pointer(points_to)) { + /* hack... */ + fputs("java.nio.Buffer", out); + return; + } fputs("Pointer", out); } @@ -292,16 +302,14 @@ static void write_binary_expression(const binary_expression_t *expression) static void write_expression(const expression_t *expression) { - const const_expression_t *constant; /* TODO */ switch(expression->kind) { - case EXPR_CONST: - constant = &expression->conste; - if(is_type_integer(expression->base.type)) { - fprintf(out, "%lld", constant->v.int_value); - } else { - fprintf(out, "%Lf", constant->v.float_value); - } + case EXPR_LITERAL_INTEGER: + case EXPR_LITERAL_INTEGER_OCTAL: + fprintf(out, "%s", expression->literal.value.begin); + break; + case EXPR_LITERAL_INTEGER_HEXADECIMAL: + fprintf(out, "0x%s", expression->literal.value.begin); break; case EXPR_REFERENCE_ENUM_VALUE: { /* UHOH... hacking */ @@ -393,7 +401,7 @@ static void write_function(const entity_t *entity) const function_type_t *function_type = (const function_type_t*) entity->declaration.type; - fputc('\t', out); + fprintf(out, "\tpublic static native "); type_t *return_type = skip_typeref(function_type->return_type); write_type(return_type); fprintf(out, " %s(", entity->base.symbol->string); @@ -434,33 +442,33 @@ void write_jna_decls(FILE *output, const translation_unit_t *unit) pset_new_init(&avoid_symbols); - ast_set_output(out); - type_set_output(out); + print_to_file(out); fprintf(out, "/* WARNING: Automatically generated file */\n"); - fputs("import com.sun.jna.Library;\n", out); fputs("import com.sun.jna.Native;\n", out); - fputs("import com.sun.jna.Platform;\n", out); fputs("import com.sun.jna.Pointer;\n", out); - fputs("import com.sun.jna.NativeLong;\n", out); fputs("\n\n", out); /* TODO: where to get the name from? */ - fputs("public interface binding extends Library {\n", out); + fputs("public class binding {\n", out); + fputs("\tstatic { Native.register(\"firm\"); }\n", out); /* read the avoid list */ FILE *avoid = fopen("avoid.config", "r"); if (avoid != NULL) { while (!feof(avoid)) { char buf[1024]; - fgets(buf, sizeof(buf), avoid); + char *res = fgets(buf, sizeof(buf), avoid); + if (res == NULL) + break; + if (buf[0] == 0) + continue; + size_t len = strlen(buf); if (buf[len-1] == '\n') buf[len-1] = 0; - if (buf[0] == 0) - continue; - char *str = malloc(len); - memcpy(str, buf, len); + char *str = malloc(len+1); + memcpy(str, buf, len+1); symbol_t *symbol = symbol_table_insert(str); pset_new_insert(&avoid_symbols, symbol); }