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 * Copyright (C) 2015 - Scilab Enterprises - Sylvain GENIN
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
18 #include "CreateMatlabTreeVariable.hxx"
27 #include "CreateMatlabVariable.h"
28 #include "api_scilab.h"
30 #include "sci_malloc.h"
33 types::InternalType* CreateMatlabTreeVariable(matvar_t *matVariable)
35 types::InternalType* pOut = NULL;
36 /* To be sure isComplex is 0 or 1 */
37 bool isComplex = (matVariable->isComplex != 0);
40 int iRank = matVariable->rank;
46 piDims = (int *)MALLOC(iRank * sizeof(int));
47 piDims[0] = (int)matVariable->dims[0];
52 piDims = (int *)MALLOC(iRank * sizeof(int));
54 for (int i = 0; i < iRank; ++i)
56 piDims[i] = (int)matVariable->dims[i];
63 return types::Double::Empty();
66 switch (matVariable->class_type)
68 case MAT_C_CELL: /* 1 */
71 if ((iRank == 2) && ((piDims[0] * piDims[1]) <= 0))
73 pOut = new types::Cell();
77 pOut = new types::Cell(iRank, piDims);
79 matvar_t** allData = (matvar_t**)(matVariable->data);
80 int iSize = pOut->getAs<types::Cell>()->getSize();
81 types::InternalType** ppIT = new types::InternalType*[iSize];
82 for (int i = 0; i < iSize; i++)
84 ppIT[i] = CreateMatlabTreeVariable(allData[i]);
87 pOut->getAs<types::Cell>()->set(ppIT);
92 case MAT_C_STRUCT: /* 2 */
94 if (matVariable->data == NULL)
96 pOut = new types::Struct();
100 pOut = new types::Struct(iRank, piDims);
101 int iSizeStruct = Mat_VarGetNumberOfFields(matVariable);
103 matvar_t** allData = (matvar_t**)(matVariable->data);
105 bool bSearchSizeStruck = false;
106 for (int i = 0; (allData[i] != NULL) && (bSearchSizeStruck == false) && (i < iSizeStruct); i++)
108 for (int j = (i - 1); j >= 0; j--)
110 if (strcmp((char*)allData[i]->name, (char*)allData[j]->name) == 0)
112 bSearchSizeStruck = true;
116 temp = to_wide_string((char*)allData[i]->name);
117 std::wstring wstField(temp);
119 pOut->getAs<types::Struct>()->addField(wstField);
122 for (int i = 0; i < pOut->getAs<types::Struct>()->getSize(); i++)
124 for (int j = 0; j < iSizeStruct; j++)
126 temp = to_wide_string((char*)allData[j]->name);
127 std::wstring wstField(temp);
129 pOut->getAs<types::Struct>()->get(i)->set(wstField, CreateMatlabTreeVariable(allData[i * iSizeStruct + j]));
136 case MAT_C_CHAR: /* 4 */
138 char* pChar = (char*)MALLOC(sizeof(char) * (piDims[1] + 1));
140 strncat(pChar, (char*)matVariable->data, piDims[1]);
141 types::String* pString = new types::String(pChar);
146 case MAT_C_SPARSE: /* 5 */
148 mat_sparse_t *sparseData = (mat_sparse_t*)matVariable->data;
150 types::Sparse* pSparse = new types::Sparse(piDims[0], piDims[1], isComplex);
151 int *colIndexes = NULL;
152 int *rowIndexes = NULL;
154 /* Computes column indexes from Matlab indexes */
155 if (sparseData->njc > 1)
157 colIndexes = (int*)MALLOC(sizeof(int) * (sparseData->njc - 1));
158 if (colIndexes == NULL)
160 Scierror(999, _("%s: No more memory.\n"), "CreateMatlabTreeVariable");
166 for (int K = 0; K < sparseData->njc - 1; K++)
168 colIndexes[K] = sparseData->jc[K + 1] - sparseData->jc[K];
172 /* Computes row indexes from Matlab indexes */
173 rowIndexes = (int*)MALLOC(sizeof(int) * sparseData->nir);
174 if (rowIndexes == NULL)
176 Scierror(999, _("%s: No more memory.\n"), "CreateMatlabTreeVariable");
183 for (int K = 0; K < sparseData->nir; K++)
185 rowIndexes[K] = sparseData->ir[K] + 1;
192 mat_complex_split_t *mat5ComplexData = NULL;
193 mat5ComplexData = (mat_complex_split_t*)sparseData->data;
195 double* pReal = (double*)mat5ComplexData->Re;
196 double* pIm = (double*)mat5ComplexData->Im;
199 for (int j = 0; j < (sparseData->njc - 1); j++)
201 for (int k = 0; k < colIndexes[j]; k++)
203 std::complex<double> dbl(pReal[i], pIm[i]);
204 pSparse->set((rowIndexes[i] - 1), j, dbl);
212 double* pReal = (double*)sparseData->data;
214 for (int j = 0; j < (sparseData->njc - 1); j++)
216 for (int k = 0; k < colIndexes[j]; k++)
218 pSparse->set((rowIndexes[i] - 1), j, pReal[i]);
228 case MAT_C_DOUBLE: /* 6 */
229 case MAT_C_SINGLE: /* 7 */
231 types::Double* pDbl = new types::Double(iRank, piDims, isComplex);
234 mat_complex_split_t *mat5ComplexData = NULL;
235 mat5ComplexData = (mat_complex_split_t*)matVariable->data;
237 pDbl->set((double*)mat5ComplexData->Re);
238 pDbl->setImg((double*)mat5ComplexData->Im);
242 pDbl->set((double*)matVariable->data);
248 case MAT_C_INT8: /* 8 */
250 types::Int8* pInt8 = new types::Int8(iRank, piDims);
252 pInt8->set((char*)matVariable->data);
257 case MAT_C_UINT8: /* 9 */
259 types::UInt8* pUInt8 = new types::UInt8(iRank, piDims);
261 pUInt8->set((unsigned char*)matVariable->data);
266 case MAT_C_INT16: /* 10 */
268 types::Int16* pInt16 = new types::Int16(iRank, piDims);
270 pInt16->set((short*)matVariable->data);
275 case MAT_C_UINT16: /* 11 */
277 types::UInt16* pUInt16 = new types::UInt16(iRank, piDims);
279 pUInt16->set((unsigned short*)matVariable->data);
284 case MAT_C_INT32: /* 12 */
286 types::Int32* pInt32 = new types::Int32(iRank, piDims);
288 pInt32->set((int*)matVariable->data);
293 case MAT_C_UINT32: /* 13 */
295 types::UInt32* pUInt32 = new types::UInt32(iRank, piDims);
297 pUInt32->set((unsigned int*)matVariable->data);
302 #ifdef __SCILAB_INT64__
303 case MAT_C_INT64: /* 14 */
305 types::Int64* pInt64 = new types::Int64(iRank, piDims);
307 pInt64->set((long long*)matVariable->data);
312 case MAT_C_UINT64: /* 15 */
314 types::UInt64* pUInt64 = new types::UInt64(iRank, piDims);
316 pUInt64->set((unsigned long long*)matVariable->data);
322 case MAT_C_INT64: /* 14 */
323 case MAT_C_UINT64: /* 15 */
325 case MAT_C_OBJECT: /* 3 to be written */
326 case MAT_C_FUNCTION: /* 16 to be written */
328 /* Empty matrix returned */
330 return types::Double::Empty();