Dynamic link update:
[scilab.git] / scilab / modules / dynamic_link / sci_gateway / c / sci_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 <string.h>
15 #include "gw_dynamic_link.h"
16 #include "stack-c.h"
17 #include "api_scilab.h"
18 #include "api_oldstack.h"
19 #include "Scierror.h"
20 #include "dynamic_link.h"
21 #include "MALLOC.h"
22 #include "localization.h"
23 #include "dl_genErrorMessage.h"
24 #include "freeArrayOfString.h"
25 #include "os_strdup.h"
26 #include "os_wcsdup.h"
27 /*-----------------------------------------------------------------------------------*/
28 static int linkNoRhs(int *_piKey);
29 static int linkOneRhsShow(int *_piKey);
30 /*-----------------------------------------------------------------------------------*/
31 int sci_link(char *fname, int *_piKey)
32 {
33     SciErr sciErr;
34     BOOL fflag              = TRUE;
35     int idsharedlibrary     = -1;
36     char *SharedLibraryName = NULL;
37     wchar_t param3flag      = L'f';
38
39     char **pstSubName       = NULL;
40     int iRowsSubName        = 0;
41     int iColsSubName        = 0;
42     int iSizeSubName        = 0;
43
44     int returnedID          = -1;
45     int iErr                = 0;
46
47     CheckRhs(0,3);
48     CheckLhs(1,1);
49
50     if(Rhs == 0)
51     {
52         return linkNoRhs(_piKey);
53     }
54     else
55     {
56         if(Rhs >= 1)
57         {
58             int* piAddress1 = NULL;
59             sciErr = getVarAddressFromPosition(_piKey, 1, &piAddress1);
60             if(sciErr.iErr)
61             {
62                 printError(&sciErr, 0);
63                 return 1;
64             }
65
66             if(isDoubleType(_piKey, piAddress1))
67             {
68                 double pdblReal = 0;
69                 if(getScalarDouble(_piKey, piAddress1, &pdblReal))
70                 {
71                     Scierror(999, _("%s : Wrong value for argument #%d: %s\n"), fname, 1, _("Unique id of a shared library expected."));
72                     return 1;
73                 }
74             }
75             else if(isStringType(_piKey, piAddress1))
76             {
77                 if(getAllocatedSingleString(_piKey, piAddress1, &SharedLibraryName))
78                 {
79                     FREE(SharedLibraryName);
80                     return 1;
81                 }
82
83                 if((Rhs == 1) && (strcmp(SharedLibraryName,"show") == 0))
84                 {
85                     return linkOneRhsShow(_piKey);
86                 }
87             }
88             else
89             {
90                 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar or a string expected.\n"), fname);
91                 return 1;
92             }
93         }
94
95         if(Rhs >= 2)
96         {
97             int* piAddress2 = NULL;
98             sciErr = getVarAddressFromPosition(_piKey, 2, &piAddress2);
99             if(sciErr.iErr)
100             {
101                 printError(&sciErr, 0);
102                 return 1;
103             }
104
105             //string scalar or vector
106             if(isStringType(_piKey, piAddress2) && (isVector(_piKey, piAddress2) || isScalar(_piKey, piAddress2)))
107             {
108                 if(getAllocatedMatrixOfString(_piKey, piAddress2, &iRowsSubName, &iColsSubName, &pstSubName))
109                 {
110                     return 1;
111                 }
112
113                 iSizeSubName = iRowsSubName * iColsSubName;
114             }
115             else
116             {
117                 Scierror(999, _("%s: Wrong type for input argument #%d: A string or a string vector expected.\n"));
118                 return 1;
119             }
120         }
121
122         if(Rhs == 3)
123         {
124             int* piAddress3     = NULL;
125             wchar_t* pwstData   = NULL;
126             int iRows           = 0;
127             int iCols           = 0;
128
129             sciErr = getVarAddressFromPosition(_piKey, 3, &piAddress3);
130             if(sciErr.iErr)
131             {
132                 printError(&sciErr, 0);
133                 return 1;
134             }
135
136             if(getAllocatedSingleWideString(_piKey, piAddress3, &pwstData))
137             {
138                 return 1;
139             }
140
141             if((wcscmp(pwstData, L"f") == 0) || (wcscmp(pwstData, L"c") == 0))
142             {
143                 if(pwstData[0] != L'f')
144                 {
145                     fflag = FALSE;
146                 }
147             }
148             else
149             {
150                 freeAllocatedSingleWideString(pwstData);
151                 Scierror(999,_("%s Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 3, "f", "c");
152                 return 0;
153             }
154         }
155
156
157         returnedID = scilabLink(idsharedlibrary, SharedLibraryName, pstSubName, iSizeSubName, fflag, &iErr);
158         if(iErr == 0)
159         {
160             if(createScalarDouble(_piKey, Rhs + 1, returnedID))
161             {
162                 return 1;
163             }
164             LhsVar(1) = Rhs + 1;
165             PutLhsVar();
166         }
167         else
168         {
169             dl_genErrorMessage(fname, iErr, SharedLibraryName);
170         }
171
172         if(Rhs >= 2)
173         {
174             freeArrayOfString(pstSubName, iSizeSubName);
175         }
176
177         if(SharedLibraryName)
178         {
179             FREE(SharedLibraryName);
180             SharedLibraryName = NULL;
181         }
182     }
183
184     return 0;
185 }
186 /*-----------------------------------------------------------------------------------*/
187 static int linkNoRhs(int* _piKey)
188 {
189     SciErr sciErr;
190     int iRet = 0;
191     int sizeFunctionsList = 0;
192     char ** FunctionsList = NULL;
193
194     FunctionsList = getNamesOfFunctionsInSharedLibraries(&sizeFunctionsList);
195
196     if( (FunctionsList) && (sizeFunctionsList > 0) )
197     {
198         sciErr = createMatrixOfString(_piKey, Rhs + 1, 1, sizeFunctionsList, FunctionsList);
199         freeArrayOfString(FunctionsList,sizeFunctionsList);
200         if(sciErr.iErr)
201         {
202             printError(&sciErr, 0);
203             return sciErr.iErr;
204         }
205         LhsVar(1) = Rhs+1;
206         PutLhsVar();
207     }
208     else
209     {
210         iRet = createEmptyMatrix(_piKey, Rhs + 1);
211         if(iRet)
212         {
213             return iRet;
214         }
215         LhsVar(1) = Rhs+1;
216         PutLhsVar();
217     }
218     return 0;
219 }
220 /*-----------------------------------------------------------------------------------*/
221 static int linkOneRhsShow(int *_piKey)
222 {
223     SciErr sciErr;
224     int iRet = 0;
225     int m1 = 0, n1 = 0, l1 = 0;
226     int *IdsList = NULL;
227     int sizeIds = 0;
228
229     ShowDynLinks();
230     IdsList = getAllIdSharedLib(&sizeIds);
231
232     if( (sizeIds>0) && (IdsList) )
233     {
234         sciErr = createMatrixOfInteger32(_piKey, Rhs + 1, 1, sizeIds, IdsList);
235         if(IdsList)
236         {
237             FREE(IdsList);
238             IdsList=NULL;
239         }
240         if(sciErr.iErr)
241         {
242             printError(&sciErr, 0);
243             return sciErr.iErr;
244         }
245     }
246     else
247     {
248         iRet = createEmptyMatrix(_piKey, Rhs + 1);
249         if(iRet)
250         {
251             return iRet;
252         }
253     }
254     LhsVar(1) = Rhs + 1;
255     PutLhsVar();
256     return 0;
257 }
258 /*-----------------------------------------------------------------------------------*/