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 * Copyright (C) 2015 - Scilab Enterprises - Sylvain GENIN
6 *
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 *
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.
15 *
16 */
17
18 #include "GetMatlabVariable.hxx"
19 #include "gatewaystruct.hxx"
20 #include "context.hxx"
21 #include "ConvertSciVarToMatVar.hxx"
22
23 extern "C"
24 {
25 #include "sci_types.h"
26 #include "api_scilab.h"
27 #include "freeArrayOfString.h"
28 #include "sci_malloc.h"
29 }
30
GetCellVariable(void * pvApiCtx,int iVar,const char * name,int matfile_version,int * parent,int item_position)31 matvar_t *GetCellVariable(void *pvApiCtx, int iVar, const char *name, int matfile_version, int * parent, int item_position)
32 {
33 types::GatewayStruct* pGS = (types::GatewayStruct*)pvApiCtx;
34 types::typed_list in = *pGS->m_pIn;
35
36 if (in[iVar - 1]->isCell() == false)
37 {
38 Scierror(999, _("%s: Wrong type for first input argument: string expected.\n"), "GetCellVariable");
39 return NULL;
40 }
41
42 types::Cell* pCell = in[iVar - 1]->getAs<types::Cell>();
43
44 return GetCellMatVar(pCell, name, matfile_version);
45 }
46
GetCellMatVar(types::Cell * pCell,const char * name,int matfile_version)47 matvar_t* GetCellMatVar(types::Cell* pCell, const char* name, int matfile_version)
48 {
49 matvar_t **cellEntries = NULL;
50
51 int Dims = pCell->getDims();
52 int* pDims = pCell->getDimsArray();
53 int prodDims = pCell->getSize();
54
55 matvar_t* pMatVarOut = NULL;
56
57 /* OTHERS LIST ENTRIES: ALL CELL VALUES */
58
59 size_t* pszDims = (size_t*)MALLOC(Dims * sizeof(size_t));
60 if (pszDims == NULL)
61 {
62 Scierror(999, _("%s: No more memory.\n"), "GetCellMatVar");
63 return NULL;
64 }
65
66 /* Total number of entries */
67 for (int K = 0; K < Dims; ++K)
68 {
69 pszDims[K] = ((int*)pDims)[K];
70 }
71
72 cellEntries = (matvar_t **)MALLOC(sizeof(matvar_t*) * prodDims);
73 if (cellEntries == NULL)
74 {
75 Scierror(999, _("%s: No more memory.\n"), "GetCellMatVar");
76 FREE(pszDims);
77 return NULL;
78 }
79
80
81 types::InternalType** ppIT = pCell->get();
82 for (int K = 0; K < prodDims; ++K)
83 {
84 cellEntries[K] = ConvertSciVarToMatVar(ppIT[K], name, matfile_version);
85 if (cellEntries[K] == NULL)
86 {
87 FREE(cellEntries);
88 FREE(pszDims);
89 return NULL;
90 }
91 }
92
93 pMatVarOut = Mat_VarCreate(name, MAT_C_CELL, MAT_T_CELL, Dims, pszDims, cellEntries, 0);
94
95 FREE(pszDims);
96
97 return pMatVarOut;
98 }
99