static bool isVarExist(int _iFile, char* _pstVarName) { //check if variable already exists int iNbItem = getVariableNames(_iFile, NULL); if (iNbItem) { char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem); iNbItem = getVariableNames(_iFile, pstVarNameList); //import all data for (int i = 0 ; i < iNbItem ; i++) { if (strcmp(pstVarNameList[i], _pstVarName) == 0) { return true; } FREE(pstVarNameList[i]); } FREE(pstVarNameList); } return false; }
void EigenSystem::buildSystemDoFIndices(SYSTEMTAG tag) { if (tag==ALL) { } else if (tag==EIGEN) { // build DoF indices for the eigen system _eigen_var_indices.clear(); _all_eigen_vars = getEigenVariableNames().size()==getVariableNames().size(); if (!_all_eigen_vars) { for (std::set<VariableName>::const_iterator it=getEigenVariableNames().begin(); it!=getEigenVariableNames().end(); it++) { unsigned int i = sys().variable_number(*it); sys().local_dof_indices(i, _eigen_var_indices); } } } }
int sci_hdf5_listvar_v2(char *fname, int* pvApiCtx) { SciErr sciErr; int *piAddr = NULL; char* pstFile = NULL; int iFile = 0; int iNbItem = 0; VarInfo* pInfo = NULL; const int nbIn = nbInputArgument(pvApiCtx); CheckInputArgument(pvApiCtx, 1, 1); CheckOutputArgument(pvApiCtx, 1, 4); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFile)) { if (pstFile) { FREE(pstFile); } Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1); return 1; } char* pstFileName = expandPathVariable(pstFile); iFile = openHDF5File(pstFileName, 0); if (iFile < 0) { Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFile); FREE(pstFileName); FREE(pstFile); return 1; } FREE(pstFileName); FREE(pstFile); //manage version information int iVersion = getSODFormatAttribute(iFile); if (iVersion != SOD_FILE_VERSION) { if (iVersion > SOD_FILE_VERSION) { //can't read file with version newer that me ! closeHDF5File(iFile); Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion); return 1; } else { //call older import functions and exit or ... EXIT ! if (iVersion == 1 || iVersion == -1) { //sciprint("old sci_listvar_in_hdf5_v1\n"); return sci_listvar_in_hdf5_v1(fname, pvApiCtx); } } } iNbItem = getVariableNames(iFile, NULL); if (iNbItem != 0) { char** pstVarNameList = (char**)MALLOC(sizeof(char*) * iNbItem); pInfo = (VarInfo*)MALLOC(iNbItem * sizeof(VarInfo)); int b; if (Lhs == 1) { sciprint("Name Type Size Bytes\n"); sciprint("---------------------------------------------------------------\n"); } iNbItem = getVariableNames(iFile, pstVarNameList); for (int i = 0; i < iNbItem; i++) { int iDataSetId = getDataSetIdFromName(iFile, pstVarNameList[i]); if (iDataSetId == 0) { break; } strncpy(pInfo[i].varName, pstVarNameList[i], sizeof(pInfo[i].varName) - 1); pInfo[i].iSize = 0; b = read_data(iDataSetId, 0, NULL, &pInfo[i]) == false; if (b) { break; } if (Lhs == 1) { sciprint("%s\n", pInfo[i].pstInfo); } } freeArrayOfString(pstVarNameList, iNbItem); } else { //no variable returms [] for each Lhs for (int i = 0 ; i < Lhs ; i++) { createEmptyMatrix(pvApiCtx, nbIn + i + 1); AssignOutputVariable(pvApiCtx, i + 1) = nbIn + i + 1; } ReturnArguments(pvApiCtx); return 0; } closeHDF5File(iFile); //1st Lhs char** pstVarName = (char**)MALLOC(sizeof(char*) * iNbItem); for (int i = 0 ; i < iNbItem ; i++) { pstVarName[i] = pInfo[i].varName; } sciErr = createMatrixOfString(pvApiCtx, nbIn + 1, iNbItem, 1, pstVarName); FREE(pstVarName); if (sciErr.iErr) { FREE(pInfo); printError(&sciErr, 0); return 1; } AssignOutputVariable(pvApiCtx, 1) = nbIn + 1; if (Lhs > 1) { //2nd Lhs double* pdblType; sciErr = allocMatrixOfDouble(pvApiCtx, nbIn + 2, iNbItem, 1, &pdblType); if (sciErr.iErr) { printError(&sciErr, 0); FREE(pInfo); return 1; } for (int i = 0 ; i < iNbItem ; i++) { pdblType[i] = pInfo[i].iType; } AssignOutputVariable(pvApiCtx, 2) = nbIn + 2; if (Lhs > 2) { //3rd Lhs int* pList = NULL; sciErr = createList(pvApiCtx, nbIn + 3, iNbItem, &pList); for (int i = 0 ; i < iNbItem ; i++) { double* pdblDims = NULL; allocMatrixOfDoubleInList(pvApiCtx, nbIn + 3, pList, i + 1, 1, pInfo[i].iDims, &pdblDims); for (int j = 0 ; j < pInfo[i].iDims ; j++) { pdblDims[j] = pInfo[i].piDims[j]; } } AssignOutputVariable(pvApiCtx, 3) = nbIn + 3; } if (Lhs > 3) { //4th Lhs double* pdblSize = NULL; sciErr = allocMatrixOfDouble(pvApiCtx, nbIn + 4, iNbItem, 1, &pdblSize); for (int i = 0 ; i < iNbItem ; i++) { pdblSize[i] = pInfo[i].iSize; } AssignOutputVariable(pvApiCtx, 4) = nbIn + 4; } } FREE(pInfo); ReturnArguments(pvApiCtx); return 0; }
int sci_hdf5_load_v2(char *fn, int* pvApiCtx) { SciErr sciErr; int* piAddr = NULL; char* pstFilename = NULL; char* pstExpandedFilename = NULL; bool bImport = true; const int nbIn = nbInputArgument(pvApiCtx); int iSelectedVar = nbIn - 1; CheckInputArgumentAtLeast(pvApiCtx , 1); CheckOutputArgument(pvApiCtx, 1, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFilename)) { if (pstFilename) { freeAllocatedSingleString(pstFilename); } Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), 2); return 1; } //open hdf5 file pstExpandedFilename = expandPathVariable(pstFilename); int iFile = openHDF5File(pstExpandedFilename, 0); if (iFile < 0) { Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), pstFilename); FREE(pstExpandedFilename); FREE(pstFilename); return 1; } FREE(pstExpandedFilename); FREE(pstFilename); //manage version information int iVersion = getSODFormatAttribute(iFile); if (iVersion != SOD_FILE_VERSION) { if (iVersion > SOD_FILE_VERSION) { //can't read file with version newer that me ! Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname.data(), SOD_FILE_VERSION, iVersion); return 1; } else { //call older import functions and exit or ... EXIT ! if (iVersion == 1 || iVersion == -1) { return sci_hdf5_load_v1(fn, pvApiCtx); } } } std::vector<wchar_t*> varList; if (iSelectedVar) { //selected variable char* pstVarName = NULL; for (int i = 0 ; i < iSelectedVar ; i++) { sciErr = getVarAddressFromPosition(pvApiCtx, i + 2, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddr, &pstVarName)) { if (pstVarName) { freeAllocatedSingleString(pstVarName); } Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), i + 1); return 1; } if (import_variable(pvApiCtx, iFile, pstVarName) == false) { FREE(pstVarName); bImport = false; break; } varList.push_back(to_wide_string(pstVarName)); FREE(pstVarName); pstVarName = NULL; } } else { //all variables int iNbItem = 0; iNbItem = getVariableNames(iFile, NULL); if (iNbItem != 0) { char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem); iNbItem = getVariableNames(iFile, pstVarNameList); //import all data for (int i = 0; i < iNbItem; i++) { if (import_variable(pvApiCtx, iFile, pstVarNameList[i]) == false) { bImport = false; break; } varList.push_back(to_wide_string(pstVarNameList[i])); } freeArrayOfString(pstVarNameList, iNbItem); } } //close the file closeHDF5File(iFile); if (bImport == true && varList.size() != 0) { createMatrixOfWideString(pvApiCtx, nbIn + 1, 1, static_cast<int>(varList.size()), varList.data()); } else { createEmptyMatrix(pvApiCtx, nbIn + 1); } for (auto & i : varList) { FREE(i); } AssignOutputVariable(pvApiCtx, 1) = nbIn + 1; ReturnArguments(pvApiCtx); // printf("End gateway !!!\n"); return 0; }
int sci_import_from_hdf5(char *fname, unsigned long fname_len) { SciErr sciErr; int* piAddr = NULL; char* pstFilename = NULL; char* pstExpandedFilename = NULL; bool bImport = true; int iSelectedVar = Rhs - 1; CheckInputArgumentAtLeast(pvApiCtx, 1); CheckOutputArgument(pvApiCtx, 1, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFilename)) { Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2); return 1; } //open hdf5 file pstExpandedFilename = expandPathVariable(pstFilename); int iFile = openHDF5File(pstExpandedFilename, 0); if (iFile < 0) { Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFilename); FREE(pstExpandedFilename); FREE(pstFilename); return 1; } FREE(pstExpandedFilename); FREE(pstFilename); //manage version information int iVersion = getSODFormatAttribute(iFile); if (iVersion != SOD_FILE_VERSION) { if (iVersion > SOD_FILE_VERSION) { //can't read file with version newer that me ! Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion); return 1; } else { //call older import functions and exit or ... EXIT ! if (iVersion == 1 || iVersion == -1) { //sciprint("old sci_import_from_hdf5_v1\n"); return sci_import_from_hdf5_v1(fname, fname_len); } } } if (iSelectedVar) { //selected variable char* pstVarName = NULL; for (int i = 0 ; i < iSelectedVar ; i++) { sciErr = getVarAddressFromPosition(pvApiCtx, i + 2, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddr, &pstVarName)) { Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, i + 1); return 1; } if (import_variable(iFile, pstVarName) == false) { bImport = false; break; } FREE(pstVarName); } } else { //all variables int iNbItem = 0; iNbItem = getVariableNames(iFile, NULL); if (iNbItem != 0) { char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem); iNbItem = getVariableNames(iFile, pstVarNameList); //import all data for (int i = 0; i < iNbItem; i++) { if (import_variable(iFile, pstVarNameList[i]) == false) { bImport = false; break; } } } } //close the file closeHDF5File(iFile); int *piReturn = NULL; sciErr = allocMatrixOfBoolean(pvApiCtx, Rhs + 1, 1, 1, &piReturn); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (bImport == true) { piReturn[0] = 1; } else { piReturn[0] = 0; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); // printf("End gateway !!!\n"); return 0; }
/*--------------------------------------------------------------------------*/ int sci_export_to_hdf5(char *fname, unsigned long fname_len) { int iNbVar = 0; int** piAddrList = NULL; char** pstNameList = NULL; char *pstFileName = NULL; bool bExport = false; bool bAppendMode = false; SciErr sciErr; CheckInputArgumentAtLeast(pvApiCtx, 1); CheckLhs(0, 1); pstNameList = (char**)MALLOC(sizeof(char*) * Rhs); iNbVar = extractVarNameList(1, Rhs, pstNameList); if (iNbVar == 0) { FREE(pstNameList); return 1; } piAddrList = (int**)MALLOC(sizeof(int*) * (iNbVar)); for (int i = 1 ; i < Rhs ; i++) { if (strcmp(pstNameList[i], "-append") == 0) { bAppendMode = true; } else { sciErr = getVarAddressFromName(pvApiCtx, pstNameList[i], &piAddrList[i]); if (sciErr.iErr) { Scierror(999, _("%s: Wrong value for input argument #%d: Defined variable expected.\n"), fname, i + 1); printError(&sciErr, 0); return 1; } } } //check append option iLevel = 0; // open hdf5 file pstFileName = expandPathVariable(pstNameList[0]); int iH5File = 0; if (bAppendMode) { iH5File = openHDF5File(pstFileName, bAppendMode); if (iH5File < 0) { iH5File = createHDF5File(pstFileName); } } else { iH5File = createHDF5File(pstFileName); } if (iH5File < 0) { FREE(pstFileName); if (iH5File == -2) { Scierror(999, _("%s: Wrong value for input argument #%d: \"%s\" is a directory"), fname, 1, pstNameList[0]); } else { Scierror(999, _("%s: Cannot open file %s.\n"), fname, pstNameList[0]); } return 1; } if (bAppendMode) { int iVersion = getSODFormatAttribute(iH5File); if (iVersion != -1 && iVersion != SOD_FILE_VERSION) { //to update version must be the same closeHDF5File(iH5File); Scierror(999, _("%s: Wrong SOD file format version. Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion); return 1; } //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 = 1 ; j < Rhs ; j++) { if (strcmp(pstNameList[i], "-append") == 0) { continue; } if (strcmp(pstVarNameList[i], pstNameList[j]) == 0) { closeHDF5File(iH5File); 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 = 1 ; i < Rhs ; i++) { if (strcmp(pstNameList[i], "-append") == 0) { continue; } bExport = export_data(iH5File, piAddrList[i], pstNameList[i]); if (bExport == false) { break; } } if (bExport && Rhs != 1) { //add or update scilab version and file version in hdf5 file if (updateScilabVersion(iH5File) < 0) { closeHDF5File(iH5File); Scierror(999, _("%s: Unable to update Scilab version in \"%s\"."), fname, pstNameList[0]); return 1; } if (updateFileVersion(iH5File) < 0) { closeHDF5File(iH5File); Scierror(999, _("%s: Unable to update HDF5 format version in \"%s\"."), fname, pstNameList[0]); return 1; } } //close hdf5 file closeHDF5File(iH5File); if (bExport == false && Rhs != 1) { //remove file deleteafile(pstFileName); } FREE(pstFileName); //create boolean return value int *piReturn = NULL; sciErr = allocMatrixOfBoolean(pvApiCtx, Rhs + 1, 1, 1, &piReturn); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (bExport == true || Rhs == 1) { piReturn[0] = 1; } else { 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; }