1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2012 - Scilab Enterprises - Antoine ELIAS
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 
16 #define H5_USE_18_API
17 
18 #ifndef _MSC_VER
19 #include <sys/time.h>
20 #else
21 #include <windows.h>
22 //#include <winbase.h>
23 #endif
24 
25 #include <string.h>
26 #include <hdf5.h>
27 #include <stdlib.h>
28 #include "sci_malloc.h"
29 #include "sci_types.h"
30 #include "h5_attributeConstants.h"
31 #include "h5_readDataFromFile_v1.h"
32 
33 //#define TIME_DEBUG
34 
find_attr_by_name_v1(hid_t loc_id,const char * name,const H5A_info_t * ainfo,void * data)35 static herr_t find_attr_by_name_v1(hid_t loc_id, const char *name, const H5A_info_t *ainfo, void *data)
36 {
37     return !strcmp(name, (const char *)data);
38 }
39 
40 /************************************************************
41 
42 Operator function.  Prints the name and type of the object
43 being examined.
44 
45 ************************************************************/
op_func_v1(hid_t loc_id,const char * name,const H5L_info_t * info,void * operator_data)46 static herr_t op_func_v1(hid_t loc_id, const char *name, const H5L_info_t *info, void *operator_data)
47 {
48     H5O_info_t oinfo;
49     herr_t status = 0;
50     int *pDataSetId = (int*)operator_data;
51     hid_t obj = H5Oopen(loc_id, name, H5P_DEFAULT);
52     if (obj < 0)
53     {
54         return -1;
55     }
56 
57     /*
58      * Get type of the object and return only datasetId
59      * through operator_data.
60      */
61     status = H5Oget_info(obj, &oinfo);
62     if (status < 0)
63     {
64         H5Oclose(obj);
65         return -1;
66     }
67 
68     if (oinfo.type == H5O_TYPE_DATASET)
69     {
70         *pDataSetId = obj;
71     }
72     else
73     {
74         H5Oclose(obj);
75     }
76 
77     return 0;
78 }
79 
readIntAttribute_v1(hid_t _iDatasetId,const char * _pstName)80 static int readIntAttribute_v1(hid_t _iDatasetId, const char *_pstName)
81 {
82     hid_t iAttributeId;
83     herr_t status;
84     hsize_t n = 0;
85     int iVal = -1;
86 
87     if (H5Aiterate(_iDatasetId, H5_INDEX_NAME, H5_ITER_NATIVE, &n, find_attr_by_name_v1, (void *)_pstName) > 0)
88     {
89         iAttributeId = H5Aopen_by_name(_iDatasetId, ".", _pstName, H5P_DEFAULT, H5P_DEFAULT);
90         if (iAttributeId < 0)
91         {
92             return -1;
93         }
94 
95         status = H5Aread(iAttributeId, H5T_NATIVE_INT, &iVal);
96         if (status < 0)
97         {
98             return -1;
99         }
100 
101         status = H5Aclose(iAttributeId);
102         if (status < 0)
103         {
104             return -1;
105         }
106     }
107     return iVal;
108 }
109 
110 /*
111 ** WARNING : this function returns an allocated value that must be freed.
112 */
readAttribute_v1(hid_t _iDatasetId,const char * _pstName)113 static char* readAttribute_v1(hid_t _iDatasetId, const char *_pstName)
114 {
115     hid_t iAttributeId;
116     hid_t iFileType, memtype, iSpace;
117     herr_t status;
118     hsize_t dims[1];
119     hsize_t n = 0;
120     size_t iDim;
121 
122     char *pstValue = NULL;
123 
124     if (H5Aiterate(_iDatasetId, H5_INDEX_NAME, H5_ITER_NATIVE, &n, find_attr_by_name_v1, (void *)_pstName) > 0)
125     {
126         iAttributeId = H5Aopen_by_name(_iDatasetId, ".", _pstName, H5P_DEFAULT, H5P_DEFAULT);
127         if (iAttributeId < 0)
128         {
129             return NULL;
130         }
131         /*
132         * Get the datatype and its size.
133         */
134         iFileType = H5Aget_type(iAttributeId);
135         iDim = H5Tget_size(iFileType);
136         iDim++;                 /* Make room for null terminator */
137 
138         /*
139         * Get dataspace and allocate memory for read buffer.  This is a
140         * two dimensional attribute so the dynamic allocation must be done
141         * in steps.
142         */
143         iSpace = H5Aget_space(iAttributeId);
144         if (iSpace < 0)
145         {
146             return NULL;
147         }
148 
149         status = H5Sget_simple_extent_dims(iSpace, dims, NULL);
150         if (status < 0)
151         {
152             return NULL;
153         }
154 
155         /*
156         * Allocate space for string data.
157         */
158         pstValue = (char *)MALLOC((size_t) ((dims[0] * iDim + 1) * sizeof(char)));
159 
160         /*
161         * Create the memory datatype.
162         */
163         memtype = H5Tcopy(H5T_C_S1);
164         status = H5Tset_size(memtype, iDim);
165         if (status < 0)
166         {
167             FREE(pstValue);
168             return NULL;
169         }
170 
171         /*
172         * Read the data.
173         */
174         status = H5Aread(iAttributeId, memtype, pstValue);
175         if (status < 0)
176         {
177             FREE(pstValue);
178             return NULL;
179         }
180 
181         status = H5Tclose(memtype);
182         if (status < 0)
183         {
184             FREE(pstValue);
185             return NULL;
186         }
187 
188         status = H5Sclose(iSpace);
189         if (status < 0)
190         {
191             FREE(pstValue);
192             return NULL;
193         }
194 
195         status = H5Tclose(iFileType);
196         if (status < 0)
197         {
198             FREE(pstValue);
199             return NULL;
200         }
201 
202         status = H5Aclose(iAttributeId);
203         if (status < 0)
204         {
205             FREE(pstValue);
206             return NULL;
207         }
208     }
209     return pstValue;
210 
211 }
212 
checkAttribute_v1(hid_t _iDatasetId,char * _pstAttribute,char * _pstValue)213 static int checkAttribute_v1(hid_t _iDatasetId, char *_pstAttribute, char *_pstValue)
214 {
215     int iRet = 0;
216     char *pstScilabClass = NULL;
217 
218     //status = H5Giterate (_iFile, "/", NULL, op_func, &iDatasetId);
219     pstScilabClass = readAttribute_v1(_iDatasetId, _pstAttribute);
220     if (pstScilabClass != NULL && strcmp(pstScilabClass, _pstValue) == 0)
221     {
222         iRet = 1;
223     }
224     if (pstScilabClass)
225     {
226         FREE(pstScilabClass);
227     }
228     return iRet;
229 }
230 
231 /*
232 ** WARNING : this function returns an allocated value that must be freed.
233 */
getScilabVersionAttribute_v1(hid_t _iFile)234 char* getScilabVersionAttribute_v1(hid_t _iFile)
235 {
236     return readAttribute_v1(_iFile, g_SCILAB_CLASS_SCI_VERSION);
237 }
238 
getSODFormatAttribute_v1(hid_t _iFile)239 int getSODFormatAttribute_v1(hid_t _iFile)
240 {
241     return readIntAttribute_v1(_iFile, g_SCILAB_CLASS_SOD_VERSION);
242 }
243 
getDatasetDimension_v1(hid_t _iDatasetId,int * _piRows,int * _piCols)244 int getDatasetDimension_v1(hid_t _iDatasetId, int *_piRows, int *_piCols)
245 {
246     int iRet = 0;
247     int iDummy = 0;
248 
249     *_piRows = readIntAttribute_v1(_iDatasetId, g_SCILAB_CLASS_ROWS);
250     *_piCols = readIntAttribute_v1(_iDatasetId, g_SCILAB_CLASS_COLS);
251 
252     return iRet;
253 }
254 
getSparseDimension_v1(hid_t _iDatasetId,int * _piRows,int * _piCols,int * _piNbItem)255 int getSparseDimension_v1(hid_t _iDatasetId, int *_piRows, int *_piCols, int *_piNbItem)
256 {
257     int iRet = 0;
258     int iDummy = 0;
259 
260     //get number of item in the sparse matrix
261     getDatasetDims_v1(_iDatasetId, _piRows, _piCols);
262     *_piNbItem = readIntAttribute_v1(_iDatasetId, g_SCILAB_CLASS_ITEMS);
263 
264     return iRet;
265 }
266 
isEmptyDataset_v1(hid_t _iDatasetId)267 static int isEmptyDataset_v1(hid_t _iDatasetId)
268 {
269     return checkAttribute_v1(_iDatasetId, (char *)g_SCILAB_CLASS_EMPTY, "true");
270 }
271 
isComplexData_v1(hid_t _iDatasetId)272 int isComplexData_v1(hid_t _iDatasetId)
273 {
274     return checkAttribute_v1(_iDatasetId, (char *)g_SCILAB_CLASS_COMPLEX, "true");
275 }
276 
getDatasetPrecision_v1(hid_t _iDatasetId,int * _piPrec)277 int getDatasetPrecision_v1(hid_t _iDatasetId, int *_piPrec)
278 {
279     int iRet = 0;
280     char *pstScilabClass = readAttribute_v1(_iDatasetId, g_SCILAB_CLASS_PREC);
281 
282     if (pstScilabClass == NULL)
283     {
284         return -1;
285     }
286     else if (strcmp(pstScilabClass, "8") == 0)
287     {
288         *_piPrec = SCI_INT8;
289     }
290     else if (strcmp(pstScilabClass, "u8") == 0)
291     {
292         *_piPrec = SCI_UINT8;
293     }
294     else if (strcmp(pstScilabClass, "16") == 0)
295     {
296         *_piPrec = SCI_INT16;
297     }
298     else if (strcmp(pstScilabClass, "u16") == 0)
299     {
300         *_piPrec = SCI_UINT16;
301     }
302     else if (strcmp(pstScilabClass, "32") == 0)
303     {
304         *_piPrec = SCI_INT32;
305     }
306     else if (strcmp(pstScilabClass, "u32") == 0)
307     {
308         *_piPrec = SCI_UINT32;
309     }
310     else if (strcmp(pstScilabClass, "64") == 0)
311     {
312         *_piPrec = SCI_INT64;
313     }
314     else if (strcmp(pstScilabClass, "u64") == 0)
315     {
316         *_piPrec = SCI_UINT64;
317     }
318     else
319     {
320         iRet = 1;
321     }
322 
323     FREE(pstScilabClass);
324     return iRet;
325 }
326 
getVariableNames_v1(hid_t _iFile,char ** pstNameList)327 int getVariableNames_v1(hid_t _iFile, char **pstNameList)
328 {
329     hsize_t i = 0;
330     hsize_t iCount = 0;
331     herr_t status = 0;
332     int iNbItem = 0;
333     H5O_info_t oinfo;
334     H5G_info_t ginfo;
335 
336     status = H5Gget_info(_iFile, &ginfo);
337     if (status != 0)
338     {
339         return 0;
340     }
341 
342     iCount = ginfo.nlinks;
343     for (i = 0; i < iCount; i++)
344     {
345         status = H5Oget_info_by_idx(_iFile, "/", H5_INDEX_NAME, H5_ITER_NATIVE, i, &oinfo, H5P_DEFAULT);
346         if (status < 0)
347         {
348             return 0;
349         }
350 
351         if (oinfo.type == H5O_TYPE_DATASET)
352         {
353             if (pstNameList != NULL)
354             {
355                 ssize_t iLen = H5Lget_name_by_idx(_iFile, ".", H5_INDEX_NAME, H5_ITER_INC, i, 0, 0, H5P_DEFAULT) + 1;
356                 pstNameList[iNbItem] = (char*)MALLOC(sizeof(char) * iLen);
357                 H5Lget_name_by_idx(_iFile, ".", H5_INDEX_NAME, H5_ITER_INC, i, pstNameList[iNbItem], iLen, H5P_DEFAULT);
358             }
359             iNbItem++;
360         }
361     }
362     return iNbItem;
363 }
364 
getDataSetIdFromName_v1(hid_t _iFile,char * _pstName)365 hid_t getDataSetIdFromName_v1(hid_t _iFile, char *_pstName)
366 {
367     return H5Dopen(_iFile, _pstName, H5P_DEFAULT);
368 }
369 
closeDataSet_v1(hid_t _id)370 void closeDataSet_v1(hid_t _id)
371 {
372     if (_id > 0)
373     {
374         herr_t status = H5Dclose(_id);
375         if (status < 0)
376         {
377             return;
378         }
379     }
380 
381     return;
382 }
383 
getDataSetId_v1(hid_t _iFile)384 hid_t getDataSetId_v1(hid_t _iFile)
385 {
386     herr_t status = 0;
387     int iDatasetId = 0;
388     hsize_t idx = 0;
389 
390     /*
391     * Begin iteration.
392     */
393     status = H5Literate(_iFile, H5_INDEX_NAME, H5_ITER_NATIVE, &idx, op_func_v1, &iDatasetId);
394     if (status < 0)
395     {
396         return -1;
397     }
398 
399     return iDatasetId;
400 }
401 
getListDims_v1(hid_t _iDatasetId,int * _piItems)402 int getListDims_v1(hid_t _iDatasetId, int *_piItems)
403 {
404     /*
405     * Get dataspace and dimensions of the dataset. This is a
406     * two dimensional dataset.
407     */
408     if (isEmptyDataset_v1(_iDatasetId))
409     {
410         *_piItems = 0;
411     }
412     else
413     {
414         *_piItems = readIntAttribute_v1(_iDatasetId, g_SCILAB_CLASS_ITEMS);
415     }
416     return 0;
417 }
418 
getDatasetDims_v1(hid_t _iDatasetId,int * _piRows,int * _piCols)419 int getDatasetDims_v1(hid_t _iDatasetId, int *_piRows, int *_piCols)
420 {
421     /*
422     * Get dataspace and dimensions of the dataset. This is a
423     * two dimensional dataset.
424     */
425     if (isEmptyDataset_v1(_iDatasetId))
426     {
427         *_piCols = 0;
428         *_piRows = 0;
429     }
430     else
431     {
432         *_piRows = readIntAttribute_v1(_iDatasetId, g_SCILAB_CLASS_ROWS);
433         *_piCols = readIntAttribute_v1(_iDatasetId, g_SCILAB_CLASS_COLS);
434     }
435     return 0;
436 }
437 
readDouble_v1(hid_t _iDatasetId,int _iRows,int _iCols,double * _pdblData)438 int readDouble_v1(hid_t _iDatasetId, int _iRows, int _iCols, double *_pdblData)
439 {
440     herr_t status;
441 
442     /*
443     * Read the data.
444     */
445     status = H5Dread(_iDatasetId, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, _pdblData);
446     if (status < 0)
447     {
448         return -1;
449     }
450 
451     status = H5Dclose(_iDatasetId);
452     if (status < 0)
453     {
454         return -1;
455     }
456 
457     return 0;
458 }
459 
readDoubleMatrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,double * _pdblData)460 int readDoubleMatrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, double *_pdblData)
461 {
462     herr_t status;
463 
464     if (_iRows != 0 && _iCols != 0)
465     {
466         hid_t obj;
467         hobj_ref_t Ref;
468 
469         //Read the data.
470         status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &Ref);
471         if (status < 0)
472         {
473             return -1;
474         }
475 
476         //Open the referenced object, get its name and type.
477         obj = H5Rdereference(_iDatasetId,
478 #if H5_VERSION_GE(1,10,0)
479                              H5P_DATASET_ACCESS_DEFAULT,
480 #endif
481                              H5R_OBJECT, &Ref);
482         readDouble_v1(obj, _iRows, _iCols, _pdblData);
483     }
484 
485     status = H5Dclose(_iDatasetId);
486     if (status < 0)
487     {
488         return -1;
489     }
490 
491     return 0;
492 }
493 
readDoubleComplexMatrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,double * _pdblReal,double * _pdblImg)494 int readDoubleComplexMatrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, double *_pdblReal, double *_pdblImg)
495 {
496     hid_t obj;
497     herr_t status;
498     hobj_ref_t pRef[2] = {0};
499 
500     //Read the data.
501     status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pRef);
502     if (status < 0)
503     {
504         return -1;
505     }
506 
507     //Open the referenced object, get its name and type.
508     obj = H5Rdereference(_iDatasetId,
509 #if H5_VERSION_GE(1,10,0)
510                          H5P_DATASET_ACCESS_DEFAULT,
511 #endif
512                          H5R_OBJECT, &pRef[0]);
513     status = readDouble_v1(obj, _iRows, _iCols, _pdblReal);
514     if (status < 0)
515     {
516         return -1;
517     }
518 
519     obj = H5Rdereference(_iDatasetId,
520 #if H5_VERSION_GE(1,10,0)
521                          H5P_DATASET_ACCESS_DEFAULT,
522 #endif
523                          H5R_OBJECT, &pRef[1]);
524     status = readDouble_v1(obj, _iRows, _iCols, _pdblImg);
525     if (status < 0)
526     {
527         return -1;
528     }
529 
530     status = H5Dclose(_iDatasetId);
531     if (status < 0)
532     {
533         return -1;
534     }
535 
536     return 0;
537 }
538 
readEmptyMatrix_v1(hid_t _iDatasetId)539 int readEmptyMatrix_v1(hid_t _iDatasetId)
540 {
541     //close dataset
542     herr_t status;
543 
544     status = H5Dclose(_iDatasetId);
545     if (status < 0)
546     {
547         return -1;
548     }
549 
550     return 0;
551 }
552 
readBooleanMatrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,int * _piData)553 int readBooleanMatrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, int *_piData)
554 {
555     herr_t status = 0;
556 
557     /*
558     * Read the data.
559     */
560     status = H5Dread(_iDatasetId, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, _piData);
561     if (status < 0)
562     {
563         return -1;
564     }
565 
566     status = H5Dclose(_iDatasetId);
567     if (status < 0)
568     {
569         return -1;
570     }
571 
572     return 0;
573 }
574 
readString_v1(hid_t _iDatasetId,char ** _pstData)575 static int readString_v1(hid_t _iDatasetId, char **_pstData)
576 {
577     hid_t iFileType, memtype, iSpace;
578     herr_t status;
579     hsize_t dims[1];
580     size_t iDim;
581 
582     /*
583     * Get the datatype and its size.
584     */
585     iFileType = H5Dget_type(_iDatasetId);
586     iDim = H5Tget_size(iFileType);
587     iDim++;                     /* Make room for null terminator */
588 
589     /*
590     * Get dataspace and allocate memory for read buffer.  This is a
591     * two dimensional attribute so the dynamic allocation must be done
592     * in steps.
593     */
594     iSpace = H5Dget_space(_iDatasetId);
595     if (iSpace < 0)
596     {
597         return -1;
598     }
599 
600     status = H5Sget_simple_extent_dims(iSpace, dims, NULL);
601     if (status < 0)
602     {
603         return -1;
604     }
605 
606     /*
607     * Allocate space for string data.
608     */
609     *_pstData = (char *)MALLOC((size_t) ((dims[0] * iDim + 1) * sizeof(char)));
610 
611     /*
612     * Create the memory datatype.
613     */
614     memtype = H5Tcopy(H5T_C_S1);
615     status = H5Tset_size(memtype, iDim);
616     if (status < 0)
617     {
618         return -1;
619     }
620 
621     /*
622     * Read the data.
623     */
624     status = H5Dread(_iDatasetId, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, *_pstData);
625     if (status < 0)
626     {
627         return -1;
628     }
629 
630     status = H5Tclose(memtype);
631     if (status < 0)
632     {
633         return -1;
634     }
635 
636     status = H5Sclose(iSpace);
637     if (status < 0)
638     {
639         return -1;
640     }
641 
642     status = H5Tclose(iFileType);
643     if (status < 0)
644     {
645         return -1;
646     }
647 
648     status = H5Dclose(_iDatasetId);
649     if (status < 0)
650     {
651         return -1;
652     }
653 
654     return 0;
655 }
656 
readStringMatrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,char ** _pstData)657 int readStringMatrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, char **_pstData)
658 {
659     int i;
660     herr_t status;
661     hsize_t dims[1];
662     hsize_t subdims[1] = { 1 };
663     hid_t space, memspace, filetype, memtype;
664     size_t iDim;
665     size_t iAllocSize = 0;
666 
667 #ifdef TIME_DEBUG
668     LARGE_INTEGER *piStart;
669     LARGE_INTEGER *piEnd;
670     LARGE_INTEGER iFreq;
671 
672     QueryPerformanceFrequency(&iFreq);
673 
674     piStart = (LARGE_INTEGER *) MALLOC(sizeof(LARGE_INTEGER) * (_iRows * _iCols + 1));
675     piEnd = (LARGE_INTEGER *) MALLOC(sizeof(LARGE_INTEGER) * (_iRows * _iCols + 1));
676 
677     QueryPerformanceCounter(&piStart[0]);
678 #endif
679 
680     /*
681     * Get the datatype and its size.
682     */
683     filetype = H5Dget_type(_iDatasetId);
684     iDim = H5Tget_size(filetype);
685     iDim++;                     /* Make room for null terminator */
686 
687     /*create sub space */
688     memspace = H5Screate_simple(1, subdims, NULL);
689     if (memspace < 0)
690     {
691         return -1;
692     }
693 
694     status = H5Sget_simple_extent_dims(memspace, dims, NULL);
695     if (status < 0)
696     {
697         return -1;
698     }
699 
700     space = H5Dget_space(_iDatasetId);
701     if (space < 0)
702     {
703         return -1;
704     }
705 
706     /*
707     * Create the memory datatype.
708     */
709     memtype = H5Tcopy(H5T_C_S1);
710     status = H5Tset_size(memtype, iDim);
711     if (status < 0)
712     {
713         return -1;
714     }
715 
716     /*
717     * Allocate space for string data.
718     */
719     iAllocSize = (size_t) ((iDim + 1) * sizeof(char));
720     for (i = 0; i < _iRows * _iCols; i++)
721     {
722         _pstData[i] = (char *)MALLOC(iAllocSize);
723     }
724 
725     /*
726     * Read the data.
727     */
728     for (i = 0; i < _iRows * _iCols; i++)
729     {
730         hsize_t start[1] = { i };
731         hsize_t count[1] = { 1 };
732 #ifdef TIME_DEBUG
733         QueryPerformanceCounter(&piStart[i + 1]);
734 #endif
735         status = H5Sselect_hyperslab(space, H5S_SELECT_SET, start, NULL, count, NULL);
736         if (status < 0)
737         {
738             return -1;
739         }
740 
741         /*
742         * Read the data.
743         */
744         status = H5Dread(_iDatasetId, memtype, memspace, space, H5P_DEFAULT, _pstData[i]);
745         if (status < 0)
746         {
747             return -1;
748         }
749 #ifdef TIME_DEBUG
750         QueryPerformanceCounter(&piEnd[i + 1]);
751 #endif
752     }
753 
754     status = H5Sclose(space);
755     if (status < 0)
756     {
757         return -1;
758     }
759 
760     status = H5Sclose(memspace);
761     if (status < 0)
762     {
763         return -1;
764     }
765 
766     status = H5Tclose(filetype);
767     if (status < 0)
768     {
769         return -1;
770     }
771 
772     status = H5Dclose(_iDatasetId);
773     if (status < 0)
774     {
775         return -1;
776     }
777 
778 #ifdef TIME_DEBUG
779     QueryPerformanceCounter(&piEnd[0]);
780 
781     //print debuf timer
782     printf("\nGlobalTime : %0.3f ms\n", ((piEnd[0].QuadPart - piStart[0].QuadPart) * 1000.0) / iFreq.QuadPart);
783     for (i = 0; i < _iRows * _iCols; i++)
784     {
785         double dblTime = ((piEnd[i + 1].QuadPart - piStart[i + 1].QuadPart) * 1000.0) / iFreq.QuadPart;
786 
787         printf("SubTime %d : %0.3f ms\n", i, dblTime);
788     }
789 #endif
790     return 0;
791 }
792 
readComplexPoly_v1(hid_t _iDatasetId,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)793 static int readComplexPoly_v1(hid_t _iDatasetId, int *_piNbCoef, double **_pdblReal, double **_pdblImg)
794 {
795     int iRows = 0;
796     int iCols = 0;
797 
798     //Get the datatype and its size.
799     getDatasetDims_v1(_iDatasetId, &iRows, &iCols);
800 
801     //Allocate space for string data.
802     *_piNbCoef = iRows * iCols;
803     *_pdblReal = (double *)MALLOC(*_piNbCoef * sizeof(double));
804     *_pdblImg = (double *)MALLOC(*_piNbCoef * sizeof(double));
805 
806     //Read the data and return result.
807     return readDoubleComplexMatrix_v1(_iDatasetId, 1, *_piNbCoef, *_pdblReal, *_pdblImg);
808 }
809 
readPoly_v1(hid_t _iDatasetId,int * _piNbCoef,double ** _pdblData)810 static int readPoly_v1(hid_t _iDatasetId, int *_piNbCoef, double **_pdblData)
811 {
812     int iRows = 0;
813     int iCols = 0;
814 
815     //Get the datatype and its size.
816     getDatasetDims_v1(_iDatasetId, &iRows, &iCols);
817 
818     *_piNbCoef = iRows * iCols;
819     *_pdblData = (double *)MALLOC(*_piNbCoef * sizeof(double));
820 
821     //Read the data and return result.
822     return readDoubleMatrix_v1(_iDatasetId, 1, *_piNbCoef, *_pdblData);
823 }
824 
readCommonPolyMatrix_v1(hid_t _iDatasetId,char * _pstVarname,int _iComplex,int _iRows,int _iCols,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)825 int readCommonPolyMatrix_v1(hid_t _iDatasetId, char *_pstVarname, int _iComplex, int _iRows, int _iCols, int *_piNbCoef, double **_pdblReal,
826                             double **_pdblImg)
827 {
828     int i = 0;
829     hid_t obj = 0;
830     char *pstVarName = 0;
831     hobj_ref_t *pData = (hobj_ref_t *) MALLOC(_iRows * _iCols * sizeof(hobj_ref_t));
832     herr_t status;
833 
834     /*
835     * Read the data.
836     */
837     status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pData);
838     if (status < 0)
839     {
840         FREE(pData);
841         return -1;
842     }
843 
844     for (i = 0; i < _iRows * _iCols; i++)
845     {
846         /*
847         * Open the referenced object, get its name and type.
848         */
849         obj = H5Rdereference(_iDatasetId,
850 #if H5_VERSION_GE(1,10,0)
851                              H5P_DATASET_ACCESS_DEFAULT,
852 #endif
853                              H5R_OBJECT, &pData[i]);
854         if (_iComplex)
855         {
856             status = readComplexPoly_v1(obj, &_piNbCoef[i], &_pdblReal[i], &_pdblImg[i]);
857         }
858         else
859         {
860             status = readPoly_v1(obj, &_piNbCoef[i], &_pdblReal[i]);
861         }
862 
863         if (status < 0)
864         {
865             FREE(pData);
866             return -1;
867         }
868     }
869 
870     pstVarName = readAttribute_v1(_iDatasetId, g_SCILAB_CLASS_VARNAME);
871     strcpy(_pstVarname, pstVarName);
872     FREE(pstVarName);
873     status = H5Dclose(_iDatasetId);
874     if (status < 0)
875     {
876         FREE(pData);
877         return -1;
878     }
879 
880     FREE(pData);
881 
882     return 0;
883 }
884 
readPolyMatrix_v1(hid_t _iDatasetId,char * _pstVarname,int _iRows,int _iCols,int * _piNbCoef,double ** _pdblData)885 int readPolyMatrix_v1(hid_t _iDatasetId, char *_pstVarname, int _iRows, int _iCols, int *_piNbCoef, double **_pdblData)
886 {
887     return readCommonPolyMatrix_v1(_iDatasetId, _pstVarname, 0, _iRows, _iCols, _piNbCoef, _pdblData, NULL);
888 }
889 
readPolyComplexMatrix_v1(hid_t _iDatasetId,char * _pstVarname,int _iRows,int _iCols,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)890 int readPolyComplexMatrix_v1(hid_t _iDatasetId, char *_pstVarname, int _iRows, int _iCols, int *_piNbCoef, double **_pdblReal, double **_pdblImg)
891 {
892     return readCommonPolyMatrix_v1(_iDatasetId, _pstVarname, 1, _iRows, _iCols, _piNbCoef, _pdblReal, _pdblImg);
893 }
894 
readInteger8Matrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,char * _pcData)895 int readInteger8Matrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, char *_pcData)
896 {
897     herr_t status = 0;
898 
899     /*
900     * Read the data.
901     */
902     status = H5Dread(_iDatasetId, H5T_NATIVE_INT8, H5S_ALL, H5S_ALL, H5P_DEFAULT, _pcData);
903     if (status < 0)
904     {
905         return -1;
906     }
907 
908     status = H5Dclose(_iDatasetId);
909     if (status < 0)
910     {
911         return -1;
912     }
913 
914     return 0;
915 }
916 
readInteger16Matrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,short * _psData)917 int readInteger16Matrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, short *_psData)
918 {
919     herr_t status = 0;
920 
921     /*
922     * Read the data.
923     */
924     status = H5Dread(_iDatasetId, H5T_NATIVE_INT16, H5S_ALL, H5S_ALL, H5P_DEFAULT, _psData);
925     if (status < 0)
926     {
927         return -1;
928     }
929 
930     status = H5Dclose(_iDatasetId);
931     if (status < 0)
932     {
933         return -1;
934     }
935 
936     return 0;
937 }
938 
readInteger32Matrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,int * _piData)939 int readInteger32Matrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, int *_piData)
940 {
941     herr_t status = 0;
942 
943     /*
944     * Read the data.
945     */
946     status = H5Dread(_iDatasetId, H5T_NATIVE_INT32, H5S_ALL, H5S_ALL, H5P_DEFAULT, _piData);
947     if (status < 0)
948     {
949         return -1;
950     }
951 
952     status = H5Dclose(_iDatasetId);
953     if (status < 0)
954     {
955         return -1;
956     }
957 
958     return 0;
959 }
960 
readInteger64Matrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,long long * _pllData)961 int readInteger64Matrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, long long *_pllData)
962 {
963     herr_t status = 0;
964 
965     /*
966     * Read the data.
967     */
968     status = H5Dread(_iDatasetId, H5T_NATIVE_INT64, H5S_ALL, H5S_ALL, H5P_DEFAULT, _pllData);
969     if (status < 0)
970     {
971         return -1;
972     }
973 
974     status = H5Dclose(_iDatasetId);
975     if (status < 0)
976     {
977         return -1;
978     }
979 
980     return 0;
981 }
982 
readUnsignedInteger8Matrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,unsigned char * _pucData)983 int readUnsignedInteger8Matrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, unsigned char *_pucData)
984 {
985     herr_t status = 0;
986 
987     /*
988     * Read the data.
989     */
990     status = H5Dread(_iDatasetId, H5T_NATIVE_UINT8, H5S_ALL, H5S_ALL, H5P_DEFAULT, _pucData);
991     if (status < 0)
992     {
993         return -1;
994     }
995 
996     status = H5Dclose(_iDatasetId);
997     if (status < 0)
998     {
999         return -1;
1000     }
1001 
1002     return 0;
1003 }
1004 
readUnsignedInteger16Matrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,unsigned short * _pusData)1005 int readUnsignedInteger16Matrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, unsigned short *_pusData)
1006 {
1007     herr_t status = 0;
1008 
1009     /*
1010     * Read the data.
1011     */
1012     status = H5Dread(_iDatasetId, H5T_NATIVE_UINT16, H5S_ALL, H5S_ALL, H5P_DEFAULT, _pusData);
1013     if (status < 0)
1014     {
1015         return -1;
1016     }
1017 
1018     status = H5Dclose(_iDatasetId);
1019     if (status < 0)
1020     {
1021         return -1;
1022     }
1023 
1024     return 0;
1025 }
1026 
readUnsignedInteger32Matrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,unsigned int * _puiData)1027 int readUnsignedInteger32Matrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, unsigned int *_puiData)
1028 {
1029     herr_t status = 0;
1030 
1031     /*
1032     * Read the data.
1033     */
1034     status = H5Dread(_iDatasetId, H5T_NATIVE_UINT32, H5S_ALL, H5S_ALL, H5P_DEFAULT, _puiData);
1035     if (status < 0)
1036     {
1037         return -1;
1038     }
1039 
1040     status = H5Dclose(_iDatasetId);
1041     if (status < 0)
1042     {
1043         return -1;
1044     }
1045 
1046     return 0;
1047 }
1048 
readUnsignedInteger64Matrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,unsigned long long * _pullData)1049 int readUnsignedInteger64Matrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, unsigned long long *_pullData)
1050 {
1051     herr_t status = 0;
1052 
1053     /*
1054     * Read the data.
1055     */
1056     status = H5Dread(_iDatasetId, H5T_NATIVE_UINT64, H5S_ALL, H5S_ALL, H5P_DEFAULT, _pullData);
1057     if (status < 0)
1058     {
1059         return -1;
1060     }
1061 
1062     status = H5Dclose(_iDatasetId);
1063     if (status < 0)
1064     {
1065         return -1;
1066     }
1067 
1068     return 0;
1069 }
1070 
readCommonSparseComplexMatrix_v1(hid_t _iDatasetId,int _iComplex,int _iRows,int _iCols,int _iNbItem,int * _piNbItemRow,int * _piColPos,double * _pdblReal,double * _pdblImg)1071 int readCommonSparseComplexMatrix_v1(hid_t _iDatasetId, int _iComplex, int _iRows, int _iCols, int _iNbItem, int *_piNbItemRow, int *_piColPos,
1072                                      double *_pdblReal, double *_pdblImg)
1073 {
1074     hid_t obj = 0;
1075     hobj_ref_t pRef[3] = {0};
1076     herr_t status;
1077 
1078     /*
1079     * Read the data.
1080     */
1081     status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pRef);
1082     if (status < 0)
1083     {
1084         return -1;
1085     }
1086 
1087     //read Row data
1088     obj = H5Rdereference(_iDatasetId,
1089 #if H5_VERSION_GE(1,10,0)
1090                          H5P_DATASET_ACCESS_DEFAULT,
1091 #endif
1092                          H5R_OBJECT, &pRef[0]);
1093     status = readInteger32Matrix_v1(obj, 1, _iRows, _piNbItemRow);
1094     if (status < 0)
1095     {
1096         return -1;
1097     }
1098 
1099     //read cols data
1100     obj = H5Rdereference(_iDatasetId,
1101 #if H5_VERSION_GE(1,10,0)
1102                          H5P_DATASET_ACCESS_DEFAULT,
1103 #endif
1104                          H5R_OBJECT, &pRef[1]);
1105     status = readInteger32Matrix_v1(obj, 1, _iNbItem, _piColPos);
1106     if (status < 0)
1107     {
1108         return -1;
1109     }
1110 
1111     //read sparse data
1112     obj = H5Rdereference(_iDatasetId,
1113 #if H5_VERSION_GE(1,10,0)
1114                          H5P_DATASET_ACCESS_DEFAULT,
1115 #endif
1116                          H5R_OBJECT, &pRef[2]);
1117 
1118     if (_iComplex)
1119     {
1120         status = readDoubleComplexMatrix_v1(obj, 1, _iNbItem, _pdblReal, _pdblImg);
1121     }
1122     else
1123     {
1124         status = readDoubleMatrix_v1(obj, 1, _iNbItem, _pdblReal);
1125     }
1126 
1127     if (status < 0)
1128     {
1129         return -1;
1130     }
1131 
1132     return 0;
1133 }
1134 
readSparseMatrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,int _iNbItem,int * _piNbItemRow,int * _piColPos,double * _pdblReal)1135 int readSparseMatrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, int _iNbItem, int *_piNbItemRow, int *_piColPos, double *_pdblReal)
1136 {
1137     return readCommonSparseComplexMatrix_v1(_iDatasetId, 0, _iRows, _iCols, _iNbItem, _piNbItemRow, _piColPos, _pdblReal, NULL);
1138 }
1139 
readSparseComplexMatrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,int _iNbItem,int * _piNbItemRow,int * _piColPos,double * _pdblReal,double * _pdblImg)1140 int readSparseComplexMatrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, int _iNbItem, int *_piNbItemRow, int *_piColPos, double *_pdblReal,
1141                                double *_pdblImg)
1142 {
1143     return readCommonSparseComplexMatrix_v1(_iDatasetId, 1, _iRows, _iCols, _iNbItem, _piNbItemRow, _piColPos, _pdblReal, _pdblImg);
1144 }
1145 
readBooleanSparseMatrix_v1(hid_t _iDatasetId,int _iRows,int _iCols,int _iNbItem,int * _piNbItemRow,int * _piColPos)1146 int readBooleanSparseMatrix_v1(hid_t _iDatasetId, int _iRows, int _iCols, int _iNbItem, int *_piNbItemRow, int *_piColPos)
1147 {
1148     hid_t obj = 0;
1149     hobj_ref_t pRef[2] = {0};
1150     herr_t status;
1151 
1152     /*
1153     * Read the data.
1154     */
1155     status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pRef);
1156     if (status < 0)
1157     {
1158         return -1;
1159     }
1160 
1161     //read Row data
1162     obj = H5Rdereference(_iDatasetId,
1163 #if H5_VERSION_GE(1,10,0)
1164                          H5P_DATASET_ACCESS_DEFAULT,
1165 #endif
1166                          H5R_OBJECT, &pRef[0]);
1167     status = readInteger32Matrix_v1(obj, 1, _iRows, _piNbItemRow);
1168     if (status < 0)
1169     {
1170         return -1;
1171     }
1172 
1173     //read cols data
1174     obj = H5Rdereference(_iDatasetId,
1175 #if H5_VERSION_GE(1,10,0)
1176                          H5P_DATASET_ACCESS_DEFAULT,
1177 #endif
1178                          H5R_OBJECT, &pRef[1]);
1179     status = readInteger32Matrix_v1(obj, 1, _iNbItem, _piColPos);
1180     if (status < 0)
1181     {
1182         return -1;
1183     }
1184 
1185     return 0;
1186 }
1187 
getScilabTypeFromDataSet_v1(hid_t _iDatasetId)1188 int getScilabTypeFromDataSet_v1(hid_t _iDatasetId)
1189 {
1190     int iVarType = 0;
1191     char *pstScilabClass = readAttribute_v1(_iDatasetId, g_SCILAB_CLASS);
1192 
1193     if (pstScilabClass == NULL)
1194     {
1195         return unknow_type;
1196     }
1197     /* HDF5 Float type + SCILAB_Class = double <=> double */
1198     if (strcmp(pstScilabClass, g_SCILAB_CLASS_DOUBLE) == 0)
1199     {
1200         iVarType = sci_matrix;
1201     }
1202     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_STRING) == 0)
1203     {
1204         iVarType = sci_strings;
1205     }
1206     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_BOOLEAN) == 0)
1207     {
1208         iVarType = sci_boolean;
1209     }
1210     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_POLY) == 0)
1211     {
1212         iVarType = sci_poly;
1213     }
1214     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_INT) == 0)
1215     {
1216         iVarType = sci_ints;
1217     }
1218     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_SPARSE) == 0)
1219     {
1220         iVarType = sci_sparse;
1221     }
1222     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_BSPARSE) == 0)
1223     {
1224         iVarType = sci_boolean_sparse;
1225     }
1226     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_LIST) == 0)
1227     {
1228         iVarType = sci_list;
1229     }
1230     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_TLIST) == 0)
1231     {
1232         iVarType = sci_tlist;
1233     }
1234     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_MLIST) == 0)
1235     {
1236         iVarType = sci_mlist;
1237     }
1238     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_VOID) == 0)
1239     {
1240         iVarType = sci_void;
1241     }
1242     else if (strcmp(pstScilabClass, g_SCILAB_CLASS_UNDEFINED) == 0)
1243     {
1244         iVarType = sci_undefined;
1245     }
1246 
1247     FREE(pstScilabClass);
1248     return iVarType;
1249 }
1250 
getListItemReferences_v1(hid_t _iDatasetId,hobj_ref_t ** _piItemRef)1251 int getListItemReferences_v1(hid_t _iDatasetId, hobj_ref_t ** _piItemRef)
1252 {
1253     int iItem = 0;
1254     herr_t status = 0;
1255 
1256     getListDims_v1(_iDatasetId, &iItem);
1257 
1258     *_piItemRef = (hobj_ref_t *) MALLOC(iItem * sizeof(hobj_ref_t));
1259 
1260     status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, *_piItemRef);
1261     if (status < 0)
1262     {
1263         return -1;
1264     }
1265 
1266     return 0;
1267 }
1268 
getListItemDataset_v1(hid_t _iDatasetId,void * _piItemRef,int _iItemPos,hid_t * _piItemDataset)1269 int getListItemDataset_v1(hid_t _iDatasetId, void *_piItemRef, int _iItemPos, hid_t *_piItemDataset)
1270 {
1271     hobj_ref_t poRef = ((hobj_ref_t *) _piItemRef)[_iItemPos];
1272 
1273     *_piItemDataset = H5Rdereference(_iDatasetId,
1274 #if H5_VERSION_GE(1,10,0)
1275                                      H5P_DATASET_ACCESS_DEFAULT,
1276 #endif
1277                                      H5R_OBJECT, &poRef);
1278 
1279     if (*_piItemDataset == 0)
1280     {
1281         return -1;
1282     }
1283 
1284     return 0;
1285 }
1286 
deleteListItemReferences_v1(hid_t _iDatasetId,void * _piItemRef)1287 int deleteListItemReferences_v1(hid_t _iDatasetId, void *_piItemRef)
1288 {
1289     herr_t status;
1290 
1291     if (_piItemRef)
1292     {
1293         hobj_ref_t *poRef = (hobj_ref_t *) _piItemRef;
1294 
1295         FREE(poRef);
1296     }
1297 
1298     status = H5Dclose(_iDatasetId);
1299     if (status < 0)
1300     {
1301         return -1;
1302     }
1303 
1304     return 0;
1305 }
1306