25631e482bbce18fd642891ed2ece445079470ba
[scilab.git] / scilab / modules / matio / src / c / CreateHyperMatrixVariable.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  * 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
22 int CreateHyperMatrixVariable(int iVar, const char *type, int *iscomplex, int * rank, int *dims, double *realdata, double *complexdata, int * parent, int item_position)
23 {
24     static const char *tlistFields[] = {"hm", "dims","entries"};
25     int nbRow = 0, nbCol = 0;
26     int K = 0, i;
27     int * hm_addr = NULL;
28     SciErr sciErr;
29     
30     /* Used for integer hypermatrices */
31     char * tmp_int8 = NULL;
32     short * tmp_int16 = NULL;
33     int * tmp_int32 = NULL;
34     unsigned char * tmp_uint8 = NULL;
35     unsigned short * tmp_uint16 = NULL;
36     unsigned int * tmp_uint32 = NULL;
37 #ifdef __SCILAB_INT64__
38     long long * tmp_int64 = NULL;
39     unsigned long long * tmp_uint64 = NULL;
40 #endif
41     
42     if (parent==NULL)
43     {
44         sciErr = createMList(pvApiCtx, iVar, 3, &hm_addr);
45         if(sciErr.iErr)
46         {
47             printError(&sciErr, 0);
48             return FALSE;
49         }
50     }
51     else
52     {
53         sciErr = createMListInList(pvApiCtx, iVar, parent, item_position, 3, &hm_addr);
54         if(sciErr.iErr)
55         {
56             printError(&sciErr, 0);
57             return FALSE;
58         }
59     }
60     
61     /* mlist fields */
62     nbRow = 1;
63     nbCol = 3;
64     sciErr = createMatrixOfStringInList(pvApiCtx, iVar, hm_addr, 1, nbRow, nbCol, (const char **)tlistFields);
65     if(sciErr.iErr)
66     {
67         printError(&sciErr, 0);
68         return FALSE;
69     }
70     
71     /* hm dimensions */
72     sciErr = createMatrixOfInteger32InList(pvApiCtx, iVar, hm_addr, 2, 1, *rank, dims);
73     if(sciErr.iErr)
74     {
75         printError(&sciErr, 0);
76         return FALSE;
77     }
78     
79     /* hm entries */
80     nbRow = 1;
81     nbCol = 1;
82     for (K=0; K<*rank; K++)
83     {
84         nbRow *= dims[K];
85     }
86     
87     if (strcmp(type,MATRIX_OF_VARIABLE_SIZE_INTEGER_DATATYPE) == 0)
88     {
89         switch(*iscomplex)
90         {
91             case I_CHAR:
92                 tmp_int8 = (char *)MALLOC(nbRow*nbCol*sizeof(char));
93                 if (tmp_int8 == NULL)
94                 {
95                     Scierror(999, _("%s: No more memory.\n"), "CreateHyperMatrixVariable");
96                     return FALSE;
97                 }
98                 for(i=0; i<nbRow*nbCol; i++)
99                 {
100                     tmp_int8[i] = ((char *)realdata)[i];
101                 }
102                 sciErr = createMatrixOfInteger8InList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, tmp_int8);
103                 if(sciErr.iErr)
104                 {
105                     printError(&sciErr, 0);
106                     FREE(tmp_int8);
107                     return FALSE;
108                 }
109                 FREE(tmp_int8);
110                 break;
111             case I_INT16:
112                 tmp_int16 = (short *)MALLOC(nbRow*nbCol*sizeof(short));
113                 if (tmp_int16 == NULL)
114                 {
115                     Scierror(999, _("%s: No more memory.\n"), "CreateHyperMatrixVariable");
116                     return FALSE;
117                 }
118                 for(i=0; i<nbRow*nbCol; i++)
119                 {
120                     tmp_int16[i] = ((short *)realdata)[i];
121                 }
122                 sciErr = createMatrixOfInteger16InList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, tmp_int16);
123                 if(sciErr.iErr)
124                 {
125                     printError(&sciErr, 0);
126                     FREE(tmp_int16);
127                     return FALSE;
128                 }
129                 
130                 FREE(tmp_int16);
131                 break;
132             case I_INT32:
133                 tmp_int32 = (int *)MALLOC(nbRow*nbCol*sizeof(int));
134                 if (tmp_int32 == NULL)
135                 {
136                     Scierror(999, _("%s: No more memory.\n"), "CreateHyperMatrixVariable");
137                     return FALSE;
138                 }
139                 for(i=0; i<nbRow*nbCol; i++)
140                 {
141                     tmp_int32[i] = ((int *)realdata)[i];
142                 }
143                 sciErr = createMatrixOfInteger32InList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, tmp_int32);
144                 if(sciErr.iErr)
145                 {
146                     printError(&sciErr, 0);
147                     FREE(tmp_int32);
148                     return FALSE;
149                 }
150                 FREE(tmp_int32);
151                 break;
152 #ifdef __SCILAB_INT64__
153             case I_INT64:
154                 tmp_int64 = (long long *)MALLOC(nbRow*nbCol*sizeof(long long));
155                 if (tmp_int64 == NULL)
156                 {
157                     Scierror(999, _("%s: No more memory.\n"), "CreateHyperMatrixVariable");
158                     return FALSE;
159                 }
160                 for(i=0; i<nbRow*nbCol; i++)
161                 {    
162                     tmp_int64[i] = ((long long *)realdata)[i];
163                 }
164                 qciErr = createMatrixOfInteger64InList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, tmp_int64);
165                 if(sciErr.iErr)
166                 {
167                     printError(&sciErr, 0);
168                     FREE(tmp_int64);
169                     return FALSE;
170                 }
171                 FREE(tmp_int64);
172                 break;
173 #endif
174             case I_UCHAR:
175                 tmp_uint8 = (unsigned char *)MALLOC(nbRow*nbCol*sizeof(unsigned char));
176                 if (tmp_uint8 == NULL)
177                 {
178                     Scierror(999, _("%s: No more memory.\n"), "CreateHyperMatrixVariable");
179                     return FALSE;
180                 }
181                 for(i=0; i<nbRow*nbCol; i++)
182                 {
183                     tmp_uint8[i] = ((unsigned char *)realdata)[i];
184                 }
185                 sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, tmp_uint8);
186                 if(sciErr.iErr)
187                 {
188                     printError(&sciErr, 0);
189                     FREE(tmp_uint8);
190                     return FALSE;
191                 }
192                 FREE(tmp_uint8);
193                 break;
194             case I_UINT16:
195                 tmp_uint16 = (unsigned short *)MALLOC(nbRow*nbCol*sizeof(unsigned short));
196                 if (tmp_uint16 == NULL)
197                 {
198                     Scierror(999, _("%s: No more memory.\n"), "CreateHyperMatrixVariable");
199                     return FALSE;
200                 }
201                 for(i=0; i<nbRow*nbCol; i++)
202                 {
203                     tmp_uint16[i] = ((unsigned short *)realdata)[i];
204                 }
205                 sciErr = createMatrixOfUnsignedInteger16InList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, tmp_uint16);
206                 if(sciErr.iErr)
207                 {
208                     printError(&sciErr, 0);
209                     FREE(tmp_uint16);
210                     return FALSE;
211                 }
212                 FREE(tmp_uint16);
213                 break;
214             case I_UINT32:
215                 tmp_uint32 = (unsigned int *)MALLOC(nbRow*nbCol*sizeof(unsigned int));
216                 if (tmp_uint32 == NULL)
217                 {
218                     Scierror(999, _("%s: No more memory.\n"), "CreateHyperMatrixVariable");
219                     return FALSE;
220                 }
221                 for(i=0;i<nbRow*nbCol; i++)
222                 {
223                     tmp_uint32[i] = ((unsigned int *)realdata)[i];
224                 }
225                 sciErr = createMatrixOfUnsignedInteger32InList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, tmp_uint32);
226                 if(sciErr.iErr)
227                 {
228                     printError(&sciErr, 0);
229                     FREE(tmp_uint32);
230                     return FALSE;
231                 }
232                 FREE(tmp_uint32);
233                 break;
234 #ifdef __SCILAB_INT64__
235             case I_UINT64:
236                 tmp_uint64 = (unsigned long long *)MALLOC(nbRow*nbCol*sizeof(unsigned long long));
237                 if (tmp_uint64 == NULL)
238                 {
239                     Scierror(999, _("%s: No more memory.\n"), "CreateHyperMatrixVariable");
240                     return FALSE;
241                 }
242                 for(i=0; i<nbRow*nbCol; i++)
243                 {
244                     tmp_uint64[i] = ((unsigned long long *)realdata)[i];
245                 }
246                 sciErr = createMatrixOfUnsignedInteger64InList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, tmp_uint64);
247                 if(sciErr.iErr)
248                 {
249                     printError(&sciErr, 0);
250                     FREE(tmp_uint64);
251                     return FALSE;
252                 }
253                 FREE(tmp_uint64);
254                 break;
255 #endif
256         }
257     }
258     else if (strcmp(type, MATRIX_OF_BOOLEAN_DATATYPE) == 0)
259     {
260         sciErr = createMatrixOfDoubleInList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, realdata);
261         if(sciErr.iErr)
262         {
263             printError(&sciErr, 0);
264             return FALSE;
265         }
266     }
267     else
268     {
269         if (*iscomplex == 0)
270         {
271             sciErr = createMatrixOfDoubleInList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, realdata);
272             if(sciErr.iErr)
273             {
274                 printError(&sciErr, 0);
275                 return FALSE;
276             }
277         }
278         else
279         {
280             sciErr = createComplexMatrixOfDoubleInList(pvApiCtx, iVar, hm_addr, 3, nbRow, nbCol, realdata, complexdata);
281             if(sciErr.iErr)
282             {
283                 printError(&sciErr, 0);
284                 return FALSE;
285             }
286         }
287     }
288     
289     return TRUE;
290 }