1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
4 *
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 *
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
13 *
14 */
15
16 /*------------------------------------------------------------------------*/
17 /* file: getPropertyAssignedValue.c */
18 /* desc : a set of functions used to get the values which will be */
19 /* assigned to handles properties from the stack */
20 /*------------------------------------------------------------------------*/
21
22 #include "getPropertyAssignedValue.h"
23 #include "api_scilab.h"
24 #include "localization.h"
25 #include "sci_malloc.h"
26 #include "BasicAlgos.h"
27 #include "freeArrayOfString.h"
28 #include "Scierror.h"
29 #include "os_string.h"
30 /*--------------------------------------------------------------------------*/
copyDoubleVectorToIntFromStack(void * _pvData,int * _piDest,int _iNbItem)31 void copyDoubleVectorToIntFromStack(void* _pvData, int* _piDest, int _iNbItem)
32 {
33 int i = 0;
34 double* values = (double*)_pvData;
35 for (i = 0 ; i < _iNbItem ; i++)
36 {
37 _piDest[i] = (int) values[i];
38 }
39 }
40 /*--------------------------------------------------------------------------*/
createCopyStringMatrixFromStack(void * _pvData,int _iNbItem)41 char ** createCopyStringMatrixFromStack(void* _pvData, int _iNbItem)
42 {
43 int i = 0;
44 char ** res = (char**)MALLOC(_iNbItem * sizeof(char *));
45 char ** values = (char**)_pvData;
46
47 if (res == NULL)
48 {
49 return NULL;
50 }
51
52 for (i = 0 ; i < _iNbItem ; i++)
53 {
54 res[i] = os_strdup(values[i]);
55 }
56
57 return res;
58
59 }
60 /*--------------------------------------------------------------------------*/
tryGetBooleanValueFromStack(void * _pvData,int _iType,int _iRows,int _iCols,char const * _pstPropertyName)61 int tryGetBooleanValueFromStack(void* _pvData, int _iType, int _iRows, int _iCols, char const* _pstPropertyName)
62 {
63 if (_iType == sci_strings)
64 {
65 if (stricmp((char*)_pvData, "on") == 0)
66 {
67 return TRUE;
68 }
69 if (stricmp((char*)_pvData, "off") == 0)
70 {
71 return FALSE;
72 }
73 if (stricmp((char*)_pvData, "1") == 0)
74 {
75 return TRUE;
76 }
77 if (stricmp((char*)_pvData, "0") == 0)
78 {
79 return FALSE;
80 }
81 if (stricmp((char*)_pvData, "T") == 0)
82 {
83 return TRUE;
84 }
85 if (stricmp((char*)_pvData, "F") == 0)
86 {
87 return FALSE;
88 }
89
90 Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected.\n"), _pstPropertyName, "on", "off");
91 return NOT_A_BOOLEAN_VALUE;
92 }
93
94 if (_iType == sci_boolean)
95 {
96 return ((int*)_pvData)[0];
97 }
98
99 if (_iType == sci_matrix)
100 {
101 if (((double*)_pvData)[0] == 0)
102 {
103 return FALSE;
104 }
105 return TRUE;
106 }
107
108 Scierror(999, _("Wrong type for '%s' property: String expected.\n"), _pstPropertyName);
109 return NOT_A_BOOLEAN_VALUE;
110 }
111 /*--------------------------------------------------------------------------*/
getStackListNbElement(void * _pvCtx,int _iRhs)112 int getStackListNbElement(void* _pvCtx, int _iRhs)
113 {
114 int* piAddr = 0;
115 int iItem = 0;
116
117 getVarAddressFromPosition(_pvCtx, _iRhs, &piAddr);
118 getListItemNumber(_pvCtx, piAddr, &iItem);
119 return iItem - 1; //why -1 ? Oo
120
121 }
122 /*--------------------------------------------------------------------------*/
createAssignedList(void * _pvCtx,int _iRhs,int _iNbItem)123 AssignedList * createAssignedList(void* _pvCtx, int _iRhs, int _iNbItem)
124 {
125 AssignedList * newList = NULL;
126 int iItem = 0;
127
128 newList = (AssignedList*)MALLOC(sizeof(AssignedList));
129
130 if (newList == NULL)
131 {
132 return NULL;
133 }
134
135 newList->iNbItem = _iNbItem + 1;
136 newList->iCurItem = 2 ; /* begin with 1 and 1 are the names */
137 newList->iRhs = _iRhs;
138
139 /* get the stack pointer */
140 getVarAddressFromPosition(_pvCtx, _iRhs, &newList->piList);
141 getListItemNumber(_pvCtx, newList->piList, &iItem);
142
143 /* check the size */
144 if (iItem != newList->iNbItem)
145 {
146 FREE(newList);
147 return NULL;
148 }
149
150 return newList;
151 }
152 /*--------------------------------------------------------------------------*/
destroyAssignedList(AssignedList * _pList)153 void destroyAssignedList(AssignedList* _pList)
154 {
155 FREE(_pList);
156 }
157 /*--------------------------------------------------------------------------*/
getAssignedListNbElement(AssignedList * _pList)158 int getAssignedListNbElement(AssignedList* _pList)
159 {
160 return _pList->iNbItem - 1;
161 }
162 /*--------------------------------------------------------------------------*/
rewindAssignedList(AssignedList * _pList)163 void rewindAssignedList(AssignedList* _pList)
164 {
165 _pList->iCurItem = 2; //why -2 ? Oo
166 }
167 /*--------------------------------------------------------------------------*/
isListCurrentElementDoubleMatrix(void * _pvCtx,AssignedList * _pList)168 BOOL isListCurrentElementDoubleMatrix(void* _pvCtx, AssignedList* _pList)
169 {
170 int* piAddrItem = NULL;
171 int iType = 0;
172 getListItemAddress(_pvCtx, _pList->piList, _pList->iCurItem, &piAddrItem);
173 getVarType(_pvCtx, piAddrItem, &iType);
174 return iType == sci_matrix;
175 }
176 /*--------------------------------------------------------------------------*/
isListCurrentElementStringMatrix(void * _pvCtx,AssignedList * _pList)177 BOOL isListCurrentElementStringMatrix(void* _pvCtx, AssignedList* _pList)
178 {
179 int* piAddrItem = NULL;
180 int iType = 0;
181 getListItemAddress(_pvCtx, _pList->piList, _pList->iCurItem, &piAddrItem);
182 getVarType(_pvCtx, piAddrItem, &iType);
183 return iType == sci_strings;
184 }
185 /*--------------------------------------------------------------------------*/
isListCurrentElementEmptyMatrix(void * _pvCtx,AssignedList * _pList)186 BOOL isListCurrentElementEmptyMatrix(void* _pvCtx, AssignedList* _pList)
187 {
188 int* piItem = NULL;
189 int iRows = 0, iCols = 0;
190
191 if (!isListCurrentElementDoubleMatrix(_pvCtx, _pList))
192 {
193 /* empty matrix is a double matrix */
194 return FALSE;
195 }
196
197 getListItemAddress(_pvCtx, _pList->piList, _pList->iCurItem, &piItem);
198 getVarDimension(_pvCtx, piItem, &iRows, &iCols);
199 if (iRows * iCols == 0)
200 {
201 return TRUE;
202 }
203
204 return FALSE;
205
206 }
207 /*--------------------------------------------------------------------------*/
getDoubleMatrixFromList(void * _pvCtx,AssignedList * _pList,int _iItem,int * _piRows,int * _piCols)208 double* getDoubleMatrixFromList(void* _pvCtx, AssignedList* _pList, int _iItem, int* _piRows, int* _piCols)
209 {
210 double* pdbl = NULL;
211
212 getMatrixOfDoubleInList(_pvCtx, _pList->piList, _iItem, _piRows, _piCols, &pdbl);
213 return pdbl;
214 }
215 /*--------------------------------------------------------------------------*/
getStringMatrixFromList(void * _pvCtx,AssignedList * _pList,int _iItem,int * _piRows,int * _piCols)216 char ** getStringMatrixFromList(void* _pvCtx, AssignedList* _pList, int _iItem, int* _piRows, int* _piCols)
217 {
218 int* piItem = NULL;
219 char** pstData = NULL;
220 getListItemAddress(_pvCtx, _pList->piList, _iItem, &piItem);
221 if (getAllocatedMatrixOfString(_pvCtx, piItem, _piRows, _piCols, &pstData))
222 {
223 return NULL;
224 }
225 return pstData;
226 }
227 /*--------------------------------------------------------------------------*/
getCurrentDoubleMatrixFromList(void * _pvCtx,AssignedList * _pList,int * _piRows,int * _piCols)228 double* getCurrentDoubleMatrixFromList(void* _pvCtx, AssignedList* _pList, int* _piRows, int* _piCols)
229 {
230 double* res = NULL;
231 if (_pList->iCurItem > _pList->iNbItem)
232 {
233 *_piRows = 0;
234 *_piCols = 0;
235 return NULL;
236 }
237
238 res = getDoubleMatrixFromList(_pvCtx, _pList, _pList->iCurItem, _piRows, _piCols);
239 _pList->iCurItem++;
240 return res;
241 }
242 /*--------------------------------------------------------------------------*/
getCurrentStringMatrixFromList(void * _pvCtx,AssignedList * _pList,int * _piRows,int * _piCols)243 char** getCurrentStringMatrixFromList(void* _pvCtx, AssignedList* _pList, int* _piRows, int* _piCols)
244 {
245 char** res = NULL;
246 if (_pList->iCurItem > _pList->iNbItem)
247 {
248 *_piRows = 0;
249 *_piCols = 0;
250 return NULL;
251 }
252
253 res = getStringMatrixFromList(_pvCtx, _pList, _pList->iCurItem, _piRows, _piCols);
254 _pList->iCurItem++;
255 return res;
256
257 }
258 /*--------------------------------------------------------------------------*/
createCopyDoubleMatrixFromList(void * _pvCtx,AssignedList * _pList,int * _piRows,int * _piCols)259 double* createCopyDoubleMatrixFromList(void* _pvCtx, AssignedList* _pList, int* _piRows, int* _piCols)
260 {
261 /* get the matrix */
262 double* stackValues = getCurrentDoubleMatrixFromList(_pvCtx, _pList, _piRows, _piCols);
263 int nbElement = *_piRows * *_piCols;
264
265 double* copyMatrix = NULL;
266
267 if (nbElement == 0)
268 {
269 return NULL;
270 }
271
272 /* copy */
273
274 copyMatrix = (double*)MALLOC((*_piRows) * (*_piCols) * sizeof(double));
275
276 if (copyMatrix == NULL)
277 {
278 *_piRows = -1;
279 *_piCols = -1;
280 return NULL;
281 }
282
283 doubleArrayCopy(copyMatrix, stackValues, nbElement);
284
285 return copyMatrix;
286
287 }
288 /*--------------------------------------------------------------------------*/
289