Revert "Update of messages"
[scilab.git] / scilab / modules / hdf5 / sci_gateway / cpp / sci_export_to_hdf5.cpp
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
4 *
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12
13 extern "C"
14 {
15 #include <string.h>
16 #include <stdio.h>
17 #include "gw_hdf5.h"
18 #include "stack-c.h"
19 #include "MALLOC.h"
20 #include "Scierror.h"
21 #include "localization.h"
22 #include "sciprint.h"
23 #include "api_scilab.h"
24 #include "h5_fileManagement.h"
25 #include "h5_writeDataToFile.h"
26 #include "freeArrayOfString.h"
27 #ifdef _MSC_VER
28  #include "strdup_windows.h"
29 #endif
30 }
31
32 //#define PRINT_DEBUG
33 int iLevel = 0;
34
35 bool export_data(int _iH5File, int *_piVar, char* _pstName);
36 bool export_list(int _iH5File, int *_piVar, char* _pstName, int _iVarType);
37 bool export_double(int _iH5File, int *_piVar, char* _pstName);
38 bool export_poly(int _iH5File, int *_piVar, char* _pstName);
39 bool export_boolean(int _iH5File, int *_piVar, char* _pstName);
40 bool export_sparse(int _iH5File, int *_piVar, char* _pstName);
41 bool export_boolean_sparse(int _iH5File, int *_piVar, char* _pstName);
42 bool export_matlab_sparse(int *_piVar, char* _pstName);
43 bool export_ints(int _iH5File, int *_piVar, char* _pstName);
44 bool export_handles(int *_piVar, char* _pstName);
45 bool export_strings(int _iH5File, int *_piVar, char* _pstName);
46 bool export_u_function(int *_piVar, char* _pstName);
47 bool export_c_function(int *_piVar, char* _pstName);
48 bool export_lib(int *_piVar, char* _pstName);
49 bool export_lufact_pointer(int *_piVar, char* _pstName);
50
51 void print_type(char* _pstType);
52 int extractVarNameList(int _iStart, int _iEnd, char** _pstNameList);
53
54 /*--------------------------------------------------------------------------*/
55 int sci_export_to_hdf5(char *fname,unsigned long fname_len)
56 {
57         CheckRhs(2,1000000);//input parameters
58         CheckLhs(1,1);//output parameter
59
60         int iRet                                                = 0;
61         int iNbVar                                      = 0;
62         int iLen                                                = 0;
63
64         int** piAddrList                = NULL;
65         char* pstFilename               = NULL;
66         char** pstNameList      = NULL;
67
68         /*get input data*/
69         pstNameList = (char**)MALLOC(sizeof(char*) * Rhs);
70         iNbVar = extractVarNameList(1, Rhs, pstNameList);
71
72         piAddrList = (int**)MALLOC(sizeof(int*) * (iNbVar - 1));
73         for(int i = 0 ; i < Rhs - 1 ; i++)
74         {
75                 iRet = getVarAddressFromName(pstNameList[i + 1], &piAddrList[i]);
76                 if(iRet)
77                 {
78                         // 2 = 1 for filename + 1 because starting arg number is 1 for human being
79                         Scierror(999,_("%s: Wrong value for input argument #%d: Defined variable expected.\n"), fname, i + 2);
80                         return 0;
81                 }
82         }
83
84         iLevel = 0;
85         //open hdf5 file
86         int iH5File = createHDF5File(pstNameList[0]); 
87
88         // export data
89         bool bExport = false;
90         for(int i = 0 ; i < Rhs - 1; i++)
91         {
92                 bExport = export_data(iH5File, piAddrList[i], pstNameList[i + 1]);
93                 if(bExport == false)
94                 {
95                         break;
96                 }
97         }
98
99         //close hdf5 file
100         closeHDF5File(iH5File);
101
102         //create boolean return value
103         int *piReturn = NULL;
104         allocMatrixOfBoolean(Rhs + 1, 1, 1, &piReturn);
105         if(bExport == true)
106         {
107                 piReturn[0] = 1;
108         }
109         else
110         {
111                 piReturn[0] = 0;
112         }
113
114         LhsVar(1) = Rhs + 1;
115         PutLhsVar();
116         return 0;
117 }
118
119 bool export_data(int _iH5File, int* _piVar, char* _pstName)
120 {
121         bool bReturn = false;
122         int iType = getVarType(_piVar);
123         switch(iType)
124         {
125         case sci_matrix :
126                 {
127                         bReturn = export_double(_iH5File, _piVar, _pstName);
128                         break;
129                 }
130         case sci_poly :
131                 {
132                         bReturn = export_poly(_iH5File, _piVar, _pstName);
133                         break;
134                 }
135         case sci_boolean :
136                 {
137                         bReturn = export_boolean(_iH5File, _piVar, _pstName);
138                         break;
139                 }
140         case sci_sparse :
141                 {
142                         bReturn = export_sparse(_iH5File, _piVar, _pstName);
143                         break;
144                 }
145         case sci_boolean_sparse :
146                 {
147                         bReturn = export_boolean_sparse(_iH5File, _piVar, _pstName);
148                         break;
149                 }
150         case sci_matlab_sparse :
151                 {
152                         bReturn = export_matlab_sparse(_piVar, _pstName);
153                         break;
154                 }
155         case sci_ints :
156                 {
157                         bReturn = export_ints(_iH5File, _piVar, _pstName);
158                         break;
159                 }
160         case sci_handles :
161                 {
162                         bReturn = export_handles(_piVar, _pstName);
163                         break;
164                 }
165         case sci_strings :
166                 {
167                         bReturn = export_strings(_iH5File, _piVar, _pstName);
168                         break;
169                 }
170         case sci_u_function :
171                 {
172                         bReturn = export_u_function(_piVar, _pstName);
173                         break;
174                 }
175         case sci_c_function :
176                 {
177                         bReturn = export_c_function(_piVar, _pstName);
178                         break;
179                 }
180         case sci_lib :
181                 {
182                         bReturn = export_lib(_piVar, _pstName);
183                         break;
184                 }
185         case sci_list :
186         case sci_tlist :
187         case sci_mlist :
188                 {
189                         bReturn = export_list(_iH5File, _piVar, _pstName, iType);
190                         break;
191                 }
192         case sci_lufact_pointer :
193                 {
194                         bReturn = export_lufact_pointer(_piVar, _pstName);
195                         break;
196                 }
197         }
198         return bReturn;
199 }
200
201 bool export_list(int _iH5File, int *_piVar, char* _pstName, int _iVarType)
202 {
203         int iRet = 0;
204         bool bReturn = false;
205         int iItemNumber = 0;
206         getListItemNumber(_piVar, &iItemNumber);
207
208         //create groupe name
209         char* pstGroupName      = createGroupName(_pstName);
210
211         char pstMsg[256];
212         sprintf(pstMsg, "list (%d)", iItemNumber);
213         print_type(pstMsg);
214
215         iLevel++;
216         //open list
217         void *pvList = openList(_iH5File, pstGroupName, iItemNumber);
218         for(int i = 0 ; i < iItemNumber ; i++)
219         {
220                 int *piNewVar = NULL;
221                 getListItemAddress(_piVar, i + 1, &piNewVar);//1 indexed
222                 if(piNewVar == NULL)
223                 {
224                         Scierror(999,"Item not found");
225                         return 0;
226                 }
227
228                 char* pstPathName               = createPathName(pstGroupName, i);
229                 bReturn                                                 = export_data(_iH5File, piNewVar, pstPathName);
230                 iRet = addItemInList(_iH5File, pvList, i, pstPathName);
231
232                 free(pstPathName);
233                 if(bReturn == false || iRet)
234                         return false;
235         }
236         iLevel--;
237         closeList(_iH5File, pvList, _pstName, iItemNumber, _iVarType);
238         free(pstGroupName);
239         //close list
240         return true;
241 }
242
243 bool export_double(int _iH5File, int *_piVar, char* _pstName)
244 {
245         int iType = getVarType(_piVar);
246         if(iType != sci_matrix)
247         {
248                 return false;
249         }
250
251         int iComplex                    = isVarComplex(_piVar);
252         int iRows                                       = 0;
253         int iCols                                       = 0;
254         double *pdblReal        = NULL;
255         double *pdblImg         = NULL;
256
257         if(iComplex)
258         {
259                 getComplexMatrixOfDouble(_piVar, &iRows, &iCols, &pdblReal, &pdblImg);
260                 writeDoubleComplexMatrix(_iH5File, _pstName, iRows, iCols, pdblReal, pdblImg);
261         }
262         else
263         {
264                 getMatrixOfDouble(_piVar, &iRows, &iCols, &pdblReal);
265                 writeDoubleMatrix(_iH5File, _pstName, iRows, iCols, pdblReal);
266         }
267
268         char pstMsg[512];
269         sprintf(pstMsg, "double (%d x %d)", iRows, iCols);
270         print_type(pstMsg);
271         return true;
272 }
273
274 bool export_poly(int _iH5File, int *_piVar, char* _pstName)
275 {
276         int iRows                                               = 0;
277         int iCols                                               = 0;
278         int* piNbCoef                           = NULL;
279         double** pdblReal               = NULL;
280         double** pdblImg                = NULL;
281         char pstVarName[64]     = {0};
282         int iVarNameLen                 = 0;
283
284         getPolyVariableName(_piVar, pstVarName, &iVarNameLen);
285
286         if(isVarComplex(_piVar))
287         {
288                 getComplexMatrixOfPoly(_piVar, &iRows, &iCols, NULL, NULL, NULL);
289                 piNbCoef = (int*)MALLOC(iRows * iCols * sizeof(int));
290                 getComplexMatrixOfPoly(_piVar, &iRows, &iCols, piNbCoef, NULL, NULL);
291                 pdblReal = (double**)MALLOC(sizeof(double*) * iRows * iCols);
292                 pdblImg = (double**)MALLOC(sizeof(double*) * iRows * iCols);
293                 for(int i = 0 ; i < iRows * iCols ; i++)
294                 {
295                         pdblReal[i] = (double*)MALLOC(sizeof(double) * piNbCoef[i]);// for null termination
296                         pdblImg[i]      = (double*)MALLOC(sizeof(double) * piNbCoef[i]);// for null termination
297                 }
298                 getComplexMatrixOfPoly(_piVar, &iRows, &iCols, piNbCoef, pdblReal, pdblImg);
299
300                 
301                 writePolyComplexMatrix(_iH5File, _pstName, pstVarName, iRows, iCols, piNbCoef, pdblReal, pdblImg);
302         }
303         else
304         {
305                 getMatrixOfPoly(_piVar, &iRows, &iCols, NULL, NULL);
306                 piNbCoef = (int*)MALLOC(iRows * iCols * sizeof(int));
307                 getMatrixOfPoly(_piVar, &iRows, &iCols, piNbCoef, NULL);
308                 pdblReal = (double**)MALLOC(sizeof(double*) * iRows * iCols);
309                 for(int i = 0 ; i < iRows * iCols ; i++)
310                 {
311                         pdblReal[i] = (double*)MALLOC(sizeof(double) * piNbCoef[i]);// for null termination
312                 }
313                 getMatrixOfPoly(_piVar, &iRows, &iCols, piNbCoef, pdblReal);
314
315                 writePolyMatrix(_iH5File, _pstName, pstVarName, iRows, iCols, piNbCoef, pdblReal);
316         }
317         char pstMsg[512];
318         sprintf(pstMsg, "poly (%d x %d)", iRows, iCols);
319         print_type(pstMsg);
320
321         if(pdblReal)
322         {
323                 for(int i = 0 ; i < iRows * iCols ; i++)
324                 {
325                         FREE(pdblReal[i]);
326                 }
327                 FREE(pdblReal);
328         }
329
330         if(pdblImg)
331         {
332                 for(int i = 0 ; i < iRows * iCols ; i++)
333                 {
334                         FREE(pdblImg[i]);
335                 }
336                 FREE(pdblImg);
337         }
338
339         FREE(piNbCoef);
340         return true;
341 }
342
343 bool export_boolean(int _iH5File, int *_piVar, char* _pstName)
344 {
345         int iRows                                       = 0;
346         int iCols                                       = 0;
347         int *piData                             = NULL;
348
349         getMatrixOfBoolean(_piVar, &iRows, &iCols, &piData);
350         writeBooleanMatrix(_iH5File, _pstName, iRows, iCols, piData);
351
352         char pstMsg[512];
353         sprintf(pstMsg, "bool (%d x %d)", iRows, iCols);
354         print_type(pstMsg);
355         return true;
356 }
357
358 bool export_boolean_sparse(int _iH5File, int *_piVar, char* _pstName)
359 {
360         int iRet                                                = 0;
361         int iRows                                               = 0;
362         int iCols                                               = 0;
363         int iNbItem                                     = 0;
364         int* piNbCoef                           = NULL;
365         int* piNbItemRow                = NULL;
366         int* piColPos                           = NULL;
367
368         iRet = getBooleanSparseMatrix(_piVar, &iRows, &iCols, &iNbItem, &piNbItemRow, &piColPos);
369         if(iRet)
370         {
371                 return false;
372         }
373
374         iRet = writeBooleanSparseMatrix(_iH5File, _pstName, iRows, iCols, iNbItem, piNbItemRow, piColPos);
375         if(iRet)
376         {
377                 return false;
378         }
379
380         char pstMsg[512];
381         sprintf(pstMsg, "boolean sparse (%d x %d)", iRows, iCols);
382         print_type(pstMsg);
383         return true;
384 }
385
386 bool export_sparse(int _iH5File, int *_piVar, char* _pstName)
387 {
388         int iRet                                                = 0;
389         int iRows                                               = 0;
390         int iCols                                               = 0;
391         int iNbItem                                     = 0;
392         int* piNbCoef                           = NULL;
393         int* piNbItemRow                = NULL;
394         int* piColPos                           = NULL;
395         double* pdblReal                = NULL;
396         double* pdblImg         = NULL;
397
398         if(isVarComplex(_piVar))
399         {
400                 iRet = getComplexSparseMatrix(_piVar, &iRows, &iCols, &iNbItem, &piNbItemRow, &piColPos, &pdblReal, &pdblImg);
401                 if(iRet)
402                 {
403                         return false;
404                 }
405
406                 iRet = writeSparseComplexMatrix(_iH5File, _pstName, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal, pdblImg);
407         }
408         else
409         {
410                 iRet = getSparseMatrix(_piVar, &iRows, &iCols, &iNbItem, &piNbItemRow, &piColPos, &pdblReal);
411                 if(iRet)
412                 {
413                         return false;
414                 }
415
416                 iRet = writeSparseMatrix(_iH5File, _pstName, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal);
417         }
418
419         if(iRet)
420         {
421                 return false;
422         }
423
424         char pstMsg[512];
425         sprintf(pstMsg, "sparse (%d x %d)", iRows, iCols);
426         print_type(pstMsg);
427         return true;
428 }
429
430 bool export_boolean_sparse(int *_piVar, char* _pstName)
431 {
432         print_type(_pstName);
433         return true;
434 }
435
436 bool export_matlab_sparse(int *_piVar, char* _pstName)
437 {
438         print_type(_pstName);
439         return true;
440 }
441
442 bool export_ints(int _iH5File, int *_piVar, char* _pstName)
443 {
444         int iRows                                       = 0;
445         int iCols                                       = 0;
446         void *piData                            = NULL;
447         int iPrec                                       = 0;
448
449         getMatrixOfIntegerPrecision(_piVar, &iPrec);
450
451         switch(iPrec)
452         {
453         case SCI_INT8 :
454                 getMatrixOfInteger8(_piVar, &iRows, &iCols, (char**)&piData);
455                 writeInterger8Matrix(_iH5File, _pstName, iRows, iCols, (char*)piData);
456                 break;
457         case SCI_INT16 : 
458                 getMatrixOfInteger16(_piVar, &iRows, &iCols, (short**)&piData);
459                 writeInterger16Matrix(_iH5File, _pstName, iRows, iCols, (short*)piData);
460                 break;
461         case SCI_INT32 : 
462                 getMatrixOfInteger32(_piVar, &iRows, &iCols, (int**)&piData);
463                 writeInterger32Matrix(_iH5File, _pstName, iRows, iCols, (int*)piData);
464                 break;
465         case SCI_INT64 : 
466                 //getMatrixOfInteger64(_piVar, &iRows, &iCols, (long long**)&piData);
467                 //writeInterger64Matrix(_iH5File, _pstName, iRows, iCols, (long long*)piData);
468                 return 1;
469                 break;
470         default : 
471                 return 1;
472                 break;
473         }
474
475         char pstMsg[512];
476         sprintf(pstMsg, "int%d (%d x %d)", 8*iPrec, iRows, iCols);
477         print_type(pstMsg);
478         return true;
479 }
480
481 bool export_handles(int *_piVar, char* _pstName)
482 {
483         print_type(_pstName);
484         return true;
485 }
486
487 bool export_strings(int _iH5File, int *_piVar, char* _pstName)
488 {
489         int iRows                               = 0;
490         int iCols                               = 0;
491         int* piLen                      = NULL;
492         char** pstData  = NULL;
493
494
495         getMatrixOfString(_piVar, &iRows, &iCols, NULL, NULL);
496         piLen = (int*)MALLOC(iRows * iCols * sizeof(int));
497         getMatrixOfString(_piVar, &iRows, &iCols, piLen, NULL);
498         pstData = (char**)MALLOC(sizeof(char*) * iRows * iCols);
499         for(int i = 0 ; i < iRows * iCols ; i++)
500         {
501                 pstData[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1));// for null termination
502         }
503         getMatrixOfString(_piVar, &iRows, &iCols, piLen, pstData);
504
505         writeStringMatrix(_iH5File, _pstName, iRows, iCols, pstData);
506
507         char pstMsg[512];
508         sprintf(pstMsg, "string (%d x %d)", iRows, iCols);
509         print_type(pstMsg);
510
511         freeArrayOfString(pstData, iRows * iCols);
512         return true;
513 }
514
515 bool export_u_function(int *_piVar, char* _pstName)
516 {
517         print_type(_pstName);
518         return true;
519 }
520
521 bool export_c_function(int *_piVar, char* _pstName)
522 {
523         print_type(_pstName);
524         return true;
525 }
526
527 bool export_lib(int *_piVar, char* _pstName)
528 {
529         print_type(_pstName);
530         return true;
531 }
532
533 bool export_lufact_pointer(int *_piVar, char* _pstName)
534 {
535         print_type(_pstName);
536         return true;
537 }
538
539 void print_type(char* _pstType)
540 {
541 #ifdef PRINT_DEBUG
542         for(int i = 0 ; i < iLevel ; i++)
543         {
544                 sciprint("\t");
545         }
546         sciprint("%s\n", _pstType);
547 #endif
548 }
549
550 int extractVarNameList(int _iStart, int _iEnd, char** _pstNameList)
551 {
552         int iCount = 0;
553
554         for(int i = _iStart ; i <= _iEnd ; i++)
555         {
556                 int iRows                                       = 0;
557                 int iCols                                       = 0;
558                 int iLen                                        = 0;
559                 int* piAddr                             = NULL;
560
561                 getVarAddressFromPosition(i, &piAddr);
562                 //get filename
563                 if(getVarType(piAddr) != sci_strings)
564                 {
565                         Scierror(999,_("%s: Wrong type for input argument #%d: A string.\n"), "export_to_hdf5", i);
566                         return 0;
567                 }
568
569                 getVarDimension(piAddr, &iRows, &iCols);
570                 if(iRows != 1 || iCols != 1)
571                 {
572                         Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), "export_to_hdf5", i);
573                         return 0;
574                 }
575                 
576                 getMatrixOfString(piAddr, &iRows, &iCols, &iLen, NULL);
577                 _pstNameList[iCount] = (char*)MALLOC((iRows * iCols + 1) * sizeof(char));//1 for null termination
578                 getMatrixOfString(piAddr, &iRows, &iCols, &iLen, &_pstNameList[iCount]);
579                 iCount++;
580         }
581         return iCount;
582 }
583
584 /*--------------------------------------------------------------------------*/