215c286b4d4518782cf36b0f170d1e2655d32ae6
[scilab.git] / scilab / modules / matio / src / c / CreateIntegerVariable.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008 - INRIA - Vincent COUVERT
4  * Copyright (C) 2010 - INRIA - Vincent COUVERT
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16
17 #include "CreateMatlabVariable.h"
18 #include "api_scilab.h"
19 #include "sci_malloc.h"
20 #include "localization.h"
21 #include "Scierror.h"
22 #include "sciprint.h"
23
24 int CreateIntegerVariable(void *pvApiCtx, int iVar, int integerType, matvar_t *matVariable, int * parent, int item_position)
25 {
26     int nbRow, nbCol, i;
27     SciErr sciErr;
28     char * tmp_int8 = NULL;
29     short * tmp_int16 = NULL;
30     int * tmp_int32 = NULL;
31     int *piDims = NULL;
32     unsigned char * tmp_uint8 = NULL;
33     unsigned short * tmp_uint16 = NULL;
34     unsigned int * tmp_uint32 = NULL;
35 #ifdef __SCILAB_INT64__
36     long long * tmp_int64 = NULL;
37     unsigned long long * tmp_uint64 = NULL;
38 #endif
39     int iSize = 0;
40
41     // Matrix dimensions
42     nbRow = (int)matVariable->dims[0];
43     nbCol = (int)matVariable->dims[1];
44     iSize = nbRow * nbCol;
45
46     if (iSize == 0)
47     {
48         double dblReal = 0;
49         SciErr sciErr = createMatrixOfDouble(pvApiCtx, iVar, 0, 0, &dblReal);
50         if (sciErr.iErr)
51         {
52             printError(&sciErr, 0);
53             Scierror(999, _("%s: Memory allocation error.\n"), "CreateIntegerVariable");
54             return FALSE;
55         }
56
57         return TRUE;
58     }
59
60     if (matVariable->rank == 2) /* 2-D array */
61     {
62         switch (integerType)
63         {
64             case SCI_INT8:
65             {
66                 tmp_int8 = (char *)MALLOC(iSize * sizeof(char));
67
68                 if (tmp_int8 == NULL)
69                 {
70                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
71                     return FALSE;
72                 }
73                 for (i = 0; i < iSize; i++)
74                 {
75                     tmp_int8[i] = ((char *)matVariable->data)[i];
76                 }
77
78                 if (parent == NULL)
79                 {
80                     sciErr = createMatrixOfInteger8(pvApiCtx, iVar, nbRow, nbCol, tmp_int8);
81                 }
82                 else
83                 {
84                     sciErr = createMatrixOfInteger8InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int8);
85                 }
86
87                 if (sciErr.iErr)
88                 {
89                     printError(&sciErr, 0);
90                     return 0;
91                 }
92
93                 FREE(tmp_int8);
94             }
95             break;
96             case SCI_INT16:
97             {
98
99                 tmp_int16 = (short *)MALLOC(iSize * sizeof(short));
100                 if (tmp_int16 == NULL)
101                 {
102                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
103                     return FALSE;
104                 }
105
106                 for (i = 0; i < iSize; i++)
107                 {
108                     tmp_int16[i] = ((short *)matVariable->data)[i];
109                 }
110
111
112                 if (parent == NULL)
113                 {
114                     sciErr = createMatrixOfInteger16(pvApiCtx, iVar, nbRow, nbCol, tmp_int16);
115                 }
116                 else
117                 {
118                     sciErr = createMatrixOfInteger16InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int16);
119                 }
120
121                 if (sciErr.iErr)
122                 {
123                     printError(&sciErr, 0);
124                     return 0;
125                 }
126
127
128                 FREE(tmp_int16);
129             }
130             break;
131             case SCI_INT32:
132             {
133                 tmp_int32 = (int *)MALLOC(iSize * sizeof(int));
134
135                 if (tmp_int32 == NULL)
136                 {
137                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
138                     return FALSE;
139                 }
140
141                 for (i = 0; i < iSize; i++)
142                 {
143                     tmp_int32[i] = ((int *)matVariable->data)[i];
144                 }
145
146
147                 if (parent == NULL)
148                 {
149                     sciErr = createMatrixOfInteger32(pvApiCtx, iVar, nbRow, nbCol, tmp_int32);
150                 }
151                 else
152                 {
153                     sciErr = createMatrixOfInteger32InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int32);
154                 }
155
156                 if (sciErr.iErr)
157                 {
158                     printError(&sciErr, 0);
159                     return 0;
160                 }
161
162
163                 FREE(tmp_int32);
164             }
165             break;
166 #ifdef __SCILAB_INT64__
167             case SCI_INT64:
168             {
169                 tmp_int64 = (long long *)MALLOC(iSize * sizeof(long long));
170                 if (tmp_int64 == NULL)
171                 {
172                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
173                     return FALSE;
174                 }
175                 for (i = 0; i < iSize; i++)
176                 {
177                     tmp_int64[i] = ((long long *)matVariable->data)[i];
178                 }
179
180
181                 if (parent == NULL)
182                 {
183                     sciErr = createMatrixOfInteger64(pvApiCtx, iVar, nbRow, nbCol, tmp_int64);
184                 }
185                 else
186                 {
187                     sciErr = createMatrixOfInteger64InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int64);
188                 }
189
190                 if (sciErr.iErr)
191                 {
192                     printError(&sciErr, 0);
193                     return 0;
194                 }
195
196
197                 FREE(tmp_int64);
198             }
199             break;
200 #endif
201             case SCI_UINT8:
202             {
203                 tmp_uint8 = (unsigned char *)MALLOC(iSize * sizeof(unsigned char));
204                 if (tmp_uint8 == NULL)
205                 {
206                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
207                     return FALSE;
208                 }
209                 for (i = 0; i < iSize; i++)
210                 {
211                     tmp_uint8[i] = ((unsigned char *)matVariable->data)[i];
212                 }
213
214
215                 if (parent == NULL)
216                 {
217                     sciErr = createMatrixOfUnsignedInteger8(pvApiCtx, iVar, nbRow, nbCol, tmp_uint8);
218                 }
219                 else
220                 {
221                     sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint8);
222                 }
223
224                 if (sciErr.iErr)
225                 {
226                     printError(&sciErr, 0);
227                     return 0;
228                 }
229
230
231                 FREE(tmp_uint8);
232             }
233             break;
234             case SCI_UINT16:
235             {
236                 tmp_uint16 = (unsigned short *)MALLOC(iSize * sizeof(unsigned short));
237                 if (tmp_uint16 == NULL)
238                 {
239                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
240                     return FALSE;
241                 }
242                 for (i = 0; i < iSize; i++)
243                 {
244                     tmp_uint16[i] = ((unsigned short *)matVariable->data)[i];
245                 }
246
247                 if (parent == NULL)
248                 {
249                     sciErr = createMatrixOfUnsignedInteger16(pvApiCtx, iVar, nbRow, nbCol, tmp_uint16);
250                 }
251                 else
252                 {
253                     sciErr = createMatrixOfUnsignedInteger16InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint16);
254                 }
255
256                 if (sciErr.iErr)
257                 {
258                     printError(&sciErr, 0);
259                     return 0;
260                 }
261
262                 FREE(tmp_uint16);
263             }
264             break;
265             case SCI_UINT32:
266             {
267                 tmp_uint32 = (unsigned int *)MALLOC(iSize * sizeof(unsigned int));
268                 if (tmp_uint32 == NULL)
269                 {
270                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
271                     return FALSE;
272                 }
273                 for (i = 0; i < iSize; i++)
274                 {
275                     tmp_uint32[i] = ((unsigned int *)matVariable->data)[i];
276                 }
277
278
279                 if (parent == NULL)
280                 {
281                     sciErr = createMatrixOfUnsignedInteger32(pvApiCtx, iVar, nbRow, nbCol, tmp_uint32);
282                 }
283                 else
284                 {
285                     sciErr = createMatrixOfUnsignedInteger32InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint32);
286                 }
287
288                 if (sciErr.iErr)
289                 {
290                     printError(&sciErr, 0);
291                     return 0;
292                 }
293
294                 FREE(tmp_uint32);
295             }
296             break;
297 #ifdef __SCILAB_INT64__
298             case SCI_UINT64:
299             {
300                 tmp_uint64 = (unsigned long long *)MALLOC(iSize * sizeof(unsigned long long));
301                 if (tmp_uint64 == NULL)
302                 {
303                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
304                     return FALSE;
305                 }
306                 for (i = 0; i < iSize; i++)
307                 {
308                     tmp_uint64[i] = ((unsigned long long *)matVariable->data)[i];
309                 }
310
311                 if (parent == NULL)
312                 {
313                     sciErr = createMatrixOfUnsignedInteger64(pvApiCtx, iVar, nbRow, nbCol, tmp_uint64);
314                 }
315                 else
316                 {
317                     sciErr = createMatrixOfUnsignedInteger64InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint64);
318                 }
319
320                 if (sciErr.iErr)
321                 {
322                     printError(&sciErr, 0);
323                     return 0;
324                 }
325
326
327                 FREE(tmp_uint64);
328             }
329             break;
330 #endif
331         }
332     }
333     else /* Multi-dimension array -> Scilab HyperMatrix */
334     {
335         piDims = (int*) MALLOC(matVariable->rank * sizeof(int));
336         if (piDims == NULL)
337         {
338             Scierror(999, _("%s: No more memory.\n"), "CreateBooleanVariable");
339             return FALSE;
340         }
341         for (i = 0; i < matVariable->rank; i++)
342         {
343             piDims[i] = (int)matVariable->dims[i];
344         }
345
346         CreateHyperMatrixVariable(pvApiCtx, iVar, matVariable->class_type, &integerType, &matVariable->rank,
347                                   piDims, matVariable , parent, item_position);
348
349         FREE(piDims);
350     }
351
352     return TRUE;
353 }