Bonkour à tous.
J'utilise SWIG pour utiliser une librairie C en JAVA.
Problème, certains champs utilisés sont des "char *" dans lequel le code C vient écrire une string.
Or, SWIG me map ça avec un String en argument de méthode :
L'appel de :Devient :
Code : Sélectionner tout - Visualiser dans une fenêtre à part DBFFieldType SHPAPI_CALL DBFGetFieldInfo( DBFHandle psDBF, int iField, char * pszFieldName, int * pnWidth, int * pnDecimals );
Bien entendu ça ne fonctionne pas, on ne peut modifier une String passée en paramètre !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 public static DBFFieldType DBFGetFieldInfo(DBFInfo psDBF, int iField, String pszFieldName, SWIGTYPE_p_int pnWidth, SWIGTYPE_p_int pnDecimals) { return DBFFieldType.swigToEnum(shapelibJNI.DBFGetFieldInfo(DBFInfo.getCPtr(psDBF), psDBF, iField, pszFieldName, SWIGTYPE_p_int.getCPtr(pnWidth), SWIGTYPE_p_int.getCPtr(pnDecimals))); }
Pourtant celà semble avoir marché un jour avec un Java plus ancien Bizarre).
Je cherche donc à mapper le char * (et pas le const char * qui lui fonctionne bien puisqu'il n'est qu'en lecture côté C) avec un StringBuilder.
J'ai bien tenté des trucs dans mon fichier .i :
Ça me laisse le loisir de mettre le code que je veux entre le monde java et le jni... Mais par contre, ça ne me génère pas la bonne fonction.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 %typemap(ctype) char * "StringBuilder" %typemap(imtype) char * "StringBuilder" %typemap(javatype) String "StringBuilder" %javamethodmodifiers DBFGetFieldInfo "private"; %rename(DBFGetFieldInfo_private) DBFGetFieldInfo; %pragma(java) modulecode=%{ public static DBFFieldType DBFGetFieldInfo(DBFInfo psDBF, int iField, StringBuilder sbFieldName, SWIGTYPE_p_int pnWidth, SWIGTYPE_p_int pnDecimals) { byte[] arBytes = new byte[11]; return DBFGetFieldInfo_private(psDBF, iField, arBytes, pnWidth, pnDecimals); } %}
Dans le java :
Et dans le shapelibJNI :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 public static DBFFieldType DBFGetFieldInfo(DBFInfo psDBF, int iField, StringBuilder sbFieldName, SWIGTYPE_p_int pnWidth, SWIGTYPE_p_int pnDecimals) { byte[] arBytes = new byte[11]; return DBFGetFieldInfo_private(psDBF, iField, arBytes, pnWidth, pnDecimals); } private static DBFFieldType DBFGetFieldInfo_private(DBFInfo psDBF, int iField, String pszFieldName, SWIGTYPE_p_int pnWidth, SWIGTYPE_p_int pnDecimals) { return DBFFieldType.swigToEnum(shapelibJNI.DBFGetFieldInfo_private(DBFInfo.getCPtr(psDBF), psDBF, iField, pszFieldName, SWIGTYPE_p_int.getCPtr(pnWidth), SWIGTYPE_p_int.getCPtr(pnDecimals))); }
On voit bien qu'au niveau du JNI c'est toujours un String qui est passé et au au niveau de DBFGetFieldInfo_private idem donc ça n'a aucune chance de fonctionner !
Code : Sélectionner tout - Visualiser dans une fenêtre à part public final static native int DBFGetFieldInfo_private(long jarg1, DBFInfo jarg1_, int jarg2, String jarg3, long jarg4, long jarg5);
Ça doit pourtant être simple !!
Partager