* Port to matio 1.5.0 * Code cleaning * Prepare code for Scilab 6 (YaSp branch merge)
[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  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13
14 #include "CreateMatlabVariable.h"
15 #include "api_scilab.h"
16 #include "MALLOC.h"
17 #include "localization.h"
18 #include "Scierror.h"
19 #include "sciprint.h"
20
21 int CreateIntegerVariable(void *pvApiCtx, int iVar, int integerType, matvar_t *matVariable, int * parent, int item_position)
22 {
23     int nbRow, nbCol, i;
24     SciErr sciErr;
25     char * tmp_int8 = NULL;
26     short * tmp_int16 = NULL;
27     int * tmp_int32 = NULL;
28     int *piDims = NULL;
29     unsigned char * tmp_uint8 = NULL;
30     unsigned short * tmp_uint16 = NULL;
31     unsigned int * tmp_uint32 = NULL;
32 #ifdef __SCILAB_INT64__
33     long long * tmp_int64 = NULL;
34     unsigned long long * tmp_uint64 = NULL;
35 #endif
36
37     // Matrix dimensions
38     nbRow = (int)matVariable->dims[0];
39     nbCol = (int)matVariable->dims[1];
40
41     if (matVariable->rank == 2) /* 2-D array */
42     {
43         switch (integerType)
44         {
45             case I_CHAR:
46                 tmp_int8 = (char *)MALLOC(nbRow * nbCol * sizeof(char));
47                 if (tmp_int8 == NULL)
48                 {
49                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
50                     return FALSE;
51                 }
52                 for (i = 0; i < nbRow * nbCol; i++)
53                 {
54                     tmp_int8[i] = ((char *)matVariable->data)[i];
55                 }
56
57                 if (parent == NULL)
58                 {
59                     sciErr = createMatrixOfInteger8(pvApiCtx, iVar, nbRow, nbCol, tmp_int8);
60                 }
61                 else
62                 {
63                     sciErr = createMatrixOfInteger8InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int8);
64                 }
65
66                 if (sciErr.iErr)
67                 {
68                     printError(&sciErr, 0);
69                     return 0;
70                 }
71
72                 FREE(tmp_int8);
73                 break;
74             case I_INT16:
75                 tmp_int16 = (short *)MALLOC(nbRow * nbCol * sizeof(short));
76                 if (tmp_int16 == NULL)
77                 {
78                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
79                     return FALSE;
80                 }
81                 for (i = 0; i < nbRow * nbCol; i++)
82                 {
83                     tmp_int16[i] = ((short *)matVariable->data)[i];
84                 }
85
86                 if (parent == NULL)
87                 {
88                     sciErr = createMatrixOfInteger16(pvApiCtx, iVar, nbRow, nbCol, tmp_int16);
89                 }
90                 else
91                 {
92                     sciErr = createMatrixOfInteger16InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int16);
93                 }
94
95                 if (sciErr.iErr)
96                 {
97                     printError(&sciErr, 0);
98                     return 0;
99                 }
100
101                 FREE(tmp_int16);
102                 break;
103             case I_INT32:
104                 tmp_int32 = (int *)MALLOC(nbRow * nbCol * sizeof(int));
105                 if (tmp_int32 == NULL)
106                 {
107                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
108                     return FALSE;
109                 }
110                 for (i = 0; i < nbRow * nbCol; i++)
111                 {
112                     tmp_int32[i] = ((int *)matVariable->data)[i];
113                 }
114
115                 if (parent == NULL)
116                 {
117                     sciErr = createMatrixOfInteger32(pvApiCtx, iVar, nbRow, nbCol, tmp_int32);
118                 }
119                 else
120                 {
121                     sciErr = createMatrixOfInteger32InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int32);
122                 }
123
124                 if (sciErr.iErr)
125                 {
126                     printError(&sciErr, 0);
127                     return 0;
128                 }
129
130                 FREE(tmp_int32);
131                 break;
132 #ifdef __SCILAB_INT64__
133             case I_INT64:
134                 tmp_int64 = (long long *)MALLOC(nbRow * nbCol * sizeof(long long));
135                 if (tmp_int64 == NULL)
136                 {
137                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
138                     return FALSE;
139                 }
140                 for (i = 0; i < nbRow * nbCol; i++)
141                 {
142                     tmp_int64[i] = ((long long *)matVariable->data)[i];
143                 }
144
145                 if (parent == NULL)
146                 {
147                     sciErr = createMatrixOfInteger64(pvApiCtx, iVar, nbRow, nbCol, tmp_int64);
148                 }
149                 else
150                 {
151                     sciErr = createMatrixOfInteger64InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int64);
152                 }
153
154                 if (sciErr.iErr)
155                 {
156                     printError(&sciErr, 0);
157                     return 0;
158                 }
159
160                 FREE(tmp_int64);
161                 break;
162 #endif
163             case I_UCHAR:
164                 tmp_uint8 = (unsigned char *)MALLOC(nbRow * nbCol * sizeof(unsigned char));
165                 if (tmp_uint8 == NULL)
166                 {
167                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
168                     return FALSE;
169                 }
170                 for (i = 0; i < nbRow * nbCol; i++)
171                 {
172                     tmp_uint8[i] = ((unsigned char *)matVariable->data)[i];
173                 }
174
175                 if (parent == NULL)
176                 {
177                     sciErr = createMatrixOfUnsignedInteger8(pvApiCtx, iVar, nbRow, nbCol, tmp_uint8);
178                 }
179                 else
180                 {
181                     sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint8);
182                 }
183
184                 if (sciErr.iErr)
185                 {
186                     printError(&sciErr, 0);
187                     return 0;
188                 }
189
190                 FREE(tmp_uint8);
191                 break;
192             case I_UINT16:
193                 tmp_uint16 = (unsigned short *)MALLOC(nbRow * nbCol * sizeof(unsigned short));
194                 if (tmp_uint16 == NULL)
195                 {
196                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
197                     return FALSE;
198                 }
199                 for (i = 0; i < nbRow * nbCol; i++)
200                 {
201                     tmp_uint16[i] = ((unsigned short *)matVariable->data)[i];
202                 }
203
204                 if (parent == NULL)
205                 {
206                     sciErr = createMatrixOfUnsignedInteger16(pvApiCtx, iVar, nbRow, nbCol, tmp_uint16);
207                 }
208                 else
209                 {
210                     sciErr = createMatrixOfUnsignedInteger16InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint16);
211                 }
212
213                 if (sciErr.iErr)
214                 {
215                     printError(&sciErr, 0);
216                     return 0;
217                 }
218
219                 FREE(tmp_uint16);
220                 break;
221             case I_UINT32:
222                 tmp_uint32 = (unsigned int *)MALLOC(nbRow * nbCol * sizeof(unsigned int));
223                 if (tmp_uint32 == NULL)
224                 {
225                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
226                     return FALSE;
227                 }
228                 for (i = 0; i < nbRow * nbCol; i++)
229                 {
230                     tmp_uint32[i] = ((unsigned int *)matVariable->data)[i];
231                 }
232
233                 if (parent == NULL)
234                 {
235                     sciErr = createMatrixOfUnsignedInteger32(pvApiCtx, iVar, nbRow, nbCol, tmp_uint32);
236                 }
237                 else
238                 {
239                     sciErr = createMatrixOfUnsignedInteger32InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint32);
240                 }
241
242                 if (sciErr.iErr)
243                 {
244                     printError(&sciErr, 0);
245                     return 0;
246                 }
247
248                 FREE(tmp_uint32);
249                 break;
250 #ifdef __SCILAB_INT64__
251             case I_UINT64:
252                 tmp_uint64 = (unsigned long long *)MALLOC(nbRow * nbCol * sizeof(unsigned long long));
253                 if (tmp_uint64 == NULL)
254                 {
255                     Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable");
256                     return FALSE;
257                 }
258                 for (i = 0; i < nbRow * nbCol; i++)
259                 {
260                     tmp_uint64[i] = ((unsigned long long *)matVariable->data)[i];
261                 }
262
263                 if (parent == NULL)
264                 {
265                     sciErr = createMatrixOfUnsignedInteger64(pvApiCtx, iVar, nbRow, nbCol, tmp_uint64);
266                 }
267                 else
268                 {
269                     sciErr = createMatrixOfUnsignedInteger64InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint64);
270                 }
271
272                 if (sciErr.iErr)
273                 {
274                     printError(&sciErr, 0);
275                     return 0;
276                 }
277
278                 FREE(tmp_uint64);
279                 break;
280 #endif
281         }
282     }
283     else /* Multi-dimension array -> Scilab HyperMatrix */
284     {
285         piDims = (int*) MALLOC(matVariable->rank * sizeof(int));
286         if (piDims == NULL)
287         {
288             Scierror(999, _("%s: No more memory.\n"), "CreateBooleanVariable");
289             return FALSE;
290         }
291         for (i = 0; i < matVariable->rank; i++)
292         {
293             piDims[i] = (int)matVariable->dims[i];
294         }
295
296         CreateHyperMatrixVariable(pvApiCtx, iVar, MATRIX_OF_VARIABLE_SIZE_INTEGER_DATATYPE,  &integerType, &matVariable->rank,
297                                   piDims, (double*)matVariable->data, NULL, parent, item_position);
298
299         FREE(piDims);
300     }
301
302     return TRUE;
303 }