1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2008 - INRIA - Vincent COUVERT
* Copyright (C) 2010 - DIGITEO - Yann COLLETTE
* Copyright (C) 2011 - DIGITEO - Vincent COUVERT
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
#include "GetMatlabVariable.h"
#include "api_scilab.h"
#include "freeArrayOfString.h"
matvar_t *GetStructVariable(void *pvApiCtx, int iVar, const char *name, int matfile_version, char **fieldNames, int nbFields, int * parent, int item_position)
{
int fieldIndex = 0;
int valueIndex = 0;
int K = 0;
size_t *pszDims = NULL;
int prodDims = 1;
matvar_t *dimensionsVariable = NULL;
matvar_t **structEntries = NULL;
int * var_addr = NULL;
int * list_addr = NULL;
SciErr sciErr;
if (parent == NULL)
{
sciErr = getVarAddressFromPosition(pvApiCtx, iVar, &var_addr);
}
else
{
sciErr = getListItemAddress(pvApiCtx, parent, item_position, &var_addr);
}
if (sciErr.iErr)
{
printError(&sciErr, 0);
return NULL;
}
/* FIRST LIST ENTRY: fieldnames --> NO NEED TO BE READ */
/* SECOND LIST ENTRY: dimensions */
/* Second input argument = "data" because we do not need to give the format because this variable is just temp */
dimensionsVariable = GetMatlabVariable(pvApiCtx, iVar, "data", 0, var_addr, 2);
pszDims = (size_t*) MALLOC(dimensionsVariable->rank * sizeof(size_t));
if (pszDims == NULL)
{
Scierror(999, _("%s: No more memory.\n"), "GetStructVariable");
freeArrayOfString(fieldNames, nbFields);
return NULL;
}
/* Total number of entries */
for (K = 0; K < dimensionsVariable->rank; K++)
{
prodDims *= ((int*)dimensionsVariable->data)[K];
pszDims[K] = ((int*)dimensionsVariable->data)[K];
}
/* OTHERS LIST ENTRIES: ALL STRUCT VALUES */
structEntries = (matvar_t **) MALLOC (sizeof(matvar_t*) * (prodDims * (nbFields - 2) + 1));
if (structEntries == NULL)
{
Scierror(999, _("%s: No more memory.\n"), "GetStructVariable");
freeArrayOfString(fieldNames, nbFields);
return NULL;
}
for (K = 0; K < prodDims * (nbFields - 2) + 1; K++)
{
structEntries[K] = NULL;
}
if (prodDims == 1) /* Scalar struct array */
{
for (fieldIndex = 2; fieldIndex < nbFields; fieldIndex++)
{
structEntries[fieldIndex - 2] = GetMatlabVariable(pvApiCtx, iVar , fieldNames[fieldIndex], matfile_version, var_addr, fieldIndex + 1);
}
}
else
{
/* All the values of each field are stored in a list */
/* Read all entries */
for (fieldIndex = 2; fieldIndex < nbFields; fieldIndex++)
{
sciErr = getListInList(pvApiCtx, var_addr, fieldIndex + 1, &list_addr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return NULL;
}
for (valueIndex = 0; valueIndex < prodDims; valueIndex++)
{
structEntries[(fieldIndex - 1) + (nbFields - 2)*valueIndex] = GetMatlabVariable(pvApiCtx, iVar , fieldNames[fieldIndex], matfile_version, list_addr, valueIndex + 1);
}
}
}
return Mat_VarCreate(name, MAT_C_STRUCT, MAT_T_STRUCT, dimensionsVariable->rank, pszDims, structEntries, 0);
}
|