3b230eab6130e92de2ade2ef29d47a78a4332b8b
[scilab.git] / scilab / modules / dynamic_link / sci_gateway / c / sci_c_link.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) INRIA - Allan CORNET
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 /*--------------------------------------------------------------------------*/
14 #include "gw_dynamic_link.h"
15 #include "stack-c.h"
16 #include "dynamic_link.h"
17 #include "MALLOC.h"
18 #include "localization.h"
19 #include "Scierror.h"
20 /*--------------------------------------------------------------------------*/
21 int sci_c_link(char *fname,unsigned long fname_len)
22 {
23         int ilib = 0;
24
25         CheckRhs(1,2);
26         CheckLhs(1,2);
27
28         /* [test,ilib]=c_link(routine-name)  */
29         /* test = c_link(routine-name)  */
30         /* test = c_link(routine-name,num)  */
31
32         if (Rhs == 2)
33         {
34                 if (VarType(2) == sci_matrix)
35                 {
36                         int m1 = 0, n1 = 0, l1 = 0;
37                         GetRhsVar(2,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l1);
38                         if ( (m1 == n1) && (n1 == 1) )
39                         {
40                                 ilib =  (int)*stk(l1);
41                         }
42                         else
43                         {
44                                 Scierror(999,_("%s : second argument must be a unique id of a shared library.\n"),fname);
45                                 return 0;
46                         }
47                 }
48                 else
49                 {
50                         Scierror(999,_("%s : second argument must be a unique id of a shared library.\n"),fname);
51                         return 0;
52                 }
53         }
54         else ilib = -1;
55
56
57         if (VarType(1) == sci_strings)
58         {
59                 char **routinename = NULL;
60                 int m1 = 0, n1 = 0;
61                 GetRhsVar(1,"S",&m1,&n1,&routinename);
62
63                 if ( (m1 == 1) && (n1 == 1) )
64                 {
65                         int *paramoutINT=(int*)MALLOC(sizeof(int));
66                         BOOL FindFunction = FALSE;
67
68                         FindFunction = c_link(routinename[0],&ilib);
69                         if (routinename) {FREE(routinename);routinename = NULL;}
70
71                         
72
73                         if ( FindFunction )
74                         {
75                                 *paramoutINT=(int)(TRUE);
76                         }
77                         else
78                         {
79                                 *paramoutINT=(int)(FALSE);
80                         }
81
82                         CreateVarFromPtr(Rhs+1,MATRIX_OF_BOOLEAN_DATATYPE, &n1, &n1, &paramoutINT);
83                         LhsVar(1)=Rhs+1;
84
85                         if (Lhs == 2)
86                         {
87                                 int one = 1;
88                                 int l = 0;
89
90                                 CreateVar(Rhs+2, MATRIX_OF_INTEGER_DATATYPE, &one, &one,&l);
91                                 *istk(l) = (int)ilib;
92                                 LhsVar(2)=Rhs+2;
93                         }
94
95                         C2F(putlhsvar)();
96                         if (paramoutINT) {FREE(paramoutINT);paramoutINT=NULL;}
97                         
98                 }
99                 else
100                 {
101                         Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname,1);
102                 }
103         }
104         else
105         {
106                 SciError(201);
107         }
108         return 0;
109 }
110 /*--------------------------------------------------------------------------*/