1 /*
2  * -------------------------------------------------------------------------
3  * validate.c -- Input validation function
4  * SWT - Scilab wavelet toolbox
5  * Copyright (C) 2005-2006  Roger Liu
6  * Copyright (C) 20010-2012  Holger Nahrstaedt
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  * -------------------------------------------------------------------------
22  */
23 
24 //#include "swt_common.h"
25 //#include "dwt.h"
26 #include "swtlib.h"
27 #include "swt_gwsupport.h"
28 #include "api_scilab.h"
29 //#include "stack-c.h"
30 #include "Scierror.h"
31 //#include "localization.h"
32 //#include "warningmode.h"
33 #include "sciprint.h"
34 
35 
swt_gwsupport_GetRealMatrixOfDoubles(void * pvApiCtx,char * fname,int ivar,int * _piRows,int * _piCols,double ** _pdblReal)36 int swt_gwsupport_GetRealMatrixOfDoubles(void* pvApiCtx, char * fname, int ivar, int * _piRows , int * _piCols, double** _pdblReal )
37 {
38   int iType = 0;
39   int iComplex = 0;
40   SciErr sciErr;
41   int *_piAddress;
42   int type;
43 
44   sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
45   if(sciErr.iErr)
46     {
47       printError(&sciErr, 0);
48       return SWT_GWSUPPORT_ERROR;
49     }
50     sciErr = getVarType(pvApiCtx, _piAddress, &type);
51     if(sciErr.iErr)
52       {
53         printError(&sciErr, 0);
54         return SWT_GWSUPPORT_ERROR;
55       }
56       if (type!=sci_matrix)
57         {
58           Scierror (999,"%s: %d input vector must be double\n",ivar,fname);
59           return SWT_GWSUPPORT_ERROR;
60         }
61     sciErr = getMatrixOfDouble(pvApiCtx, _piAddress, _piRows, _piCols, _pdblReal);
62     if(sciErr.iErr)
63       {
64         printError(&sciErr, 0);
65         return SWT_GWSUPPORT_ERROR;
66       }
67       return SWT_GWSUPPORT_OK;
68 }
69 
70 
swt_gwsupport_GetComplexMatrixOfDoubles(void * pvApiCtx,char * fname,int ivar,int * _piRows,int * _piCols,double ** _pdblReal,double ** _pdblImag)71 int swt_gwsupport_GetComplexMatrixOfDoubles( void* pvApiCtx, char * fname, int ivar, int * _piRows , int * _piCols, double** _pdblReal , double** _pdblImag )
72 {
73   int iType = 0;
74   int iComplex = 0;
75   SciErr sciErr;
76   int *_piAddress;
77   int type;
78 
79   sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
80   if(sciErr.iErr)
81     {
82       printError(&sciErr, 0);
83       return SWT_GWSUPPORT_ERROR;
84     }
85     sciErr = getVarType(pvApiCtx, _piAddress, &type);
86     if(sciErr.iErr)
87       {
88         printError(&sciErr, 0);
89         return SWT_GWSUPPORT_ERROR;
90       }
91       if (type!=sci_matrix)
92         {
93           Scierror (999,"%s: %d input vector must be double\n",ivar,fname);
94           return SWT_GWSUPPORT_ERROR;
95         }
96         sciErr = getComplexMatrixOfDouble(pvApiCtx, _piAddress, _piRows, _piCols, _pdblReal,_pdblImag);
97         if(sciErr.iErr)
98           {
99             printError(&sciErr, 0);
100             return SWT_GWSUPPORT_ERROR;
101           }
102           return SWT_GWSUPPORT_OK;
103         }
104 
105 
swt_gwsupport_GetRealMatrixOfDoublesAsInteger(void * pvApiCtx,char * fname,int ivar,int * _piRows,int * _piCols,int ** _pdblReal)106 int swt_gwsupport_GetRealMatrixOfDoublesAsInteger( void* pvApiCtx, char * fname, int ivar, int * _piRows , int * _piCols, int** _pdblReal )
107 {
108   int iType = 0;
109   int iComplex = 0;
110   SciErr sciErr;
111   int *_piAddress;
112   int type;
113 
114   sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
115   if(sciErr.iErr)
116     {
117       printError(&sciErr, 0);
118       return SWT_GWSUPPORT_ERROR;
119     }
120     sciErr = getVarType(pvApiCtx, _piAddress, &type);
121     if(sciErr.iErr)
122       {
123         printError(&sciErr, 0);
124         return SWT_GWSUPPORT_ERROR;
125       }
126       if (type!=sci_matrix)
127         {
128           Scierror (999,"%s: %d input vector must be double\n",ivar,fname);
129           return SWT_GWSUPPORT_ERROR;
130         }
131     sciErr = getMatrixOfDoubleAsInteger(pvApiCtx, _piAddress, _piRows, _piCols, _pdblReal);
132     if(sciErr.iErr)
133       {
134         printError(&sciErr, 0);
135         return SWT_GWSUPPORT_ERROR;
136       }
137       return SWT_GWSUPPORT_OK;
138     }
139 
swt_gwsupport_GetRealHypermatofdouble(void * pvApiCtx,char * fname,int ivar,int ** _dims,int * _ndims,double ** _pdblReal)140     int swt_gwsupport_GetRealHypermatofdouble( void* pvApiCtx, char * fname, int ivar, int ** _dims , int *_ndims, double** _pdblReal )
141     {
142       int iType = 0;
143       int iComplex = 0;
144       SciErr sciErr;
145       int *_piAddress;
146       int type;
147 
148       sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
149       if(sciErr.iErr)
150         {
151           printError(&sciErr, 0);
152           return SWT_GWSUPPORT_ERROR;
153         }
154         sciErr = getHypermatOfDouble(pvApiCtx, _piAddress, _dims, _ndims, _pdblReal);
155         if(sciErr.iErr)
156           {
157             printError(&sciErr, 0);
158             return SWT_GWSUPPORT_ERROR;
159           }
160           return SWT_GWSUPPORT_OK;
161         }
162     // int swt_gwsupport_GetRealMatrixOfUnsignedInteger16( void* pvApiCtx, char * fname, int ivar, int * _piRows , int * _piCols, unsigned short** _pusData16 )
163     // {
164     //   SciErr sciErr;
165     //   int *_piAddress;
166     //   int type;
167     //
168     //   sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
169     //   if(sciErr.iErr)
170     //     {
171     //       printError(&sciErr, 0);
172     //       return SWT_GWSUPPORT_ERROR;
173     //     }
174     //     // sciErr = getVarType(pvApiCtx, _piAddress, &type);
175     //     // if(sciErr.iErr)
176     //     //   {
177     //     //     printError(&sciErr, 0);
178     //     //     return SWT_GWSUPPORT_ERROR;
179     //     //   }
180     //       // if (type!=sci_matrix)
181     //       //   {
182     //       //     Scierror (999,"%s: %d input vector must be double\n",ivar,fname);
183     //       //     return SWT_GWSUPPORT_ERROR;
184     //       //   }
185     //         SciErr = getMatrixOfUnsignedInteger16(pvApiCtx, _piAddress, _piRows, _piCols, _pusData16);
186     //         if(sciErr.iErr)
187     //           {
188     //             printError(&sciErr, 0);
189     //             return SWT_GWSUPPORT_ERROR;
190     //           }
191     //           return SWT_GWSUPPORT_OK;
192     //         }
swt_gwsupport_GetMatrixOfString(void * pvApiCtx,char * fname,int ivar,int * _piRows,int * _piCols)193     char ** swt_gwsupport_GetMatrixOfString( void* pvApiCtx, char * fname, int ivar, int * _piRows , int * _piCols)
194     {
195       SciErr sciErr;
196       int *_piAddress;
197       int *_piLen=NULL;
198       char ** _pstStrings=NULL;
199       int type;
200       int i;
201 
202       sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
203       if(sciErr.iErr)
204         {
205           printError(&sciErr, 0);
206           return NULL;
207         }
208 
209 
210         sciErr = getVarType(pvApiCtx, _piAddress, &type);
211         if(sciErr.iErr)
212           {
213             printError(&sciErr, 0);
214             return NULL;
215           }
216           // if (type!=sci_string)
217           //   {
218           //     Scierror (999,"%s: %d input vector must be double\n",ivar,fname);
219           //     return SWT_GWSUPPORT_ERROR;
220           //   }
221             //first call to retrieve dimensions
222             sciErr = getMatrixOfString(pvApiCtx, _piAddress, _piRows, _piCols, NULL, NULL);
223             if(sciErr.iErr)
224               {
225                 printError(&sciErr, 0);
226                 return NULL;
227               }
228               //sciprint("dim %d x %d",(*_piRows),(*_piCols));
229               _piLen = (int*)malloc(sizeof(int) * (*_piRows) * (*_piCols));
230               //second call to retrieve length of each string
231               sciErr = getMatrixOfString(pvApiCtx, _piAddress, _piRows, _piCols, _piLen, NULL);
232               if(sciErr.iErr)
233                 {
234                   printError(&sciErr, 0);
235                   return NULL;
236                 }
237 
238                 _pstStrings = (char**)malloc(sizeof(char*) * (*_piRows) * (*_piCols));
239                 for(i = 0 ; i < (*_piRows) * (*_piCols) ; i++)
240                   {
241                     //sciprint("i %d length %d",i,_piLen[i] + 1);
242                     _pstStrings[i] = (char*)malloc(sizeof(char) * (_piLen[i] + 1));//+ 1 for null termination
243                   }
244 
245 
246                 //third call to retrieve data
247                 sciErr = getMatrixOfString(pvApiCtx, _piAddress, _piRows, _piCols, _piLen, _pstStrings);
248                 //sciprint("str 1  %c 2 %c",_pstStrings[0][0],_pstStrings[1][0]);
249                 free (_piLen);
250             if(sciErr.iErr)
251               {
252                 printError(&sciErr, 0);
253                 return NULL;
254               }
255               return _pstStrings;
256             }
257 
258 
swt_gwsupport_GetScalarString(void * pvApiCtx,char * fname,int ivar,char ** mystring)259     int swt_gwsupport_GetScalarString( void* pvApiCtx, char * fname, int ivar , char** mystring )
260     {
261       int *_piAddress;
262       int iRet = 0;
263       SciErr sciErr;
264 
265       sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
266       if(sciErr.iErr)
267         {
268           printError(&sciErr, 0);
269           return SWT_GWSUPPORT_ERROR;
270         }
271         iRet = getAllocatedSingleString(pvApiCtx, _piAddress, mystring);
272         if (iRet)
273           {
274             Scierror(999,"%s: Wrong type for input argument #%d: Single string expected.\n", fname,ivar);
275             return SWT_GWSUPPORT_ERROR;
276           }
277           return SWT_GWSUPPORT_OK;
278         }
279 
swt_gwsupport_AllocMatrixOfDoubles(void * pvApiCtx,char * fname,int ovar,int _piRows,int _piCols,double ** _pdblReal)280         int swt_gwsupport_AllocMatrixOfDoubles ( void* pvApiCtx, char * fname, int ovar , int _piRows , int _piCols , double ** _pdblReal )
281         {
282           SciErr sciErr;
283           sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + ovar, _piRows, _piCols, _pdblReal);
284           if(sciErr.iErr)
285             {
286               printError(&sciErr, 0);
287               return SWT_GWSUPPORT_ERROR;
288             }
289             AssignOutputVariable(pvApiCtx,ovar) = nbInputArgument(pvApiCtx)+ovar;
290             return SWT_GWSUPPORT_OK;
291           }
292 
swt_gwsupport_AllocComplexMatrixOfDoubles(void * pvApiCtx,char * fname,int ovar,int _piRows,int _piCols,double ** _pdblReal,double ** _pdblImg)293           int swt_gwsupport_AllocComplexMatrixOfDoubles ( void* pvApiCtx, char * fname, int ovar , int _piRows , int _piCols , double** _pdblReal, double** _pdblImg )
294           {
295             SciErr sciErr;
296             sciErr = allocComplexMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + ovar, _piRows, _piCols, _pdblReal,_pdblImg);
297             if(sciErr.iErr)
298               {
299                 printError(&sciErr, 0);
300                 return SWT_GWSUPPORT_ERROR;
301               }
302               AssignOutputVariable(pvApiCtx,ovar) = nbInputArgument(pvApiCtx)+ovar;
303               return SWT_GWSUPPORT_OK;
304             }
305 
swt_gwsupport_AllocMatrixOfDoublesAsInteger(void * pvApiCtx,char * fname,int ovar,int _piRows,int _piCols,int ** _pdblReal)306           int swt_gwsupport_AllocMatrixOfDoublesAsInteger ( void* pvApiCtx, char * fname, int ovar , int _piRows , int _piCols , int ** _pdblReal )
307           {
308             SciErr sciErr;
309             sciErr = allocMatrixOfDoubleAsInteger(pvApiCtx, nbInputArgument(pvApiCtx) + ovar, _piRows, _piCols, _pdblReal);
310             if(sciErr.iErr)
311               {
312                 printError(&sciErr, 0);
313                 return SWT_GWSUPPORT_ERROR;
314               }
315               AssignOutputVariable(pvApiCtx,ovar) = nbInputArgument(pvApiCtx)+ovar;
316               return SWT_GWSUPPORT_OK;
317             }
swt_gwsupport_AllocMatrixOfUnsignedInteger16(void * pvApiCtx,char * fname,int ovar,int _piRows,int _piCols,unsigned short ** _pusData16)318             int swt_gwsupport_AllocMatrixOfUnsignedInteger16 ( void* pvApiCtx, char * fname, int ovar , int _piRows , int _piCols , unsigned short** _pusData16)
319             {
320               SciErr sciErr;
321               sciErr = allocMatrixOfUnsignedInteger16(pvApiCtx, nbInputArgument(pvApiCtx) + ovar, _piRows, _piCols, _pusData16);
322               if(sciErr.iErr)
323                 {
324                   printError(&sciErr, 0);
325                   return SWT_GWSUPPORT_ERROR;
326                 }
327                 AssignOutputVariable(pvApiCtx,ovar) = nbInputArgument(pvApiCtx)+ovar;
328                 return SWT_GWSUPPORT_OK;
329               }
swt_gwsupport_AllocMatrixOfInteger32(void * pvApiCtx,char * fname,int ovar,int _piRows,int _piCols,int ** _piData32)330               int swt_gwsupport_AllocMatrixOfInteger32 ( void* pvApiCtx, char * fname, int ovar , int _piRows , int _piCols , int** _piData32)
331               {
332                 SciErr sciErr;
333                 sciErr = allocMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + ovar, _piRows, _piCols, _piData32);
334                 if(sciErr.iErr)
335                   {
336                     printError(&sciErr, 0);
337                     return SWT_GWSUPPORT_ERROR;
338                   }
339                   AssignOutputVariable(pvApiCtx,ovar) = nbInputArgument(pvApiCtx)+ovar;
340                   return SWT_GWSUPPORT_OK;
341                 }
swt_gwsupport_CreateMatrixOfString(void * pvApiCtx,char * fname,int ovar,int _piRows,int _piCols,char ** pstData)342             int swt_gwsupport_CreateMatrixOfString ( void* pvApiCtx, char * fname, int ovar , int _piRows , int _piCols , char ** pstData )
343             {
344               SciErr sciErr;
345               sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + ovar, _piRows, _piCols, pstData);
346               if(sciErr.iErr)
347                 {
348                   printError(&sciErr, 0);
349                   return SWT_GWSUPPORT_ERROR;
350                 }
351                 free(pstData);
352                 AssignOutputVariable(pvApiCtx,ovar) = nbInputArgument(pvApiCtx)+ovar;
353                 return SWT_GWSUPPORT_OK;
354               }
355 
swt_gwsupport_CreateHypermatOfDouble(void * pvApiCtx,char * fname,int ovar,int * dims,int ndims,double * pdblReal)356               int swt_gwsupport_CreateHypermatOfDouble ( void* pvApiCtx, char * fname, int ovar , int* dims , int ndims, double* pdblReal)
357               {
358                 SciErr sciErr;
359                 sciErr = createHypermatOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + ovar, dims, ndims, pdblReal);
360                 if(sciErr.iErr)
361                   {
362                     printError(&sciErr, 0);
363                     return SWT_GWSUPPORT_ERROR;
364                   }
365                   //free(pdblReal);
366                   AssignOutputVariable(pvApiCtx,ovar) = nbInputArgument(pvApiCtx)+ovar;
367                   return SWT_GWSUPPORT_OK;
368                 }
369 
370 
swt_gwsupport_GetType(void * pvApiCtx,int ivar)371               int swt_gwsupport_GetType( void* pvApiCtx, int ivar)
372               {
373                 SciErr sciErr;
374                 int *_piAddress;
375                 int type;
376 
377                 sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
378                 if(sciErr.iErr)
379                   {
380                     printError(&sciErr, 0);
381                     return 0;
382                   }
383 
384 
385                   sciErr = getVarType(pvApiCtx, _piAddress, &type);
386                   if(sciErr.iErr)
387                     {
388                       printError(&sciErr, 0);
389                       return 0;
390                     }
391                     return type;
392 
393 
394               }
395 
swt_gwsupport_GetMatrixdims(void * pvApiCtx,int ivar,int * _piRows,int * _piCols)396             int swt_gwsupport_GetMatrixdims( void* pvApiCtx, int ivar, int * _piRows , int * _piCols){
397 
398               SciErr sciErr;
399               int *_piAddress;
400               int type;
401 
402               sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
403               if(sciErr.iErr)
404                 {
405                   printError(&sciErr, 0);
406                   return SWT_GWSUPPORT_ERROR;
407                 }
408                 sciErr = getVarDimension(pvApiCtx, _piAddress, _piRows, _piCols);
409                 if(sciErr.iErr)
410                   {
411                     printError(&sciErr, 0);
412                     return SWT_GWSUPPORT_ERROR;
413                   }
414 
415                   return SWT_GWSUPPORT_OK;
416 
417 
418             }
419 
420 
swt_gwsupport_IsVarComplex(void * pvApiCtx,int ivar)421             int swt_gwsupport_IsVarComplex( void* pvApiCtx, int ivar)
422             {
423               SciErr sciErr;
424               int *_piAddress;
425 
426               sciErr = getVarAddressFromPosition(pvApiCtx, ivar, &_piAddress);
427               if(sciErr.iErr)
428                 {
429                   printError(&sciErr, 0);
430                   return 0;
431                 }
432 
433                 return isVarComplex(pvApiCtx, _piAddress);
434 
435                 }
436