f5d1a4bcda795740fa2dda65f1336444bd8dba31
[scilab.git] / scilab / modules / jvm / src / c / getClasspath.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 "getClasspath.h"\r
8 #include "getScilabJNIEnv.h"\r
9 #include "fromjava.h"\r
10 #include "MALLOC.h"\r
11 /*-----------------------------------------------------------------------------------*/ \r
12 char **getClasspath(int *sizeClasspathArray)\r
13 {\r
14         char **ClasspathArray=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 classpath */\r
23                         /* doesn't require to add -cp SCI/jar/modules/jvm.jar when you use javasci */\r
24                         cls = (*currentENV)->FindClass(currentENV, "javasci/ClassPath");\r
25                 }\r
26                 else\r
27                 {\r
28                         cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/jvm/ClassPath");\r
29                 }\r
30 \r
31                 if (cls)\r
32                 {\r
33                         jmethodID mid=NULL;\r
34                         mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "getClassPath","()[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                                 *sizeClasspathArray=(*currentENV)->GetArrayLength(currentENV,jStrings);\r
41                                 if (*sizeClasspathArray>0)\r
42                                 {\r
43                                         int i=0;\r
44 \r
45                                         ClasspathArray=(char**)MALLOC(sizeof(char*)*(*sizeClasspathArray));\r
46                                         for (i=0;i<*sizeClasspathArray;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 \r
56                                                 #if _MSC_VER\r
57                                                 if (strlen(str) > 1) strcpy(string,&str[1]);\r
58                                                 #else\r
59                                                 if (strlen(str) > 5) strcpy(string,&str[5]);\r
60                                                 #endif\r
61                                                 else strcpy(string,str);\r
62                                                 ClasspathArray[i]=string;\r
63                                                 (*currentENV)->ReleaseStringUTFChars(currentENV, jelement, str);\r
64                                         }\r
65                                 }\r
66                         }\r
67                 }\r
68         }\r
69         return ClasspathArray;\r
70 }\r
71 /*-----------------------------------------------------------------------------------*/ \r