SciErr getHypermatType(void *_pvCtx, int *_piAddress, int *_piType) { return getVarType(_pvCtx, _piAddress, _piType); }
/*--------------------------------------------------------------------------*/ SciErr getProcessMode(void *_pvCtx, int _iPos, int *_piAddRef, int *_piMode) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iRows1 = 0; int iCols1 = 0; int iRows2 = 0; int iCols2 = 0; int iType2 = 0; int iMode = 0; int *piAddr2 = NULL; sciErr = getVarDimension(_pvCtx, _piAddRef, &iRows1, &iCols1); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument dimension"), "getProcessMode"); return sciErr; } //sciprint("getProcessMode ->"); sciErr = getinternalVarAddress(_pvCtx, _iPos, &piAddr2); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get variable address"), "getProcessMode"); return sciErr; } sciErr = getVarType(_pvCtx, piAddr2, &iType2); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument type"), "getProcessMode"); return sciErr; } if (iType2 == sci_matrix && !isVarComplex(_pvCtx, piAddr2)) { double *pdblReal2 = NULL; sciErr = getMatrixOfDouble(_pvCtx, piAddr2, &iRows2, &iCols2, &pdblReal2); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument data"), "getProcessMode"); return sciErr; } if (iRows2 != 1 || iCols2 != 1) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Wrong size for argument %d: (%d,%d) expected.\n"), "getProcessMode", _iPos, 1, 1); return sciErr; } iMode = (int)pdblReal2[0]; } else if (iType2 == sci_strings) { int iLen = 0; char *pstMode[1] = { "" }; sciErr = getVarDimension(_pvCtx, piAddr2, &iRows2, &iCols2); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument dimension"), "getProcessMode"); return sciErr; } if (iRows2 != 1 || iCols2 != 1) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Wrong size for argument %d: (%d,%d) expected.\n"), "getProcessMode", _iPos, 1, 1); return sciErr; } sciErr = getMatrixOfString(_pvCtx, piAddr2, &iRows2, &iCols2, &iLen, NULL); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument data"), "getProcessMode"); return sciErr; } pstMode[0] = (char *)MALLOC(sizeof(char) * (iLen + 1)); //+1 for null termination sciErr = getMatrixOfString(_pvCtx, piAddr2, &iRows2, &iCols2, &iLen, pstMode); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument data"), "getProcessMode"); return sciErr; } iMode = (int)pstMode[0][0]; FREE(pstMode[0]); } else { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Wrong type for input argument #%d: A string or a scalar expected.\n"), "getProcessMode", _iPos); return sciErr; } if (iMode == ROW_LETTER || iMode == BY_ROWS) { *_piMode = BY_ROWS; } else if (iMode == COL_LETTER || iMode == BY_COLS) { *_piMode = BY_COLS; } else if (iMode == STAR_LETTER || iMode == BY_ALL) { *_piMode = BY_ALL; } else if (iMode == MTLB_LETTER || iMode == BY_MTLB) { *_piMode = 0; if (iRows1 > 1) { *_piMode = 1; } else if (iCols1 > 1) { *_piMode = 2; } } else { addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), "getProcessMode", _iPos, "'*', 'r', 'c', 'm', '0', '1', '2'", "-1"); return sciErr; } return sciErr; }
/*--------------------------------------------------------------------------*/ int sci_fscanfMat(char *fname, void* pvApiCtx) { SciErr sciErr; int *piAddressVarOne = NULL; int m1 = 0, n1 = 0; int iType1 = 0; char *filename = NULL; char *expandedFilename = NULL; char *Format = NULL; char *separator = NULL; BOOL bIsDefaultSeparator = TRUE; fscanfMatResult *results = NULL; CheckRhs(1, 3); CheckLhs(1, 2); if (Rhs == 3) { int *piAddressVarThree = NULL; int m3 = 0, n3 = 0; int iType3 = 0; sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if (isStringType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 3); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &separator)) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } bIsDefaultSeparator = FALSE; } if (Rhs >= 2) { int *piAddressVarTwo = NULL; int m2 = 0, n2 = 0; int iType2 = 0; sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { freeVar(&filename, &expandedFilename, &Format, &separator); printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2); if (sciErr.iErr) { freeVar(&filename, &expandedFilename, &Format, &separator); printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } if (isStringType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &Format)) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { Format = os_strdup(DEFAULT_FSCANFMAT_FORMAT); } sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { freeVar(&filename, &expandedFilename, &Format, &separator); printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &filename)) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } expandedFilename = expandPathVariable(filename); if (bIsDefaultSeparator) { #define NB_DEFAULT_SUPPORTED_SEPARATORS 2 /* bug 8148 */ /* default separator can be a space or a tabulation */ char *supportedSeparators[NB_DEFAULT_SUPPORTED_SEPARATORS] = {DEFAULT_FSCANFMAT_SEPARATOR, "\t"}; int i = 0; for (i = 0; i < NB_DEFAULT_SUPPORTED_SEPARATORS; i++) { results = fscanfMat(expandedFilename, Format, supportedSeparators[i]); if (results && results->err == FSCANFMAT_NO_ERROR) { break; } freeFscanfMatResult(results); } } else { results = fscanfMat(expandedFilename, Format, separator); } if (results == NULL) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } freeVar(NULL, &expandedFilename, &Format, &separator); switch (results->err) { case FSCANFMAT_NO_ERROR: { if ( (results->values) && (results->m > 0) && (results->n > 0)) { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, results->m, results->n, results->values); if (sciErr.iErr) { FREE(filename); freeFscanfMatResult(results); printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { if (createEmptyMatrix(pvApiCtx, Rhs + 1)) { FREE(filename); freeFscanfMatResult(results); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } LhsVar(1) = Rhs + 1; if (Lhs == 2) { if (results->text) { sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, results->sizeText, 1, (char const * const*) results->text); if (sciErr.iErr) { FREE(filename); freeFscanfMatResult(results); printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { if (createSingleString(pvApiCtx, Rhs + 2, "")) { FREE(filename); freeFscanfMatResult(results); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } LhsVar(2) = Rhs + 2; } freeFscanfMatResult(results); FREE(filename); PutLhsVar(); return 0; } case FSCANFMAT_MOPEN_ERROR: { Scierror(999, _("%s: can not open file %s.\n"), fname, filename); FREE(filename); return 0; } case FSCANFMAT_READLINES_ERROR: { Scierror(999, _("%s: can not read file %s.\n"), fname, filename); FREE(filename); return 0; } case FSCANFMAT_FORMAT_ERROR: { Scierror(999, _("%s: Invalid format.\n"), fname); FREE(filename); return 0; } case FSCANFMAT_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); FREE(filename); return 0; } default: case FSCANFMAT_ERROR: { Scierror(999, _("%s: error.\n"), fname); FREE(filename); return 0; } } FREE(filename); freeFscanfMatResult(results); return 0; }
/*--------------------------------------------------------------------------*/ int get_nax_arg(void* _pvCtx, int pos, rhs_opts opts[], int ** nax, BOOL * flagNax) { int i, m, n, first_opt = FirstOpt(), kopt; if (pos < first_opt) { int* piAddr = 0; int iType = 0; int* piData = NULL; getVarAddressFromPosition(_pvCtx, pos, &piAddr); getVarType(_pvCtx, piAddr, &iType); if (iType) { getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData); if (n * m != 4) { return 1; } for (i = 0 ; i < 4; ++i) { // When i = 1 or 3 we talk about the number of ticks, this value can be -1 to say 'AutoTicks' piData[i] = Max(piData[i], -(i % 2)); } *nax = piData; *flagNax = TRUE; } else { *nax = getDefNax(); *flagNax = FALSE; } } else if ((kopt = FindOpt("nax", opts))) { int* piAddr = 0; int* piData = NULL; getVarAddressFromPosition(_pvCtx, kopt, &piAddr); getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData); if (m * n != 4) { return 1; } for (i = 0 ; i < 4; ++i) { // When i = 1 or 3 we talk about the number of ticks, this value can be -1 to say 'AutoTicks' piData[i] = Max(piData[i], -(i % 2)); } *nax = piData; *flagNax = TRUE; } else { *nax = getDefNax(); *flagNax = FALSE; } return 1; }
SciErr getCommonMatrixOfPoly(void* _pvCtx, int* _piAddress, int _iComplex, int* _piRows, int* _piCols, int* _piNbCoef, double** _pdblReal, double** _pdblImg) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iType = 0; int iSize = 0; int *piOffset = NULL; double *pdblReal = NULL; double *pdblImg = NULL; if(_piAddress == NULL) { addErrorMessage(&sciErr, API_ERROR_INVALID_POINTER, _("%s: Invalid argument address"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly"); return sciErr; } sciErr = getVarType(_pvCtx, _piAddress, &iType); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly", getRhsFromAddress(_pvCtx, _piAddress)); return sciErr; } if(iType != sci_poly) { addErrorMessage(&sciErr, API_ERROR_INVALID_TYPE, _("%s: Invalid argument type, %s excepted"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly", _("polynomial matrix")); return sciErr; } if(isVarComplex(_pvCtx, _piAddress) != _iComplex) { addErrorMessage(&sciErr, API_ERROR_INVALID_COMPLEXITY, _("%s: Bad call to get a non complex matrix"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly"); return sciErr; } sciErr = getVarDimension(_pvCtx, _piAddress, _piRows, _piCols); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly", getRhsFromAddress(_pvCtx, _piAddress)); return sciErr; } iSize = *_piRows * *_piCols; if(_piNbCoef == NULL) { return sciErr; } piOffset = _piAddress + 8; //4 for header and 4 for variable name for(int i = 0 ; i < iSize ; i++) { _piNbCoef[i] = piOffset[i + 1] - piOffset[i]; } if(_pdblReal == NULL) { return sciErr; } pdblReal = (double*)(piOffset + iSize + 1 + ((iSize + 1) % 2 == 0 ? 0 : 1 )); for(int i = 0 ; i < iSize ; i++) { memcpy(_pdblReal[i], pdblReal + piOffset[i] - 1, sizeof(double) * _piNbCoef[i]); } if(_iComplex == 1) { pdblImg = pdblReal + piOffset[iSize] - 1; for(int i = 0 ; i < iSize ; i++) { memcpy(_pdblImg[i], pdblImg + piOffset[i] - 1, sizeof(double) * _piNbCoef[i]); } } return sciErr; }
/*--------------------------------------------------------------------------*/ int sci_fftw_flags(char *fname, void* pvApiCtx) { /* declaration of variables to store scilab parameters address */ static int m1 = 0, n1 = 0; char **Str1 = NULL; char **Str3 = NULL; unsigned int uiVar1 = 0; int* piDataOut = NULL; int* piAddr1 = NULL; int* piLen = NULL; int iType = 0; /* please update me ! */ static int nb_flag = 22; static char *Str[] = { /* documented flags */ "FFTW_MEASURE", "FFTW_DESTROY_INPUT", "FFTW_UNALIGNED", "FFTW_CONSERVE_MEMORY", "FFTW_EXHAUSTIVE", "FFTW_PRESERVE_INPUT", "FFTW_PATIENT", "FFTW_ESTIMATE", /* undocumented beyond-guru flags */ "FFTW_ESTIMATE_PATIENT", "FFTW_BELIEVE_PCOST", "FFTW_NO_DFT_R2HC", "FFTW_NO_NONTHREADED", "FFTW_NO_BUFFERING", "FFTW_NO_INDIRECT_OP", "FFTW_ALLOW_LARGE_GENERIC", "FFTW_NO_RANK_SPLITS", "FFTW_NO_VRANK_SPLITS", "FFTW_NO_VRECURSE", "FFTW_NO_SIMD", "FFTW_NO_SLOW", "FFTW_NO_FIXED_RADIX_LARGE_N", "FFTW_ALLOW_PRUNING" }; static unsigned flagt[] = { /* documented flags */ FFTW_MEASURE, FFTW_DESTROY_INPUT, FFTW_UNALIGNED, FFTW_CONSERVE_MEMORY, FFTW_EXHAUSTIVE, FFTW_PRESERVE_INPUT, FFTW_PATIENT, FFTW_ESTIMATE, /* undocumented beyond-guru flags */ FFTW_ESTIMATE_PATIENT, FFTW_BELIEVE_PCOST, FFTW_NO_DFT_R2HC, FFTW_NO_NONTHREADED, FFTW_NO_BUFFERING, FFTW_NO_INDIRECT_OP, FFTW_ALLOW_LARGE_GENERIC, FFTW_NO_RANK_SPLITS, FFTW_NO_VRANK_SPLITS, FFTW_NO_VRECURSE, FFTW_NO_SIMD, FFTW_NO_SLOW, FFTW_NO_FIXED_RADIX_LARGE_N, FFTW_ALLOW_PRUNING }; unsigned flagv = 0; int i = 0, j = 0; SciErr sciErr; CheckInputArgument(pvApiCtx, 0, 1); if (nbInputArgument(pvApiCtx) == 0) { // nothing } else { //get variable address of the input argument sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } getVarType(pvApiCtx, piAddr1, &iType); switch (iType) { case sci_ints: { /* int */ int iPrecision = 0; int* pi32Data = NULL; unsigned int* pui32Data = NULL; getMatrixOfIntegerPrecision(pvApiCtx, piAddr1, &iPrecision); if (iPrecision != SCI_INT32 && iPrecision != SCI_UINT32) { Scierror(999, _("%s: Wrong type for input argument #%d: A int32 expected.\n"), fname, 1); return 1; } if (iPrecision == SCI_INT32) { sciErr = getMatrixOfInteger32(pvApiCtx, piAddr1, &m1, &n1, &pi32Data); uiVar1 = (unsigned int)pi32Data[0]; } else { sciErr = getMatrixOfUnsignedInteger32(pvApiCtx, piAddr1, &m1, &n1, &pui32Data); uiVar1 = pui32Data[0]; } if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); printError(&sciErr, 0); return 1; } break; } case sci_matrix: { /* double */ double* pdblData = NULL; sciErr = getMatrixOfDouble(pvApiCtx, piAddr1, &m1, &n1, &pdblData); if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); printError(&sciErr, 0); return 1; } uiVar1 = (unsigned int)pdblData[0]; break; } case sci_strings: { /* string */ //fisrt call to retrieve dimensions sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } piLen = (int*)MALLOC(sizeof(int) * m1 * n1); //second call to retrieve length of each string sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, NULL); if (sciErr.iErr) { free(piLen); printError(&sciErr, 0); return 1; } Str1 = (char**)MALLOC(sizeof(char*) * m1 * n1); for (i = 0 ; i < m1 * n1 ; i++) { Str1[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination } //third call to retrieve data sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, Str1); if (sciErr.iErr) { free(piLen); freeArrayOfString(Str1, m1 * n1); printError(&sciErr, 0); return 1; } for (j = 0; j < m1 * n1; j++) { for (i = 0; i < nb_flag; i++) { if (strcmp(Str1[j], Str[i]) == 0) { break; } } if (i == nb_flag) { free(piLen); freeArrayOfString(Str1, m1 * n1); Scierror(999, _("%s: Wrong values for input argument #%d: FFTW flag expected.\n"), fname, 1); return 0; } else { if (i > 0) { flagv = ( flagv | (1U << (i - 1)) ); } } } uiVar1 = (unsigned int)flagv; free(piLen); freeArrayOfString(Str1, m1 * n1); m1 = 1; n1 = 1; break; } default: Scierror(53, _("%s: Wrong type for input argument #%d.\n"), fname, 1); return 1; } if (m1 != 1 || n1 != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n"), fname, 1, 1, 1); return 1; } setCurrentFftwFlags(uiVar1); } /* return value of Sci_Plan.flags in position 2 */ sciErr = allocMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + 2, 1, 1, &piDataOut); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: No more memory.\n"), fname); return 1; } piDataOut[0] = (int) getCurrentFftwFlags(); /*Test for only FFTW_MEASURE*/ if (getCurrentFftwFlags() == 0) { j = 1; if ((Str3 = (char **)MALLOC(sizeof(char *))) == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); return 1; } Str3[0] = os_strdup(Str[0]); if (Str3[0] == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); FREE(Str3); return 1; } } else { j = 0; for (i = 1; i < nb_flag; i++) { if ((getCurrentFftwFlags() & flagt[i]) == flagt[i]) { j++; if (Str3) { Str3 = (char **)REALLOC(Str3, sizeof(char *) * j); } else { Str3 = (char **)MALLOC(sizeof(char *) * j); } if (Str3 == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); return 1; } Str3[j - 1] = os_strdup(Str[i]); if (Str3[j - 1] == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); freeArrayOfString(Str3, j); return 1; } } } } if (Str3 == NULL) { Scierror(999, _("%s: Failed to generate the planner name.\n"), fname); return 1; } /* Create the string matrix as return of the function */ sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 3, j, 1, Str3); freeArrayOfString(Str3, j); // Data have been copied into Scilab memory if (sciErr.iErr) { printError(&sciErr, 0); return 1; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 2; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 3; ReturnArguments(pvApiCtx); return 0; }
/******************************************************************************* Interface for MATIO function called Mat_Open Scilab function name : matfile_open *******************************************************************************/ int sci_matfile_open(char *fname, unsigned long fname_len) { int nbRow = 0, nbCol = 0; mat_t *matfile; int fileIndex = 0; char * filename = NULL; char * optionStr = NULL; int option = 0, var_type; int * filename_addr = NULL, * option_addr = NULL, * version_addr = NULL; char * versionStr = NULL; int version = MAT_FT_MAT5; // By default, use MAtlab 5 files SciErr sciErr; CheckRhs(1, 3); CheckLhs(1, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &filename_addr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getVarType(pvApiCtx, filename_addr, &var_type); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (var_type == sci_strings) { getAllocatedSingleString(pvApiCtx, filename_addr, &filename); sciErr = getVarDimension(pvApiCtx, filename_addr, &nbRow, &nbCol); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (nbCol != 1) { Scierror(999, _("%s: Wrong size for first input argument: A string expected.\n"), fname); freeAllocatedSingleString(filename); return FALSE; } } else { Scierror(999, _("%s: Wrong type for first input argument: A string expected.\n"), fname); freeAllocatedSingleString(filename); return FALSE; } if (Rhs >= 2) { sciErr = getVarAddressFromPosition(pvApiCtx, 2, &option_addr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getVarType(pvApiCtx, option_addr, &var_type); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (var_type == sci_strings) { getAllocatedSingleString(pvApiCtx, option_addr, &optionStr); sciErr = getVarDimension(pvApiCtx, option_addr, &nbRow, &nbCol); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (nbCol != 1) { Scierror(999, _("%s: Wrong size for second input argument: A string expected.\n"), fname); freeAllocatedSingleString(filename); freeAllocatedSingleString(optionStr); return FALSE; } if (strcmp(optionStr, "r") == 0) { option = MAT_ACC_RDONLY; } else if (strcmp(optionStr, "w") == 0) { option = MAT_ACC_RDWR; } else { Scierror(999, _("%s: Wrong value for second input argument: 'r' or 'w' expected.\n"), fname); freeAllocatedSingleString(filename); freeAllocatedSingleString(optionStr); return FALSE; } } else { Scierror(999, _("%s: Wrong type for second input argument: A string expected.\n"), fname); freeAllocatedSingleString(filename); freeAllocatedSingleString(optionStr); return FALSE; } } else { /* Default option value */ option = MAT_ACC_RDONLY; } if (Rhs >= 3) { sciErr = getVarAddressFromPosition(pvApiCtx, 3, &version_addr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getVarType(pvApiCtx, version_addr, &var_type); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } printf("sci_strings %d %d\n", var_type, sci_strings); if (var_type == sci_strings) { getAllocatedSingleString(pvApiCtx, version_addr, &versionStr); sciErr = getVarDimension(pvApiCtx, version_addr, &nbRow, &nbCol); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (nbCol != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3); freeAllocatedSingleString(filename); freeAllocatedSingleString(optionStr); freeAllocatedSingleString(versionStr); return FALSE; } if (strcmp(versionStr, "7.3") == 0) { version = MAT_FT_MAT73; // Matlab 7.3 file } else { version = MAT_FT_MAT5; // Default, Matlab 5 file } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3); return 0; } } if (option == MAT_ACC_RDWR) // Write, option = "w" { /* create a Matlab 5 or 7.3 file */ matfile = Mat_CreateVer(filename, NULL, version); } else // Read, option = "r" { /* Try to open the file (as a Matlab 5 file) */ matfile = Mat_Open(filename, option); } if (matfile == NULL) /* Opening failed */ { /* Function returns -1 */ fileIndex = -1; } if (matfile != NULL) /* Opening succeed */ { /* Add the file to the manager */ matfile_manager(MATFILEMANAGER_ADDFILE, &fileIndex, &matfile); } /* Return the index */ createScalarDouble(pvApiCtx, Rhs + 1, (double)fileIndex); freeAllocatedSingleString(filename); freeAllocatedSingleString(optionStr); freeAllocatedSingleString(versionStr); LhsVar(1) = Rhs + 1; PutLhsVar(); return TRUE; }
int sci_gpuOnes(char *fname) { CheckLhs(1, 1); void* pvPtr = NULL; int* piAddr = NULL; SciErr sciErr; int inputType; int iRows = 0; int iCols = 0; GpuPointer* gpOut = NULL; try { if (!isGpuInit()) { throw "gpu is not initialised. Please launch gpuInit() before use this function."; } if (Rhs == 1) { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { throw sciErr; } sciErr = getVarType(pvApiCtx, piAddr, &inputType); if (inputType == sci_pointer) { sciErr = getPointer(pvApiCtx, piAddr, (void**)&pvPtr); if (sciErr.iErr) { throw sciErr; } GpuPointer* gmat = (GpuPointer*)(pvPtr); if (!PointerManager::getInstance()->findGpuPointerInManager(gmat)) { throw "gpuOnes : Bad type for input argument #1. Only variables created with GPU functions allowed."; } if (useCuda() && gmat->getGpuType() != GpuPointer::CudaType) { throw "gpuOnes : Bad type for input argument #1: A Cuda pointer expected."; } if (useCuda() == false && gmat->getGpuType() != GpuPointer::OpenCLType) { throw "gpuOnes : Bad type for input argument #1: A OpenCL pointer expected."; } if (gmat->getDims() > 2) { throw "gpuOnes : Hypermatrix not yet implemented."; } iRows = gmat->getRows(); iCols = gmat->getCols(); } else if (inputType == sci_matrix) { // Get size and data double* h; sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &h); } else { throw "gpuOnes : Bad type for input argument #1 : A Matrix or GPU pointer expected."; } } else { if (Rhs > 2) { throw "gpuOnes : Hypermatrix not yet implemented."; } int* piDimsArray = new int[Rhs]; for (int i = 0; i < Rhs; i++) { sciErr = getVarAddressFromPosition(pvApiCtx, i + 1, &piAddr); if (sciErr.iErr) { throw sciErr; } sciErr = getVarType(pvApiCtx, piAddr, &inputType); if (inputType != sci_matrix) { throw "gpuOnes : Bad type for input argument #%d : A Matrix expected."; } double* h; sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &h); if (iRows * iCols != 1) { char str[100]; sprintf(str, "gpuOnes : Wrong size for input argument #%d : A scalar expected.", i + 1); throw str; } piDimsArray[i] = (int)h[0]; } iRows = piDimsArray[0]; iCols = piDimsArray[1]; delete piDimsArray; } #ifdef WITH_CUDA if (useCuda()) { gpOut = new PointerCuda(iRows, iCols, false); gpOut->initMatrix(1); } #endif #ifdef WITH_OPENCL if (!useCuda()) { Scierror(999, "gpuOnes: not implemented with OpenCL.\n"); } #endif PointerManager::getInstance()->addGpuPointerInManager(gpOut); sciErr = createPointer(pvApiCtx, Rhs + 1, (void*)gpOut); if (sciErr.iErr) { throw sciErr; } LhsVar(1) = Rhs + 1; PutLhsVar(); return 0; } #ifdef WITH_CUDA catch (cudaError_t cudaE) { GpuError::treat_error<CUDAmode>((CUDAmode::Status)cudaE); } #endif catch (const char* str) { Scierror(999, "%s\n", str); } catch (SciErr E) { printError(&E, 0); } return EXIT_FAILURE; }
//function to remove specified columns int sci_sym_delete_cols(char *fname, unsigned long fname_len){ // Error management variables SciErr sciErr1,sciErr2; double status=1.0;//assume error status double num;//variable to store the number of columns to be deleted obtained from user in scilab int count=0;//iterator variable int num_cols;//stores the number of columns in the loaded problem int iType= 0;//stores the datatype of matrix int rows=0,columns=0;//integer variables to denote the number of rows and columns in the array denoting the column numbers to be deleted unsigned int *value=NULL;//pointer to integer array allocated dynamically having the indices to be deleted double *array_ptr=NULL;//double array pointer to the array denoting the column numbers to be deleted int *piAddressVarOne = NULL;//pointer used to access first and second arguments of the function int output=0;//output parameter for the symphony sym_delete_cols function CheckInputArgument(pvApiCtx, 1, 1);//Check we have exactly one argument as input or not CheckOutputArgument(pvApiCtx, 1, 1);//Check we have exactly one argument on output side or not //load address of 1st argument into piAddressVarOne sciErr2=getVarAddressFromPosition(pvApiCtx,1,&piAddressVarOne); if (sciErr2.iErr){ printError(&sciErr2, 0); return 0; } //check if it is double type sciErr2 = getVarType(pvApiCtx, piAddressVarOne, &iType); if(sciErr2.iErr || iType != sci_matrix) { printError(&sciErr2, 0); return 0; } //getting the first argument as a double array sciErr2=getMatrixOfDouble(pvApiCtx,piAddressVarOne,&rows,&columns,&array_ptr); if (sciErr2.iErr){ printError(&sciErr2, 0); return 0; } //dynamically allocate the integer array value=(unsigned int *)malloc(sizeof(unsigned int)*columns); //store double values in the integer array by typecasting while(count<columns) { value[count]=(unsigned int)array_ptr[count]; count++; } sciprint("\n"); //ensure that environment is active if(global_sym_env==NULL){ sciprint("Error: Symphony environment not initialized. Please run 'sym_open()' first.\n"); } else { int flag=0;//flag used for finding if the indices to be deleted are valid output=sym_get_num_cols(global_sym_env,&num_cols);//function to find the number of columns in the loaded problem if(output==FUNCTION_TERMINATED_ABNORMALLY) { Scierror(999, "An error occured. Has a problem been loaded?\n"); free(value);//freeing the memory of the allocated pointer return 0; } for(count=0;count<columns;count++)//loop used to check if all the indices mentioned to be deleted are valid { if(value[count]<0 || value[count]>=num_cols){ flag=1; break; } } if(flag==1) { Scierror(999,"Not valid indices..\n"); sciprint("valid indices are from 0 to %d",num_cols-1); free(value);//freeing the memory of the allocated pointer return 0; } //only when the number of columns to be deleted is lesser than the actual number of columns ,execution is proceeded with if(columns<=num_cols){ output=sym_delete_cols(global_sym_env,(unsigned int)columns,value);//symphony function to delete the columns specified if(output==FUNCTION_TERMINATED_NORMALLY) { sciprint("Execution is successfull\n"); status=0.0; } else if(output==FUNCTION_TERMINATED_ABNORMALLY) { Scierror(999,"Problem occured while deleting the columns,Are the column numbers correct?\n"); sciprint("Function terminated abnormally\n"); status=1.0; } } else{ sciprint("These many number of variables dont exist in the problem\n"); status=1.0; } } int e=createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx)+1,status); if (e){ AssignOutputVariable(pvApiCtx, 1) = 0; free(value);//freeing the memory of the allocated pointer return 1; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); free(value);//freeing the memory of the allocated pointer return 0; }
//-------------------------------------------------------------------------------------------------------- int detect_fannerrorlist(int StackPos) { int m_param, n_param, * pi_param_addr; int type, * pi_len = NULL, i; char ** LabelList = NULL; SciErr _sciErr; _sciErr = getVarAddressFromPosition(pvApiCtx, StackPos, &pi_param_addr); if (_sciErr.iErr) { printError(&_sciErr, 0); return -1; } _sciErr = getVarType(pvApiCtx, pi_param_addr, &type); if (_sciErr.iErr) { printError(&_sciErr, 0); return -1; } if (type!=sci_mlist) { Scierror(999,"Argument %d is not a mlist\n",StackPos); return 0; } _sciErr = getMatrixOfStringInList(pvApiCtx, pi_param_addr, 1, &m_param, &n_param, NULL, NULL); if (_sciErr.iErr) { printError(&_sciErr, 0); return -1; } pi_len = (int*)MALLOC(sizeof(int) * m_param * n_param); _sciErr = getMatrixOfStringInList(pvApiCtx, pi_param_addr, 1, &m_param, &n_param, pi_len, NULL); if (_sciErr.iErr) { printError(&_sciErr, 0); return -1; } LabelList = (char**)MALLOC(sizeof(char*) * m_param * n_param); for(i=0; i<m_param*n_param; i++) { LabelList[i] = (char*)MALLOC(sizeof(char) * (pi_len[i] + 1)); // + 1 for null termination } _sciErr = getMatrixOfStringInList(pvApiCtx, pi_param_addr, 1, &m_param, &n_param, pi_len, LabelList); if (_sciErr.iErr) { printError(&_sciErr, 0); return -1; } if (strcmp(LabelList[0],"fannerrorlist") != 0) { Scierror(999,"Argument %d is not a fannerrorlist\n",StackPos); return 0; } return 0; }
//-------------------------------------------------------------------------------------------------------- FILE * createFILEFromScilabFannErrorStruct(unsigned int StackPos, int * res) { int m_param, n_param, * pi_param_addr; int type, * pi_len = NULL, i; char ** LabelList = NULL; FILE * log_file = NULL; SciErr _sciErr; *res = -1; _sciErr = getVarAddressFromPosition(pvApiCtx, StackPos, &pi_param_addr); if (_sciErr.iErr) { printError(&_sciErr, 0); return NULL; } _sciErr = getVarType(pvApiCtx, pi_param_addr, &type); if (_sciErr.iErr) { printError(&_sciErr, 0); return NULL; } if (type!=sci_mlist) { Scierror(999,"Argument %d is not a mlist\n",StackPos); return NULL; } _sciErr = getMatrixOfStringInList(pvApiCtx, pi_param_addr, 1, &m_param, &n_param, NULL, NULL); if (_sciErr.iErr) { printError(&_sciErr, 0); return NULL; } pi_len = (int*)MALLOC(sizeof(int) * m_param * n_param); _sciErr = getMatrixOfStringInList(pvApiCtx, pi_param_addr, 1, &m_param, &n_param, pi_len, NULL); if (_sciErr.iErr) { printError(&_sciErr, 0); return NULL; } LabelList = (char**)MALLOC(sizeof(char*) * m_param * n_param); for(i=0; i<m_param*n_param; i++) { LabelList[i] = (char*)MALLOC(sizeof(char) * (pi_len[i] + 1)); // + 1 for null termination } _sciErr = getMatrixOfStringInList(pvApiCtx, pi_param_addr, 1, &m_param, &n_param, pi_len, LabelList); if (_sciErr.iErr) { printError(&_sciErr, 0); return NULL; } if (strcmp(LabelList[0],"fannerrorlist") != 0) { Scierror(999,"Argument 1 is not a fannerrorlist\r\n"); return NULL; } _sciErr = getPointerInList(pvApiCtx, pi_param_addr, 2, (void **)&log_file); if (_sciErr.iErr) { printError(&_sciErr, 0); return NULL; } *res = 0; return log_file; }
/*--------------------------------------------------------------------------*/ int sci_norm(char *fname, void* pvApiCtx) { SciErr sciErr; // Arguments' addresses int *pAAddr = NULL; int *pflagAddr = NULL; // Arguments' values double *pA = NULL; char *pflagChar = NULL; doublecomplex *pAC = NULL; double flagVal = 0; // Arguments' properties (type, dimensions, length) int iLen = 0; int iType = 0; int iRows = 0; int iCols = 0; // Return value double ret = 0; double RowsColsTemp = 0; int i = 0; int isMat = 0; int isComplex = 0; CheckInputArgument(pvApiCtx, 1, 2); CheckOutputArgument(pvApiCtx, 1, 1); // Checking A. sciErr = getVarAddressFromPosition(pvApiCtx, 1, &pAAddr); // Retrieving A address. if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); printError(&sciErr, 0); return 0; } sciErr = getVarType(pvApiCtx, pAAddr, &iType); // Retrieving A type. if (iType != sci_matrix) { OverLoad(1); return 0; } if (isVarComplex(pvApiCtx, pAAddr)) { sciErr = getComplexZMatrixOfDouble(pvApiCtx, pAAddr, &iRows, &iCols, &pAC); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: Real or complex matrix expected.\n"), fname, 1); return 0; } isComplex = 1; for (i = 0; i < iRows * iCols; ++i) // Checking A for %inf, which is not supported by Lapack. { if (la_isinf(pAC[i].r) != 0 || la_isinf(pAC[i].i) != 0 || ISNAN(pAC[i].r) || ISNAN(pAC[i].i)) { Scierror(264, _("%s: Wrong value for argument #%d: Must not contain NaN or Inf.\n"), fname, 1); return 0; } } } else { sciErr = getMatrixOfDouble(pvApiCtx, pAAddr, &iRows, &iCols, &pA); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: Real or complex matrix expected.\n"), fname, 1); return 0; } for (i = 0 ; i < iRows * iCols ; i++) // Checking A for %inf, which is not supported by Lapack. { if (la_isinf(pA[i]) != 0 || ISNAN(pA[i])) { Scierror(264, _("%s: Wrong value for argument #%d: Must not contain NaN or Inf.\n"), fname, 1); return 0; } } } if (iRows == 0) // A = [] => returning 0. { createScalarDouble(pvApiCtx, Rhs + 1, 0); AssignOutputVariable(pvApiCtx, 1) = Rhs + 1; return 0; } if (iRows > 1 && iCols > 1) // If A is a matrix, only 1, 2 and %inf are allowed as second argument. { isMat = 1; } if (iRows == 1) // If iRows == 1, then transpose A to consider it like a vector. { RowsColsTemp = iRows; iRows = iCols; iCols = (int)RowsColsTemp; } if (Rhs == 1) // One argument => returning norm 2. { // Call normP() or normPC(). if (isComplex) { ret = normPC(pAC, iRows, iCols, 2); // if A is a complex matrix, call the complex function. } else { ret = normP(pA, iRows, iCols, 2); } createScalarDouble(pvApiCtx, Rhs + 1, ret); AssignOutputVariable(pvApiCtx, 1) = Rhs + 1; return 0; } // Checking flag. sciErr = getVarAddressFromPosition(pvApiCtx, 2, &pflagAddr); // Retrieving flag address. if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getVarType(pvApiCtx, pflagAddr, &iType); // Retrieving flag type. if (iType != sci_strings && iType != sci_matrix) { Scierror(999, _("%s: Wrong type for input argument #%d: String or integer expected.\n"), fname, 2); return 0; } if (iType == sci_strings) { if (getAllocatedSingleString(pvApiCtx, pflagAddr, &pflagChar)) // Retrieving flag dimensions. { Scierror(205, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2); return 0; } iLen = (int)strlen(pflagChar); if (iLen != 3 && iLen != 1) { Scierror(116, _("%s: Wrong value for input argument #%d: %s, %s, %s, or %s expected.\n"), fname, 2, "i", "inf", "f", "fro"); freeAllocatedSingleString(pflagChar); return 0; } if (strcmp(pflagChar, "inf") != 0 && strcmp(pflagChar, "i") != 0 && strcmp(pflagChar, "fro") != 0 && strcmp(pflagChar, "f") != 0) // flag must be = "inf", "i", "fro" or "f". { Scierror(116, _("%s: Wrong value for input argument #%d: %s, %s, %s or %s expected.\n"), fname, 2, "i", "inf", "f", "fro"); freeAllocatedSingleString(pflagChar); return 0; } if (isComplex) { ret = normStringC(pAC, iRows, iCols, pflagChar); // if A is a complex matrix, call the complex function. } else { ret = normString(pA, iRows, iCols, pflagChar); // flag is a string => returning the corresponding norm. } createScalarDouble(pvApiCtx, Rhs + 1, ret); AssignOutputVariable(pvApiCtx, 1) = Rhs + 1; freeAllocatedSingleString(pflagChar); return 0; } else { if (isVarComplex(pvApiCtx, pflagAddr)) { Scierror(999, _("%s: Wrong type for input argument #%d: A real expected.\n"), fname, 2); return 0; } if (getScalarDouble(pvApiCtx, pflagAddr, &flagVal)) // Retrieving flag value & dimensions as a double. { printError(&sciErr, 0); Scierror(999, _("%s: Wrong size for input argument #%d: A real scalar expected.\n"), fname, 2); return 0; } // Call the norm functions. if (la_isinf(flagVal) == 1 && flagVal > 0) // flag = %inf { if (isComplex) { ret = normStringC(pAC, iRows, iCols, "inf"); // if A is a complex matrix, call the complex function. } else { ret = normString(pA, iRows, iCols, "inf"); // The infinite norm is computed by normString(). } createScalarDouble(pvApiCtx, Rhs + 1, ret); AssignOutputVariable(pvApiCtx, 1) = Rhs + 1; return 0; } else { if (isMat == 1 && flagVal != 1 && flagVal != 2 && la_isinf(flagVal) == 0) { Scierror(116, _("%s: Wrong value for input argument #%d: %s, %s, %s or %s expected.\n"), fname, 2, "1", "2", "inf", "-inf"); return 0; } if (isComplex) { ret = normPC(pAC, iRows, iCols, flagVal); // if A is a complex matrix, call the complex function. } else { ret = normP(pA, iRows, iCols, flagVal); // flag is an integer => returning the corresponding norm. } createScalarDouble(pvApiCtx, Rhs + 1, ret); AssignOutputVariable(pvApiCtx, 1) = Rhs + 1; return 0; } } return 0; }
matvar_t *GetCharVariable(void *pvApiCtx, int iVar, const char *name, int * parent, int item_position) { int rank = 0, i = 0, j = 0; size_t *pszDims = NULL; int *piDims = NULL; matvar_t *createdVar = NULL; int* piLen = NULL; char** pstData = NULL; char* pstMatData = NULL; int * piAddr = NULL; int * item_addr = NULL; int var_type; int saveDim = 0; /* Used to save old dimension before restoring it */ SciErr sciErr; if (parent == NULL) { sciErr = getVarAddressFromPosition(pvApiCtx, iVar, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return NULL; } sciErr = getVarType(pvApiCtx, piAddr, &var_type); if (sciErr.iErr) { printError(&sciErr, 0); return NULL; } } else { sciErr = getListItemAddress(pvApiCtx, parent, item_position, &item_addr); if (sciErr.iErr) { printError(&sciErr, 0); return NULL; } sciErr = getVarType(pvApiCtx, item_addr, &var_type); if (sciErr.iErr) { printError(&sciErr, 0); return NULL; } } if (var_type == sci_strings) /* 2-D array */ { rank = 2; piDims = (int*)MALLOC(sizeof(int) * rank); if (piDims == NULL) { Scierror(999, _("%s: No more memory.\n"), "GetCharVariable"); return NULL; } if (parent == NULL) { // First call to retrieve dimensions sciErr = getMatrixOfString(pvApiCtx, piAddr, &piDims[0], &piDims[1], NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } piLen = (int *)MALLOC(piDims[0] * piDims[1] * sizeof(int)); // Second call to retrieve length of each string sciErr = getMatrixOfString(pvApiCtx, piAddr, &piDims[0], &piDims[1], piLen, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } pstData = (char**)MALLOC(sizeof(char*) * piDims[0] * piDims[1]); for (i = 0 ; i < piDims[0] * piDims[1] ; i++) { pstData[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1)); //+ 1 for null termination } // Third call to retrieve data sciErr = getMatrixOfString(pvApiCtx, piAddr, &piDims[0], &piDims[1], piLen, pstData); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } } else { // First call to retrieve dimensions sciErr = getMatrixOfStringInList(pvApiCtx, parent, item_position, &piDims[0], &piDims[1], NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return NULL; } piLen = (int *)MALLOC(piDims[0] * piDims[1] * sizeof(int)); // Second call to retrieve length of each string sciErr = getMatrixOfStringInList(pvApiCtx, parent, item_position, &piDims[0], &piDims[1], piLen, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return NULL; } pstData = (char**)MALLOC(sizeof(char*) * piDims[0] * piDims[1]); for (i = 0 ; i < piDims[0] * piDims[1] ; i++) { pstData[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1)); //+ 1 for null termination } // Third call to retrieve data sciErr = getMatrixOfStringInList(pvApiCtx, parent, item_position, &piDims[0], &piDims[1], piLen, pstData); if (sciErr.iErr) { printError(&sciErr, 0); return NULL; } } pszDims = (size_t*) MALLOC(rank * sizeof(size_t)); if (pszDims == NULL) { Scierror(999, _("%s: No more memory.\n"), "GetCharVariable"); return NULL; } for (i = 0; i < rank; i++) { pszDims[i] = piDims[i]; } if (piDims[0] == 0) /* Empty character string */ { createdVar = Mat_VarCreate(name, MAT_C_CHAR, MAT_T_UINT8, rank, pszDims, pstData[0], 0); } else if (piDims[0]*piDims[1] == 1) /* Scalar character string */ { saveDim = piDims[1]; pszDims[1] = piLen[0]; createdVar = Mat_VarCreate(name, MAT_C_CHAR, MAT_T_UINT8, rank, pszDims, pstData[0], 0); pszDims[1] = saveDim; } else /* More than one character string -> save as a Cell */ { if (piDims[0] == 1) { /* TODO: Should be saved as a cell */ Scierror(999, _("%s: Row array of strings saving is not implemented.\n"), "GetCharVariable"); freeArrayOfString(pstData, piDims[0]*piDims[1]); FREE(pszDims); FREE(piDims); FREE(piLen); return NULL; } else if (piDims[1] == 1) { /* Check that all strings have the same length */ for (i = 0 ; i < piDims[0] ; i++) { if (piLen[0] != piLen[i]) { /* TODO: Should be saved as a cell */ Scierror(999, _("%s: Column array of strings with different lengths saving is not implemented.\n"), "GetCharVariable"); freeArrayOfString(pstData, piDims[0]*piDims[1]); FREE(pszDims); FREE(piDims); FREE(piLen); return NULL; } } /* Reorder characters */ pstMatData = (char*)MALLOC(sizeof(char) * piDims[0] * piLen[0]); for (i = 0 ; i < piDims[0] ; i++) { for (j = 0 ; j < piLen[0] ; j++) { pstMatData[i + j * piDims[0]] = pstData[i][j]; } } /* Save the variable */ saveDim = piDims[1]; pszDims[1] = piLen[0]; createdVar = Mat_VarCreate(name, MAT_C_CHAR, MAT_T_UINT8, rank, pszDims, pstMatData, 0); pszDims[1] = saveDim; freeArrayOfString(pstData, piDims[0]*piDims[1]); /* FREE now because dimensions are changed just below */ FREE(pstMatData); FREE(pszDims); FREE(piDims); FREE(piLen); } else { /* TODO: Should be saved as a cell */ Scierror(999, _("%s: 2D array of strings saving is not implemented.\n"), "GetCharVariable"); freeArrayOfString(pstData, piDims[0]*piDims[1]); FREE(pszDims); FREE(piDims); FREE(piLen); return NULL; } } } else { Scierror(999, _("%s: Wrong type for first input argument: String matrix expected.\n"), "GetCharVariable"); freeArrayOfString(pstData, piDims[0]*piDims[1]); FREE(pszDims); FREE(piDims); FREE(piLen); return NULL; } return createdVar; }
/*--------------------------------------------------------------------------*/ int sci_scinotes(char *fname, unsigned long fname_len) { SciErr sciErr; CheckRhs(0, 3); CheckLhs(0, 1); if (Rhs == 0) { try { callSciNotesW(NULL, 0); } catch (GiwsException::JniCallMethodException exception) { Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str()); } catch (GiwsException::JniException exception) { Scierror(999, "%s: %s\n", fname, exception.whatStr().c_str()); } } else { int m1 = 0, n1 = 0; int *piAddressVarOne = NULL; wchar_t **pStVarOne = NULL; int *lenStVarOne = NULL; int i = 0; int iType1 = 0; char *functionName = NULL; sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (iType1 != sci_strings) { Scierror(999, _("%s: Wrong type for argument #%d: String matrix expected.\n"), fname, 1); return 0; } /* get dimensions */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } lenStVarOne = (int *)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); return 0; } /* get lengths */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); FREE(lenStVarOne); return 0; } pStVarOne = (wchar_t **) MALLOC(sizeof(wchar_t *) * (m1 * n1)); if (pStVarOne == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); FREE(lenStVarOne); return 0; } for (i = 0; i < m1 * n1; i++) { pStVarOne[i] = (wchar_t *) MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1)); if (pStVarOne[i] == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); for (; i >= 0; i--) { FREE(pStVarOne[i]); } FREE(pStVarOne); FREE(lenStVarOne); return 0; } } /* get strings */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (Rhs >= 2) //get line numbers { int *piAddressVarTwo = NULL; int m2 = 0, n2 = 0; double *pdblVarTwo = NULL; int iType2 = 0; sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (iType2 != sci_matrix && iType2 != sci_strings) { Scierror(999, _("%s: Wrong type for argument #%d: Real matrix or \'readonly\' expected.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (iType2 == sci_strings) { /* get dimensions */ wchar_t **pStVarTwo = NULL; int *lenStVarTwo = NULL; sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, lenStVarTwo, NULL); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (m2 != 1 || n2 != 1) { Scierror(999, _("%s: Wrong type for argument #%d: Real matrix or \'readonly\' expected.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } lenStVarTwo = (int *)MALLOC(sizeof(int)); if (lenStVarTwo == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } /* get lengths */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, lenStVarTwo, pStVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } pStVarTwo = (wchar_t **) MALLOC(sizeof(wchar_t *)); if (pStVarTwo == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } pStVarTwo[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * (lenStVarTwo[0] + 1)); if (pStVarTwo[0] == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); FREE(pStVarTwo); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } /* get strings */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, lenStVarTwo, pStVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); FREE(pStVarTwo); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } try { callSciNotesWWithOption(pStVarOne, pStVarTwo, m1 * n1); } catch (GiwsException::JniCallMethodException exception) { Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str()); FREE(pStVarTwo); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } catch (GiwsException::JniException exception) { Scierror(999, "%s: %s\n", fname, exception.whatStr().c_str()); FREE(pStVarTwo); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } freeArrayOfWideString(pStVarTwo, 1); FREE(lenStVarTwo); } else { if (isVarComplex(pvApiCtx, piAddressVarTwo) == 1) { Scierror(999, _("%s: Wrong type for argument #%d: Real matrix expected.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &pdblVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (m2 * n2 != m1 * n1) { Scierror(999, _("%s: Wrong size for input arguments #%d and #%d: Same dimensions expected.\n"), fname, 1, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (Rhs == 3) { int *piAddressVarThree = NULL; sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if (!isStringType(pvApiCtx, piAddressVarThree)) { Scierror(999, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 3); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } int ret = getAllocatedSingleString(pvApiCtx, piAddressVarThree, &functionName); if (ret) { Scierror(999, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 3); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } } try { callSciNotesWWithLineNumberAndFunction(pStVarOne, pdblVarTwo, functionName, m1 * n1); } catch (GiwsException::JniCallMethodException exception) { Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str()); } catch (GiwsException::JniException exception) { Scierror(999, "%s: %s\n", fname, exception.whatStr().c_str()); } } } else { try { callSciNotesW(pStVarOne, m1 * n1); } catch (GiwsException::JniCallMethodException exception) { Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str()); } catch (GiwsException::JniException exception) { Scierror(999, "%s: %s\n", fname, exception.whatStr().c_str()); } } freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); if (functionName) { freeAllocatedSingleString(functionName); } } LhsVar(1) = 0; PutLhsVar(); return 0; }
/*-------------------------------------------------------------------------- * sciset(choice-name,x1,x2,x3,x4,x5) * or xset() *-----------------------------------------------------------*/ int sci_set(char *fname, void *pvApiCtx) { SciErr sciErr; int i = 0; int* piAddr1 = NULL; int isMatrixOfString = 0; char* pstNewProperty = NULL; unsigned long hdl; int iObjUID = 0; int iType = 0; int* piType = &iType; int iSetProperty = 0; int iRhs = nbInputArgument(pvApiCtx); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1); if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 1; } if (isMListType(pvApiCtx, piAddr1) || isTListType(pvApiCtx, piAddr1)) { OverLoad(1); return 0; } CheckInputArgumentAtLeast(pvApiCtx, 2); CheckOutputArgument(pvApiCtx, 0, 1); if (isDoubleType(pvApiCtx, piAddr1)) /* tclsci handle */ { /* call "set" for tcl/tk see tclsci/sci_gateway/c/sci_set.c */ OverLoad(1); return 0; } if (iRhs == 2) { #define NB_PROPERTIES_SUPPORTED 7 /* No object specified */ /* ONLY supported properties are */ /* 'current_entity' */ /* 'hdl' */ /* 'current_figure' */ /* 'current_axes' */ /* 'default_values' */ /* 'figure_style' for compatibility but do nothing */ /* others values must return a error */ char *propertiesSupported[NB_PROPERTIES_SUPPORTED] = { "current_entity", "hdl", "current_figure", "current_axes", "figure_style", "default_values", "auto_clear" }; int iPropertyFound = 0; int* piAddr2 = NULL; int iType2 = 0; int iRows2 = 0; int iCols2 = 0; void* pvData = NULL; char* pstProperty = NULL; if (isStringType(pvApiCtx, piAddr1) == 0) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, 1); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddr1, &pstProperty)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, 1); return 1; } sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2); if (sciErr.iErr) { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 1; } sciErr = getVarType(pvApiCtx, piAddr2, &iType2); if (sciErr.iErr) { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 1; } switch (iType2) { case sci_matrix: sciErr = getMatrixOfDouble(pvApiCtx, piAddr2, &iRows2, &iCols2, (double**)&pvData); if (sciErr.iErr) { freeAllocatedSingleString(pstProperty); printError(&sciErr, 0); Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), fname, 2); return sciErr.iErr; } break; case sci_handles : sciErr = getMatrixOfHandle(pvApiCtx, piAddr2, &iRows2, &iCols2, (long long**)&pvData); if (sciErr.iErr) { freeAllocatedSingleString(pstProperty); printError(&sciErr, 0); Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of handle expected.\n"), fname, 3); return 1; } break; case sci_strings : if (strcmp(pstProperty, "tics_labels") == 0 || strcmp(pstProperty, "auto_ticks") == 0 || strcmp(pstProperty, "axes_visible") == 0 || strcmp(pstProperty, "axes_reverse") == 0 || strcmp(pstProperty, "text") == 0 || strcmp(pstProperty, "ticks_format") == 0) { isMatrixOfString = 1; if (getAllocatedMatrixOfString(pvApiCtx, piAddr2, &iRows2, &iCols2, (char***)&pvData)) { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Wrong size for input argument #%d: A matrix of string expected.\n"), fname, 2); return 1; } } else { if (getAllocatedSingleString(pvApiCtx, piAddr2, (char**)&pvData)) { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, 2); return 1; } iRows2 = (int)strlen((char*)pvData); iCols2 = 1; isMatrixOfString = 0; } break; } for (i = 0; i < NB_PROPERTIES_SUPPORTED; i++) { if (strcmp(propertiesSupported[i], pstProperty) == 0) { iPropertyFound = 1; } } if (iPropertyFound) { callSetProperty(pvApiCtx, 0, pvData, iType2, iRows2, iCols2, pstProperty); if (iType2 == sci_strings) { //free allocated data if (isMatrixOfString == 1) { freeAllocatedMatrixOfString(iRows2, iCols2, (char**)pvData); } else { freeAllocatedSingleString((char*)pvData); } } } else { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Wrong value for input argument #%d: a valid property expected.\n"), fname, 1); if (iType2 == sci_strings) { if (isMatrixOfString == 1) { freeAllocatedMatrixOfString(iRows2, iCols2, (char**)pvData); } else { freeAllocatedSingleString((char*)pvData); } } return 0; } freeAllocatedSingleString(pstProperty); AssignOutputVariable(pvApiCtx, 1) = 0; ReturnArguments(pvApiCtx); return 0; } if (iRhs % 2 != 1) { Scierror(999, _("%s: Wrong number of input argument(s) : an odd number is expected.\n"), fname); return 0; } /* after the call to sciSet get the status : 0 <=> OK, */ /* -1 <=> Error, */ /* 1 <=> nothing done */ /* set or create a graphic window */ if (isHandleType(pvApiCtx, piAddr1) == 0 && isStringType(pvApiCtx, piAddr1) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: A handle or a string expected.\n"), fname, 1); return 0; } if (isStringType(pvApiCtx, piAddr1)) { char* pstPath = NULL; if (getAllocatedSingleString(pvApiCtx, piAddr1, &pstPath)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, 1); return 1; } iObjUID = search_path(pstPath); if (iObjUID == 0) { Scierror(999, _("%s: Unable to find handle for path %s.\n"), fname, pstPath); freeAllocatedSingleString(pstPath); return 1; } } else { //matrix of handle are managed by a %h_set if (isScalar(pvApiCtx, piAddr1) == FALSE) { OverLoad(1); return 0; } if (getScalarHandle(pvApiCtx, piAddr1, (long long*)&hdl)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single handle expected.\n"), fname, 1); return 1; } iObjUID = getObjectFromHandle(hdl); } if (iObjUID == 0) { Scierror(999, _("%s: The handle is not or no more valid.\n"), fname); return 0; } for (i = 1 ; i < iRhs ; i = i + 2) { int setStatus = 0; int* piAddr2 = NULL; int* piAddr3 = NULL; int iPos = i + 1; int isData = 0; int iRows3 = 0; int iCols3 = 0; int iType3 = 0; void* pvData = NULL; char* pstProperty = NULL; sciErr = getVarAddressFromPosition(pvApiCtx, iPos, &piAddr2); if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, iPos); return 1; } if (isStringType(pvApiCtx, piAddr2) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, iPos); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddr2, &pstProperty)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, iPos); return 1; } sciErr = getVarAddressFromPosition(pvApiCtx, iPos + 1, &piAddr3); if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, iPos + 1); return 1; } if ((pstProperty[0] == 'd' || pstProperty[0] == 'D') && stricmp("data", pstProperty) == 0) { //send to datamodel isData = 1; } if (stricmp(pstProperty, "user_data") == 0 || stricmp(pstProperty, "userdata") == 0 || stricmp(pstProperty, "display_function_data") == 0 || stricmp(pstProperty, "data") == 0) { /* in this case set_user_data_property * directly uses the third position in the stack * to get the variable which is to be set in * the user_data property (any data type is allowed) S. Steer */ pvData = (void*)piAddr3; /*position in the stack */ iRows3 = -1; /*unused */ iCols3 = -1; /*unused */ iType3 = -1; } else { sciErr = getVarType(pvApiCtx, piAddr3, &iType3); if (sciErr.iErr) { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, iPos + 1); return 1; } switch (iType3) { case sci_matrix : sciErr = getMatrixOfDouble(pvApiCtx, piAddr3, &iRows3, &iCols3, (double**)&pvData); break; case sci_boolean : sciErr = getMatrixOfBoolean(pvApiCtx, piAddr3, &iRows3, &iCols3, (int**)&pvData); break; case sci_handles : sciErr = getMatrixOfHandle(pvApiCtx, piAddr3, &iRows3, &iCols3, (long long**)&pvData); break; case sci_strings : if (strcmp(pstProperty, "tics_labels") != 0 && strcmp(pstProperty, "auto_ticks") != 0 && strcmp(pstProperty, "tight_limits") != 0 && strcmp(pstProperty, "axes_visible") != 0 && strcmp(pstProperty, "axes_reverse") != 0 && strcmp(pstProperty, "text") != 0 && stricmp(pstProperty, "string") != 0 && stricmp(pstProperty, "tooltipstring") != 0 && stricmp(pstProperty, "ticks_format") != 0) /* Added for uicontrols */ { if (isScalar(pvApiCtx, piAddr3) == 0) { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, iPos + 1); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddr3, (char**)&pvData)) { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, iPos + 1); return 1; } iRows3 = (int)strlen((char*)pvData); iCols3 = 1; isMatrixOfString = 0; } else { isMatrixOfString = 1; getAllocatedMatrixOfString(pvApiCtx, piAddr3, &iRows3, &iCols3, (char***)&pvData); } break; case sci_list : iCols3 = 1; sciErr = getListItemNumber(pvApiCtx, piAddr3, &iRows3); pvData = (void*)piAddr3; /* In this case l3 is the list position in stack */ break; default : pvData = (void*)piAddr3; /* In this case l3 is the list position in stack */ break; } if (sciErr.iErr) { freeAllocatedSingleString(pstProperty); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, iPos + 1); return 1; } } setStatus = callSetProperty(pvApiCtx, iObjUID, pvData, iType3, iRows3, iCols3, pstProperty); if (iType3 == sci_strings) { //free allacted data if (isMatrixOfString == 1) { freeAllocatedMatrixOfString(iRows3, iCols3, (char**)pvData); } else { freeAllocatedSingleString((char*)pvData); } } freeAllocatedSingleString(pstProperty); } AssignOutputVariable(pvApiCtx, 1) = 0; ReturnArguments(pvApiCtx); return 0; }
/*--------------------------------------------------------------------------*/ int sci_msprintf(char *fname, unsigned long fname_len) { SciErr sciErr; int iType = 0; int *piAddressVarOne = NULL; char *ptrFormat = NULL; int K = 0; int i = 0; int lenghtFormat = 0; int NumberPercent = 0; int NumberCols = 0; int nmax = 0; int cat_to_last = 0; int ll = 0; char **pStrs = NULL; char **pOutputStrings = NULL; char *pStrTmp = NULL; char *pStrTmp1 = NULL; int lcount = 0; int rval = 0; int blk = 200; int k = 0; int mOut = 0; int nOut = 0; int lenghtSplitChar = (int)strlen(SPLIT_ON_CR_IN_FORMAT); Nbvars = 0; CheckRhs(1, 1000); CheckLhs(0, 1); for (K = 2; K <= Rhs; K++) { int iTypeK = 0; int *piAddressVarK = NULL; sciErr = getVarAddressFromPosition(pvApiCtx, K, &piAddressVarK); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarK, &iTypeK); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if ( (iTypeK != sci_matrix) && (iTypeK != sci_strings) ) { OverLoad(K); return 0; } } sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (checkVarDimension(pvApiCtx, piAddressVarOne, 1, 1) != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &ptrFormat)) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (ptrFormat == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } else { char *ptrFormatTmp = strsub(ptrFormat, CR_IN_FORMAT, SPLIT_ON_CR_IN_FORMAT); if (ptrFormatTmp) { freeAllocatedSingleString(ptrFormat); ptrFormat = ptrFormatTmp; } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } lenghtFormat = (int)strlen(ptrFormat); for (i = 0; i < lenghtFormat; i++) { if (ptrFormat[i] == PERCENT_CHAR) { NumberPercent++; if ( (i + 1 < lenghtFormat) && (ptrFormat[i + 1] == PERCENT_CHAR)) { NumberPercent--; i++; } } } if ( (Rhs - 1) > NumberPercent ) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: Wrong number of input arguments: at most %d expected.\n"), fname, NumberPercent); return 0; } if ( Rhs > 1 ) { for ( i = 2 ; i <= Rhs ; i++ ) { int iRows = 0; int iCols = 0; int *piAddressVarI = NULL; sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddressVarI); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i); return 0; } sciErr = getVarDimension(pvApiCtx, piAddressVarI, &iRows, &iCols); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i); return 0; } NumberCols += iCols; } } if ( NumberCols != NumberPercent ) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: Wrong number of input arguments: data doesn't fit with format.\n"), fname); return 0; } mOut = 0; /* output line counter */ nmax = 0; pOutputStrings = NULL; lcount = 1; cat_to_last = 0; while (1) { if ((rval = do_xxprintf("msprintf", (FILE *) 0, ptrFormat, Rhs, 1, lcount, (char **) &pStrs)) < 0) { break; } lcount++; pStrTmp = (char *)pStrs; if (pStrTmp == NULL) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: Wrong value of input argument #%d: data doesn't fit with format.\n"), fname, 1); return 0; } pStrTmp1 = pStrTmp; while (*pStrTmp != '\0') { if (strncmp(pStrTmp, SPLIT_ON_CR_IN_FORMAT, lenghtSplitChar) == 0) { k = (int)(pStrTmp - pStrTmp1); if (!cat_to_last) { /*add a new line */ if (mOut == nmax) { nmax += blk; if (pOutputStrings) { pOutputStrings = (char **) REALLOC(pOutputStrings, nmax * sizeof(char **)); } else { pOutputStrings = (char **) MALLOC(nmax * sizeof(char **)); } if (pOutputStrings == NULL) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: No more memory.\n"), fname); return 0; } } pOutputStrings[mOut] = (char*)MALLOC((k + 1) * sizeof(char)); if (pOutputStrings[mOut] == NULL) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: No more memory.\n"), fname); return 0; } strncpy(pOutputStrings[mOut], pStrTmp1, k); pOutputStrings[mOut][k] = EMPTY_CHAR; mOut++; } else { /* cat to previous line */ ll = (int)strlen(pOutputStrings[mOut - 1]); pOutputStrings[mOut - 1] = (char*)REALLOC(pOutputStrings[mOut - 1], (k + 1 + ll) * sizeof(char)); if (pOutputStrings[mOut - 1] == NULL) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: No more memory.\n"), fname); return 0; } strncpy(&(pOutputStrings[mOut - 1][ll]), pStrTmp1, k); pOutputStrings[mOut - 1][k + ll] = EMPTY_CHAR; } k = 0; pStrTmp += lenghtSplitChar; pStrTmp1 = pStrTmp; cat_to_last = 0; } else { pStrTmp++; } } k = (int)(pStrTmp - pStrTmp1); if (k > 0) { if ((!cat_to_last) || (mOut == 0)) { /*add a new line */ if (mOut == nmax) { nmax += blk; if (pOutputStrings) { pOutputStrings = (char **) REALLOC(pOutputStrings, nmax * sizeof(char **)); if (pOutputStrings == NULL) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: No more memory.\n"), fname); return 0; } } else { pOutputStrings = (char **) MALLOC(nmax * sizeof(char **)); if (pOutputStrings == NULL) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: No more memory.\n"), fname); return 0; } } } pOutputStrings[mOut] = (char*) MALLOC((k + 1) * sizeof(char)); if (pOutputStrings[mOut] == NULL) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: No more memory.\n"), fname); return 0; } strncpy(pOutputStrings[mOut], pStrTmp1, k); pOutputStrings[mOut][k] = EMPTY_CHAR; mOut++; } else { /* cat to previous line */ ll = (int)strlen(pOutputStrings[mOut - 1]); pOutputStrings[mOut - 1] = (char*)REALLOC(pOutputStrings[mOut - 1], (k + 1 + ll) * sizeof(char)); if (pOutputStrings[mOut - 1] == NULL) { if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } Scierror(999, _("%s: No more memory.\n"), fname); return 0; } strncpy(&(pOutputStrings[mOut - 1][ll]), pStrTmp1, k); pOutputStrings[mOut - 1][k + ll] = EMPTY_CHAR; } } if (strncmp(pStrTmp - lenghtSplitChar, SPLIT_ON_CR_IN_FORMAT, lenghtSplitChar) != 0) { cat_to_last = 1; } if (Rhs == 1) break; } if (ptrFormat) { FREE(ptrFormat); ptrFormat = NULL; } if (rval == RET_BUG) return 0; /* Create a Scilab String */ nOut = 1; sciErr = createMatrixOfString(pvApiCtx, Rhs + 1 , mOut, nOut, pOutputStrings); /* lstr must not be freed */ freeArrayOfString(pOutputStrings, mOut * nOut); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); } else { LhsVar(1) = Rhs + 1; PutLhsVar(); } return 0; }
int sci_gpuKronecker(char *fname) { CheckRhs(2, 2); CheckLhs(1, 1); SciErr sciErr; int* piAddr_A = NULL; int* piAddr_B = NULL; GpuPointer* gpuPtrA = NULL; GpuPointer* gpuPtrB = NULL; GpuPointer* gpuPtrC = NULL; double* h = NULL; double* hi = NULL; int rows = 0; int cols = 0; void* pvPtrA = NULL; void* pvPtrB = NULL; int inputType_A; int inputType_B; try { if (!isGpuInit()) { throw "gpu is not initialised. Please launch gpuInit() before use this function."; } sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr_A); if (sciErr.iErr) { throw sciErr; } sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr_B); if (sciErr.iErr) { throw sciErr; } /* ---- Check type of arguments and get data ---- */ /* */ /* Pointer to host / Pointer to device */ /* Matrix real / Matrix complex */ /* */ /* ---------------------------------------------- */ sciErr = getVarType(pvApiCtx, piAddr_A, &inputType_A); if (sciErr.iErr) { throw sciErr; } sciErr = getVarType(pvApiCtx, piAddr_B, &inputType_B); if (sciErr.iErr) { throw sciErr; } if (inputType_A == sci_pointer) { sciErr = getPointer(pvApiCtx, piAddr_A, (void**)&pvPtrA); if (sciErr.iErr) { throw sciErr; } gpuPtrA = (GpuPointer*)pvPtrA; if (!PointerManager::getInstance()->findGpuPointerInManager(gpuPtrA)) { throw "gpuKronecker : Bad type for input argument #1: Variables created with GPU functions expected."; } if (useCuda() && gpuPtrA->getGpuType() != GpuPointer::CudaType) { throw "gpuKronecker : Bad type for input argument #1: A Cuda pointer expected."; } if (useCuda() == false && gpuPtrA->getGpuType() != GpuPointer::OpenCLType) { throw "gpuKronecker : Bad type for input argument #1: A OpenCL pointer expected."; } } else if (inputType_A == sci_matrix) { if (isVarComplex(pvApiCtx, piAddr_A)) { sciErr = getComplexMatrixOfDouble(pvApiCtx, piAddr_A, &rows, &cols, &h, &hi); if (sciErr.iErr) { throw sciErr; } #ifdef WITH_CUDA if (useCuda()) { gpuPtrA = new PointerCuda(h, hi, rows, cols); } #endif #ifdef WITH_OPENCL if (!useCuda()) { throw "gpuKronecker: not implemented with OpenCL."; } #endif } else { sciErr = getMatrixOfDouble(pvApiCtx, piAddr_A, &rows, &cols, &h); if (sciErr.iErr) { throw sciErr; } #ifdef WITH_CUDA if (useCuda()) { gpuPtrA = new PointerCuda(h, rows, cols); } #endif #ifdef WITH_OPENCL if (!useCuda()) { throw "gpuKronecker: not implemented with OpenCL."; } #endif } } else { throw "gpuKronecker : Bad type for input argument #1: A GPU or CPU matrix expected."; } if (inputType_B == sci_pointer) { sciErr = getPointer(pvApiCtx, piAddr_B, (void**)&pvPtrB); if (sciErr.iErr) { throw sciErr; } gpuPtrB = (GpuPointer*)pvPtrB; if (!PointerManager::getInstance()->findGpuPointerInManager(gpuPtrB)) { throw "gpuKronecker : Bad type for input argument #2: Variables created with GPU functions expected."; } if (useCuda() && gpuPtrB->getGpuType() != GpuPointer::CudaType) { throw "gpuKronecker : Bad type for input argument #2: A Cuda pointer expected."; } if (useCuda() == false && gpuPtrB->getGpuType() != GpuPointer::OpenCLType) { throw "gpuKronecker : Bad type for input argument #2: A OpenCL pointer expected."; } } else if (inputType_B == sci_matrix) { if (isVarComplex(pvApiCtx, piAddr_B)) { sciErr = getComplexMatrixOfDouble(pvApiCtx, piAddr_B, &rows, &cols, &h, &hi); if (sciErr.iErr) { throw sciErr; } #ifdef WITH_CUDA if (useCuda()) { gpuPtrB = new PointerCuda(h, hi, rows, cols); } #endif #ifdef WITH_OPENCL if (!useCuda()) { Scierror(999, "gpuKronecker: not implemented with OpenCL.\n"); } #endif } else { sciErr = getMatrixOfDouble(pvApiCtx, piAddr_B, &rows, &cols, &h); if (sciErr.iErr) { throw sciErr; } #ifdef WITH_CUDA if (useCuda()) { gpuPtrB = new PointerCuda(h, rows, cols); } #endif #ifdef WITH_OPENCL if (!useCuda()) { Scierror(999, "gpuKronecker: not implemented with OpenCL.\n"); } #endif } } else { throw "gpuKronecker : Bad type for input argument #2: A GPU or CPU matrix expected."; } #ifdef WITH_OPENCL if (!useCuda()) { throw "gpuKronecker: not implemented with OpenCL."; } #endif //performe operation. gpuPtrC = gpuKronecker(gpuPtrA, gpuPtrB); // Keep the result on the Device. PointerManager::getInstance()->addGpuPointerInManager(gpuPtrC); sciErr = createPointer(pvApiCtx, Rhs + 1, (void*)gpuPtrC); if (sciErr.iErr) { throw sciErr; } LhsVar(1) = Rhs + 1; if (inputType_A == sci_matrix && gpuPtrA != NULL) { delete gpuPtrA; } if (inputType_B == sci_matrix && gpuPtrB != NULL) { delete gpuPtrB; } PutLhsVar(); return 0; } catch (const char* str) { Scierror(999, "%s\n", str); } catch (SciErr E) { printError(&E, 0); } if (useCuda()) { if (inputType_A == sci_matrix && gpuPtrA != NULL) { delete gpuPtrA; } if (inputType_B == sci_matrix && gpuPtrB != NULL) { delete gpuPtrB; } if (gpuPtrC != NULL) { delete gpuPtrC; } } return EXIT_FAILURE; }
/* ==================================================================== */ int sci_fsum(char *fname, void* pvApiCtx) { SciErr sciErr; int m1 = 0, n1 = 0; int *piAddressVarOne = NULL; double *pdVarOne = NULL; int iType1 = 0; int m2 = 0, n2 = 0; int *piAddressVarTwo = NULL; double *pdVarTwo = NULL; int iType2 = 0; int m_out = 0, n_out = 0; double dOut = 0.0; /* --> result = csum(3,8) /* check that we have only 2 parameters input */ /* check that we have only 1 parameters output */ CheckInputArgument(pvApiCtx, 2, 2) ; CheckOutputArgument(pvApiCtx, 1, 1) ; /* get Address of inputs */ sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } /* check input type */ sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if ( iType1 != sci_matrix ) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), fname, 1); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if ( iType2 != sci_matrix ) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), fname, 2); return 0; } /* get matrix */ sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &pdVarOne); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &pdVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } /* check size */ if ( (m1 != n1) && (n1 != 1) ) { Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 1); return 0; } if ( (m2 != n2) && (n2 != 1) ) { Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 2); return 0; } /* call fortran subroutine fsum */ F2C(fsum)(&pdVarOne[0], &pdVarTwo[0], &dOut); /* create result on stack */ m_out = 1; n_out = 1; sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m_out, n_out, &dOut); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; return 0; }
/*--------------------------------------------------------------------------*/ int sci_frexp(char *fname, void *pvApiCtx) { SciErr sciErr; int i; int iRows = 0; int iCols = 0; int iType = 0; int *piAddr = NULL; double *pdblReal = NULL; double *pdblCoef = NULL; double *pdblExp = NULL; CheckRhs(1, 1); CheckLhs(2, 2); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getVarType(pvApiCtx, piAddr, &iType); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (iType != sci_matrix) { OverLoad(1); return 0; } if (isVarComplex(pvApiCtx, piAddr)) { Scierror(999, _("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 1); return 0; } sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = allocMatrixOfDouble(pvApiCtx, Rhs + 1, iRows, iCols, &pdblCoef); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = allocMatrixOfDouble(pvApiCtx, Rhs + 2, iRows, iCols, &pdblExp); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } for (i = 0; i < iRows * iCols; i++) { pdblCoef[i] = dfrexps(pdblReal[i], &pdblExp[i]); } LhsVar(1) = Rhs + 1; LhsVar(2) = Rhs + 2; PutLhsVar(); return 0; }
/* * The generic solution to the multiplex operators is to translate * them to a MAL loop. * The call optimizer.multiplex(MOD,FCN,A1,...An) introduces the following code * structure: * * resB:= bat.new(A1); * barrier (h,t1):= iterator.new(A1); * t2:= algebra.fetch(A2,h) * ... * cr:= MOD.FCN(t1,...,tn); * bat.append(resB,cr); * redo (h,t):= iterator.next(A1); * end h; * * The algorithm consists of two phases: phase one deals with * collecting the relevant information, phase two is the actual * code construction. */ static str OPTexpandMultiplex(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { int i = 2, iter = 0; int hvar, tvar; str mod, fcn; int *alias, *resB; InstrPtr q; int tt; int bat = (getModuleId(pci) == batmalRef) ; //if ( optimizerIsApplied(mb,"multiplex")) //return 0; (void) cntxt; (void) stk; for (i = 0; i < pci->retc; i++) { tt = getBatType(getArgType(mb, pci, i)); if (tt== TYPE_any) throw(MAL, "optimizer.multiplex", SQLSTATE(HY002) "Target tail type is missing"); if (isAnyExpression(getArgType(mb, pci, i))) throw(MAL, "optimizer.multiplex", SQLSTATE(HY002) "Target type is missing"); } mod = VALget(&getVar(mb, getArg(pci, pci->retc))->value); mod = putName(mod); fcn = VALget(&getVar(mb, getArg(pci, pci->retc+1))->value); fcn = putName(fcn); if(mod == NULL || fcn == NULL) throw(MAL, "optimizer.multiplex", SQLSTATE(HY001) MAL_MALLOC_FAIL); #ifndef NDEBUG fprintf(stderr,"#WARNING To speedup %s.%s a bulk operator implementation is needed\n#", mod,fcn); fprintInstruction(stderr, mb, stk, pci, LIST_MAL_DEBUG); #endif /* search the iterator bat */ for (i = pci->retc+2; i < pci->argc; i++) if (isaBatType(getArgType(mb, pci, i))) { iter = getArg(pci, i); break; } if( i == pci->argc) throw(MAL, "optimizer.multiplex", SQLSTATE(HY002) "Iterator BAT type is missing"); #ifdef DEBUG_OPT_MULTIPLEX { char *tpenme; fprintf(stderr,"#calling the optimize multiplex script routine\n"); fprintFunction(stderr,mb, 0, LIST_MAL_ALL ); tpenme = getTypeName(getVarType(mb,iter)); fprintf(stderr,"#multiplex against operator %d %s\n",iter, tpenme); GDKfree(tpenme); fprintInstruction(stderr,mb, 0, pci,LIST_MAL_ALL); } #endif /* * Beware, the operator constant (arg=1) is passed along as well, * because in the end we issue a recursive function call that should * find the actual arguments at the proper place of the callee. */ alias= (int*) GDKmalloc(sizeof(int) * pci->maxarg); resB = (int*) GDKmalloc(sizeof(int) * pci->retc); if (alias == NULL || resB == NULL) { GDKfree(alias); GDKfree(resB); return NULL; } /* resB := new(refBat) */ for (i = 0; i < pci->retc; i++) { q = newFcnCall(mb, batRef, newRef); resB[i] = getArg(q, 0); tt = getBatType(getArgType(mb, pci, i)); setVarType(mb, getArg(q, 0), newBatType(tt)); q = pushType(mb, q, tt); } /* barrier (h,r) := iterator.new(refBat); */ q = newFcnCall(mb, iteratorRef, newRef); q->barrier = BARRIERsymbol; hvar = newTmpVariable(mb, TYPE_any); getArg(q,0) = hvar; tvar = newTmpVariable(mb, TYPE_any); q= pushReturn(mb, q, tvar); (void) pushArgument(mb,q,iter); /* $1:= algebra.fetch(Ai,h) or constant */ for (i = pci->retc+2; i < pci->argc; i++) { if (getArg(pci, i) != iter && isaBatType(getArgType(mb, pci, i))) { q = newFcnCall(mb, algebraRef, "fetch"); alias[i] = newTmpVariable(mb, getBatType(getArgType(mb, pci, i))); getArg(q, 0) = alias[i]; q= pushArgument(mb, q, getArg(pci, i)); (void) pushArgument(mb, q, hvar); } } /* cr:= mod.CMD($1,...,$n); */ q = newFcnCall(mb, mod, fcn); for (i = 0; i < pci->retc; i++) { int nvar = 0; if (bat) { tt = getBatType(getArgType(mb, pci, i)); nvar = newTmpVariable(mb, newBatType(tt)); } else { nvar = newTmpVariable(mb, TYPE_any); } if (i) q = pushReturn(mb, q, nvar); else getArg(q, 0) = nvar; } for (i = pci->retc+2; i < pci->argc; i++) { if (getArg(pci, i) == iter) { q = pushArgument(mb, q, tvar); } else if (isaBatType(getArgType(mb, pci, i))) { q = pushArgument(mb, q, alias[i]); } else { q = pushArgument(mb, q, getArg(pci, i)); } } for (i = 0; i < pci->retc; i++) { InstrPtr a = newFcnCall(mb, batRef, appendRef); a = pushArgument(mb, a, resB[i]); (void) pushArgument(mb, a, getArg(q,i)); } /* redo (h,r):= iterator.next(refBat); */ q = newFcnCall(mb, iteratorRef, nextRef); q->barrier = REDOsymbol; getArg(q,0) = hvar; q= pushReturn(mb, q, tvar); (void) pushArgument(mb,q,iter); q = newAssignment(mb); q->barrier = EXITsymbol; getArg(q,0) = hvar; (void) pushReturn(mb, q, tvar); for (i = 0; i < pci->retc; i++) { q = newAssignment(mb); getArg(q, 0) = getArg(pci, i); (void) pushArgument(mb, q, resB[i]); } GDKfree(alias); GDKfree(resB); return MAL_SUCCEED; }
/*--------------------------------------------------------------------------*/ int get_rect_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], double ** rect) { int m, n, first_opt = FirstOpt(), kopt, i; if (pos < first_opt) { int* piAddr = 0; int iType = 0; double* pdblData = NULL; getVarAddressFromPosition(_pvCtx, pos, &piAddr); getVarType(_pvCtx, piAddr, &iType); if (iType) { getMatrixOfDouble(_pvCtx, piAddr, &m, &n, &pdblData); if (m * n != 4) { Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, pos, 4); return 0; } *rect = pdblData; for (i = 0; i < 4; i++) { if (finite((*rect)[i]) == 0) { Scierror(999, "%s: Wrong values (Nan or Inf) for input argument: %d finite values expected\n", fname, 4); return 0; } } } else { /** global value can be modified **/ double zeros[4] = { 0.0, 0.0, 0.0, 0.0 }; setDefRect(zeros); *rect = getDefRect(); } } else if ((kopt = FindOpt("rect", opts))) /* named argument: rect=value */ { int* piAddr = 0; double* pdblData = NULL; getVarAddressFromPosition(_pvCtx, kopt, &piAddr); getMatrixOfDouble(_pvCtx, piAddr, &m, &n, &pdblData); if (m * n != 4) { Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, kopt, 4); return 0; } *rect = pdblData; for (i = 0; i < 4; i++) { if (finite((*rect)[i]) == 0) { Scierror(999, "%s: Wrong values (Nan or Inf) for input argument: %d finite values expected\n", fname, 4); return 0; } } } else { /** global value can be modified **/ double zeros[4] = { 0.0, 0.0, 0.0, 0.0 }; setDefRect(zeros); *rect = getDefRect(); } return 1; }
/* ========================================================================== */ int sci_gpuLU(char *fname) { CheckRhs(1,2); CheckLhs(2,2); #ifdef WITH_CUDA cublasStatus status; #endif SciErr sciErr; int* piAddr_A = NULL; double* h_A = NULL; double* hi_A = NULL; int rows_A; int cols_A; int* piAddr_Opt = NULL; double* option = NULL; int rows_Opt; int cols_Opt; void* d_A = NULL; int na; void* pvPtr = NULL; int size_A = sizeof(double); bool bComplex_A = FALSE; int inputType_A; int inputType_Opt; double res; int posOutput = 1; try { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr_A); if(sciErr.iErr) throw sciErr; if(Rhs == 2) { sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr_Opt); if(sciErr.iErr) throw sciErr; sciErr = getVarType(pvApiCtx, piAddr_Opt, &inputType_Opt); if(sciErr.iErr) throw sciErr; if(inputType_Opt == sci_matrix) { sciErr = getMatrixOfDouble(pvApiCtx, piAddr_Opt, &rows_Opt, &cols_Opt, &option); if(sciErr.iErr) throw sciErr; } else throw "Option syntax is [number,number]."; } else { rows_Opt=1; cols_Opt=2; option = (double*)malloc(2*sizeof(double)); option[0]=0; option[1]=0; } if(rows_Opt != 1 || cols_Opt != 2) throw "Option syntax is [number,number]."; if((int)option[1] == 1 && !isGpuInit()) throw "gpu is not initialised. Please launch gpuInit() before use this function."; sciErr = getVarType(pvApiCtx, piAddr_A, &inputType_A); if(sciErr.iErr) throw sciErr; #ifdef WITH_CUDA if (useCuda()) { if(inputType_A == sci_pointer) { sciErr = getPointer(pvApiCtx, piAddr_A, (void**)&pvPtr); if(sciErr.iErr) throw sciErr; gpuMat_CUDA* gmat; gmat = static_cast<gpuMat_CUDA*>(pvPtr); if(!gmat->useCuda) throw "Please switch to OpenCL mode before use this data."; rows_A=gmat->rows; cols_A=gmat->columns; if(gmat->complex) { bComplex_A = TRUE; size_A = sizeof(cuDoubleComplex); d_A=(cuDoubleComplex*)gmat->ptr->get_ptr(); } else d_A=(double*)gmat->ptr->get_ptr(); // Initialize CUBLAS status = cublasInit(); if (status != CUBLAS_STATUS_SUCCESS) throw status; na = rows_A * cols_A; } else if(inputType_A == 1) { // Get size and data if(isVarComplex(pvApiCtx, piAddr_A)) { sciErr = getComplexMatrixOfDouble(pvApiCtx, piAddr_A, &rows_A, &cols_A, &h_A, &hi_A); if(sciErr.iErr) throw sciErr; size_A = sizeof(cuDoubleComplex); bComplex_A = TRUE; } else { sciErr = getMatrixOfDouble(pvApiCtx, piAddr_A, &rows_A, &cols_A, &h_A); if(sciErr.iErr) throw sciErr; } na = rows_A * cols_A; // Initialize CUBLAS status = cublasInit(); if (status != CUBLAS_STATUS_SUCCESS) throw status; // Allocate device memory status = cublasAlloc(na, size_A, (void**)&d_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; // Initialize the device matrices with the host matrices if(!bComplex_A) { status = cublasSetMatrix(rows_A,cols_A, sizeof(double), h_A, rows_A, (double*)d_A, rows_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; } else writecucomplex(h_A, hi_A, rows_A, cols_A, (cuDoubleComplex *)d_A); } else throw "Bad argument type."; cuDoubleComplex resComplex; // Performs operation if(!bComplex_A) status = decomposeBlockedLU(rows_A, cols_A, rows_A, (double*)d_A, 1); // else // resComplex = cublasZtrsm(na,(cuDoubleComplex*)d_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; // Put the result in scilab switch((int)option[0]) { case 2 : case 1 : sciprint("The first option must be 0 for this function. Considered as 0.\n"); case 0 : // Keep the result on the Host. { // Put the result in scilab if(!bComplex_A) { double* h_res = NULL; sciErr=allocMatrixOfDouble(pvApiCtx, Rhs + posOutput, rows_A, cols_A, &h_res); if(sciErr.iErr) throw sciErr; status = cublasGetMatrix(rows_A,cols_A, sizeof(double), (double*)d_A, rows_A, h_res, rows_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; } else { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + posOutput, 1, 1, &resComplex.x,&resComplex.y); if(sciErr.iErr) throw sciErr; } LhsVar(posOutput)=Rhs+posOutput; posOutput++; break; } default : throw "First option argument must be 0 or 1 or 2."; } switch((int)option[1]) { case 0 : // Don't keep the data input on Device. { if(inputType_A == sci_matrix) { status = cublasFree(d_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; d_A = NULL; } break; } case 1 : // Keep data of the fisrt argument on Device and return the Device pointer. { if(inputType_A == sci_matrix) { gpuMat_CUDA* dptr; gpuMat_CUDA tmp={getCudaContext()->genMatrix<double>(getCudaQueue(),rows_A*cols_A),rows_A,cols_A}; dptr=new gpuMat_CUDA(tmp); dptr->useCuda = true; dptr->ptr->set_ptr((double*)d_A); if(bComplex_A) dptr->complex=TRUE; else dptr->complex=FALSE; sciErr = createPointer(pvApiCtx,Rhs+posOutput, (void*)dptr); if(sciErr.iErr) throw sciErr; LhsVar(posOutput)=Rhs+posOutput; } else throw "The first input argument is already a GPU variable."; posOutput++; break; } default : throw "Second option argument must be 0 or 1."; } // Shutdown status = cublasShutdown(); if (status != CUBLAS_STATUS_SUCCESS) throw status; } #endif #ifdef WITH_OPENCL if (!useCuda()) { throw "not implemented with OpenCL."; } #endif if(Rhs == 1) { free(option); option = NULL; } if(posOutput < Lhs+1) throw "Too many output arguments."; if(posOutput > Lhs+1) throw "Too few output arguments."; PutLhsVar(); return 0; } catch(const char* str) { Scierror(999,"%s\n",str); } catch(SciErr E) { printError(&E, 0); } #ifdef WITH_CUDA catch(cudaError_t cudaE) { GpuError::treat_error<CUDAmode>((CUDAmode::Status)cudaE); } catch(cublasStatus CublasE) { GpuError::treat_error<CUDAmode>((CUDAmode::Status)CublasE,1); } if (useCuda()) { if(inputType_A == 1 && d_A != NULL) cudaFree(d_A); } #endif #ifdef WITH_OPENCL if (!useCuda()) { Scierror(999,"not implemented with OpenCL.\n"); } #endif if(Rhs == 1 && option != NULL) free(option); return EXIT_FAILURE; }
/*--------------------------------------------------------------------------*/ int get_style_arg(void* _pvCtx, char *fname, int pos, int n1, rhs_opts opts[], int ** style) { int m = 0, n = 0, first_opt = FirstOpt(), kopt = 0, un = 1, ix = 0, i = 0, l1 = 0; if ( pos < first_opt ) /* regular argument */ { int* piAddr = 0; int iType = 0; int* piData = NULL; getVarAddressFromPosition(_pvCtx, pos, &piAddr); getVarType(_pvCtx, piAddr, &iType); if (iType) { getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData); if (m * n < n1) { Scierror(999, _("%s: Wrong size for input argument #%d: %d < %d expected.\n"), fname, pos, m * n, n1); return 0; } if ( n1 == 1 && m * n == 1 ) { *style = (int*)MALLOC(2 * sizeof(int)); (*style)[0] = piData[0]; (*style)[1] = 1; } else { *style = (int*)MALLOC(m * n * sizeof(int)); for (i = 0; i < m * n; i++) { (*style)[i] = piData[i]; } } } else /* zero type argument --> default value */ { ix = Max(n1, 2); *style = (int*)MALLOC(ix * sizeof(int)); (*style)[1] = 1; for ( i = 0 ; i < n1 ; ++i ) { (*style)[i] = i + 1; } } } else if ((kopt = FindOpt("style", opts))) { /* optinal argument: style=value */ int* piAddr = 0; int* piData = NULL; getVarAddressFromPosition(_pvCtx, kopt, &piAddr); getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData); if (m * n < n1) { Scierror(999, _("%s: Wrong size for input argument #%d: %d < %d expected.\n"), fname, kopt, m * n, n1); return 0; } if (n1 == 1 && m * n == 1) { *style = (int*)MALLOC(2 * sizeof(int)); (*style)[0] = piData[0]; (*style)[1] = 1; } else { *style = (int*)MALLOC(m * n * sizeof(int)); for (i = 0; i < m * n; i++) { (*style)[i] = piData[i]; } } } else /* unspecified argument --> default value */ { ix = Max(n1, 2); *style = (int*)MALLOC(ix * sizeof(int)); (*style)[1] = 1; for (i = 0 ; i < n1 ; ++i) { (*style)[i] = i + 1; } } return 1; }
int getOptionals(void* _pvCtx, char* pstFuncName, rhs_opts opts[]) { types::GatewayStruct* pStr = (types::GatewayStruct*)_pvCtx; types::optional_list opt = *pStr->m_pOpt; int i = 0; /* reset first field since opts is declared static in calling function */ while (opts[i].pstName != NULL) { opts[i].iPos = -1; i++; } for (const auto& o : opt) { int typeOfOpt = -1; char* pstOpts = wide_string_to_UTF8(o.first.c_str()); int index = findOptional(_pvCtx, pstOpts, opts); FREE(pstOpts); if (index < 0) { sciprint(_("%ls: Unrecognized optional arguments %ls.\n"), pStr->m_pstName, o.first.c_str()); printOptionalNames(_pvCtx, opts); return 0; } opts[index].iPos = i + 1; types::GenericType* pGT = (types::GenericType*)o.second; getVarType(_pvCtx, (int*)pGT, &typeOfOpt); opts[index].iType = typeOfOpt; if (typeOfOpt == sci_implicit_poly) { types::InternalType* pIT = NULL; types::ImplicitList* pIL = pGT->getAs<types::ImplicitList>(); pIT = pIL->extractFullMatrix(); types::Double* impResult = (types::Double*)pIT; opts[index].iRows = impResult->getRows(); opts[index].iCols = impResult->getCols(); opts[index].piAddr = (int*)impResult; opts[index].iType = sci_matrix; } else { opts[index].iRows = pGT->getRows(); opts[index].iCols = pGT->getCols(); opts[index].piAddr = (int*)pGT; } } // int index = -1; //GatewayStruct* pStr = (GatewayStruct*)_pvCtx; // wchar_t* pwstProperty = to_wide_string(pstProperty); // for(int i = 0 ; i < pStr->m_pOpt->size() ; i++) // { // std::pair<std::wstring, InternalType*> current = (*pStr->m_pOpt)[i]; // if(wcscmp(current.first.c_str(), pwstProperty) == 0) // { // index = i; // break; // } // } // FREE(pwstProperty); return 1; }
int inlineMALblock(MalBlkPtr mb, int pc, MalBlkPtr mc) { int i, k, l, n; InstrPtr *ns, p,q; int *nv, *np = NULL; p = getInstrPtr(mb, pc); q = getInstrPtr(mc, 0); ns = GDKzalloc((l = (mb->ssize + mc->ssize + p->retc - 3)) * sizeof(InstrPtr)); if (ns == NULL) return -1; if ( mc->ptop > 0){ np = (int*) GDKmalloc(mc->ptop * sizeof(int)); if (np == 0){ GDKfree(ns); return -1; } } nv = (int*) GDKmalloc(mc->vtop * sizeof(int)); if (nv == 0){ GDKfree(ns); if( np) GDKfree(np); return -1; } /* add all properties of the new block to the target environment */ for (n = 0; n < mc->ptop; n++) { int propid = newProperty(mb); if (propid < 0) { assert(0); return -1; } np[n] = propid; mb->prps[propid].idx = mc->prps[n].idx; mb->prps[propid].op = mc->prps[n].op; mb->prps[propid].var = mc->prps[n].var; /* fixed later */ } /* add all variables of the new block to the target environment */ for (n = 0; n < mc->vtop; n++) { VarPtr ov, v; if (isExceptionVariable(mc->var[n]->name)) { nv[n] = newVariable(mb,GDKstrdup(mc->var[n]->name),TYPE_str); if (isVarUDFtype(mc,n)) setVarUDFtype(mb,nv[n]); if (isVarUsed(mc,n)) setVarUsed(mb,nv[n]); } else if (isVarTypedef(mc,n)) { nv[n] = newTypeVariable(mb,getVarType(mc,n)); } else if (isVarConstant(mc,n)) { nv[n] = cpyConstant(mb,getVar(mc,n)); } else { nv[n] = newTmpVariable(mb, getVarType(mc, n)); if (isVarUDFtype(mc,n)) setVarUDFtype(mb,nv[n]); if (isVarUsed(mc,n)) setVarUsed(mb,nv[n]); } /* remap the properties */ ov = getVar(mc, n); v = getVar(mb, nv[n]); if (ov->propc > v->maxprop) { int size = sizeof(VarRecord); VarPtr vnew = (VarPtr) GDKzalloc(size + ov->propc * sizeof(int)); memcpy((char*) vnew, (char*) v, size); vnew->maxprop = ov->propc; mb->var[nv[n]] = vnew; GDKfree(v); v = getVar(mb, nv[n]); } for (i = 0; i < ov->propc; i++) v->prps[i] = np[ov->prps[i]]; v->propc = ov->propc; } /* change the property variables to the new context */ for (n = 0; n < mc->ptop; n++) { if (mc->prps[n].var) mb->prps[np[n]].var = nv[mc->prps[n].var]; assert( mb->prps[np[n]].var >= 0); } /* use an alias mapping to keep track of the actual arguments */ for (n = p->retc; n < p->argc; n++) nv[getArg(q,n)] = getArg(p, n); k = 0; /* find the return statement of the inline function */ for (i = 1; i < mc->stop - 1; i++) { q = mc->stmt[i]; if( q->barrier== RETURNsymbol || q->barrier== YIELDsymbol){ /* add the mapping of the return variables */ for(n=0; n<p->retc; n++) nv[getArg(q,n)] = getArg(p,n); } } /* copy the stable part */ for (i = 0; i < pc; i++) ns[k++] = mb->stmt[i]; for (i = 1; i < mc->stop - 1; i++) { q = mc->stmt[i]; if( q->token == ENDsymbol) break; /* copy the instruction and fix variable references */ ns[k] = copyInstruction(q); for (n = 0; n < q->argc; n++) getArg(ns[k], n) = nv[getArg(q, n)]; if (q->barrier == RETURNsymbol || q->barrier == YIELDsymbol) { for(n=0; n<q->retc; n++) clrVarFixed(mb,getArg(ns[k],n)); /* for typing */ setModuleId(ns[k],getModuleId(q)); setFunctionId(ns[k],getFunctionId(q)); ns[k]->barrier = 0; ns[k]->token = ASSIGNsymbol; } k++; } /* copy the remainder of the stable part */ freeInstruction(p); for (i = pc + 1; i < mb->stop; i++){ ns[k++] = mb->stmt[i]; } /* remove any free instruction */ for(; i<mb->ssize; i++) if( mb->stmt[i]){ freeInstruction(mb->stmt[i]); mb->stmt[i]= 0; } GDKfree(mb->stmt); mb->stmt = ns; mb->ssize = l; mb->stop = k; GDKfree(np); GDKfree(nv); return pc; }
/*--------------------------------------------------------------------------*/ int sci_figure(char * fname, void* pvApiCtx) { SciErr sciErr; int* piAddr = NULL; int iFig = 0; int iRhs = nbInputArgument(pvApiCtx); int iId = 0; int iPos = 0; int i = 0; int iAxes = 0; int iPropertyOffset = 0; BOOL bDoCreation = TRUE; BOOL bVisible = TRUE; // Create a visible figure by default BOOL bDockable = TRUE; // Create a dockable figure by default BOOL bDefaultAxes = TRUE; // Create an Axes by default double* figureSize = NULL; double* axesSize = NULL; double* position = NULL; double val[4]; BOOL bMenuBar = TRUE; BOOL bToolBar = TRUE; BOOL bInfoBar = TRUE; BOOL bResize = TRUE; int iMenubarType = 1; // Create a 'figure' menubar by default int iToolbarType = 1; // Create a 'figure' toolbar by default double dblId = 0; BOOL status = FALSE; //figure(num) -> scf(num) //figure() -> scf() //figure(x, "...", ...) // figure() if (iRhs == 0) // Auto ID { iId = getValidDefaultFigureId(); iFig = createNewFigureWithAxes(); setGraphicObjectProperty(iFig, __GO_ID__, &iId, jni_int, 1); iAxes = setDefaultProperties(iFig, TRUE); initBar(iFig, bMenuBar, bToolBar, bInfoBar); createScalarHandle(pvApiCtx, iRhs + 1, getHandle(iFig)); AssignOutputVariable(pvApiCtx, 1) = iRhs + 1; ReturnArguments(pvApiCtx); return 0; } if (iRhs == 1) { //figure(x); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (isVarMatrixType(pvApiCtx, piAddr) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: An integer value expected.\n"), fname, 1); return 0; } if (getScalarDouble(pvApiCtx, piAddr, &dblId)) { Scierror(999, _("%s: No more memory.\n"), fname); return 0; } iId = (int)(dblId + 0.5); //avoid 1.999 -> 1 //get current fig from id iFig = getFigureFromIndex(iId); if (iFig == 0) // Figure does not exists, create a new one { iFig = createNewFigureWithAxes(); setGraphicObjectProperty(iFig, __GO_ID__, &iId, jni_int, 1); iAxes = setDefaultProperties(iFig, TRUE); } initBar(iFig, bMenuBar, bToolBar, bInfoBar); createScalarHandle(pvApiCtx, iRhs + 1, getHandle(iFig)); AssignOutputVariable(pvApiCtx, 1) = iRhs + 1; ReturnArguments(pvApiCtx); return 0; } // Prepare property analysis if (iRhs % 2 == 0) { //get highest value of winsid to create the new windows @ + 1 iId = getValidDefaultFigureId(); iPos = 0; } else { iPos = 1; //figure(x, ...); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (isVarMatrixType(pvApiCtx, piAddr) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: An integer value expected.\n"), fname, 1); return 0; } if (getScalarDouble(pvApiCtx, piAddr, &dblId)) { Scierror(999, _("%s: No more memory.\n"), fname); return 0; } iId = (int)(dblId + 0.5); //avoid 1.999 -> 1 //get current fig from id iFig = getFigureFromIndex(iId); if (iFig != 0) // Figure already exists { bDoCreation = FALSE; } } if (bDoCreation) { int* piAddrProp = NULL; char* pstProName = NULL; int* piAddrData = NULL; for (i = iPos + 1 ; i <= iRhs ; i += 2) { //get property name sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddrProp); if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddrProp, &pstProName)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i); return 1; } if (stricmp(pstProName, "dockable") != 0 && stricmp(pstProName, "toolbar") != 0 && stricmp(pstProName, "menubar") != 0 && stricmp(pstProName, "default_axes") != 0 && stricmp(pstProName, "visible") != 0 && stricmp(pstProName, "figure_size") != 0 && stricmp(pstProName, "axes_size") != 0 && stricmp(pstProName, "position") != 0 && stricmp(pstProName, "menubar_visible") != 0 && stricmp(pstProName, "toolbar_visible") != 0 && stricmp(pstProName, "resize") != 0 && stricmp(pstProName, "infobar_visible") != 0) { freeAllocatedSingleString(pstProName); continue; } //get address of value on stack sciErr = getVarAddressFromPosition(pvApiCtx, i + 1, &piAddrData); if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i + 1); freeAllocatedSingleString(pstProName); return 1; } //check property value to compatibility if (stricmp(pstProName, "dockable") == 0) { bDockable = getStackArgumentAsBoolean(pvApiCtx, piAddrData); if (bDockable == -1) { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "dockable", "on", "off"); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "toolbar") == 0) { char* pstVal = NULL; if (isStringType(pvApiCtx, piAddrData) == FALSE || isScalar(pvApiCtx, piAddrData) == FALSE) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i); freeAllocatedSingleString(pstProName); } if (getAllocatedSingleString(pvApiCtx, piAddrData, &pstVal)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i + 1); freeAllocatedSingleString(pstProName); return 1; } if (stricmp(pstVal, "none") == 0) { iToolbarType = 0; } else if (stricmp(pstVal, "figure") == 0) { iToolbarType = 1; } else { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "toolbar", "none", "figure"); freeAllocatedSingleString(pstProName); freeAllocatedSingleString(pstVal); return 1; } freeAllocatedSingleString(pstVal); } else if (stricmp(pstProName, "menubar") == 0) { char* pstVal = NULL; if (isStringType(pvApiCtx, piAddrData) == FALSE || isScalar(pvApiCtx, piAddrData) == FALSE) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i + 1); freeAllocatedSingleString(pstProName); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddrData, &pstVal)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i + 1); freeAllocatedSingleString(pstProName); return 1; } if (stricmp(pstVal, "none") == 0) { iMenubarType = 0; } else if (stricmp(pstVal, "figure") == 0) { iMenubarType = 1; } else { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "menubar", "none", "figure"); freeAllocatedSingleString(pstProName); freeAllocatedSingleString(pstVal); return 1; } freeAllocatedSingleString(pstVal); } else if (stricmp(pstProName, "default_axes") == 0) { bDefaultAxes = getStackArgumentAsBoolean(pvApiCtx, piAddrData); if (bDefaultAxes == -1) { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "default_axes", "on", "off"); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "visible") == 0) { bVisible = getStackArgumentAsBoolean(pvApiCtx, piAddrData); if (bVisible == -1) { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "visible", "on", "off"); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "figure_size") == 0) { int iRows = 0; int iCols = 0; if (isDoubleType(pvApiCtx, piAddrData) == FALSE) { Scierror(999, _("%s: Wrong type for input argument #%d: A double vector expected.\n"), fname, i + 1); freeAllocatedSingleString(pstProName); return 1; } getMatrixOfDouble(pvApiCtx, piAddrData, &iRows, &iCols, &figureSize); if (iRows * iCols != 2) { Scierror(999, _("Wrong size for '%s' property: %d elements expected.\n"), "figure_size", 2); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "axes_size") == 0) { int iRows = 0; int iCols = 0; if (isDoubleType(pvApiCtx, piAddrData) == FALSE) { Scierror(999, _("%s: Wrong type for input argument #%d: A double vector expected.\n"), fname, i + 1); freeAllocatedSingleString(pstProName); return 1; } getMatrixOfDouble(pvApiCtx, piAddrData, &iRows, &iCols, &axesSize); if (iRows * iCols != 2) { Scierror(999, _("Wrong size for '%s' property: %d elements expected.\n"), "axes_size", 2); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "position") == 0) { int iRows = 0; int iCols = 0; double* pdbl = NULL; if (isDoubleType(pvApiCtx, piAddrData)) { getMatrixOfDouble(pvApiCtx, piAddrData, &iRows, &iCols, &pdbl); if (iRows * iCols != 4) { Scierror(999, _("Wrong size for '%s' property: %d elements expected.\n"), "position", 4); freeAllocatedSingleString(pstProName); return 1; } position = pdbl; axesSize = (pdbl + 2); } else if (isStringType(pvApiCtx, piAddrData) && isScalar(pvApiCtx, piAddrData)) { char* pstVal = NULL; int iVal = 0; if (getAllocatedSingleString(pvApiCtx, piAddrData, &pstVal)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i + 1); freeAllocatedSingleString(pstProName); return 1; } iVal = sscanf(pstVal, "%lf|%lf|%lf|%lf", &val[0], &val[1], &val[2], &val[3]); freeAllocatedSingleString(pstVal); if (iVal != 4) { Scierror(999, _("Wrong value for '%s' property: string or 1 x %d real row vector expected.\n"), "position", 4); freeAllocatedSingleString(pstProName); return 1; } position = val; axesSize = (val + 2); } else { Scierror(999, _("Wrong value for '%s' property: string or 1 x %d real row vector expected.\n"), "position", 4); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "resize") == 0) { bResize = getStackArgumentAsBoolean(pvApiCtx, piAddrData); if (bResize == -1) { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "resize", "on", "off"); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "menubar_visible") == 0) { bMenuBar = getStackArgumentAsBoolean(pvApiCtx, piAddrData); if (bMenuBar == -1) { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "menubar_visible", "on", "off"); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "toolbar_visible") == 0) { bToolBar = getStackArgumentAsBoolean(pvApiCtx, piAddrData); if (bToolBar == -1) { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "toolbar_visible", "on", "off"); freeAllocatedSingleString(pstProName); return 1; } } else if (stricmp(pstProName, "infobar_visible") == 0) { bInfoBar = getStackArgumentAsBoolean(pvApiCtx, piAddrData); if (bInfoBar == -1) { Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "infobar_visible", "on", "off"); freeAllocatedSingleString(pstProName); return 1; } } freeAllocatedSingleString(pstProName); } iFig = createFigure(bDockable, iMenubarType, iToolbarType, bDefaultAxes, bVisible); setGraphicObjectProperty(iFig, __GO_ID__, &iId, jni_int, 1); iAxes = setDefaultProperties(iFig, bDefaultAxes); } //set(iFig, iPos, iPos + 1) for (i = iPos + 1 ; i <= iRhs ; i += 2) { int isMatrixOfString = 0; int* piAddrProp = NULL; char* pstProName = NULL; int* piAddrData = NULL; int iRows = 0; int iCols = 0; void* _pvData = NULL; int iType = 0; //get property name sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddrProp); if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddrProp, &pstProName)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i); return 1; } if (bDoCreation && ( stricmp(pstProName, "dockable") == 0 || stricmp(pstProName, "toolbar") == 0 || stricmp(pstProName, "menubar") == 0 || stricmp(pstProName, "default_axes") == 0 || stricmp(pstProName, "visible") == 0 || stricmp(pstProName, "figure_size") == 0 || stricmp(pstProName, "axes_size") == 0 || stricmp(pstProName, "position") == 0 || stricmp(pstProName, "resize") == 0 || stricmp(pstProName, "menubar_visible") == 0 || stricmp(pstProName, "toolbar_visible") == 0 || stricmp(pstProName, "infobar_visible") == 0)) { // Already set creating new figure // but let the set_ function fail if figure already exists freeAllocatedSingleString(pstProName); continue; } //get address of value on stack sciErr = getVarAddressFromPosition(pvApiCtx, i + 1, &piAddrData); if (sciErr.iErr) { Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i + 1); freeAllocatedSingleString(pstProName); return 1; } getVarType(pvApiCtx, piAddrData, &iType); if ((strcmp(pstProName, "user_data") == 0) || (stricmp(pstProName, "userdata") == 0)) { /* in this case set_user_data_property * directly uses the third position in the stack * to get the variable which is to be set in * the user_data property (any data type is allowed) S. Steer */ _pvData = (void*)piAddrData; /*position in the stack */ iRows = -1; /*unused */ iCols = -1; /*unused */ iType = -1; } else { switch (iType) { case sci_matrix : getMatrixOfDouble(pvApiCtx, piAddrData, &iRows, &iCols, (double**)&_pvData); break; case sci_boolean : getMatrixOfBoolean(pvApiCtx, piAddrData, &iRows, &iCols, (int**)&_pvData); break; case sci_handles : getMatrixOfHandle(pvApiCtx, piAddrData, &iRows, &iCols, (long long**)&_pvData); break; case sci_strings : if ( strcmp(pstProName, "tics_labels") != 0 && strcmp(pstProName, "auto_ticks") != 0 && strcmp(pstProName, "axes_visible") != 0 && strcmp(pstProName, "axes_reverse") != 0 && strcmp(pstProName, "text") != 0 && stricmp(pstProName, "string") != 0 && stricmp(pstProName, "tooltipstring") != 0) /* Added for uicontrols */ { if (getAllocatedSingleString(pvApiCtx, piAddrData, (char**)&_pvData)) { Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, 3); freeAllocatedSingleString(pstProName); return 1; } iRows = (int)strlen((char*)_pvData); iCols = 1; } else { isMatrixOfString = 1; if (getAllocatedMatrixOfString(pvApiCtx, piAddrData, &iRows, &iCols, (char***)&_pvData)) { Scierror(999, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 3); freeAllocatedSingleString(pstProName); return 1; } } break; case sci_list : iCols = 1; getListItemNumber(pvApiCtx, piAddrData, &iRows); _pvData = (void*)piAddrData; /* In this case l3 is the list position in stack */ break; default : _pvData = (void*)piAddrData; /* In this case l3 is the list position in stack */ break; } } callSetProperty(pvApiCtx, iFig, _pvData, iType, iRows, iCols, pstProName); // If backgroundcolor is set : // * add it to colormap => performed by callSetProperty // * set background to index => performed by callSetProperty // * copy value into axes background property if (stricmp(pstProName, "backgroundcolor") == 0 && iAxes > 0) { int iBackground = 0; int *piBackground = &iBackground; getGraphicObjectProperty(iFig, __GO_BACKGROUND__, jni_int, (void **)&piBackground); setGraphicObjectProperty(iAxes, __GO_BACKGROUND__, piBackground, jni_int, 1); } freeAllocatedSingleString(pstProName); if (iType == sci_strings) { //free allacted data if (isMatrixOfString == 1) { freeAllocatedMatrixOfString(iRows, iCols, (char**)_pvData); } else { freeAllocatedSingleString((char*)_pvData); } } } if (position) { int pos[2]; pos[0] = (int)position[0]; pos[1] = (int)position[1]; setGraphicObjectProperty(iFig, __GO_POSITION__, pos, jni_int_vector, 2); } //axes_size if (axesSize) { int axes[2]; axes[0] = (int)axesSize[0]; axes[1] = (int)axesSize[1]; setGraphicObjectProperty(iFig, __GO_AXES_SIZE__, axes, jni_int_vector, 2); } else //no size, use default axes_size { int* piAxesSize = NULL; getGraphicObjectProperty(getFigureModel(), __GO_AXES_SIZE__, jni_int_vector, (void **)&piAxesSize); setGraphicObjectProperty(iFig, __GO_AXES_SIZE__, piAxesSize, jni_int_vector, 2); releaseGraphicObjectProperty(__GO_AXES_SIZE__, piAxesSize, jni_int_vector, 2); } initBar(iFig, bMenuBar, bToolBar, bInfoBar); if (axesSize == NULL && figureSize) //figure_size { int figure[2]; figure[0] = (int)figureSize[0]; figure[1] = (int)figureSize[1]; setGraphicObjectProperty(iFig, __GO_SIZE__, figure, jni_int_vector, 2); } setGraphicObjectProperty(iFig, __GO_RESIZE__, (void*)&bResize, jni_bool, 1); //return new created fig createScalarHandle(pvApiCtx, iRhs + 1, getHandle(iFig)); AssignOutputVariable(pvApiCtx, 1) = iRhs + 1; ReturnArguments(pvApiCtx); return 0; }
/*--------------------------------------------------------------------------*/ SciErr getDimFromVar(void *_pvCtx, int *_piAddress, int *_piVal) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iType = 0; int iRows = 0; int iCols = 0; double *pdblReal = NULL; sciErr = getVarType(_pvCtx, _piAddress, &iType); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument type"), "getDimFromVar"); return sciErr; } if (iType == sci_matrix) { if (isVarComplex(_pvCtx, _piAddress)) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Wrong type for argument %d: Real matrix expected.\n"), "getDimFromVar", getRhsFromAddress(_pvCtx, _piAddress)); return sciErr; } sciErr = getMatrixOfDouble(_pvCtx, _piAddress, &iRows, &iCols, &pdblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument data"), "getDimFromVar"); return sciErr; } *_piVal = (int)Max(pdblReal[0], 0); } else if (iType == sci_ints) { int iPrec = 0; sciErr = getVarDimension(_pvCtx, _piAddress, &iRows, &iCols); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument dimension"), "getDimFromVar"); return sciErr; } if (iRows != 1 || iCols != 1) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Wrong size for argument %d: (%d,%d) expected.\n"), "getProcessMode", getRhsFromAddress(_pvCtx, _piAddress), 1, 1); return sciErr; } sciErr = getMatrixOfIntegerPrecision(_pvCtx, _piAddress, &iPrec); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument precision"), "getDimFromVar"); return sciErr; } switch (iPrec) { case SCI_INT8: { char *pcData = NULL; sciErr = getMatrixOfInteger8(_pvCtx, _piAddress, &iRows, &iCols, &pcData); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument data"), "getDimFromVar"); return sciErr; } *_piVal = pcData[0]; } break; case SCI_UINT8: { unsigned char *pucData = NULL; sciErr = getMatrixOfUnsignedInteger8(_pvCtx, _piAddress, &iRows, &iCols, &pucData); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument data"), "getDimFromVar"); return sciErr; } *_piVal = pucData[0]; } break; case SCI_INT16: { short *psData = NULL; sciErr = getMatrixOfInteger16(_pvCtx, _piAddress, &iRows, &iCols, &psData); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument data"), "getDimFromVar"); return sciErr; } *_piVal = psData[0]; } break; case SCI_UINT16: { unsigned short *pusData = NULL; sciErr = getMatrixOfUnsignedInteger16(_pvCtx, _piAddress, &iRows, &iCols, &pusData); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument data"), "getDimFromVar"); return sciErr; } *_piVal = pusData[0]; } break; case SCI_INT32: { int *piData = NULL; sciErr = getMatrixOfInteger32(_pvCtx, _piAddress, &iRows, &iCols, &piData); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument data"), "getDimFromVar"); return sciErr; } *_piVal = piData[0]; } break; case SCI_UINT32: { unsigned int *puiData = NULL; sciErr = getMatrixOfUnsignedInteger32(_pvCtx, _piAddress, &iRows, &iCols, &puiData); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Unable to get argument data"), "getDimFromVar"); return sciErr; } *_piVal = puiData[0]; } break; } } else { addErrorMessage(&sciErr, API_ERROR_GET_DIMFROMVAR, _("%s: Wrong type for input argument #%d: A real scalar or an integer scalar expected.\n"), "getDimFromVar", getRhsFromAddress(_pvCtx, _piAddress)); return sciErr; } return sciErr; }
int OPTpushrangesImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { int i,j, limit,actions=0; InstrPtr p, *old; int x,y,z; Range range; if( mb->errors) return 0; range= (Range) GDKzalloc(mb->vtop * sizeof(RangeRec)); if (range == NULL) return 0; OPTDEBUGpushranges mnstr_printf(cntxt->fdout,"#Range select optimizer started\n"); (void) stk; (void) pci; limit = mb->stop; old = mb->stmt; /* * In phase I we collect information about constants */ for (i = 0; i < limit; i++) { p = old[i]; if( p->barrier) break; /* end of optimizer */ for(j=p->retc; j< p->argc; j++) range[getArg(p,j)].used++; for(j=0; j<p->retc; j++){ range[getArg(p,j)].lastupdate= i; if( range[getArg(p,j)].lastrange == 0) range[getArg(p,j)].lastrange= i; } if( getModuleId(p)== algebraRef && ( getFunctionId(p)== selectRef || getFunctionId(p)== uselectRef) ){ /* * The operation X:= algebra.select(Y,L,H,Li,Hi) is analysed. * First, we attempt to propagate the range known for Y onto the * requested range of X. This may lead to smaller range of * even the conclusion that X is necessarily empty. * Of course, only under the condition that Y has not been changed by a * side-effect since it was bound to X. */ x= getArg(p,1); y= getArg(p,2); if( range[x].lcst && isVarConstant(mb,y) ){ /* merge lowerbound */ if( ATOMcmp( getVarGDKType(mb,y), VALptr( &getVarConstant(mb,range[x].lcst)), VALptr( &getVarConstant(mb,y)) ) > 0){ getArg(p,2)= range[x].lcst; z= range[x].srcvar; if( getArg(p,1) == x && range[z].lastupdate == range[z].lastrange){ getArg(p,1) = z; actions++; } } y= getArg(p,3); /* merge higherbound */ if( ATOMcmp( getVarGDKType(mb,y), VALptr( &getVarConstant(mb,range[x].hcst)), VALptr( &getVarConstant(mb,y)) ) < 0 || ATOMcmp( getVarGDKType(mb,y), VALptr( &getVarConstant(mb,y)), ATOMnilptr(getVarType(mb,y)) ) == 0){ getArg(p,3)= range[x].hcst; z= range[x].srcvar; if( getArg(p,1) == x && range[z].lastupdate == range[z].lastrange){ getArg(p,1) = z; actions++; } } } /* * The second step is to assign the result of this exercise to the * result variable. */ x= getArg(p,0); if( isVarConstant(mb, getArg(p,2)) ){ range[x].lcst = getArg(p,2); range[x].srcvar= getArg(p,1); range[x].lastupdate= range[x].lastrange = i; } if( isVarConstant(mb, getArg(p,3)) ){ range[x].hcst = getArg(p,3); range[x].srcvar= getArg(p,1); range[x].lastupdate= range[x].lastrange = i; } /* * If both range bounds are constant, we can also detect empty results. * It is empty if L> H or when L=H and the bounds are !(true,true). */ x= getArg(p,2); y= getArg(p,3); if( isVarConstant(mb, x) && isVarConstant(mb, y) ){ z =ATOMcmp( getVarGDKType(mb,y), VALptr( &getVarConstant(mb,x)), VALptr( &getVarConstant(mb,y))); x= p->argc > 4; x= x && isVarConstant(mb,getArg(p,4)); x= x && isVarConstant(mb,getArg(p,5)); x= x && getVarConstant(mb,getArg(p,4)).val.btval; x= x && getVarConstant(mb,getArg(p,5)).val.btval; if( z > 0 || (z==0 && p->argc>4 && !x)) { int var = getArg(p, 0); wrd zero = 0; ValRecord v, *vp; vp = VALset(&v, TYPE_wrd, &zero); varSetProp(mb, var, rowsProp, op_eq, vp); /* create an empty replacement */ x = getArgType(mb, p, 1); p->argc=1; getModuleId(p)= batRef; getFunctionId(p)= newRef; p= pushArgument(mb,p, newTypeVariable(mb, getHeadType(x))); (void) pushArgument(mb,p, newTypeVariable(mb, getTailType(x))); actions++; } } } } OPTDEBUGpushranges for(j=0; j< mb->vtop; j++) if( range[j].used ) printRange(cntxt, mb,range,j); /* * Phase II, if we succeeded in pushing constants around and * changing instructions, we might as well try once more to perform * aliasRemoval, constantExpression, and pushranges. */ GDKfree(range); return actions; }
/*--------------------------------------------------------------------------*/ int sci_isdir(char *fname, unsigned long fname_len) { SciErr sciErr; int *piAddressVarOne = NULL; wchar_t **pStVarOne = NULL; int iType = 0; int *lenStVarOne = NULL; int m1 = 0, n1 = 0; BOOL *results = NULL; int i = 0; /* Check Input & Output parameters */ CheckRhs(1, 1); CheckLhs(1, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (iType != sci_strings) { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1); return 0; } sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } results = (BOOL*)MALLOC(sizeof(BOOL) * (m1 * n1)); if (results == NULL) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pStVarOne, m1 * n1); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL); if (sciErr.iErr) { printError(&sciErr, 0); FREE(results); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); if (pStVarOne == NULL) { FREE(lenStVarOne); FREE(results); lenStVarOne = NULL; Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } for (i = 0; i < m1 * n1; i++) { pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1)); if (pStVarOne[i] == NULL) { if (i == 0) { FREE(pStVarOne); } else { freeArrayOfWideString(pStVarOne, i - 1); } if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } FREE(results); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } for (i = 0; i < m1 * n1; i++) { wchar_t *expandedPath = expandPathVariableW(pStVarOne[i]); if (expandedPath) { results[i] = isdirW(expandedPath); FREE(expandedPath); expandedPath = NULL; } else { results[i] = FALSE; } } if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pStVarOne, m1 * n1); sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m1, n1, results); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; if (results) { FREE(results); results = NULL; } PutLhsVar(); return 0; }
/*--------------------------------------------------------------------------*/ int sci_javaclasspath(char *fname, unsigned long fname_len) { int *piAddressVarOne = NULL; int iType = 0; SciErr sciErr; Rhs = Max(Rhs, 0); CheckRhs(0, 1); CheckLhs(0, 1); if (Rhs == 0) { int nbRow = 0; int nbCol = 1; char **Strings = NULL; Strings = getClasspath(&nbRow); createMatrixOfString(pvApiCtx, Rhs + 1, nbRow, nbCol, Strings); LhsVar(1) = Rhs + 1; PutLhsVar(); freeArrayOfString(Strings, nbRow * nbCol); } else { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if ( iType == sci_strings ) { char **pStVarOne = NULL; int *lenStVarOne = NULL; static int n1 = 0, m1 = 0; int i = 0; /* get dimensions */ sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); return 0; } /* get lengths */ sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } pStVarOne = (char **)MALLOC(sizeof(char*) * (m1 * n1)); if (pStVarOne == NULL) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } Scierror(999, _("%s: No more memory.\n"), fname); return 0; } for (i = 0; i < m1 * n1; i++) { pStVarOne[i] = (char*)MALLOC(sizeof(char*) * (lenStVarOne[i] + 1)); } /* get strings */ sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { freeArrayOfString(pStVarOne, m1 * n1); if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } for (i = 0; i < m1 * n1 ; i++) { if (!addToClasspath(pStVarOne[i], STARTUP)) { Scierror(999, _("%s: Could not add URL to system classloader : %s.\n"), fname, pStVarOne[i]); freeArrayOfString(pStVarOne, m1 * n1); return 0; } } LhsVar(1) = 0; PutLhsVar(); freeArrayOfString(pStVarOne, m1 * n1); } else { Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1); } } return 0; }