SciErr createMatrixOfBoolean(void* _pvCtx, int _iVar, int _iRows, int _iCols, const int* _piBool) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piBool = NULL; if(_iRows == 0 && _iCols == 0) { double dblReal = 0; sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } sciErr = allocMatrixOfBoolean(_pvCtx, _iVar, _iRows, _iCols, &piBool); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_BOOLEAN, _("%s: Unable to create variable in Scilab memory"), "createMatrixOfBoolean"); return sciErr; } memcpy(piBool, _piBool, sizeof(int) * _iRows * _iCols); return sciErr; }
/*--------------------------------------------------------------------------*/ int createScalarBoolean(void* _pvCtx, int _iVar, int _iBool) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piBool = NULL; sciErr = allocMatrixOfBoolean(_pvCtx, _iVar, 1, 1, &piBool); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_SCALAR_BOOLEAN, _("%s: Unable to create variable in Scilab memory"), "createScalarBoolean"); printError(&sciErr, 0); return sciErr.iErr; } piBool[0] = _iBool; return 0; }
int write_boolean(char *fname, unsigned long fname_len) { SciErr sciErr; int i, j; //first variable info : boolean matrix of boolean 3 x 4 int iRows1 = 3; int iCols1 = 4; int* pboolOutput1 = NULL; //second variable info : boolean matrix of boolean 4 x 6 int iRows2 = 4; int iCols2 = 6; int* pboolOutput2 = NULL; /************************ * First variable * ************************/ //alloc array of data in OS memory pboolOutput1 = (int*)malloc(sizeof(int) * iRows1 * iCols1); //fill array with incremental values //[ %t %f %t %f // %t %f %t %f // %t %f %t %f ] for (i = 0 ; i < iRows1 ; i++) { for (j = 0 ; j < iCols1 ; j++) { pboolOutput1[i + iRows1 * j] = (i * iCols1 + j) % 2 == FALSE; } } //can be written in a single loop //for(i = 0 ; i < iRows1 * iCols1; i++) //{ // pboolOutput1[i] = i % 2 == FALSE; //} //create a variable from a existing data array sciErr = createMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iRows1, iCols1, pboolOutput1); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //after creation, we can free memory. free(pboolOutput1); /************************* * Second variable * *************************/ //reserve space in scilab memory and fill it sciErr = allocMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 2, iRows2, iCols2, &pboolOutput2); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //fill array with incremental values //[ %f %t %f %t %f %t // %f %t %f %t %f %t // %f %t %f %t %f %t // %f %t %f %t %f %t ] for (i = 0 ; i < iRows2 ; i++) { for (j = 0 ; j < iCols2 ; j++) { pboolOutput2[i + iRows2 * j] = (i * iCols2 + j) % 2 == TRUE; } } //can be written in a single loop //for(i = 0 ; i < iRows2 * iCols2; i++) //{ // pboolOutput2[i] = i % 2 == TRUE; //} // /!\ DO NOT FREE MEMORY, in this case, it's the Scilab memory //assign allocated variables to Lhs position AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; return 0; }
/*--------------------------------------------------------------------------*/ int sci_uigetfont(char *fname, void* pvApiCtx) { SciErr sciErr; int* piAddrfontNameAdr = NULL; int* piAddrfontSizeAdr = NULL; int* piAddrboldAdr = NULL; int* boldAdr = NULL; int* piAddritalicAdr = NULL; int* italicAdr = NULL; double* fontSizeAdr = NULL; int fontChooserID = 0; int nbRow = 0; int nbCol = 0; char **fontNameAdr = NULL; int fontNameSize = 0; char *selectedFontName = NULL; int selectedFontSize = 0; BOOL selectedBold = FALSE; BOOL selectedItalic = FALSE; CheckInputArgument(pvApiCtx, 0, 4); CheckOutputArgument(pvApiCtx, 1, 4); /* Default font name */ if (nbInputArgument(pvApiCtx) >= 1) { if ((checkInputArgumentType(pvApiCtx, 1, sci_strings))) { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrfontNameAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of string at position 1. if (getAllocatedMatrixOfString(pvApiCtx, piAddrfontNameAdr, &nbRow, &nbCol, &fontNameAdr)) { Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 1); return 1; } fontNameSize = nbRow * nbCol; if (fontNameSize != 1) { freeAllocatedMatrixOfString(nbRow, nbCol, fontNameAdr); Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname, 1); return FALSE; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1); return FALSE; } } /* Default font size */ if (nbInputArgument(pvApiCtx) >= 2) { if ((checkInputArgumentType(pvApiCtx, 2, sci_matrix))) { sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrfontSizeAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 2. sciErr = getMatrixOfDouble(pvApiCtx, piAddrfontSizeAdr, &nbRow, &nbCol, &fontSizeAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 2); return 1; } if (nbRow * nbCol != 1) { freeAllocatedMatrixOfString(nbRow, nbCol, fontNameAdr); Scierror(999, _("%s: Wrong size for input argument #%d: A real expected.\n"), fname, 2); return FALSE; } } else { freeAllocatedMatrixOfString(nbRow, nbCol, fontNameAdr); Scierror(999, _("%s: Wrong type for input argument #%d: A real expected.\n"), fname, 2); return FALSE; } } /* Is the default font bold ? */ if (nbInputArgument(pvApiCtx) >= 3) { if ((checkInputArgumentType(pvApiCtx, 3, sci_boolean))) { sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddrboldAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of boolean at position 3. sciErr = getMatrixOfBoolean(pvApiCtx, piAddrboldAdr, &nbRow, &nbCol, &boldAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: Boolean matrix expected.\n"), fname, 3); return 1; } if (nbRow * nbCol != 1) { freeAllocatedMatrixOfString(nbRow, nbCol, fontNameAdr); Scierror(999, _("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 3); return FALSE; } } else { freeAllocatedMatrixOfString(nbRow, nbCol, fontNameAdr); Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 3); return FALSE; } } /* Is the default font italic ? */ if (nbInputArgument(pvApiCtx) >= 4) { if ((checkInputArgumentType(pvApiCtx, 4, sci_boolean))) { sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddritalicAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of boolean at position 4. sciErr = getMatrixOfBoolean(pvApiCtx, piAddritalicAdr, &nbRow, &nbCol, &italicAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: Boolean matrix expected.\n"), fname, 4); return 1; } if (nbRow * nbCol != 1) { freeAllocatedMatrixOfString(nbRow, nbCol, fontNameAdr); Scierror(999, _("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 4); return FALSE; } } else { freeAllocatedMatrixOfString(nbRow, nbCol, fontNameAdr); Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 4); return FALSE; } } /* Create the Java Object */ fontChooserID = createFontChooser(); /* Default font */ if (fontNameAdr != NULL) { setFontChooserFontName(fontChooserID, fontNameAdr[0]); } /* Default size */ if (fontSizeAdr != 0) { setFontChooserFontSize(fontChooserID, (int)fontSizeAdr[0]); } /* Default bold */ if (boldAdr != 0) { setFontChooserBold(fontChooserID, booltoBOOL(boldAdr[0])); } /* Default italic */ if (italicAdr != 0) { setFontChooserItalic(fontChooserID, booltoBOOL(italicAdr[0])); } /* Display it and wait for a user input */ fontChooserDisplayAndWait(fontChooserID); /* Return the selected font */ /* Read the user answer */ selectedFontName = getFontChooserFontName(fontChooserID); if (strcmp(selectedFontName, "")) /* The user selected a font */ { selectedFontSize = getFontChooserFontSize(fontChooserID); selectedBold = getFontChooserBold(fontChooserID); selectedItalic = getFontChooserItalic(fontChooserID); nbRow = 1; nbCol = 1; if (nbOutputArgument(pvApiCtx) >= 1) { sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow, nbCol, (const char * const*) &selectedFontName); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } } if (selectedFontName) { freeAllocatedSingleString(selectedFontName); } if (nbOutputArgument(pvApiCtx) >= 2) { sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, nbRow, nbCol, &fontSizeAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } *fontSizeAdr = selectedFontSize; } if (nbOutputArgument(pvApiCtx) >= 3) { sciErr = allocMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 3, nbRow, nbCol, &boldAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } *boldAdr = selectedBold; } if (nbOutputArgument(pvApiCtx) >= 4) { sciErr = allocMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 4, nbRow, nbCol, &italicAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } *italicAdr = selectedItalic; } } else /* The user canceled */ { if (selectedFontName) { freeAllocatedSingleString(selectedFontName); } nbRow = 0; nbCol = 0; if (nbOutputArgument(pvApiCtx) >= 1) { /* Return "" as font name */ char* fontNameEmpty = NULL; if (allocSingleString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow * nbCol, (const char**) &fontNameEmpty)) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } } if (nbOutputArgument(pvApiCtx) >= 2) { /* Return [] as font size */ sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, nbRow, nbCol, &fontSizeAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } } if (nbOutputArgument(pvApiCtx) >= 3) { /* Return [] as bold value */ sciErr = allocMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 3, nbRow, nbCol, &boldAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } } if (nbOutputArgument(pvApiCtx) >= 4) { /* Return [] as italic value */ sciErr = allocMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 4, nbRow, nbCol, &italicAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } } } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3; AssignOutputVariable(pvApiCtx, 4) = nbInputArgument(pvApiCtx) + 4; if (fontNameSize) { freeAllocatedMatrixOfString(nbRow, nbCol, fontNameAdr); } ReturnArguments(pvApiCtx); return TRUE; }
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; 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; }