Coverity: Matio module memory leaks fixed
[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                     FREE(tmp_int8);
91                     return 0;
92                 }
93
94                 FREE(tmp_int8);
95             }
96             break;
97             case SCI_INT16:
98             {
99
100                 tmp_int16 = (short *)MALLOC(iSize * sizeof(short));
101                 if (tmp_int16 == NULL)
102                 {
103                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
104                     return FALSE;
105                 }
106
107                 for (i = 0; i < iSize; i++)
108                 {
109                     tmp_int16[i] = ((short *)matVariable->data)[i];
110                 }
111
112
113                 if (parent == NULL)
114                 {
115                     sciErr = createMatrixOfInteger16(pvApiCtx, iVar, nbRow, nbCol, tmp_int16);
116                 }
117                 else
118                 {
119                     sciErr = createMatrixOfInteger16InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int16);
120                 }
121
122                 if (sciErr.iErr)
123                 {
124                     printError(&sciErr, 0);
125                     FREE(tmp_int16);
126                     return 0;
127                 }
128
129
130                 FREE(tmp_int16);
131             }
132             break;
133             case SCI_INT32:
134             {
135                 tmp_int32 = (int *)MALLOC(iSize * sizeof(int));
136
137                 if (tmp_int32 == NULL)
138                 {
139                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
140                     return FALSE;
141                 }
142
143                 for (i = 0; i < iSize; i++)
144                 {
145                     tmp_int32[i] = ((int *)matVariable->data)[i];
146                 }
147
148
149                 if (parent == NULL)
150                 {
151                     sciErr = createMatrixOfInteger32(pvApiCtx, iVar, nbRow, nbCol, tmp_int32);
152                 }
153                 else
154                 {
155                     sciErr = createMatrixOfInteger32InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int32);
156                 }
157
158                 if (sciErr.iErr)
159                 {
160                     printError(&sciErr, 0);
161                     FREE(tmp_int32);
162                     return 0;
163                 }
164
165
166                 FREE(tmp_int32);
167             }
168             break;
169 #ifdef __SCILAB_INT64__
170             case SCI_INT64:
171             {
172                 tmp_int64 = (long long *)MALLOC(iSize * sizeof(long long));
173                 if (tmp_int64 == NULL)
174                 {
175                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
176                     return FALSE;
177                 }
178                 for (i = 0; i < iSize; i++)
179                 {
180                     tmp_int64[i] = ((long long *)matVariable->data)[i];
181                 }
182
183
184                 if (parent == NULL)
185                 {
186                     sciErr = createMatrixOfInteger64(pvApiCtx, iVar, nbRow, nbCol, tmp_int64);
187                 }
188                 else
189                 {
190                     sciErr = createMatrixOfInteger64InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int64);
191                 }
192
193                 if (sciErr.iErr)
194                 {
195                     printError(&sciErr, 0);
196                     FREE(tmp_int64);
197                     return 0;
198                 }
199
200
201                 FREE(tmp_int64);
202             }
203             break;
204 #endif
205             case SCI_UINT8:
206             {
207                 tmp_uint8 = (unsigned char *)MALLOC(iSize * sizeof(unsigned char));
208                 if (tmp_uint8 == NULL)
209                 {
210                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
211                     return FALSE;
212                 }
213                 for (i = 0; i < iSize; i++)
214                 {
215                     tmp_uint8[i] = ((unsigned char *)matVariable->data)[i];
216                 }
217
218
219                 if (parent == NULL)
220                 {
221                     sciErr = createMatrixOfUnsignedInteger8(pvApiCtx, iVar, nbRow, nbCol, tmp_uint8);
222                 }
223                 else
224                 {
225                     sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint8);
226                 }
227
228                 if (sciErr.iErr)
229                 {
230                     printError(&sciErr, 0);
231                     FREE(tmp_uint8);
232                     return 0;
233                 }
234
235
236                 FREE(tmp_uint8);
237             }
238             break;
239             case SCI_UINT16:
240             {
241                 tmp_uint16 = (unsigned short *)MALLOC(iSize * sizeof(unsigned short));
242                 if (tmp_uint16 == NULL)
243                 {
244                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
245                     return FALSE;
246                 }
247                 for (i = 0; i < iSize; i++)
248                 {
249                     tmp_uint16[i] = ((unsigned short *)matVariable->data)[i];
250                 }
251
252                 if (parent == NULL)
253                 {
254                     sciErr = createMatrixOfUnsignedInteger16(pvApiCtx, iVar, nbRow, nbCol, tmp_uint16);
255                 }
256                 else
257                 {
258                     sciErr = createMatrixOfUnsignedInteger16InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint16);
259                 }
260
261                 if (sciErr.iErr)
262                 {
263                     printError(&sciErr, 0);
264                     FREE(tmp_uint16);
265                     return 0;
266                 }
267
268                 FREE(tmp_uint16);
269             }
270             break;
271             case SCI_UINT32:
272             {
273                 tmp_uint32 = (unsigned int *)MALLOC(iSize * sizeof(unsigned int));
274                 if (tmp_uint32 == NULL)
275                 {
276                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
277                     return FALSE;
278                 }
279                 for (i = 0; i < iSize; i++)
280                 {
281                     tmp_uint32[i] = ((unsigned int *)matVariable->data)[i];
282                 }
283
284
285                 if (parent == NULL)
286                 {
287                     sciErr = createMatrixOfUnsignedInteger32(pvApiCtx, iVar, nbRow, nbCol, tmp_uint32);
288                 }
289                 else
290                 {
291                     sciErr = createMatrixOfUnsignedInteger32InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint32);
292                 }
293
294                 if (sciErr.iErr)
295                 {
296                     printError(&sciErr, 0);
297                     FREE(tmp_uint32);
298                     return 0;
299                 }
300
301                 FREE(tmp_uint32);
302             }
303             break;
304 #ifdef __SCILAB_INT64__
305             case SCI_UINT64:
306             {
307                 tmp_uint64 = (unsigned long long *)MALLOC(iSize * sizeof(unsigned long long));
308                 if (tmp_uint64 == NULL)
309                 {
310                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
311                     return FALSE;
312                 }
313                 for (i = 0; i < iSize; i++)
314                 {
315                     tmp_uint64[i] = ((unsigned long long *)matVariable->data)[i];
316                 }
317
318                 if (parent == NULL)
319                 {
320                     sciErr = createMatrixOfUnsignedInteger64(pvApiCtx, iVar, nbRow, nbCol, tmp_uint64);
321                 }
322                 else
323                 {
324                     sciErr = createMatrixOfUnsignedInteger64InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint64);
325                 }
326
327                 if (sciErr.iErr)
328                 {
329                     printError(&sciErr, 0);
330                     FREE(tmp_uint64);
331                     return 0;
332                 }
333
334
335                 FREE(tmp_uint64);
336             }
337             break;
338 #endif
339         }
340     }
341     else /* Multi-dimension array -> Scilab HyperMatrix */
342     {
343         piDims = (int*) MALLOC(matVariable->rank * sizeof(int));
344         if (piDims == NULL)
345         {
346             Scierror(999, _("%s: No more memory.\n"), "CreateBooleanVariable");
347             return FALSE;
348         }
349         for (i = 0; i < matVariable->rank; i++)
350         {
351             piDims[i] = (int)matVariable->dims[i];
352         }
353
354         CreateHyperMatrixVariable(pvApiCtx, iVar, matVariable->class_type, &integerType, &matVariable->rank,
355                                   piDims, matVariable , parent, item_position);
356
357         FREE(piDims);
358     }
359
360     return TRUE;
361 }