add javalibrarypath function
[scilab.git] / scilab / modules / jvm / src / c / getLibrarypath.c
1 /*-----------------------------------------------------------------------------------*/\r
2 /* INRIA 2007 */\r
3 /* Allan CORNET */\r
4 /*-----------------------------------------------------------------------------------*/ \r
5 #include <jni.h>\r
6 #include <string.h>\r
7 #include "getLibrarypath.h"\r
8 #include "getScilabJNIEnv.h"\r
9 #include "fromjava.h"\r
10 #include "MALLOC.h"\r
11 /*-----------------------------------------------------------------------------------*/ \r
12 char **getLibrarypath(int *sizeLibrarypathArray)\r
13 {\r
14         char **LibrarypathArray=NULL;\r
15         JNIEnv * currentENV = getScilabJNIEnv();\r
16         if (currentENV)\r
17         {\r
18                 jclass cls=NULL;\r
19                 if (IsFromJava())\r
20                 {\r
21                         /* Boot loader for scilab and javasci */\r
22                         /* if scilab is called from java (javasci), we need to update standard java.library.path */\r
23                         /* doesn't require to add -cp SCI/modules/jvm/jar/org.scilab.modules.jvm.jar when you use javasci */\r
24                         cls = (*currentENV)->FindClass(currentENV, "javasci/LibraryPath");\r
25                 }\r
26                 else\r
27                 {\r
28                         cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/jvm/LibraryPath");\r
29                 }\r
30 \r
31                 if (cls)\r
32                 {\r
33                         jmethodID mid=NULL;\r
34                         mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "getLibraryPath","()[Ljava/lang/String;");\r
35                         if (mid)\r
36                         {\r
37                                 jobjectArray jStrings;\r
38                                 \r
39                                 jStrings=(*currentENV)->CallStaticObjectMethod(currentENV,cls, mid,NULL);\r
40                                 *sizeLibrarypathArray=(*currentENV)->GetArrayLength(currentENV,jStrings);\r
41                                 if (*sizeLibrarypathArray>0)\r
42                                 {\r
43                                         int i=0;\r
44 \r
45                                         LibrarypathArray=(char**)MALLOC(sizeof(char*)*(*sizeLibrarypathArray));\r
46                                         for (i=0;i<*sizeLibrarypathArray;i++)\r
47                                         {\r
48                                                 jstring jelement;\r
49                                                 const char *str = NULL;\r
50                                                 char *string = NULL;\r
51 \r
52                                                 jelement = (jstring)(*currentENV)->GetObjectArrayElement(currentENV, jStrings, i);\r
53                                                 str = (*currentENV)->GetStringUTFChars(currentENV,jelement, 0);\r
54                                                 string = (char*)MALLOC(sizeof(char)*(strlen(str)+1));\r
55                                                 strcpy(string,str);\r
56                                                 LibrarypathArray[i]=string;\r
57                                                 (*currentENV)->ReleaseStringUTFChars(currentENV, jelement, str);\r
58                                         }\r
59                                 }\r
60                         }\r
61                 }\r
62         }\r
63         return LibrarypathArray;\r
64 }\r
65 /*-----------------------------------------------------------------------------------*/ \r