1

I have a C function that returns an unsigned char * that can either be a pointer to a byte array (binary data representing a File..etc) or a pointer to an array of characters. I'm currently using the SWIG %array_class that wraps all C functions that return an unsigned char pointer and creates a Java array utility (SampleArrayUtil.java) to handle the population and retrieval on the Java side.

My problem is that I also use wrap the unsigned char * using: %apply char * { unsigned char * }; so that I get an array of Strings on the Java side. I don't want to wrap the unsigned char * return value (using %apply char * { unsigned char * };) when I get binary data back, I want to just have the byte array on the Java side. I was thinking of creating another C function to handle the binary data, but I'm unsure how to wrap this new function as it will also return an unsigned char * (see getValueFromRowAsByteArray)

C Functions:

unsigned char * getValueFromRowAsStringArray(struct result_row *row, attribute_type type, int32_t *len)

unsigned char * getValueFromRowAsByteArray(struct result_row *row, attribute_type type, int32_t *len)
//*row* input param with data results, *type* input enum type for the data type being requested and *len* is an output param that contains the length of the data being returned.

SWIG Interface File for Wrapping C Function Returning unsigned char * (array of char):

%module Sample
%include "typemaps.i"
%include "stdint.i"
%include "arrays_java.i"
%include "carrays.i"
%array_class(unsigned char, SampleArrayUtil);
%{
#include "C_API.h"
%}
%apply char * { unsigned char * };
%include "C_API.h"
Flexo
  • 87,323
  • 22
  • 191
  • 272
c12
  • 9,557
  • 48
  • 157
  • 253
  • So you're looking to have different types returned on the Java interface from the same types on the C side - you want to selectively apply the typemaps? (Just checking we're on the same page before I write an answer) – Flexo Feb 18 '12 at 11:51
  • @awoodland - yes that's correct. – c12 Feb 18 '12 at 17:24

1 Answers1

2

You can apply different type maps to the same types in different places in at least two ways.

Firstly you can change the active typemap with %apply or %clear, e.g.:

%module test

%include "stdint.i"

%apply intptr_t { unsigned char * };
unsigned char * test1();

%apply char * { unsigned char * };
unsigned char * test2();

%clear unsigned char *;
unsigned char * test3();

Gives three functions in Java with different return types, according to the active typemap.

Secondly you can also write more specific typemaps though, for example:

%apply long long { unsigned char * test4 };
%apply char * { unsigned char * test5 };
unsigned char * test4();
unsigned char * test5();

Only applies to test4 and test5 respectively - it matches on the type and the function name. In Java this results in:

  public static long test4() {
    return testJNI.test4();
  }

  public static String test5() {
    return testJNI.test5();
  }

For arguments you can match on the type and the parameter name in the function signature similarly.

Flexo
  • 87,323
  • 22
  • 191
  • 272