Coverity #1321058, #1321094, #1321156, #1354293 fixed
[scilab.git] / scilab / modules / matio / src / c / CreateDoubleVariable.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 - DIGITEO - Yann COLLETTE
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
21 int CreateDoubleVariable(void *pvApiCtx, int iVar, matvar_t *matVariable, int * parent, int item_position)
22 {
23     int nbRow = 0, nbCol = 0;
24     mat_complex_split_t *mat5ComplexData = NULL;
25     SciErr sciErr;
26     int *piDims = NULL;
27     int i = 0;
28
29     if (matVariable->rank == 2) /* 2-D array */
30     {
31         nbRow = (int)matVariable->dims[0];
32         nbCol = (int)matVariable->dims[1];
33         if (matVariable->isComplex == 0)
34         {
35             if (parent == NULL)
36             {
37                 sciErr = createMatrixOfDouble(pvApiCtx, iVar, nbRow, nbCol, (double*)matVariable->data);
38                 if (sciErr.iErr)
39                 {
40                     printError(&sciErr, 0);
41                     return 0;
42                 }
43             }
44             else
45             {
46                 sciErr = createMatrixOfDoubleInList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, (double*)matVariable->data);
47                 if (sciErr.iErr)
48                 {
49                     printError(&sciErr, 0);
50                     return 0;
51                 }
52             }
53         }
54         else
55         {
56             /* Since MATIO 1.3.2 data is a ComplexSplit for MAT4 and MAT5 formats */
57             mat5ComplexData = matVariable->data;
58             if (parent == NULL)
59             {
60                 sciErr = createComplexMatrixOfDouble(pvApiCtx, iVar, nbRow, nbCol, (double*)mat5ComplexData->Re, (double*)mat5ComplexData->Im);
61             }
62             else
63             {
64                 sciErr = createComplexMatrixOfDoubleInList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol,
65                          (double*)mat5ComplexData->Re, (double*)mat5ComplexData->Im);
66             }
67         }
68     }
69     else /* Multi-dimension array -> Scilab HyperMatrix */
70     {
71         piDims = (int *) MALLOC(matVariable->rank * sizeof(int));
72         for (i = 0 ; i < matVariable->rank ; ++i)
73         {
74             piDims[i] = (int)matVariable->dims[i];
75         }
76
77         CreateHyperMatrixVariable(pvApiCtx, iVar, matVariable->class_type, &matVariable->isComplex, &matVariable->rank,
78                                   piDims, matVariable, parent, item_position);
79
80         FREE(piDims);
81     }
82     return TRUE;
83 }