/*--------------------------------------------------------------------------*/ SciErr getDimFromNamedVar(void *_pvCtx, const char *_pstName, int *_piVal) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int *piAddr = NULL; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_NAMED_DIMFROMVAR, _("%s: Unable to get dimension from variable \"%s\""), "getDimFromNamedVar", _pstName); return sciErr; } sciErr = getDimFromVar(_pvCtx, piAddr, _piVal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_NAMED_DIMFROMVAR, _("%s: Unable to get dimension from variable \"%s\""), "getDimFromNamedVar", _pstName); return sciErr; } return sciErr; }
SciErr readCommonNamedMatrixOfPoly(void* _pvCtx, const char* _pstName, int _iComplex, int* _piRows, int* _piCols, int* _piNbCoef, double** _pdblReal, double** _pdblImg) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piAddr = NULL; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_POLY, _("%s: Unable to get variable \"%s\""), _iComplex ? "readNamedComplexMatrixOfPoly" : "readNamedMatrixOfPoly", _pstName); return sciErr; } if (_iComplex == 1) { sciErr = getComplexMatrixOfPoly(_pvCtx, piAddr, _piRows, _piCols, _piNbCoef, _pdblReal, _pdblImg); } else { sciErr = getMatrixOfPoly(_pvCtx, piAddr, _piRows, _piCols, _piNbCoef, _pdblReal); } if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_POLY, _("%s: Unable to get variable \"%s\""), _iComplex ? "readNamedComplexMatrixOfPoly" : "readNamedMatrixOfPoly", _pstName); return sciErr; } return sciErr; }
SciErr readNamedPointer(void* _pvCtx, const char* _pstName, void** _pvPtr) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piAddr = NULL; void *pvPtr = NULL; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_POINTER, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfBoolean", _pstName); return sciErr; } sciErr = getPointer(_pvCtx, piAddr, &pvPtr); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_POINTER, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfBoolean", _pstName); return sciErr; } *_pvPtr = pvPtr; return sciErr; }
/*--------------------------------------------------------------------------*/ int isNamedVarExist(void *_pvCtx, const char *_pstName) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iVarID[nsiz]; int *piAddr = NULL; int funs = C2F(com).fun; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr || piAddr == NULL) { Fin = -1; C2F(str2name)(_pstName, iVarID, (int)strlen(_pstName)); C2F(funs)(iVarID); if (Fin > 0) { Fin = 1; C2F(com).fun = funs; return 1; } return 0; } return 1; }
SciErr readNamedMatrixOfBoolean(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piBool) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piAddr = NULL; int* piBool = NULL; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_BOOLEAN, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfBoolean", _pstName); return sciErr; } sciErr = getMatrixOfBoolean(_pvCtx, piAddr, _piRows, _piCols, &piBool); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_BOOLEAN, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfBoolean", _pstName); return sciErr; } if(_piBool) { memcpy(_piBool, piBool, sizeof(int) * *_piRows * *_piCols); } return sciErr; }
/*--------------------------------------------------------------------------*/ static int getnumberoflibraries(void) { int nbrlibraries = 0; int Lused = 0; int Ltotal = 0; int j = 0; C2F(getvariablesinfo)(&Ltotal, &Lused); for (j = 1; j < Lused + 1; ++j) { char *NameVariable = getLocalNamefromId(j); int * header = NULL; SciErr sciErr = getVarAddressFromName(pvApiCtx, NameVariable, &header); if (!sciErr.iErr) { if (header && (header[0] == sci_lib)) { nbrlibraries++; } } if (NameVariable) { FREE(NameVariable); NameVariable = NULL; } } return nbrlibraries; }
/*--------------------------------------------------------------------------*/ int isNamedVarMatrixType(void *_pvCtx, const char *_pstName) { int *piAddr = NULL; SciErr sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr) { return 0; } return isVarMatrixType(_pvCtx, piAddr); }
/*--------------------------------------------------------------------------*/ int isNamedVarComplex(void *_pvCtx, const char *_pstName) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int *piAddr = NULL; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr) { return 0; } return isVarComplex(_pvCtx, piAddr); }
/*--------------------------------------------------------------------------*/ BOOL isScilabMacroVariable(const char * functionName) { if (functionName) { int *piAddr = NULL; SciErr sciErr = getVarAddressFromName(pvApiCtx, functionName, &piAddr); if (sciErr.iErr == 0) { int typeVariable = 0; sciErr = getVarType(pvApiCtx, piAddr, &typeVariable); if (sciErr.iErr == 0) { return (BOOL)(typeVariable == sci_c_function); } } } return FALSE; }
/*--------------------------------------------------------------------------*/ SciErr getNamedVarType(void *_pvCtx, const char *_pstName, int *_piType) { int *piAddr = NULL; SciErr sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_NAMED_UNDEFINED_VAR, _("%s: Unable to get variable \"%s\""), "getNamedVarType", _pstName); return sciErr; } sciErr = getVarType(_pvCtx, piAddr, _piType); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_NAMED_TYPE, _("%s: Unable to get type of variable \"%s\""), "getNamedVarType", _pstName); return sciErr; } return sciErr; }
/*--------------------------------------------------------------------------*/ SciErr readNamedMatrixOfWideString(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piwLength, wchar_t** _pwstStrings) { int* piAddr = NULL; SciErr sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_WIDE_STRING, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfWideString", _pstName); return sciErr; } sciErr = getMatrixOfWideString(_pvCtx, piAddr, _piRows, _piCols, _piwLength, _pwstStrings); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_WIDE_STRING, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfWideString", _pstName); return sciErr; } return sciErr; }
/*--------------------------------------------------------------------------*/ SciErr getNamedVarDimension(void *_pvCtx, const char *_pstName, int *_piRows, int *_piCols) { SciErr sciErr = sciErrInit(); int *piAddr = NULL; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_NAMED_VARDIM, _("%s: Unable to get dimension of variable \"%s\""), "getNamedVarDimension", _pstName); return sciErr; } sciErr = getVarDimension(_pvCtx, piAddr, _piRows, _piCols); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_NAMED_VARDIM, _("%s: Unable to get dimension of variable \"%s\""), "getNamedVarDimension", _pstName); return sciErr; } return sciErr; }
SciErr readCommonNamedMatrixOfDouble(void* _pvCtx, const char* _pstName, int _iComplex, int* _piRows, int* _piCols, double* _pdblReal, double* _pdblImg) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piAddr = NULL; double* pdblReal = NULL; double* pdblImg = NULL; int iSize = 0; int iOne = 1; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_DOUBLE, _("%s: Unable to get variable \"%s\""), _iComplex ? "readNamedComplexMatrixOfDouble" : "readNamedMatrixOfDouble", _pstName); return sciErr; } sciErr = getCommonMatrixOfDouble(_pvCtx, piAddr, _iComplex, _piRows, _piCols, &pdblReal, &pdblImg); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_DOUBLE, _("%s: Unable to get variable \"%s\""), _iComplex ? "readNamedComplexMatrixOfDouble" : "readNamedMatrixOfDouble", _pstName); return sciErr; } if(_pdblReal == NULL || (_iComplex && _pdblImg == NULL)) { return sciErr; } iSize = (*_piRows) * (*_piCols); /* dcopy source to dest */ C2F(dcopy)(&iSize, pdblReal, &iOne, _pdblReal, &iOne); if(_iComplex) { C2F(dcopy)(&iSize, pdblImg, &iOne, _pdblImg, &iOne); } return sciErr; }
SciErr readNamedBooleanSparseMatrix(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piNbItem, int* _piNbItemRow, int* _piColPos) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piAddr = NULL; int* piNbItemRow = 0; int* piColPos = 0; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_BOOLEAN_SPARSE, _("%s: Unable to get variable \"%s\""), "readNamedBooleanSparseMatrix", _pstName); return sciErr; } sciErr = getBooleanSparseMatrix(_pvCtx, piAddr, _piRows, _piCols, _piNbItem, &piNbItemRow, &piColPos); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_BOOLEAN_SPARSE, _("API_ERROR_READ_NAMED_BOOLEAN_SPARSE")); return sciErr; } if(_piNbItemRow == NULL) { return sciErr; } memcpy(_piNbItemRow, piNbItemRow, *_piRows * sizeof(int)); if(_piColPos == NULL) { return sciErr; } memcpy(_piColPos, piColPos, *_piNbItem * sizeof(int)); return sciErr; }
SciErr readCommonNamedSparseMatrix(void* _pvCtx, const char* _pstName, int _iComplex, int* _piRows, int* _piCols, int* _piNbItem, int* _piNbItemRow, int* _piColPos, double* _pdblReal, double* _pdblImg) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piAddr = NULL; int* piNbItemRow = 0; int* piColPos = 0; int iOne = 1; double* pdblReal = NULL; double* pdblImg = NULL; sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_SPARSE, _("%s: Unable to get variable \"%s\""), _iComplex ? "readNamedComplexSparseMatrix" : "readNamedSparseMatrix", _pstName); return sciErr; } if(_iComplex == 1) { sciErr = getComplexSparseMatrix(_pvCtx, piAddr, _piRows, _piCols, _piNbItem, &piNbItemRow, &piColPos, &pdblReal, &pdblImg); } else { sciErr = getSparseMatrix(_pvCtx, piAddr, _piRows, _piCols, _piNbItem, &piNbItemRow, &piColPos, &pdblReal); } if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_READ_NAMED_SPARSE, _("%s: Unable to get variable \"%s\""), _iComplex ? "readNamedComplexSparseMatrix" : "readNamedSparseMatrix", _pstName); return sciErr; } if(_piNbItemRow == NULL) { return sciErr; } memcpy(_piNbItemRow, piNbItemRow, *_piRows * sizeof(int)); if(_piColPos == NULL) { return sciErr; } memcpy(_piColPos, piColPos, *_piNbItem * sizeof(int)); if(_pdblReal == NULL) { return sciErr; } C2F(dcopy)(_piNbItem, pdblReal, &iOne, _pdblReal, &iOne); if(_iComplex && _pdblImg) { C2F(dcopy)(_piNbItem, pdblImg, &iOne, _pdblImg, &iOne); } return sciErr; }
void ScilabObjects::removeVar(int * addr, void * pvApiCtx) { SciErr err; int type, row, col, * id; err = getVarType(pvApiCtx, addr, &type); if (err.iErr) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (type == sci_mlist && (isExternalObjOrClass(addr, pvApiCtx))) { err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id); if (err.iErr) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } int envId = getEnvironmentId(addr, pvApiCtx); ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId); env.removeobject(*id); } else if (type == sci_strings) { char * varName = 0; if (getAllocatedSingleString(pvApiCtx, addr, &varName)) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } err = getVarAddressFromName(pvApiCtx, varName, &addr); if (err.iErr) { freeAllocatedSingleString(varName); return; } err = getVarType(pvApiCtx, addr, &type); if (err.iErr) { freeAllocatedSingleString(varName); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (type == sci_mlist && isExternalObjOrClass(addr, pvApiCtx)) { err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id); if (err.iErr) { freeAllocatedSingleString(varName); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } int envId = getEnvironmentId(addr, pvApiCtx); ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId); env.removeobject(*id); deleteNamedVariable(pvApiCtx, varName); freeAllocatedSingleString(varName); } } }
int ScilabGateway::wrapAsRef(char * fname, const int envId, void * pvApiCtx) { SciErr err; int * tmpvar = 0; int * addr = 0; char * varName = 0; int idObj; if (Rhs == 0) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong number of arguments : more than 1 argument expected.")); } ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId); ScilabGatewayOptions & options = env.getGatewayOptions(); OptionsHelper::setCopyOccurred(false); ScilabObjects::initialization(env, pvApiCtx); options.setIsNew(false); CheckOutputArgument(pvApiCtx, Rhs, Rhs); tmpvar = new int[Rhs + 1]; *tmpvar = 0; for (int i = 1; i < Rhs + 1; i++) { err = getVarAddressFromPosition(pvApiCtx, i, &addr); if (err.iErr) { ScilabObjects::removeTemporaryVars(envId, tmpvar); delete[] tmpvar; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (!isScalar(pvApiCtx, addr) || !isStringType(pvApiCtx, addr)) { ScilabObjects::removeTemporaryVars(envId, tmpvar); delete[] tmpvar; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Can only wrap as a reference to a named variable")); } if (getAllocatedSingleString(pvApiCtx, addr, &varName)) { ScilabObjects::removeTemporaryVars(envId, tmpvar); delete[] tmpvar; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } err = getVarAddressFromName(pvApiCtx, varName, &addr); freeAllocatedSingleString(varName); if (err.iErr) { ScilabObjects::removeTemporaryVars(envId, tmpvar); delete[] tmpvar; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } try { idObj = ScilabObjects::getArgumentId(addr, tmpvar, true, false, envId, pvApiCtx); } catch (ScilabAbstractEnvironmentException & /*e*/) { delete[] tmpvar; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot wrap argument %d."), i); } try { ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + i, idObj, envId, pvApiCtx); } catch (ScilabAbstractEnvironmentException & /*e*/) { ScilabObjects::removeTemporaryVars(envId, tmpvar); delete[] tmpvar; throw; } LhsVar(i) = Rhs + i; } // We don't remove tmpvar since it contains id which are put on rhs delete[] tmpvar; PutLhsVar(); return 0; }
/*--------------------------------------------------------------------------*/ int sci_save(char *fname, unsigned long fname_len) { SciErr sciErr; int iOldSave = FALSE; int* piAddr1 = NULL; int iType1 = 0; CheckRhs(1, 100000); CheckLhs(0, 1); //filename or file descriptor sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } sciErr = getVarType(pvApiCtx, piAddr1, &iType1); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (iType1 == sci_strings) { int* piAddrI = NULL; int* piAddrI2 = NULL; int iTypeI = 0; int iRowsI = 0; int iColsI = 0; char* pstVarI = NULL; if (Rhs > 1) { int i = 0; for (i = 2 ; i <= Rhs ; i++) { sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddrI); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } sciErr = getVarType(pvApiCtx, piAddrI, &iTypeI); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (iTypeI != sci_strings) { iOldSave = TRUE; break; } sciErr = getVarDimension(pvApiCtx, piAddrI, &iRowsI, &iColsI); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (iRowsI != 1 || iColsI != 1) { iOldSave = TRUE; break; } if (getAllocatedSingleString(pvApiCtx, piAddrI, &pstVarI)) { return 1; } if (strcmp(pstVarI, "-append") != 0) { //try to get variable by name sciErr = getVarAddressFromName(pvApiCtx, pstVarI, &piAddrI2); if (sciErr.iErr) { // Try old save because here the input variable can be of type "string" but not a variable name // Ex: a=""; save(filename, a); iOldSave = TRUE; break; } if (piAddrI2 == 0) { iOldSave = TRUE; break; } } freeAllocatedSingleString(pstVarI); } } else { iOldSave = FALSE; } } else { iOldSave = TRUE; } //new save to sod format if (iOldSave == FALSE) { int lw = 0; //call "overload" to prepare data to export_to_hdf5 function. C2F(overload) (&lw, "save", (unsigned long)strlen("save")); } //old save if (iOldSave) { //show warning only for variable save, not for environment. if (getWarningMode() && Rhs > 1) { sciprint(_("%s: Scilab 6 will not support the file format used.\n"), _("Warning")); sciprint(_("%s: Please quote the variable declaration. Example, save('myData.sod',a) becomes save('myData.sod','a').\n"), _("Warning")); sciprint(_("%s: See help('save') for the rational.\n"), _("Warning")); } C2F(intsave)(); } 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; int iRhs = nbInputArgument(pvApiCtx); CheckInputArgumentAtLeast(pvApiCtx, 1); CheckOutputArgument(pvApiCtx, 0, 1); pstNameList = (char**)MALLOC(sizeof(char*) * iRhs); iNbVar = extractVarNameList(1, iRhs, pstNameList); if (iNbVar == 0) { FREE(pstNameList); return 1; } piAddrList = (int**)MALLOC(sizeof(int*) * (iNbVar)); for (int i = 1 ; i < iRhs ; 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; } } } 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; } } // export data for (int i = 1 ; i < iRhs ; i++) { if (strcmp(pstNameList[i], "-append") == 0) { continue; } if (isVarExist(iH5File, pstNameList[i])) { if (bAppendMode) { if (deleteHDF5Var(iH5File, pstNameList[i])) { closeHDF5File(iH5File); Scierror(999, _("%s: Unable to delete existing variable \"%s\"."), fname, pstNameList[i]); return 1; } } else { closeHDF5File(iH5File); Scierror(999, _("%s: Variable \'%s\' already exists in file \'%s\'\nUse -append option to replace existing variable\n."), fname, pstNameList[i], pstNameList[0]); return 1; } } bExport = export_data(iH5File, piAddrList[i], pstNameList[i]); if (bExport == false) { break; } } if (bExport && iRhs != 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); //delete file in case of error but nor in append mode if (bExport == false && bAppendMode == false && iRhs != 1) { //remove file deleteafile(pstFileName); } FREE(pstFileName); //create boolean return value int *piReturn = NULL; sciErr = allocMatrixOfBoolean(pvApiCtx, iRhs + 1, 1, 1, &piReturn); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (bExport == true || iRhs == 1) { piReturn[0] = 1; } else { piReturn[0] = 0; } //free memory for (int i = 0 ; i < iRhs ; i++) { FREE(pstNameList[i]); } FREE(pstNameList); FREE(piAddrList); LhsVar(1) = iRhs + 1; PutLhsVar(); return 0; }
int ScilabGateway::import(char * fname, const int envId, void * pvApiCtx) { SciErr err; int rows, cols; char ** className = 0; std::string * name = 0; int named = 1; int * addr = 0; int ret = 0; int nbArgs = Rhs; int error = 0; char * cwd = 0; CheckInputArgumentAtLeast(pvApiCtx, 1); ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId); OptionsHelper & helper = env.getOptionsHelper(); ScilabGatewayOptions & options = env.getGatewayOptions(); OptionsHelper::setCopyOccurred(false); ScilabObjects::initialization(env, pvApiCtx); options.setIsNew(false); err = getVarAddressFromPosition(pvApiCtx, Rhs, &addr); if (err.iErr) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (isBooleanType(pvApiCtx, addr)) { nbArgs = Rhs - 1; if (getScalarBoolean(pvApiCtx, addr, &named)) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } } if (nbArgs == 1) { err = getVarAddressFromPosition(pvApiCtx, 1, &addr); if (err.iErr) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (!isStringType(pvApiCtx, addr)) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), 1); } if (getAllocatedMatrixOfString(pvApiCtx, addr, &rows, &cols, &className)) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } } else { className = (char**)MALLOC(sizeof(char *) * nbArgs); for (int i = 1; i <= nbArgs; i++) { err = getVarAddressFromPosition(pvApiCtx, i, &addr); if (err.iErr) { freeAllocatedMatrixOfString(1, i, className); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (!isStringType(pvApiCtx, addr)) { freeAllocatedMatrixOfString(1, i, className); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), i); } if (getAllocatedSingleString(pvApiCtx, addr, &(className[i - 1]))) { freeAllocatedMatrixOfString(1, i, className); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } } rows = 1; cols = nbArgs; } if (named) { name = new std::string[rows * cols]; for (int i = 0; i < rows * cols; i++) { name[i] = std::string(className[i]); if (helper.getUseLastName()) { std::size_t pos = name[i].find_last_of('.'); if (pos != std::string::npos) { if (pos == name[i].size() - 1) { freeAllocatedMatrixOfString(rows, cols, className); delete[] name; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("The class name cannot end with a dot.")); } name[i] = name[i].substr(pos + 1); } } else { std::size_t pos = name[i].find_first_of('.'); if (pos != std::string::npos) { if (pos == 0) { freeAllocatedMatrixOfString(rows, cols, className); delete[] name; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("The class name cannot start with a dot.")); } name[i] = name[i].substr(0, pos); } } if (isNamedVarExist(pvApiCtx, name[i].c_str())) { addr = 0; err = getVarAddressFromName(pvApiCtx, name[i].c_str(), &addr); if (err.iErr || addr == 0 || !ScilabObjects::isValidExternal(addr, pvApiCtx) || ScilabObjects::getEnvironmentId(addr, pvApiCtx) != envId) { freeAllocatedMatrixOfString(rows, cols, className); delete[] name; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("A variable with this name is already existing")); } } } } if (!named && rows * cols != Lhs) { freeAllocatedMatrixOfString(rows, cols, className); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Bad number of output arguments"), __FILE__, __LINE__); } const int type = helper.getNewAllowed() ? EXTERNAL_CLASS : EXTERNAL_OBJECT; cwd = scigetcwd(&error); if (error) { FREE(cwd); cwd = 0; } for (int i = 0; i < rows * cols; i++) { try { ret = env.loadclass(className[i], cwd, static_cast<bool>(named), helper.getAllowReload()); } catch (std::exception & e) { FREE(cwd); freeAllocatedMatrixOfString(rows, cols, className); if (named) { delete[] name; } throw; } if (named) { try { ScilabObjects::createNamedEnvironmentObject(type, name[i].c_str(), ret, envId, pvApiCtx); } catch (ScilabAbstractEnvironmentException & e) { FREE(cwd); freeAllocatedMatrixOfString(rows, cols, className); delete[] name; throw; } } else { try { ScilabObjects::createEnvironmentObjectAtPos(type, Rhs + i + 1, ret, envId, pvApiCtx); } catch (ScilabAbstractEnvironmentException & e) { FREE(cwd); freeAllocatedMatrixOfString(rows, cols, className); env.removeobject(ret); throw; } LhsVar(i + 1) = Rhs + i + 1; } } FREE(cwd); freeAllocatedMatrixOfString(rows, cols, className); if (named) { delete[] name; LhsVar(1) = 0; } PutLhsVar(); return 0; }