From d9238dcc54db5a70a09f466c6996cf9988165ab7 Mon Sep 17 00:00:00 2001 From: Antoine ELIAS Date: Thu, 26 Apr 2012 11:23:10 +0200 Subject: [PATCH] add "-append" argument to export_to_hdf5 function, fix some memory leaks in hdf5 module Change-Id: I4cd872a025a4103d12abf74b4aae1102e96e52cd --- scilab/modules/fileio/macros/listvarinfile.sci | 20 ++- scilab/modules/hdf5/includes/h5_writeDataToFile.h | 8 +- .../hdf5/sci_gateway/cpp/sci_export_to_hdf5.cpp | 159 +++++++++++--------- .../hdf5/sci_gateway/cpp/sci_import_from_hdf5.cpp | 2 +- .../hdf5/sci_gateway/cpp/sci_listvar_in_hdf5.cpp | 16 +- scilab/modules/hdf5/src/c/h5_fileManagement.c | 23 +-- scilab/modules/hdf5/src/c/h5_readDataFromFile.c | 26 ++-- scilab/modules/hdf5/src/c/h5_writeDataToFile.c | 121 ++++++++++----- scilab/modules/io/macros/load.sci | 3 +- 9 files changed, 224 insertions(+), 154 deletions(-) diff --git a/scilab/modules/fileio/macros/listvarinfile.sci b/scilab/modules/fileio/macros/listvarinfile.sci index 60cb563..726b71b 100644 --- a/scilab/modules/fileio/macros/listvarinfile.sci +++ b/scilab/modules/fileio/macros/listvarinfile.sci @@ -19,12 +19,24 @@ function varargout = listvarinfile(filename) error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "listvarinfile", 1)); end - //check file format if is_hdf5_file(filename) then - [a,b,c,d] = listvar_in_hdf5(filename); + listvarFunction = listvar_in_hdf5; else - [a,b,c,d] = %_listvarinfile(filename); + listvarFunction = %_listvarinfile; end - varargout = list(a,b,c,d); + //check output argument count + if lhs == 1 then + a = listvarFunction(filename); + varargout = list(a); + elseif lhs == 2 then + [a,b] = listvarFunction(filename); + varargout = list(a,b); + elseif lhs == 3 then + [a,b,c] = listvarFunction(filename); + varargout = list(a,b,c); + elseif lhs == 4 then + [a,b,c,d] = listvarFunction(filename); + varargout = list(a,b,c,d); + end endfunction diff --git a/scilab/modules/hdf5/includes/h5_writeDataToFile.h b/scilab/modules/hdf5/includes/h5_writeDataToFile.h index 877b8a5..867e202 100644 --- a/scilab/modules/hdf5/includes/h5_writeDataToFile.h +++ b/scilab/modules/hdf5/includes/h5_writeDataToFile.h @@ -29,10 +29,10 @@ HDF5_SCILAB_IMPEXP int writeBooleanMatrix(int _iFile, char* _pstDatasetName, int HDF5_SCILAB_IMPEXP int writePolyMatrix(int _iFile, char* _pstDatasetName, char* _pstVarName, int _iRows, int _iCols, int* _piNbCoef, double** _pdblData); HDF5_SCILAB_IMPEXP int writePolyComplexMatrix(int _iFile, char* _pstDatasetName, char* _pstVarName, int _iRows, int _iCols, int* _piNbCoef, double** _pdblReal, double** _pdblImg); -HDF5_SCILAB_IMPEXP int writeInterger8Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, char* _piData8); -HDF5_SCILAB_IMPEXP int writeInterger16Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, short* _piData16); -HDF5_SCILAB_IMPEXP int writeInterger32Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, int* _piData32); -HDF5_SCILAB_IMPEXP int writeInterger64Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, long long* _piData64); +HDF5_SCILAB_IMPEXP int writeInteger8Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, char* _piData8); +HDF5_SCILAB_IMPEXP int writeInteger16Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, short* _piData16); +HDF5_SCILAB_IMPEXP int writeInteger32Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, int* _piData32); +HDF5_SCILAB_IMPEXP int writeInteger64Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, long long* _piData64); HDF5_SCILAB_IMPEXP int writeUnsignedInterger8Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, unsigned char* _piData8); HDF5_SCILAB_IMPEXP int writeUnsignedInterger16Matrix(int _iFile, char* _pstDatasetName, int _iRows, int _iCols, unsigned short* _piData16); diff --git a/scilab/modules/hdf5/sci_gateway/cpp/sci_export_to_hdf5.cpp b/scilab/modules/hdf5/sci_gateway/cpp/sci_export_to_hdf5.cpp index 772eeadf..470b689 100644 --- a/scilab/modules/hdf5/sci_gateway/cpp/sci_export_to_hdf5.cpp +++ b/scilab/modules/hdf5/sci_gateway/cpp/sci_export_to_hdf5.cpp @@ -24,6 +24,7 @@ extern "C" #include "expandPathVariable.h" #include "h5_fileManagement.h" #include "h5_writeDataToFile.h" +#include "h5_readDataFromFile.h" #include "freeArrayOfString.h" #ifdef _MSC_VER #include "strdup_windows.h" @@ -66,47 +67,63 @@ int sci_export_to_hdf5(char *fname, unsigned long fname_len) char** pstNameList = NULL; char *pstFileName = NULL; bool bExport = false; + int iStartVarName = 1; + bool bAppendMode = false; SciErr sciErr; - CheckRhs(1, 100000);//output parameter + CheckInputArgumentAtLeast(pvApiCtx, 2); + CheckLhs(0, 1); #ifndef _MSC_VER forceJHDF5load(); #endif - /*get input data*/ - if (Rhs < 2) - { - Scierror(999, _("%s: Wrong number of input argument(s): At most %d expected.\n"), fname, 2); - return 0; - } - pstNameList = (char**)MALLOC(sizeof(char*) * Rhs); iNbVar = extractVarNameList(1, Rhs, pstNameList); if (iNbVar == 0) { FREE(pstNameList); - return 0; + return 1; } - piAddrList = (int**)MALLOC(sizeof(int*) * (iNbVar - 1)); - for (int i = 0 ; i < Rhs - 1 ; i++) + if(strcmp(pstNameList[1], "-append") == 0) { - sciErr = getVarAddressFromName(pvApiCtx, pstNameList[i + 1], &piAddrList[i]); + bAppendMode = true; + iStartVarName = 2; + } + + piAddrList = (int**)MALLOC(sizeof(int*) * (iNbVar - iStartVarName)); + for (int i = iStartVarName ; i < Rhs ; i++) + { + sciErr = getVarAddressFromName(pvApiCtx, pstNameList[i], &piAddrList[i - iStartVarName]); if (sciErr.iErr) { - // i+1 = i for filename + 1 because starting arg number is 1 for human being - Scierror(999, _("%s: Wrong value for input argument #%d: Defined variable expected.\n"), fname, i + 2); + Scierror(999, _("%s: Wrong value for input argument #%d: Defined variable expected.\n"), fname, i + 1); printError(&sciErr, 0); - return 0; + return 1; } } + //check append option + iLevel = 0; // open hdf5 file pstFileName = expandPathVariable(pstNameList[0]); - int iH5File = createHDF5File(pstFileName); + int iH5File = 0; + if(bAppendMode) + { + iH5File = openHDF5File(pstFileName); + if(iH5File < 0) + { + iH5File = createHDF5File(pstFileName); + } + } + else + { + iH5File = createHDF5File(pstFileName); + } + if (iH5File < 0) { @@ -120,13 +137,42 @@ int sci_export_to_hdf5(char *fname, unsigned long fname_len) Scierror(999, _("%s: Cannot open file %s.\n"), fname, pstNameList[0]); } - return 0; + return 1; + } + + + if(bAppendMode) + { + //check if variable already exists + int iNbItem = getVariableNames(iH5File, NULL); + if(iNbItem) + { + char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem); + + iNbItem = getVariableNames(iH5File, pstVarNameList); + + //import all data + for(int i = 0 ; i < iNbItem ; i++) + { + for(int j = 0 ; j < Rhs - iStartVarName; j++) + { + if(strcmp(pstVarNameList[i], pstNameList[j + iStartVarName]) == 0) + { + + Scierror(999, _("%s: Variable \'%s\' already exists in file \'%s\'."), fname, pstVarNameList[i], pstNameList[0]); + return 1; + } + } + FREE(pstVarNameList[i]); + } + FREE(pstVarNameList); + } } // export data - for (int i = 0 ; i < Rhs - 1; i++) + for (int i = 0 ; i < Rhs - iStartVarName; i++) { - bExport = export_data(iH5File, piAddrList[i], pstNameList[i + 1]); + bExport = export_data(iH5File, piAddrList[i], pstNameList[i + iStartVarName]); if (bExport == false) { break; @@ -148,7 +194,7 @@ int sci_export_to_hdf5(char *fname, unsigned long fname_len) if (sciErr.iErr) { printError(&sciErr, 0); - return 0; + return 1; } if (bExport == true) @@ -160,6 +206,16 @@ int sci_export_to_hdf5(char *fname, unsigned long fname_len) piReturn[0] = 0; } + + //free memory + for(int i = 0 ; i < Rhs ; i++) + { + FREE(pstNameList[i]); + } + FREE(pstNameList); + + FREE(piAddrList); + LhsVar(1) = Rhs + 1; PutLhsVar(); return 0; @@ -256,6 +312,12 @@ static bool export_data(int _iH5File, int* _piVar, char* _pstName) bReturn = export_void(_iH5File, _piVar, _pstName); break; } + + default : + { + bReturn = false; + break; + } } return bReturn; } @@ -639,7 +701,7 @@ static bool export_ints(int _iH5File, int *_piVar, char* _pstName) printError(&sciErr, 0); return false; } - iRet = writeInterger8Matrix(_iH5File, _pstName, iRows, iCols, (char*)piData); + iRet = writeInteger8Matrix(_iH5File, _pstName, iRows, iCols, (char*)piData); break; case SCI_UINT8 : sciErr = getMatrixOfUnsignedInteger8(pvApiCtx, _piVar, &iRows, &iCols, (unsigned char**)&piData); @@ -657,7 +719,7 @@ static bool export_ints(int _iH5File, int *_piVar, char* _pstName) printError(&sciErr, 0); return false; } - iRet = writeInterger16Matrix(_iH5File, _pstName, iRows, iCols, (short*)piData); + iRet = writeInteger16Matrix(_iH5File, _pstName, iRows, iCols, (short*)piData); break; case SCI_UINT16 : sciErr = getMatrixOfUnsignedInteger16(pvApiCtx, _piVar, &iRows, &iCols, (unsigned short**)&piData); @@ -675,7 +737,7 @@ static bool export_ints(int _iH5File, int *_piVar, char* _pstName) printError(&sciErr, 0); return false; } - iRet = writeInterger32Matrix(_iH5File, _pstName, iRows, iCols, (int*)piData); + iRet = writeInteger32Matrix(_iH5File, _pstName, iRows, iCols, (int*)piData); break; case SCI_UINT32 : sciErr = getMatrixOfUnsignedInteger32(pvApiCtx, _piVar, &iRows, &iCols, (unsigned int**)&piData); @@ -693,7 +755,7 @@ static bool export_ints(int _iH5File, int *_piVar, char* _pstName) // printError(&sciErr, 0); // return false; //} - //iRet = writeInterger64Matrix(_iH5File, _pstName, iRows, iCols, (long long*)piData); + //iRet = writeInteger64Matrix(_iH5File, _pstName, iRows, iCols, (long long*)piData); //break; case SCI_UINT64 : //sciErr = getMatrixOfUnsignedInteger64(_piVar, &iRows, &iCols, (unsigned long long**)&piData); @@ -818,11 +880,11 @@ int extractVarNameList(int _iStart, int _iEnd, char** _pstNameList) for (int i = _iStart ; i <= _iEnd ; i++) { - int iRows = 0; - int iCols = 0; - int iLen = 0; - int* piAddr = NULL; - int iType = 0; + int iRows = 0; + int iCols = 0; + int iLen = 0; + int* piAddr = NULL; + int iType = 0; SciErr sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddr); if (sciErr.iErr) @@ -831,49 +893,12 @@ int extractVarNameList(int _iStart, int _iEnd, char** _pstNameList) return 0; } - //get filename - sciErr = getVarType(pvApiCtx, piAddr, &iType); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - if (iType != sci_strings) + if(getAllocatedSingleString(pvApiCtx, piAddr, &_pstNameList[iCount])) { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "export_to_hdf5", i); return 0; } - sciErr = getVarDimension(pvApiCtx, piAddr, &iRows, &iCols); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - if (iRows != 1 || iCols != 1) - { - Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), "export_to_hdf5", i); - Scierror(999, _("%s: Type: %dx%d\n"), "export_to_hdf5", iRows, iCols); - return 0; - } - - sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, &iLen, NULL); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - _pstNameList[iCount] = (char*)MALLOC((iLen + 1) * sizeof(char));//+1 for null termination - sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, &iLen, &_pstNameList[iCount]); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - iCount++; } return iCount; diff --git a/scilab/modules/hdf5/sci_gateway/cpp/sci_import_from_hdf5.cpp b/scilab/modules/hdf5/sci_gateway/cpp/sci_import_from_hdf5.cpp index f601c8a..df28b18 100644 --- a/scilab/modules/hdf5/sci_gateway/cpp/sci_import_from_hdf5.cpp +++ b/scilab/modules/hdf5/sci_gateway/cpp/sci_import_from_hdf5.cpp @@ -60,7 +60,7 @@ int sci_import_from_hdf5(char *fname, unsigned long fname_len) int* piAddr = NULL; char* pstFilename = NULL; char* pstExpandedFilename = NULL; - bool bImport = false; + bool bImport = true; int iSelectedVar = Rhs - 1; bool bSelectedVar = false; diff --git a/scilab/modules/hdf5/sci_gateway/cpp/sci_listvar_in_hdf5.cpp b/scilab/modules/hdf5/sci_gateway/cpp/sci_listvar_in_hdf5.cpp index 1b3c4b8..8eabb17 100644 --- a/scilab/modules/hdf5/sci_gateway/cpp/sci_listvar_in_hdf5.cpp +++ b/scilab/modules/hdf5/sci_gateway/cpp/sci_listvar_in_hdf5.cpp @@ -64,7 +64,6 @@ int sci_listvar_in_hdf5(char *fname, unsigned long fname_len) VarInfo* pInfo = NULL; bool bRet = true; - CheckRhs(1, 1); CheckLhs(1, 4); @@ -98,7 +97,7 @@ int sci_listvar_in_hdf5(char *fname, unsigned long fname_len) char** pstVarNameList = (char**)MALLOC(sizeof(char*) * iNbItem); pInfo = (VarInfo*)MALLOC(iNbItem * sizeof(VarInfo)); - if(Lhs != 4) + if(Lhs == 1) { sciprint("Name Type Size Bytes\n"); sciprint("---------------------------------------------------------------\n"); @@ -122,12 +121,23 @@ int sci_listvar_in_hdf5(char *fname, unsigned long fname_len) break; } - if(Lhs != 4) + if(Lhs == 1) { sciprint("%s\n", pInfo[i].pstInfo); } } } + else + {//no variable returms [] for each Lhs + for(int i = 0 ; i < Lhs ; i++) + { + createEmptyMatrix(pvApiCtx, Rhs + i + 1); + LhsVar(i+1) = Rhs + i + 1; + } + + PutLhsVar(); + return 0; + } //1st Lhs char** pstVarName = (char**)MALLOC(sizeof(char*) * iNbItem); diff --git a/scilab/modules/hdf5/src/c/h5_fileManagement.c b/scilab/modules/hdf5/src/c/h5_fileManagement.c index 2b4b9c8..7f3e809 100644 --- a/scilab/modules/hdf5/src/c/h5_fileManagement.c +++ b/scilab/modules/hdf5/src/c/h5_fileManagement.c @@ -53,9 +53,12 @@ int createHDF5File(char *name) scichdir(pathdest); } + FREE(pathdest); /*bug 5629 : to prevent replace directory by file*/ if (isdir(filename)) { + FREE(filename); + FREE(currentpath); return -2; } @@ -71,21 +74,8 @@ int createHDF5File(char *name) scichdir(currentpath); - if (currentpath) - { - FREE(currentpath); - currentpath = NULL; - } - if (filename) - { - FREE(filename); - filename = NULL; - } - if (pathdest) - { - FREE(pathdest); - pathdest = NULL; - } + FREE(currentpath); + FREE(filename); return file; } @@ -101,7 +91,6 @@ int openHDF5File(char *name) /* Used to avoid stack trace to be displayed */ H5E_auto2_t oldfunc; - /* TO DO : remove when HDF5 will be fixed ... */ /* HDF5 does not manage no ANSI characters */ /* UGLY workaround :( */ @@ -122,7 +111,7 @@ int openHDF5File(char *name) /* Turn off error handling */ H5Eset_auto2(H5E_DEFAULT, NULL, NULL); - file = H5Fopen (filename, H5F_ACC_RDONLY, H5P_DEFAULT); + file = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT); /* The following test will display the backtrace in case of error */ /* Deactivated because displayed each time we call 'load' to open a non-HDF5 file */ /*if (file < 0) diff --git a/scilab/modules/hdf5/src/c/h5_readDataFromFile.c b/scilab/modules/hdf5/src/c/h5_readDataFromFile.c index 18b8a5c..392ad36 100644 --- a/scilab/modules/hdf5/src/c/h5_readDataFromFile.c +++ b/scilab/modules/hdf5/src/c/h5_readDataFromFile.c @@ -414,19 +414,18 @@ int readDoubleMatrix(int _iDatasetId, int _iRows, int _iCols, double *_pdblData) if (_iRows != 0 && _iCols != 0) { hid_t obj; - hobj_ref_t *pRef = (hobj_ref_t *) MALLOC(1 * sizeof(hobj_ref_t)); + hobj_ref_t Ref; //Read the data. - status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pRef); + status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &Ref); if (status < 0) { return -1; } //Open the referenced object, get its name and type. - obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pRef[0]); + obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &Ref); readDouble(obj, _iRows, _iCols, _pdblData); - FREE(pRef); } status = H5Dclose(_iDatasetId); @@ -442,7 +441,7 @@ int readDoubleComplexMatrix(int _iDatasetId, int _iRows, int _iCols, double *_pd { hid_t obj; herr_t status; - hobj_ref_t *pRef = (hobj_ref_t *) MALLOC(2 * sizeof(hobj_ref_t)); + hobj_ref_t pRef[2] = {0}; //Read the data. status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pRef); @@ -775,6 +774,7 @@ int readCommonPolyMatrix(int _iDatasetId, char *_pstVarname, int _iComplex, int status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pData); if (status < 0) { + FREE(pData); return -1; } @@ -795,20 +795,22 @@ int readCommonPolyMatrix(int _iDatasetId, char *_pstVarname, int _iComplex, int if (status < 0) { + FREE(pData); return -1; } } pstVarName = readAttribute(_iDatasetId, g_SCILAB_CLASS_VARNAME); strcpy(_pstVarname, pstVarName); + FREE(pstVarName); status = H5Dclose(_iDatasetId); if (status < 0) { + FREE(pData); return -1; } FREE(pData); - FREE(pstVarName); return 0; } @@ -1003,7 +1005,7 @@ int readCommonSparseComplexMatrix(int _iDatasetId, int _iComplex, int _iRows, in double *_pdblReal, double *_pdblImg) { hid_t obj = 0; - hobj_ref_t *pRef = (hobj_ref_t *) MALLOC(3 * sizeof(hobj_ref_t)); + hobj_ref_t pRef[3] = {0}; herr_t status; /* @@ -1048,8 +1050,6 @@ int readCommonSparseComplexMatrix(int _iDatasetId, int _iComplex, int _iRows, in return -1; } - FREE(pRef); - return 0; } @@ -1067,7 +1067,7 @@ int readSparseComplexMatrix(int _iDatasetId, int _iRows, int _iCols, int _iNbIte int readBooleanSparseMatrix(int _iDatasetId, int _iRows, int _iCols, int _iNbItem, int *_piNbItemRow, int *_piColPos) { hid_t obj = 0; - hobj_ref_t *pRef = (hobj_ref_t *) MALLOC(2 * sizeof(hobj_ref_t)); + hobj_ref_t pRef[2] = {0}; herr_t status; /* @@ -1095,8 +1095,6 @@ int readBooleanSparseMatrix(int _iDatasetId, int _iRows, int _iCols, int _iNbIte return -1; } - FREE(pRef); - return 0; } @@ -1163,10 +1161,6 @@ int getScilabTypeFromDataSet(int _iDatasetId) iVarType = sci_undefined; } - if (iVarType == 0) - { - return 0; - } FREE(pstScilabClass); return iVarType; } diff --git a/scilab/modules/hdf5/src/c/h5_writeDataToFile.c b/scilab/modules/hdf5/src/c/h5_writeDataToFile.c index d0e6860..409ba17 100644 --- a/scilab/modules/hdf5/src/c/h5_writeDataToFile.c +++ b/scilab/modules/hdf5/src/c/h5_writeDataToFile.c @@ -401,18 +401,23 @@ char *createGroupName(char *_pstGroupName) return pstGroupName; } -char *createPathName(char *_pstGroupName, int _iIndex) +char* createPathName(char *_pstGroupName, int _iIndex) { char *pstName = NULL; char *pstPathName = NULL; - pstName = (char *)MALLOC(((int)log10((double)(_iIndex + 1)) + 3) * sizeof(char)); + int iNameLen = (int)log10((double)_iIndex + 1) + 1; + iNameLen += 2; //for both '#' + iNameLen += 1; //for null termanation + + pstName = (char *)MALLOC(iNameLen * sizeof(char)); //1 for null termination, 2 for '#' characters sprintf(pstName, "#%d#", _iIndex); pstPathName = (char *)MALLOC((strlen(_pstGroupName) + strlen(pstName) + 2) * sizeof(char)); //1 for null termination, 1 for separator, 2 for '#' characters sprintf(pstPathName, "%s/%s", _pstGroupName, pstName); + FREE(pstName); return pstPathName; } @@ -530,11 +535,6 @@ static hobj_ref_t writeCommomDoubleMatrix(int _iFile, char *_pstGroupName, char herr_t status = 0; hobj_ref_t iRef = 0; - char *pstPathName = NULL; - - //createGroupe and dataset name - pstPathName = createPathName(_pstGroupName, _iIndex); - if (_iRows * _iCols == 0) { double dblZero = 0; @@ -580,6 +580,8 @@ static hobj_ref_t writeCommomDoubleMatrix(int _iFile, char *_pstGroupName, char } else { + char *pstPathName = NULL; + //Create dataspace. Setting maximum size to NULL sets the maximum //size to be the current size. space = H5Screate_simple(1, dims, NULL); @@ -588,17 +590,22 @@ static hobj_ref_t writeCommomDoubleMatrix(int _iFile, char *_pstGroupName, char return (hobj_ref_t) (-1); } + //createGroupe and dataset name + pstPathName = createPathName(_pstGroupName, _iIndex); + //Create the dataset and write the array data to it. iCompress = enableCompression(9, 1, dims); dset = H5Dcreate(_iFile, pstPathName, H5T_NATIVE_DOUBLE, space, iCompress); if (dset < 0) { + FREE(pstPathName); return (hobj_ref_t) (-1); } status = H5Dwrite(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, _pdblData); if (status < 0) { + FREE(pstPathName); return (hobj_ref_t) (-1); } @@ -606,6 +613,7 @@ static hobj_ref_t writeCommomDoubleMatrix(int _iFile, char *_pstGroupName, char status = addAttribute(dset, g_SCILAB_CLASS, g_SCILAB_CLASS_DOUBLE); if (status < 0) { + FREE(pstPathName); return (hobj_ref_t) (-1); } @@ -613,6 +621,7 @@ static hobj_ref_t writeCommomDoubleMatrix(int _iFile, char *_pstGroupName, char status = addIntAttribute(dset, g_SCILAB_CLASS_ROWS, _iRows); if (status < 0) { + FREE(pstPathName); return (hobj_ref_t) (-1); } @@ -620,6 +629,7 @@ static hobj_ref_t writeCommomDoubleMatrix(int _iFile, char *_pstGroupName, char status = addIntAttribute(dset, g_SCILAB_CLASS_COLS, _iCols); if (status < 0) { + FREE(pstPathName); return (hobj_ref_t) (-1); } @@ -627,8 +637,11 @@ static hobj_ref_t writeCommomDoubleMatrix(int _iFile, char *_pstGroupName, char status = H5Rcreate(&iRef, _iFile, pstPathName, H5R_OBJECT, -1); if (status < 0) { + FREE(pstPathName); return (hobj_ref_t) (-1); } + + FREE(pstPathName); } //Close and release resources. @@ -644,8 +657,6 @@ static hobj_ref_t writeCommomDoubleMatrix(int _iFile, char *_pstGroupName, char return (hobj_ref_t) (-1); } - FREE(pstPathName); - return iRef; } @@ -764,11 +775,13 @@ int writeDoubleComplexMatrix(int _iFile, char *_pstDatasetName, int _iRows, int if (status < 0) { + FREE(pstGroupName); return -1; } pRef[0] = writeCommomDoubleMatrix(_iFile, pstGroupName, _pstDatasetName, 0, _iRows, _iCols, _pdblReal); pRef[1] = writeCommomDoubleMatrix(_iFile, pstGroupName, _pstDatasetName, 1, _iRows, _iCols, _pdblImg); + FREE(pstGroupName); if (pRef[0] == 0 || pRef[1] == 0) { return 1; @@ -836,7 +849,6 @@ int writeDoubleComplexMatrix(int _iFile, char *_pstDatasetName, int _iRows, int return -1; } - FREE(pstGroupName); return 0; } @@ -918,13 +930,11 @@ static int writeCommonPolyMatrix(int _iFile, char *_pstDatasetName, char *_pstVa hid_t iCompress = 0; hobj_ref_t *pData = 0; - char *pstName = NULL; char *pstPathName = NULL; - char *pstGroupName = NULL; // Create ref matrix - pData = (hobj_ref_t *) MALLOC(_iRows * _iCols * sizeof(hobj_ref_t)); + pData = (hobj_ref_t *)MALLOC(dims[0] * sizeof(hobj_ref_t)); // Generate groupname ## pstGroupName = createGroupName(_pstDatasetName); @@ -934,7 +944,7 @@ static int writeCommonPolyMatrix(int _iFile, char *_pstDatasetName, char *_pstVa status = H5Gclose(group); //Now create each String as a dedicated DataSet. - for (i = 0; i < _iRows * _iCols; i++) + for (i = 0 ; i < dims[0] ; i++) { pstPathName = createPathName(pstGroupName, i); @@ -950,23 +960,31 @@ static int writeCommonPolyMatrix(int _iFile, char *_pstDatasetName, char *_pstVa if (status < 0) { + FREE(pstPathName); + FREE(pstGroupName); + FREE(pData); return -1; } + // create the ref status = H5Rcreate(&pData[i], _iFile, pstPathName, H5R_OBJECT, -1); if (status < 0) { + FREE(pstPathName); + FREE(pstGroupName); + FREE(pData); return -1; } - FREE(pstName); FREE(pstPathName); } + FREE(pstGroupName); //Create dataspace. Setting maximum size to NULL sets the maximum size to be the current size. space = H5Screate_simple(1, dims, NULL); if (status < 0) { + FREE(pData); return -1; } @@ -975,14 +993,18 @@ static int writeCommonPolyMatrix(int _iFile, char *_pstDatasetName, char *_pstVa dset = H5Dcreate(_iFile, _pstDatasetName, H5T_STD_REF_OBJ, space, iCompress); if (dset < 0) { + FREE(pData); return -1; } status = H5Dwrite(dset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pData); if (status < 0) { + FREE(pData); return -1; } + + FREE(pData); //Add attribute SCILAB_Class = poly to dataset status = addAttribute(dset, g_SCILAB_CLASS, g_SCILAB_CLASS_POLY); if (status < 0) @@ -1034,9 +1056,6 @@ static int writeCommonPolyMatrix(int _iFile, char *_pstDatasetName, char *_pstVa return -1; } - FREE(pstGroupName); - FREE(pData); - return 0; } @@ -1051,7 +1070,7 @@ int writePolyComplexMatrix(int _iFile, char *_pstDatasetName, char *_pstVarName, return writeCommonPolyMatrix(_iFile, _pstDatasetName, _pstVarName, 1, _iRows, _iCols, _piNbCoef, _pdblReal, _pdblImg); } -int writeInterger8Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCols, char *_pcData) +int writeInteger8Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCols, char *_pcData) { hsize_t piDims[1] = { _iRows * _iCols }; herr_t status = 0; @@ -1122,7 +1141,7 @@ int writeInterger8Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCo return 0; } -int writeInterger16Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCols, short *_psData) +int writeInteger16Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCols, short *_psData) { hsize_t piDims[1] = { _iRows * _iCols }; herr_t status = 0; @@ -1192,7 +1211,7 @@ int writeInterger16Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iC return 0; } -int writeInterger32Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCols, int *_piData) +int writeInteger32Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCols, int *_piData) { hsize_t piDims[1] = { _iRows * _iCols }; herr_t status = 0; @@ -1264,7 +1283,7 @@ int writeInterger32Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iC return 0; } -int writeInterger64Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCols, long long *_pllData) +int writeInteger64Matrix(int _iFile, char *_pstDatasetName, int _iRows, int _iCols, long long *_pllData) { hsize_t piDims[1] = { _iRows * _iCols }; herr_t status = 0; @@ -1633,17 +1652,13 @@ int writeCommonSparseComplexMatrix(int _iFile, char *_pstDatasetName, int _iComp hid_t dset = 0; hid_t group = 0; hid_t iCompress = 0; - hobj_ref_t *pDataRef = 0; + hobj_ref_t pDataRef[3] = {0}; char *pstRowPath = NULL; char *pstColPath = NULL; char *pstDataPath = NULL; char *pstGroupName = NULL; - // Create ref matrix - //3 refs : 1 for data, 1 for Number Item by row ( row size ) and 1 for column position - pDataRef = (hobj_ref_t *) MALLOC(3 * sizeof(hobj_ref_t)); - // Generate groupname ## pstGroupName = createGroupName(_pstDatasetName); @@ -1652,33 +1667,44 @@ int writeCommonSparseComplexMatrix(int _iFile, char *_pstDatasetName, int _iComp status = H5Gclose(group); if (status < 0) { + FREE(pstGroupName); return -1; } //Create each sub dataset and insert data pstRowPath = createPathName(pstGroupName, 0); - status = writeInterger32Matrix(_iFile, pstRowPath, 1, _iRows, _piNbItemRow); + status = writeInteger32Matrix(_iFile, pstRowPath, 1, _iRows, _piNbItemRow); if (status < 0) { + FREE(pstRowPath); + FREE(pstGroupName); return -1; } status = H5Rcreate(&pDataRef[0], _iFile, pstRowPath, H5R_OBJECT, -1); if (status < 0) { + FREE(pstRowPath); + FREE(pstGroupName); return -1; } pstColPath = createPathName(pstGroupName, 1); - status = writeInterger32Matrix(_iFile, pstColPath, 1, _iNbItem, _piColPos); + status = writeInteger32Matrix(_iFile, pstColPath, 1, _iNbItem, _piColPos); if (status < 0) { + FREE(pstRowPath); + FREE(pstColPath); + FREE(pstGroupName); return -1; } status = H5Rcreate(&pDataRef[1], _iFile, pstColPath, H5R_OBJECT, -1); if (status < 0) { + FREE(pstRowPath); + FREE(pstColPath); + FREE(pstGroupName); return -1; } @@ -1691,14 +1717,23 @@ int writeCommonSparseComplexMatrix(int _iFile, char *_pstDatasetName, int _iComp { status = writeDoubleMatrix(_iFile, pstDataPath, 1, _iNbItem, _pdblReal); } + if (status < 0) { + FREE(pstRowPath); + FREE(pstColPath); + FREE(pstDataPath); + FREE(pstGroupName); return -1; } status = H5Rcreate(&pDataRef[2], _iFile, pstDataPath, H5R_OBJECT, -1); if (status < 0) { + FREE(pstRowPath); + FREE(pstColPath); + FREE(pstDataPath); + FREE(pstGroupName); return -1; } @@ -1706,6 +1741,7 @@ int writeCommonSparseComplexMatrix(int _iFile, char *_pstDatasetName, int _iComp FREE(pstRowPath); FREE(pstColPath); FREE(pstDataPath); + FREE(pstGroupName); //Create dataspace. Setting maximum size to NULL sets the maximum size to be the current size. space = H5Screate_simple(1, dims, NULL); @@ -1776,8 +1812,6 @@ int writeCommonSparseComplexMatrix(int _iFile, char *_pstDatasetName, int _iComp return -1; } - FREE(pstGroupName); - return 0; } @@ -1800,16 +1834,12 @@ int writeBooleanSparseMatrix(int _iFile, char *_pstDatasetName, int _iRows, int hid_t dset = 0; hid_t group = 0; hid_t iCompress = 0; - hobj_ref_t *pDataRef = 0; + hobj_ref_t pDataRef[2] = {0}; char *pstRowPath = NULL; char *pstColPath = NULL; char *pstGroupName = NULL; - // Create ref matrix - //2 refs : 1 for data, 1 for Number Item by row ( row size ) and 1 for column position - pDataRef = (hobj_ref_t *) MALLOC(2 * sizeof(hobj_ref_t)); - // Generate groupname ## pstGroupName = createGroupName(_pstDatasetName); @@ -1818,39 +1848,51 @@ int writeBooleanSparseMatrix(int _iFile, char *_pstDatasetName, int _iRows, int status = H5Gclose(group); if (status < 0) { + FREE(pstGroupName); return -1; } //Create each sub dataset and insert data pstRowPath = createPathName(pstGroupName, 0); - status = writeInterger32Matrix(_iFile, pstRowPath, 1, _iRows, _piNbItemRow); + status = writeInteger32Matrix(_iFile, pstRowPath, 1, _iRows, _piNbItemRow); if (status < 0) { + FREE(pstRowPath); + FREE(pstGroupName); return -1; } status = H5Rcreate(&pDataRef[0], _iFile, pstRowPath, H5R_OBJECT, -1); if (status < 0) { + FREE(pstRowPath); + FREE(pstGroupName); return -1; } pstColPath = createPathName(pstGroupName, 1); - status = writeInterger32Matrix(_iFile, pstColPath, 1, _iNbItem, _piColPos); + status = writeInteger32Matrix(_iFile, pstColPath, 1, _iNbItem, _piColPos); if (status < 0) { + FREE(pstRowPath); + FREE(pstColPath); + FREE(pstGroupName); return -1; } status = H5Rcreate(&pDataRef[1], _iFile, pstColPath, H5R_OBJECT, -1); if (status < 0) { + FREE(pstRowPath); + FREE(pstColPath); + FREE(pstGroupName); return -1; } //FREE group names FREE(pstRowPath); FREE(pstColPath); + FREE(pstGroupName); //Create dataspace. Setting maximum size to NULL sets the maximum size to be the current size. space = H5Screate_simple(1, dims, NULL); @@ -1910,9 +1952,6 @@ int writeBooleanSparseMatrix(int _iFile, char *_pstDatasetName, int _iRows, int return -1; } - FREE(pstGroupName); - FREE(pDataRef); - return 0; } diff --git a/scilab/modules/io/macros/load.sci b/scilab/modules/io/macros/load.sci index 4ef008b..5355011 100644 --- a/scilab/modules/io/macros/load.sci +++ b/scilab/modules/io/macros/load.sci @@ -728,7 +728,8 @@ function load(filename, varargin) loadFunction = %_load; end - variableList = listvarinfile(filename); + //multiple output variables to prevent listinfile prints + [variableList, __varB__, __varC__, __varD__] = listvarinfile(filename); // if size(varargin) <> 0 then -- 1.7.9.5