int getDoubleMatrixFromScilab(int argNum, int *rows, int *cols, double **dest)
{
	int *varAddress;
	SciErr sciErr;
	const char errMsg[]="Wrong type for input argument #%d: A matrix of double is expected.\n";
	const int errNum=999;
	//same steps as above
	sciErr = getVarAddressFromPosition(pvApiCtx, argNum, &varAddress);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 1;
	}
	if ( !isDoubleType(pvApiCtx,varAddress) ||  isVarComplex(pvApiCtx,varAddress) )
	{
		Scierror(errNum,errMsg,argNum);
		return 1;
	}
	getMatrixOfDouble(pvApiCtx, varAddress, rows, cols, dest);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 1;
	}
	return 0;
}
int getIntFromScilab(int argNum, int *dest)
{
	SciErr sciErr;
	int iRet,*varAddress;
	double inputDouble;
	const char errMsg[]="Wrong type for input argument #%d: An integer is expected.\n";
	const int errNum=999;
	//same steps as above
	sciErr = getVarAddressFromPosition(pvApiCtx, argNum, &varAddress);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 1;
	}
	if ( !isDoubleType(pvApiCtx,varAddress) ||  isVarComplex(pvApiCtx,varAddress) )
	{
		Scierror(errNum,errMsg,argNum);
		return 1;
	}
	iRet = getScalarDouble(pvApiCtx, varAddress, &inputDouble);
	//check that an int is stored in the double by casting and recasting
	if(iRet || ((double)((int)inputDouble))!=inputDouble)
	{
		Scierror(errNum,errMsg,argNum);
		return 1;
	}
	*dest=(int)inputDouble;
	return 0;
}
Beispiel #3
0
/*--------------------------------------------------------------------------*/
int checkParam(void* _pvCtx, int _iPos, char* fname)
{
    SciErr sciErr;
    int* piAddr = NULL;

    //get var address
    sciErr = getVarAddressFromPosition(_pvCtx, _iPos, &piAddr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, _iPos);
        return 1;
    }

    //check is real scalar double
    if ( isScalar(_pvCtx, piAddr) == 0 ||
            isDoubleType(_pvCtx, piAddr) == 0 ||
            isVarComplex(_pvCtx, piAddr) == 1)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: An integer value expected.\n"), fname, _iPos);
        return 1;
    }

    return 0;
}
int getDoubleFromScilab(int argNum, double *dest)
{
	//data declarations
	SciErr sciErr;
	int iRet,*varAddress;
	const char errMsg[]="Wrong type for input argument #%d: A double is expected.\n";
	const int errNum=999;
	//get variable address
	sciErr = getVarAddressFromPosition(pvApiCtx, argNum, &varAddress);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 1;
	}
	//check that it is a non-complex double
	if ( !isDoubleType(pvApiCtx,varAddress) ||  isVarComplex(pvApiCtx,varAddress) )
	{
		Scierror(errNum,errMsg,argNum);
		return 1;
	}
	//retrieve and store
	iRet = getScalarDouble(pvApiCtx, varAddress, dest);
	if(iRet)
	{
		Scierror(errNum,errMsg,argNum);
		return 1;
	}
	return 0;
}
int getFixedSizeDoubleMatrixFromScilab(int argNum, int rows, int cols, double **dest)
{
	int *varAddress,inputMatrixRows,inputMatrixCols;
	SciErr sciErr;
	const char errMsg[]="Wrong type for input argument #%d: A matrix of double of size %d by %d is expected.\n";
	const int errNum=999;
	//same steps as above
	sciErr = getVarAddressFromPosition(pvApiCtx, argNum, &varAddress);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 1;
	}
	if ( !isDoubleType(pvApiCtx,varAddress) ||  isVarComplex(pvApiCtx,varAddress) )
	{
		Scierror(errNum,errMsg,argNum,rows,cols);
		return 1;
	}
	sciErr = getMatrixOfDouble(pvApiCtx, varAddress, &inputMatrixRows, &inputMatrixCols,NULL);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 1;
	}
	//check that the matrix has the correct number of rows and columns
	if(inputMatrixRows!=rows || inputMatrixCols!=cols)
	{
		Scierror(errNum,errMsg,argNum,rows,cols);
		return 1;
	}
	getMatrixOfDouble(pvApiCtx, varAddress, &inputMatrixRows, &inputMatrixCols, dest);
	return 0;
}
int sci_sym_setObjSense(char *fname){
	
	//error management variable
	SciErr sciErr;
	int iRet;
	
	//data declarations
	int *varAddress;
	double objSense;
	
	//ensure that environment is active
	if(global_sym_env==NULL){
		sciprint("Error: Symphony environment not initialized. Please run 'sym_open()' first.\n");
		return 1;
	}
	
	//code to check arguments and get them
	CheckInputArgument(pvApiCtx,1,1) ;
	CheckOutputArgument(pvApiCtx,1,1) ;
	
	//code to process input
	sciErr = getVarAddressFromPosition(pvApiCtx, 1, &varAddress);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 1;
	}
	if ( !isDoubleType(pvApiCtx,varAddress) ||  isVarComplex(pvApiCtx,varAddress) )
	{
		Scierror(999, "Wrong type for input argument #1:\nEither 1 (sym_minimize) or -1 (sym_maximize) is expected.\n");
		return 1;
	}
	iRet = getScalarDouble(pvApiCtx, varAddress, &objSense);
	if(iRet || (objSense!=-1 && objSense!=1))
	{
		Scierror(999, "Wrong type for input argument #1:\nEither 1 (sym_minimize) or -1 (sym_maximize) is expected.\n");
		return 1;
	}
	iRet=sym_set_obj_sense(global_sym_env,objSense);
	if(iRet==FUNCTION_TERMINATED_ABNORMALLY){
		Scierror(999, "An error occured.\n");
		return 1;
	}else{
		if(objSense==1)
			sciprint("The solver has been set to minimize the objective.\n");
		else
			sciprint("The solver has been set to maximize the objective.\n");
	}
	
	//code to give output
	if(return0toScilab())
		return 1;
	
	return 0;
}
Beispiel #7
0
/*--------------------------------------------------------------------------*/
int getStackArgumentAsBoolean(void* _pvCtx, int* _piAddr)
{
    if (isScalar(_pvCtx, _piAddr))
    {
        if (isDoubleType(_pvCtx, _piAddr))
        {
            double dbl = 0;
            getScalarDouble(_pvCtx, _piAddr, &dbl);
            return ((int)dbl == 0 ? FALSE : TRUE);
        }
        else if (isBooleanType(_pvCtx, _piAddr))
        {
            int i = 0;
            getScalarBoolean(_pvCtx, _piAddr, &i);
            return (i == 0 ? FALSE : TRUE);
        }
        else if (isStringType(_pvCtx, _piAddr))
        {
            int ret = 0;
            char* pst = NULL;
            if (getAllocatedSingleString(_pvCtx, _piAddr, &pst))
            {
                return -1;
            }

            if (stricmp(pst, "on") == 0)
            {
                ret = TRUE;
            }

            freeAllocatedSingleString(pst);

            return ret;
        }
    }
    return -1;
}
//both basic and advanced loader use this code
static int commonCodePart2()
{
	//get input 3: lower bounds of variables
	if(getFixedSizeDoubleMatrixFromScilab(3,1,numVars,&lowerBounds))
	{
		cleanupBeforeExit();
		return 1;
	}
	
	//get input 4: upper bounds of variables
	if(getFixedSizeDoubleMatrixFromScilab(4,1,numVars,&upperBounds))
	{
		cleanupBeforeExit();
		return 1;
	}
	
	//get input 5: coefficients of variables in objective function to be minimized
	if(getFixedSizeDoubleMatrixFromScilab(5,1,numVars,&objective))
	{
		cleanupBeforeExit();
		return 1;
	}
	
	//get input 6: array that specifies wether a variable is constrained to be an integer
	sciErr = getVarAddressFromPosition(pvApiCtx, 6, &varAddress);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		cleanupBeforeExit();return 1;
	}
	if ( !isBooleanType(pvApiCtx, varAddress) )
	{
		Scierror(999, "Wrong type for input argument #6: A matrix of booleans is expected.\n");
		cleanupBeforeExit();return 1;
	}
	sciErr = getMatrixOfBoolean(pvApiCtx, varAddress, &inputMatrixRows, &inputMatrixCols, &isIntVarBool);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		cleanupBeforeExit();return 1;
	}
	if(inputMatrixRows!=1 || inputMatrixCols!=numVars)
	{
		Scierror(999, "Wrong type for input argument #6: Incorrectly sized matrix.\n");
		cleanupBeforeExit();return 1;
	}
	for(colIter=0;colIter<numVars;colIter++)
	{
		if(isIntVarBool[colIter])
			isIntVar[colIter]=TRUE;
		else
			isIntVar[colIter]=FALSE;
	}
	
	//get input 7: wether to minimize or maximize objective
	sciErr = getVarAddressFromPosition(pvApiCtx, 7, &varAddress);
	if (sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 1;
	}
	if ( !isDoubleType(pvApiCtx,varAddress) ||  isVarComplex(pvApiCtx,varAddress) )
	{
		Scierror(999, "Wrong type for input argument #7: Either 1 (sym_minimize) or -1 (sym_maximize) is expected.\n");
		return 1;
	}
	iRet = getScalarDouble(pvApiCtx, varAddress, &objSense);
	if(iRet || (objSense!=-1 && objSense!=1))
	{
		Scierror(999, "Wrong type for input argument #7: Either 1 (sym_minimize) or -1 (sym_maximize) is expected.\n");
		return 1;
	}
	iRet=sym_set_obj_sense(global_sym_env,objSense);
	if(iRet==FUNCTION_TERMINATED_ABNORMALLY)
	{
		Scierror(999, "An error occured.\n");
		return 1;
	}
	
	//get input 9: constraint lower bound
	if(getFixedSizeDoubleMatrixFromScilab(9,numConstr,1,&conLower))
	{
		cleanupBeforeExit();
		return 1;
	}
	
	//get input 10: constraint upper bound
	if(getFixedSizeDoubleMatrixFromScilab(10,numConstr,1,&conUpper))
	{
		cleanupBeforeExit();
		return 1;
	}
	
	//deduce type of constraint
	for(rowIter=0;rowIter<numConstr;rowIter++)
	{
		if(conLower[rowIter]>conUpper[rowIter])
		{
			Scierror(999, "Error: the lower bound of constraint %d is more than its upper bound.\n",rowIter);
			cleanupBeforeExit();
			return 1;
		}
		if(conLower[rowIter]==(-INFINITY) && conUpper[rowIter]==INFINITY){
			conType[rowIter]='N';
			conRange[rowIter]=0;
			conRHS[rowIter]=0;
		}else if(conLower[rowIter]==(-INFINITY)){
			conType[rowIter]='L';
			conRange[rowIter]=0;
			conRHS[rowIter]=conUpper[rowIter];
		}else if(conUpper[rowIter]==INFINITY){
			conType[rowIter]='G';
			conRange[rowIter]=0;
			conRHS[rowIter]=conLower[rowIter];
		}else if(conUpper[rowIter]==conLower[rowIter]){
			conType[rowIter]='E';
			conRange[rowIter]=0;
			conRHS[rowIter]=conLower[rowIter];
		}else{
			conType[rowIter]='R';
			conRange[rowIter]=conUpper[rowIter]-conLower[rowIter];
			conRHS[rowIter]=conUpper[rowIter];
		}
	}
	
	/*
	//for debug: show all data
	sciprint("Vars: %d Constr: %d ObjType: %lf\n",numVars,numConstr,objSense);
	for(colIter=0;colIter<numVars;colIter++)
		sciprint("Var %d: upper: %lf lower: %lf isInt: %d ObjCoeff: %lf\n",colIter,lowerBounds[colIter],upperBounds[colIter],isIntVar[colIter],objective[colIter]);
	for(rowIter=0;rowIter<numConstr;rowIter++)
		sciprint("Constr %d: type: %c lower: %lf upper: %lf range: %lf\n",rowIter,conType[rowIter],conLower[rowIter],conRange[rowIter]);
	*/
	
	//call problem loader
	sym_explicit_load_problem(global_sym_env,numVars,numConstr,conMatrixColStart,conMatrixRowIndex,conMatrix,lowerBounds,upperBounds,isIntVar,objective,NULL,conType,conRHS,conRange,TRUE);
	sciprint("Problem loaded into environment.\n");
	
	//code to give output
	cleanupBeforeExit();
	
	return 0;
}
/*--------------------------------------------------------------------------*/
int sci_dec2base(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int *piAddressVarTwo = NULL;
    int m = 0, n = 0;

    double *dValues = NULL;
    char **convertedValues = NULL;
    unsigned int iBaseUsed = 0;
    double dBaseUsed = 0.;
    unsigned int nbDigits = 0;
    error_convertbase err = ERROR_CONVERTBASE_NOK;

    CheckRhs(2, 3);
    CheckLhs(1, 1);

    if (Rhs == 3)
    {
        double dParamThree = 0.;
        unsigned int iParamThree = 0;
        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 (!isDoubleType(pvApiCtx, piAddressVarThree))
        {
            Scierror(999,_("%s: Wrong type for input argument #%d: A scalar integer value expected.\n"), fname, 3);
            return 0;
        }

        if (!isScalar(pvApiCtx, piAddressVarThree))
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: A scalar integer value expected.\n"), fname, 3);
            return 0;
        }

        if (getScalarDouble(pvApiCtx, piAddressVarThree, &dParamThree) != 0)
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }

        iParamThree = (unsigned int)dParamThree;

        if (dParamThree != (double)iParamThree)
        {
            Scierror(999,_("%s: Wrong value for input argument #%d: A integer value expected.\n"), fname, 3);
            return 0;
        }

        nbDigits = iParamThree;
    }

    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
        return 0;
    }

    if (!isDoubleType(pvApiCtx, piAddressVarTwo))
    {
        Scierror(999,_("%s: Wrong type for input argument #%d: A integer value expected.\n"), fname, 2);
        return 0;
    }

    if (!isScalar(pvApiCtx, piAddressVarTwo))
    {
        Scierror(999,_("%s: Wrong size for input argument #%d.\n"), fname, 2);
        return 0;
    }

    if (getScalarDouble(pvApiCtx, piAddressVarTwo, &dBaseUsed) != 0)
    {
        Scierror(999, _("%s: No more memory.\n"), fname);
        return 0;
    }

    iBaseUsed = (unsigned int)dBaseUsed;
    if (dBaseUsed != (double)iBaseUsed)
    {
        Scierror(999, _("%s: Wrong value for input argument #%d: A integer value expected.\n"), fname, 2);
        return 0;
    }

    if ((iBaseUsed < 2) && (iBaseUsed > 36))
    {
        Scierror(999, _("%s: Wrong value for input argument #%d: Must be between %d and %d."), fname, 2, 2, 36);
        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;
    }

    if (isEmptyMatrix(pvApiCtx, piAddressVarOne))
    {
        if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }
        else
        {
            LhsVar(1) = Rhs + 1;
            PutLhsVar();
            return 0;
        }
    }

    if (!isDoubleType(pvApiCtx, piAddressVarOne))
    {
        Scierror(999,_("%s: Wrong type for input argument #%d: A matrix of integer value expected.\n"), fname, 1);
        return 0;
    }

    if (isVarComplex(pvApiCtx, piAddressVarOne))
    {
        Scierror(999,_("%s: Wrong type for input argument #%d: A matrix of integer value expected.\n"), fname, 1);
        return 0;
    }

    sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m, &n , &dValues);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    convertedValues = (char **)MALLOC(sizeof(char*) * (m *n));
    if (convertedValues == NULL)
    {
        Scierror(999, _("%s: No more memory.\n"), fname);
        return 0;
    }

    convertedValues = convertMatrixOfDec2Base(dValues, m * n, iBaseUsed, nbDigits, &err);
    if ((err != ERROR_CONVERTBASE_OK) || (convertedValues == NULL))
    {
        freeArrayOfString(convertedValues, m * n);
        convertedValues = NULL;

        switch (err)
        {
        case ERROR_CONVERTBASE_NOT_INTEGER_VALUE:
            Scierror(999, _("%s: Wrong value for input argument #%d: Must be between 0 and 2^52.\n"), fname, 1);
            return 0;

        case ERROR_CONVERTBASE_NOT_IN_INTERVAL:
            Scierror(999,_("%s: Wrong value(s) for input argument #%d: A matrix of positive integer values expected.\n"), fname, 1);
            return 0;

        case ERROR_CONVERTBASE_ALLOCATION:
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;

        case ERROR_CONVERTBASE_NOK: default:
            Scierror(999, _("%s: Wrong value for input argument #%d: cannot convert value(s).\n"), fname, 1);
            return 0;
        }
    }

    sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m, n, convertedValues);
    freeArrayOfString(convertedValues, m * n);
    convertedValues = NULL;

    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999,_("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}
Beispiel #10
0
/*--------------------------------------------------------------------------*/
int C2F(sci_errclear)(char *fname,unsigned long fname_len)
{
    Rhs = Max(0,Rhs);
    CheckRhs(0,2);
    CheckLhs(1,1);

    if (Rhs == 1)
    {
        SciErr sciErr;
        int *piAddressVarOne = 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;
        }

        if (isDoubleType(pvApiCtx, piAddressVarOne))
        {
            double dValue = 0.;
            int iValue = 0;
            int iLastErrorValue = getLastErrorValue();

            if (!isScalar(pvApiCtx, piAddressVarOne))
            {
                Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 1);
                return 0;
            }

             getScalarDouble(pvApiCtx, piAddressVarOne, &dValue);
             iValue = (int)dValue;

             if ((double)iValue != dValue)
             {
                 Scierror(999,_("%s: Wrong value for input argument #%d: An integer value expected.\n"), fname, 1);
                 return 0;
             }

             if ((iValue == iLastErrorValue) || (iValue <= 0))
             {
                /* clear fortran common error */
                C2F(errgst).err2 = 0;

                /* clear last error buffer (C) */
                clearLastError();
             }
        }
        else
        {
            Scierror(999,_("%s: Wrong type for input argument #%d: An integer value expected.\n"), fname, 1);
            return 0;
        }
    }
    else
    {
        /* clear fortran common error */
        C2F(errgst).err2 = 0;

        /* clear last error buffer (C) */
        clearLastError();
    }
    LhsVar(1) = 0;
    PutLhsVar();
	return 0;
}
Beispiel #11
0
/*--------------------------------------------------------------------------*/ 
int sci_mputl(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int *piAddressVarTwo = NULL;

    char **pStVarOne			= NULL;
    int *lenStVarOne			= NULL;
    int mOne = 0, nOne = 0;
    int mnOne = 0;

    char *filename = NULL;
    int fileDescriptor = -1;
    BOOL bCloseFile = FALSE;

    int i = 0;
    int mputlErr = MPUTL_ERROR;

    if (Rhs != 2)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2);
        return 0;
    }

    if (Lhs != 1)
    {
        Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), fname, 1);
        return 0;
    }

    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
        return 0;
    }

    if ( isDoubleType(pvApiCtx, piAddressVarTwo) )
    {
        double dValue = 0.;

        if (!isScalar(pvApiCtx, piAddressVarTwo))
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: Integer expected.\n"), fname, 2);
            return 0;
        }

        if ( getScalarDouble(pvApiCtx, piAddressVarTwo, &dValue) == 0)
        {
            fileDescriptor = (int)dValue;
        }
        else
        {
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    } 
    else if ( isStringType(pvApiCtx, piAddressVarTwo) )
    {
        if (!isScalar(pvApiCtx, piAddressVarTwo))
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: String expected.\n"), fname, 2);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &filename) == 0)
        {
            #define WRITE_ONLY_TEXT_MODE "wt"
            int f_swap = 0;
            double res = 0.0;
            int ierr = 0;
            char *expandedFileName = expandPathVariable(filename);
            
            C2F(mopen)(&fileDescriptor, expandedFileName, WRITE_ONLY_TEXT_MODE, &f_swap, &res, &ierr);
            if (expandedFileName) {FREE(expandedFileName); expandedFileName = NULL;}

            switch (ierr)
            {
            case MOPEN_NO_ERROR:
                bCloseFile = TRUE;
                break;
            case MOPEN_NO_MORE_LOGICAL_UNIT:
                {
                    freeAllocatedSingleString(filename);
                    Scierror(66, _("%s: Too many files opened!\n"), fname);
                    return 0;
                }
                break;
            case MOPEN_CAN_NOT_OPEN_FILE:
                {
                    Scierror(999, _("%s: Cannot open file %s.\n"), fname, filename);
                    freeAllocatedSingleString(filename);
                    return 0;
                }
                break;
            case MOPEN_NO_MORE_MEMORY:
                {
                    freeAllocatedSingleString(filename);
                    Scierror(999, _("%s: No more memory.\n"), fname);
                    return 0;
                }
                break;
            case MOPEN_INVALID_FILENAME:
                {
                    if (filename)
                    {
                        Scierror(999, _("%s: invalid filename %s.\n"), fname, filename);
                    }
                    else
                    {
                        freeAllocatedSingleString(filename);
                        Scierror(999, _("%s: invalid filename.\n"), fname);
                    }
                    return 0;
                }
                break;
            case MOPEN_INVALID_STATUS: default:
                {
                    freeAllocatedSingleString(filename);
                    Scierror(999, _("%s: invalid status.\n"), fname);
                    return 0;
                }
                break;
            }
            bCloseFile = TRUE;
            freeAllocatedSingleString(filename);
        }
        else
        {
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    else
    {
        Scierror(999,_("%s: Wrong type for input argument #%d: a String or Integer expected.\n"), fname, 2);
    }

    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;
    }

    if (!isStringType(pvApiCtx, piAddressVarOne))
    {
        Scierror(999,_("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1);
        return 0;
    }

    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne,&mOne, &nOne, NULL, NULL);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    if ( !((mOne == 1) || (nOne == 1)) )
    {
        Scierror(999,_("%s: Wrong size for input argument #%d: A 1-by-n or m-by-1 array expected.\n"), fname, 1);
        return 0;
    }

    mnOne = mOne * nOne;

    lenStVarOne = (int*)MALLOC(sizeof(int) * mnOne);
    if (lenStVarOne == NULL)
    {
        Scierror(999, _("%s: No more memory.\n"), fname);
        return 0;
    }

    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne,&mOne, &nOne, lenStVarOne, NULL);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    pStVarOne = (char**) MALLOC(sizeof(char*) * mnOne);
    if (pStVarOne == NULL)
    {
        FREE(lenStVarOne); lenStVarOne = NULL;
        Scierror(999, _("%s: No more memory.\n"), fname);
        return 0;
    }

    for (i = 0; i < mnOne; i++)
    {
        pStVarOne[i] = (char*)MALLOC(sizeof(char) * (lenStVarOne[i] + 1));
        if (pStVarOne[i] == NULL)
        {
            freeArrayOfString(pStVarOne, i);
            if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }
    }

    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, pStVarOne);
    if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
    if(sciErr.iErr)
    {
        freeArrayOfString(pStVarOne, mnOne);
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    mputlErr = mputl(fileDescriptor, pStVarOne, mnOne);
    freeArrayOfString(pStVarOne, mnOne);

    if (bCloseFile)
    {
        double dErrClose = 0.;
        C2F(mclose)(&fileDescriptor, &dErrClose);
        bCloseFile = FALSE;
    }

    switch (mputlErr)
    {
    case MPUTL_NO_ERROR:
        createScalarBoolean(pvApiCtx, Rhs + 1, TRUE);
        LhsVar(1) = Rhs + 1;
        PutLhsVar();
        break;

    case MPUTL_INVALID_FILE_DESCRIPTOR:
        // commented for compatiblity
        // Scierror(999, _("%s: invalid file descriptor.\n"), fname);
        // break;
    case MPUTL_ERROR:
    case MPUTL_NO_WRITE_RIGHT:
    default:
        createScalarBoolean(pvApiCtx, Rhs + 1, FALSE);
        LhsVar(1) = Rhs + 1;
        PutLhsVar();
        break;
    }

    return 0;
}
Beispiel #12
0
/*--------------------------------------------------------------------------*/
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;
}
/*--------------------------------------------------------------------------*/
int sci_archive_compress(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    char *pStVarOne = NULL;
    char *pStVarOneExpanded = NULL;

    int *piAddressVarTwo = NULL;
    char **pStVarTwo = NULL;
    int m1 = 0;
    int n1 = 0;
    int i = 0;

    int *piAddressVarThree = NULL;
    char *pStVarThree = NULL;

    char **file_list = NULL;
    /* Check Input & Output parameters */
    CheckRhs(3, 3);
    CheckLhs(1, 2);
    int result = 0;
    int *error = 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;
    }

    if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
        return 0;
    }


    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    if (isDoubleType(pvApiCtx, piAddressVarTwo))
    {
        if (isEmptyMatrix(pvApiCtx, piAddressVarTwo))
        {
            if (createEmptyMatrix(pvApiCtx, Rhs + 2))
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
            return 0;
        }

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
        return 0;
    }

    if (isStringType(pvApiCtx, piAddressVarTwo) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 2);
        return 0;
    }

    if (getAllocatedMatrixOfString(pvApiCtx, piAddressVarTwo, &m1, &n1, &pStVarTwo))
    {
        freeAllocatedMatrixOfString(m1, n1, pStVarTwo);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
        return 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: A string expected.\n"), fname, 3);
        return 0;
    }


    if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &pStVarOne))
    {
        if (pStVarOne)
        {
            freeAllocatedSingleWideString(pStVarOne);
        }

        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &pStVarThree))
    {
        if (pStVarThree)
        {
            freeAllocatedSingleWideString(pStVarThree);
        }

        freeAllocatedSingleString(pStVarOne);
	freeAllocatedSingleString(pStVarTwo);
        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 0;
    }


    int size = 0;
    while(i < (m1*n1))
    {
	size++;
	if(file_list!=NULL)
	{
		file_list = (char**)REALLOC(file_list,sizeof(char*)*(size));
	}
	else
	{
		file_list = (char**)MALLOC(sizeof(char*)*(size));
	}
	file_list[size-1] = (char*)MALLOC(sizeof(char)*512);
	strcpy(file_list[size-1],expandPathVariable(pStVarTwo[i]));
	i++;
    }


    i = 0;
    if(strlen(pStVarThree)>2)
    {
	    Scierror(999, _("%s: Sorry unrecognised format type.\n"), fname);
	    return 0;
    }
    while(i<strlen(pStVarThree))
    {
	    if(pStVarThree[i]!='Z' && pStVarThree[i]!='j' && pStVarThree[i]!='y' && pStVarThree[i]!='z')
	    {
		Scierror(999, _("%s: Sorry unrecognised format type.\n"), fname);
		return 0;
	    }
	    i++;
    }
    result = archive_compress(pStVarOne,file_list,size,pStVarThree,&error);


                if(error == 1)
                {
                Scierror(999, _("%s: Sorry the file could not be opened.\n"), fname);
                return 0;
            }
                if(error == 2)
                {
                Scierror(999, _("%s: Sorry the file header could not be read\n"), fname);
                return 0;
            }
                if(error == 3)
                {
                Scierror(999, _("%s: Sorry the file header could not be written.\n"), fname);
                return 0;
            }

                freeAllocatedSingleString(pStVarOne);
		freeAllocatedSingleString(pStVarTwo);
                createScalarInteger32(pvApiCtx, Rhs + 1, result);
                LhsVar(1) = Rhs + 1;
                PutLhsVar();
                return 0;

            }
Beispiel #14
0
/*--------------------------------------------------------------------------
 * sciset(choice-name,x1,x2,x3,x4,x5)
 * or   xset()
 *-----------------------------------------------------------*/
int sci_set(char *fname, unsigned long fname_len)
{
    SciErr sciErr;

    int* piAddr1 = NULL;
    int* piAddr2 = NULL;
    int* piAddr3 = NULL;
    int lw = 0;
    int isMatrixOfString = 0;

    char* pstProperty = NULL;
    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
    if (sciErr.iErr)
    {
        //error
        return 1;
    }

    if (isMListType(pvApiCtx, piAddr1) || isTListType(pvApiCtx, piAddr1))
    {
        OverLoad(1);
        return 0;
    }

    CheckRhs(2, 3);
    CheckLhs(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;
    }
    else                        /* others types */
    {
        int iRows1 = 0, iCols1 = 0;
        int iRows2 = 0, iCols2 = 0;
        int iRows3 = 0, iCols3 = 0;
        void* _pvData = NULL;
        unsigned long hdl;
        char *pobjUID = NULL;

        int iType1 = 0;

        int valueType = 0;      /* type of the rhs */

        int setStatus = 0;

        /* after the call to sciSet get the status : 0 <=> OK,          */
        /*                                          -1 <=> Error,       */
        /*                                           1 <=> nothing done */

        /*  set or create a graphic window */
        sciErr = getVarType(pvApiCtx, piAddr1, &iType1);
        if (sciErr.iErr)
        {
            //error
            return 1;
        }
        switch (iType1)
        {
            case sci_handles:
                /* first is a scalar argument so it's a gset(hdl,"command",[param]) */
                /* F.Leray; INFO: case 9 is considered for a matrix of graphic handles */
                CheckRhs(3, 3);

                if (isScalar(pvApiCtx, piAddr1) == FALSE)
                {
                    OverLoad(1);
                    return 0;
                }

                getScalarHandle(pvApiCtx, piAddr1, (long long*)&hdl);
                pobjUID = (char*)getObjectFromHandle(hdl);

                getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);
                getAllocatedSingleString(pvApiCtx, piAddr2, &pstProperty);
                valueType = getInputArgumentType(pvApiCtx, 3);

                getVarAddressFromPosition(pvApiCtx, 3, &piAddr3);
                if ((strcmp(pstProperty, "user_data") == 0) || (stricmp(pstProperty, "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*)piAddr3;         /*position in the stack */
                    iRows3 = -1;   /*unused */
                    iCols3 = -1;   /*unused */
                    valueType = -1;
                }
                else if (valueType == sci_matrix)
                {
                    getMatrixOfDouble(pvApiCtx, piAddr3, &iRows3, &iCols3, (double**)&_pvData);
                }
                else if (valueType == sci_boolean)
                {
                    getMatrixOfBoolean(pvApiCtx, piAddr3, &iRows3, &iCols3, (int**)&_pvData);
                }
                else if (valueType == sci_handles)
                {
                    getMatrixOfHandle(pvApiCtx, piAddr3, &iRows3, &iCols3, (long long**)&_pvData);
                }
                else if (valueType == 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 && stricmp(pstProperty, "string") != 0 &&
                            stricmp(pstProperty, "tooltipstring") != 0) /* Added for uicontrols */
                    {
                        getAllocatedSingleString(pvApiCtx, piAddr3, (char**)&_pvData);
                        iRows3 = (int)strlen((char*)_pvData);
                        iCols3 = 1;
                    }
                    else
                    {
                        isMatrixOfString = 1;
                        getAllocatedMatrixOfString(pvApiCtx, piAddr3, &iRows3, &iCols3, (char***)&_pvData);
                    }
                }
                else if (valueType == sci_list) /* Added for callbacks */
                {
                    iCols3 = 1;
                    getListItemNumber(pvApiCtx, piAddr3, &iRows3);
                    _pvData = (void*)piAddr3;         /* In this case l3 is the list position in stack */
                }
                break;

            case sci_strings:      /* first is a string argument so it's a set("command",[param]) */
                CheckRhs(2, 2);
                getAllocatedSingleString(pvApiCtx, piAddr1, &pstProperty);
                hdl = 0;
                pobjUID = NULL;
                valueType = getInputArgumentType(pvApiCtx, 2);
                getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);

                if (valueType == sci_matrix)
                {
                    getMatrixOfDouble(pvApiCtx, piAddr2, &iRows3, &iCols3, (double**)&_pvData);
                }
                else if (valueType == sci_handles)
                {
                    getMatrixOfHandle(pvApiCtx, piAddr2, &iRows3, &iCols3, (long long**)&_pvData);
                }
                else if (valueType == 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)
                    {
                        isMatrixOfString = 1;
                        getAllocatedMatrixOfString(pvApiCtx, piAddr2, &iRows3, &iCols3, (char***)&_pvData);
                    }
                    else
                    {
                        getAllocatedSingleString(pvApiCtx, piAddr2, (char**)&_pvData);
                        iRows3 = (int)strlen((char*)_pvData);
                        iCols3 = 1;
                    }
                }
                break;

            default:
                Scierror(999, _("%s: Wrong type for input argument #%d: String or handle expected.\n"), fname, 1);
                return 0;
                break;
        }

        if (hdl != 0)
        {
            pobjUID = (char*)getObjectFromHandle(hdl);

            if (pobjUID == NULL)
            {
                Scierror(999, _("%s: The handle is not or no more valid.\n"), fname);
                return 0;
            }

            // Only set the property whitout doing anythig else.
            //static int sciSet(void* _pvCtx, char *pobjUID, char *marker, void* value, int valueType, int *numrow, int *numcol)
            setStatus = callSetProperty(pvApiCtx, pobjUID, _pvData, valueType, iRows3, iCols3, pstProperty);
            if (valueType == sci_strings)
            {
                //free allacted data
                if (isMatrixOfString == 1)
                {
                    freeAllocatedMatrixOfString(iRows3, iCols3, (char**)_pvData);
                }
                else
                {
                    freeAllocatedSingleString((char*)_pvData);
                }
            }
        }
        else
        {
#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 i = 0;
            int iPropertyFound = 0;

            for (i = 0; i < NB_PROPERTIES_SUPPORTED; i++)
            {

                if (strcmp(propertiesSupported[i], pstProperty) == 0)
                {
                    iPropertyFound = 1;
                }
            }

            if (iPropertyFound)
            {
                // we do nothing with "figure_style" "new" (to remove in 5.4)
                int bDoSet = ((isMatrixOfString) && (strcmp(pstProperty, "figure_style") == 0) && (strcmp(((char**)_pvData)[0], "new") == 0)) != 1;

                if (bDoSet)
                {
                    setStatus = callSetProperty(pvApiCtx, NULL, _pvData, valueType, iRows3, iCols3, pstProperty);
                    if (valueType == sci_strings)
                    {
                        //free allocated data
                        if (isMatrixOfString == 1)
                        {
                            freeAllocatedMatrixOfString(iRows3, iCols3, (char**)_pvData);
                        }
                        else
                        {
                            freeAllocatedSingleString((char*)_pvData);
                        }
                    }
                }
            }
            else
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: a valid property expected.\n"), fname, 1);
                if (isMatrixOfString)
                {
                    freeArrayOfString((char **)_pvData, iRows3 * iCols3);
                }
                return 0;
            }
        }

        AssignOutputVariable(pvApiCtx, 1) = 0;
        ReturnArguments(pvApiCtx);
    }

    return 0;
}
Beispiel #15
0
/*--------------------------------------------------------------------------*/
int C2F(sci_funcprot)(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    CheckLhs(1, 1);
    CheckRhs(0, 1);

    if (Rhs == 0)
    {
        double dOut = (double) getfuncprot();

        if (createScalarDouble(pvApiCtx, Rhs + 1, dOut) != 0)
        {
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
    }
    else if (Rhs == 1)
    {
        int ilevel = 0;
        int *piAddressVarOne = NULL;
        double dVarOne = 0.;
        double dPreviousValue = (double) getfuncprot();

        /* get Address of inputs */
        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;
        }

        /* check input type */
        if (isDoubleType(pvApiCtx, piAddressVarOne) != 1)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), fname, 1);
            return 0;
        }

        if (isScalar(pvApiCtx, piAddressVarOne) != 1)
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 1);
            return 0;
        }

        if (getScalarDouble(pvApiCtx, piAddressVarOne, &dVarOne) != 0)
        {
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
            return 0;
        }

        ilevel = (int) dVarOne;

        if (dVarOne != (double)ilevel)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: An integer value expected.\n"), fname, 1);
            return 0;
        }

        if (!setfuncprot(ilevel))
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: 0,1 or 2 expected.\n"), fname, 1);
            return 0;
        }
        else
        {
            if (createScalarDouble(pvApiCtx, Rhs + 1, dPreviousValue) != 0)
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }

            LhsVar(1) = Rhs + 1;
            PutLhsVar();
        }
    }
    return 0;
}
bool OpenCLParser::convert(std::string fileNameIN, std::string fileNameOUT) {

	if ( access( fileNameIN.c_str(), F_OK ) == -1 ) {
		LOG(ERROR) << "kernel source file = '" << fileNameIN.c_str() << "' doesn't exist";
		return false;
	}

	if ( access( fileNameIN.c_str(), R_OK ) == -1 ) {
		LOG(ERROR) << "kernel source file = '" << fileNameIN.c_str() << "' isn't readable";
		return false;
	}

	if ( access( fileNameOUT.c_str(), F_OK ) == 0 ) {

		 struct stat statIN;
		 if (stat(fileNameIN.c_str(), &statIN) == -1) {
		       perror(fileNameIN.c_str());
		       return false;
		 }

		 struct stat statOUT;
		 if (stat(fileNameOUT.c_str(), &statOUT) == -1) {
		       perror(fileNameOUT.c_str());
		       return false;
		 }

		 if ( statOUT.st_mtime > statIN.st_mtime ) {
			 DLOG(INFO) << "kernel source file = '" << fileNameOUT.c_str() << "' up-to-date";
			 return true;
		 }
	}

	std::ifstream file;
	file.open(fileNameIN.c_str(), std::ifstream::in );
	if ( ! file.is_open() ) {
		LOG(ERROR) << "failed to open file = '" << fileNameIN.c_str() << "' for reading";
		return false;
	}

    std::string line;
    std::string kernel_buffer;
    std::string kernel_name;
    std::string kernel_type;
    std::string kernel_name_typed;
    std::string kernel_line_typed;
    std::string kernel_modified;
    std::string type_replace;
    std::string stdOpenCL;

    stdOpenCL += "// This file was auto-generated from file '" + fileNameIN + "' to conform to standard OpenCL\n";

    bool recording = false;

    while (std::getline(file, line)) {

    	if ( isAttributeLine(line) ) {
    		if ( recording ) {
          		recording = false;
       		}
        	kernel_name_typed = getTypedKernelName(line);
        	kernel_line_typed = "__kernel void " + kernel_name_typed + getTypedKernelLine(line) + " {";

        	if ( isFloatType(kernel_name_typed) ) {
        		type_replace = "float";
        	}
        	if ( isDoubleType(kernel_name_typed) ) {
        		type_replace = "double";
        	}

        	kernel_modified = kernel_line_typed + "\n" + kernel_buffer;

       		boost::regex re;
       		re = boost::regex("\\sT\\s", boost::regex::perl);
       		kernel_modified = boost::regex_replace(kernel_modified, re, " "+type_replace+" ");

       		re = boost::regex("\\sT\\*\\s", boost::regex::perl);
       		kernel_modified = boost::regex_replace(kernel_modified, re, " "+type_replace+"* ");

        	stdOpenCL += kernel_modified;
        	continue;
    	}

    	if ( isTemplateKernelLine(line) ) {
    		kernel_name = getKernelName(line);
    		kernel_type = getKernelType(line);
        DLOG(INFO)<<"found template kernel '"<<kernel_name<<"' with type '"<<kernel_type<<"'";

        	if ( recording == false ) {
        		recording = true;
        	} else {
        		LOG(ERROR) << "error parsing kernel source file = '" << fileNameIN.c_str() << "'";
        		return false;
        	}
        	continue;
    	}

    	if ( recording ) {
    		kernel_buffer += line + "\n";
    	} else {
        	kernel_buffer = "";
        	stdOpenCL += line + "\n";
    	}

    }

    std::ofstream out(fileNameOUT.c_str());
    out << stdOpenCL;
    out.close();
    DLOG(INFO) << "convert AMD OpenCL '"<<fileNameIN.c_str()<<"' to standard OpenCL '"<<fileNameOUT.c_str()<<"'";
	return true;
}
Beispiel #17
0
/*--------------------------------------------------------------------------*/
int sci_mgetl(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int numberOfLinesToRead = -1;

    Rhs = Max(0, Rhs);

    CheckRhs(1, 2);
    CheckLhs(1, 1);

    if (Rhs == 2)
    {
        int *piAddressVarTwo = NULL;

        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
            return 0;
        }

        if ( isDoubleType(pvApiCtx, piAddressVarTwo) )
        {
            double dValue = 0.;
            if (!isScalar(pvApiCtx, piAddressVarTwo))
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: Integer expected.\n"), fname, 2);
                return 0;
            }

            if ( getScalarDouble(pvApiCtx, piAddressVarTwo, &dValue) == 0)
            {
                numberOfLinesToRead = (int)dValue;
            }
            else
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: Integer expected.\n"), fname, 2);
            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;
    }

    if ( isStringType(pvApiCtx, piAddressVarOne) || isDoubleType(pvApiCtx, piAddressVarOne) )
    {
        char **wcReadedStrings = NULL;
        int numberOfLinesReaded = 0;
        int fileDescriptor = -1;
        int iErrorMgetl = 0;
        BOOL bCloseFile = FALSE;

        if (!isScalar(pvApiCtx, piAddressVarOne))
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: String or logical unit expected.\n"), fname, 1);
            return 0;
        }

        if (isStringType(pvApiCtx, piAddressVarOne))
        {
            char *fileName = NULL;
            if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &fileName) == 0)
            {
                char *expandedFileName = expandPathVariable(fileName);
                freeAllocatedSingleString(fileName);
                fileName = NULL;

                if (IsAlreadyOpenedInScilab(expandedFileName))
                {
                    int fd = GetIdFromFilename(expandedFileName);
                    fileDescriptor = fd;
                    if (expandedFileName)
                    {
                        FREE(expandedFileName);
                        expandedFileName = NULL;
                    }
                    bCloseFile = FALSE;
                }
                else
                {
#define READ_ONLY_TEXT_MODE "rt"
                    int fd = 0;
                    int f_swap = 0;
                    double res = 0.0;
                    int ierr = 0;

                    C2F(mopen)(&fd, expandedFileName, READ_ONLY_TEXT_MODE, &f_swap, &res, &ierr);
                    bCloseFile = TRUE;

                    switch (ierr)
                    {
                        case MOPEN_NO_ERROR:
                            fileDescriptor = fd;
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            break;
                        case MOPEN_NO_MORE_LOGICAL_UNIT:
                        {
                            Scierror(66, _("%s: Too many files opened!\n"), fname);
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            return 0;
                        }
                        break;
                        case MOPEN_CAN_NOT_OPEN_FILE:
                        {
                            Scierror(999, _("%s: Cannot open file %s.\n"), fname, expandedFileName);
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            return 0;
                        }
                        break;
                        case MOPEN_NO_MORE_MEMORY:
                        {
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            Scierror(999, _("%s: No more memory.\n"), fname);
                            return 0;
                        }
                        break;
                        case MOPEN_INVALID_FILENAME:
                        {
                            Scierror(999, _("%s: invalid filename %s.\n"), fname, expandedFileName);
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            return 0;
                        }
                        break;
                        case MOPEN_INVALID_STATUS:
                        default:
                        {
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            Scierror(999, _("%s: invalid status.\n"), fname);
                            return 0;
                        }
                        break;
                    }
                }
            }
            else
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }
        else /* double */
        {
            double dValue = 0.;

            if ( !getScalarDouble(pvApiCtx, piAddressVarOne, &dValue) )
            {
                FILE *fd = NULL;
                fileDescriptor = (int)dValue;
                if ((fileDescriptor == STDIN_ID) || (fileDescriptor == STDOUT_ID))
                {
                    SciError(244);
                    return 0;
                }

                fd = GetFileOpenedInScilab(fileDescriptor);
                if (fd == NULL)
                {
                    Scierror(245, _("%s: No input file associated to logical unit %d.\n"), fname, fileDescriptor);
                    return 0;
                }
            }
            else
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }

        wcReadedStrings = mgetl(fileDescriptor, numberOfLinesToRead, &numberOfLinesReaded, &iErrorMgetl);

        if (bCloseFile)
        {
            double dErrClose = 0.;
            C2F(mclose)(&fileDescriptor, &dErrClose);
        }

        switch (iErrorMgetl)
        {
            case MGETL_NO_ERROR:
            {
                if (numberOfLinesReaded == 0)
                {
                    if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
                    {
                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                }
                else
                {
                    int m = numberOfLinesReaded;
                    int n = 1;

                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m, n, wcReadedStrings);
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0);
                        Scierror(17, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                }

                freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
                wcReadedStrings = NULL;
            }
            break;

            case MGETL_EOF:
            {
                if (numberOfLinesReaded == 0)
                {
                    if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
                    {
                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                }
                else
                {
                    int m = numberOfLinesReaded;
                    int n = 1;

                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m, n, wcReadedStrings);
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0);
                        Scierror(17, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
                    wcReadedStrings = NULL;
                }
            }
            break;

            case MGETL_MEMORY_ALLOCATION_ERROR:
            {
                if (wcReadedStrings)
                {
                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
                    wcReadedStrings = NULL;
                }
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
            break;

            case MGETL_ERROR:
            {
                if (wcReadedStrings)
                {
                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
                    wcReadedStrings = NULL;
                }
                Scierror(999, _("%s: error.\n"), fname);
                return 0;
            }
            break;
        }

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
    }
    else
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String or logical unit expected.\n"), fname, 1);
    }

    return 0;
}
Beispiel #18
0
/* ==================================================================== */
int sci_foo(char *fname, void* pvApiCtx, unsigned long fname_len)
{
    // Error management variable
    SciErr sciErr;

    ////////// Variables declaration //////////
    int m1 = 0, n1 = 0;
    int *piAddressVarOne = NULL;
    double *matrixOfDouble = NULL;
    double *newMatrixOfDouble = NULL;

    int m2 = 0, n2 = 0;
    int *piAddressVarTwo = NULL;
    int *matrixOfBoolean = NULL;
    int *newMatrixOfBoolean = NULL;
    int i = 0;


    ////////// Check the number of input and output arguments //////////
    /* --> [c, d] = foo(a, b) */
    /* check that we have only 2 input arguments */
    /* check that we have only 2 output argument */
    CheckInputArgument(pvApiCtx, 2, 2) ;
    CheckOutputArgument(pvApiCtx, 2, 2) ;


    ////////// Manage the first input argument (double) //////////
    /* get Address of inputs */
    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    /* Check that the first input argument is a real matrix (and not complex) */
    if ( !isDoubleType(pvApiCtx, piAddressVarOne) ||  isVarComplex(pvApiCtx, piAddressVarOne) )
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), fname, 1);
        return 0;
    }

    /* get matrix */
    sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &matrixOfDouble);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    ////////// Manage the second input argument (boolean) //////////

    /* get Address of inputs */
    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    if ( !isBooleanType(pvApiCtx, piAddressVarTwo) )
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A boolean matrix expected.\n"), fname, 2);
        return 0;
    }

    /* get matrix */
    sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2, &matrixOfBoolean);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    ////////// Check the consistency of the two input arguments //////////

    if ((m1 != m2) | - (n1 != n2))
    {
        Scierror(999, _("%s: Wrong size for input arguments: Same size expected.\n"), fname, 1);
        return 0;
    }


    newMatrixOfDouble = (double*)malloc(sizeof(double) * m1 * n1);
    ////////// Application code //////////
    // Could be replaced by a call to a library

    for (i = 0; i < m1 * n1; i++)
    {
        /* For each element of the matrix, multiply by 2 */
        newMatrixOfDouble[i] = matrixOfDouble[i] * 2;
    }

    newMatrixOfBoolean = (int*)malloc(sizeof(double) * m2 * n2);
    for (i = 0; i < m2 * n2; i++)
    {
        /* For each element of the matrix, invert the value */
        newMatrixOfBoolean[i] = matrixOfBoolean[i] == TRUE ? FALSE : TRUE;
    }

    ////////// Create the output arguments //////////

    /* Create the matrix as return of the function */
    sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, newMatrixOfDouble);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    /* Create the matrix as return of the function */
    sciErr = createMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 2, m2, n2, newMatrixOfBoolean);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    ////////// Return the output arguments to the Scilab engine //////////

    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
    AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;

    ReturnArguments(pvApiCtx);

    return 0;
}
Beispiel #19
0
/*--------------------------------------------------------------------------*/
static int sci_format_tworhs(char *fname)
{
    /* format(1, 10) */
    int *piAddressVarOne = NULL;
    int *piAddressVarTwo = NULL;

    SciErr 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 = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
        return 0;
    }

    if (!isScalar(pvApiCtx, piAddressVarOne))
    {
        Scierror(999, _("%s: Wrong size for input argument #%d.\n"), fname, 1);
        return 0;
    }

    if (!isScalar(pvApiCtx, piAddressVarTwo))
    {
        Scierror(999, _("%s: Wrong size for input argument #%d.\n"), fname, 2);
        return 0;
    }

    if (isDoubleType(pvApiCtx, piAddressVarOne) && isDoubleType(pvApiCtx, piAddressVarTwo))
    {
        double type_value_d = 0.;
        double v_value_d = 0.;
        int v_value = 0;

        if (getScalarDouble(pvApiCtx, piAddressVarOne, &v_value_d) != 0)
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }

        if (getScalarDouble(pvApiCtx, piAddressVarTwo, &type_value_d) != 0)
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }

        v_value = (int)v_value_d;

        if ((type_value_d != (double)0) && (type_value_d != (double)1))
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: '0' or '1' expected.\n"), fname, 2);
            return 0;
        }

        if (v_value_d != (double)v_value)
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: An integer value expected.\n"), fname, 1);
            return 0;
        }

        if (type_value_d == 1)
        {
            if ((v_value < format_MIN) || (v_value > format_MAX))
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the interval [%d, %d].\n"), fname, 1, format_MIN, format_MAX);
                return 0;
            }

            setVariableFormat(v_value);
        }
        else
        {
            if ((v_value < format_e_MIN) || (v_value > format_MAX))
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the interval [%d, %d].\n"), fname, 1, format_e_MIN, format_MAX);
                return 0;
            }
            set_e_Format(v_value);
        }

        LhsVar(1) = 0;
        PutLhsVar();
    }
    /* format('e',10) & format(10,'e') syntax */
    else if ((isStringType(pvApiCtx, piAddressVarOne) &&
              (isDoubleType(pvApiCtx, piAddressVarTwo)) || (isDoubleType(pvApiCtx, piAddressVarOne) && isStringType(pvApiCtx, piAddressVarTwo))))
    {
        char *param = NULL;
        unsigned int value = 0;

        if (isStringType(pvApiCtx, piAddressVarOne))
        {
            double dvalue = 0;

            if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &param) != 0)
            {
                Scierror(999, _("%s: No more memory.\n"), fname);
                return 0;
            }

            if (getScalarDouble(pvApiCtx, piAddressVarTwo, &dvalue) != 0)
            {
                freeAllocatedSingleString(param);
                param = NULL;

                Scierror(999, _("%s: No more memory.\n"), fname);
                return 0;
            }
            value = (int)dvalue;
            if (dvalue != (double)value)
            {
                freeAllocatedSingleString(param);
                param = NULL;

                Scierror(999, _("%s: Wrong value for input argument #%d: An integer value expected.\n"), fname, 2);
                return 0;
            }
        }
        else                    /* matrix */
        {
            double dvalue = 0;

            if (getScalarDouble(pvApiCtx, piAddressVarOne, &dvalue) != 0)
            {
                Scierror(999, _("%s: No more memory.\n"), fname);
                return 0;
            }

            value = (int)dvalue;
            if (dvalue != (double)value)
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: An integer value expected.\n"), fname, 1);
                return 0;
            }

            if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &param) != 0)
            {
                Scierror(999, _("%s: No more memory.\n"), fname);
                return 0;
            }
        }

        if ((strcmp(e_type_format, param) == 0) || (strcmp(v_type_format, param) == 0))
        {

            if (strcmp(e_type_format, param) == 0)
            {
                freeAllocatedSingleString(param);
                param = NULL;

                if ((value < format_e_MIN) || (value > format_MAX))
                {
                    Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the interval [%d, %d].\n"), fname, 2, format_e_MIN,
                             format_MAX);
                    return 0;
                }

                set_e_Format(value);
            }
            else                /* v_type_format */
            {
                freeAllocatedSingleString(param);
                param = NULL;

                if ((value < format_MIN) || (value > format_MAX))
                {
                    Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the interval [%d, %d].\n"), fname, 2, format_MIN, format_MAX);
                    return 0;
                }

                setVariableFormat(value);
            }

            LhsVar(1) = 0;
            PutLhsVar();
        }
        else
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, e_type_format, v_type_format);
        }
    }
    else
    {
        Scierror(999, _("%s: Wrong type for inputs arguments.\n"), fname);
    }
    return 0;
}
Beispiel #20
0
int sci_tbx_sum(char *fname) {
  SciErr sciErr;
  
  int *piAddressVarOne = NULL;
  double dVarOne = 0.0;
  
  int *piAddressVarTwo = NULL;
  double dVarTwo = 0.0;
  
  double dOut = 0.0;
  
  /* check that we have only 2 input arguments */
  /* check that we have only 1 output argument */
  CheckRhs(2,2);
  CheckLhs(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 */
  if ( !isDoubleType(pvApiCtx, piAddressVarOne) )
  {
    Scierror(999,"%s: Wrong type for input argument #%d: A scalar expected.\n", fname, 1);
    return 0;
  }
  
  if ( !isDoubleType(pvApiCtx, piAddressVarTwo) )
  {
    Scierror(999,"%s: Wrong type for input argument #%d: A scalar expected.\n", fname, 2);
    return 0;
  }

  if ( getScalarDouble(pvApiCtx, piAddressVarOne, &dVarOne) )
  {
    Scierror(999,"%s: Wrong size for input argument #%d: A scalar expected.\n", fname, 1);
    return 0;
  }

  if ( getScalarDouble(pvApiCtx, piAddressVarTwo, &dVarTwo) )
  {
    Scierror(999,"%s: Wrong size for input argument #%d: A scalar expected.\n", fname, 2);
    return 0;
  }
  
  /* call c business function */  
  dOut = business_sum(dVarOne, dVarTwo);
  
  /* create result on stack */
  createScalarDouble(pvApiCtx, Rhs + 1, dOut);
  LhsVar(1) = Rhs + 1; 
}
Beispiel #21
0
/*--------------------------------------------------------------------------*/
static int sci_format_onerhs(char *fname)
{
    int *piAddressVarOne = NULL;
    SciErr 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;
    }

    if (isStringType(pvApiCtx, piAddressVarOne))
    {
        char *param = NULL;

        if (!isScalar(pvApiCtx, piAddressVarOne))
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, e_type_format, v_type_format);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &param) == 0)
        {
            if ((strcmp(e_type_format, param) == 0) || (strcmp(v_type_format, param) == 0))
            {
                double previous_mode = 0;
                double previous_numberDigits = 0;

                getFormat(&previous_mode, &previous_numberDigits);
                if (strcmp(e_type_format, param) == 0)
                {
                    set_e_Format((int)previous_numberDigits);
                }
                else            /* v_type_format */
                {
                    setVariableFormat((int)previous_numberDigits);
                }

                freeAllocatedSingleString(param);
                param = NULL;

                LhsVar(1) = 0;
                PutLhsVar();
            }
            else
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, e_type_format, v_type_format);
                return 0;
            }
        }
        else
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }
    }
    else if (isDoubleType(pvApiCtx, piAddressVarOne))
    {
        if (isScalar(pvApiCtx, piAddressVarOne))
        {
            double dValue = 0.;

            if (getScalarDouble(pvApiCtx, piAddressVarOne, &dValue) == 0)
            {
                double mode = 0.;
                double previous_numberDigits = 0.;

                unsigned int value = (int)dValue;

                if (dValue != (double)value)
                {
                    Scierror(999, _("%s: Wrong value for input argument #%d: An integer value expected.\n"), fname, 1);
                    return 0;
                }

                getFormat(&mode, &previous_numberDigits);

                if (mode == mode_e)
                {
                    if ((value < format_e_MIN) || (value > format_MAX))
                    {
                        Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the interval [%d, %d].\n"), fname, 1, format_e_MIN,
                                 format_MAX);
                        return 0;
                    }

                    set_e_Format(value);
                }
                else            /* mode_variable */
                {
                    if ((value < format_MIN) || (value > format_MAX))
                    {
                        Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the interval [%d, %d].\n"), fname, 1, format_MIN,
                                 format_MAX);
                        return 0;
                    }

                    setVariableFormat(value);
                }
            }
            LhsVar(1) = 0;
            PutLhsVar();
        }
        else
        {
            if (checkVarDimension(pvApiCtx, piAddressVarOne, 1, 2) || checkVarDimension(pvApiCtx, piAddressVarOne, 2, 1))
            {
                int nbRowsOne = 0;
                int nbColsOne = 0;
                double *pDouble = NULL;

                sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &nbRowsOne, &nbColsOne, &pDouble);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                    return 0;
                }

                if ((pDouble[1] != mode_e) && (pDouble[1] != mode_variable))
                {
                    Scierror(999, _("%s: Wrong value for input argument #%d.\n"), fname, 1);
                    return 0;
                }
                else
                {
                    if (pDouble[1] == mode_e)
                    {
                        if ((pDouble[0] < format_e_MIN) || (pDouble[0] > format_MAX))
                        {
                            Scierror(999, _("%s: Wrong value for input argument #%d.\n"), fname, 1);
                            return 0;
                        }
                        set_e_Format((int)pDouble[0]);

                    }
                    else        /* mode_variable */
                    {
                        if ((pDouble[0] < format_MIN) || (pDouble[0] > format_MAX))
                        {
                            Scierror(999, _("%s: Wrong value for input argument #%d.\n"), fname, 1);
                            return 0;
                        }
                        setVariableFormat((int)pDouble[0]);
                    }

                    LhsVar(1) = 0;
                    PutLhsVar();
                }
            }
            else
            {
                Scierror(999, _("%s: Wrong size for input argument #%d.\n"), fname, 1);
                return 0;
            }
        }
    }
    else
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string or a scalar integer value expected.\n"), fname, 1);
    }
    return 0;
}
Beispiel #22
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;
}
Beispiel #23
0
/*--------------------------------------------------------------------------*/
int sci_getdate(char *fname, unsigned long fname_len)
{
    SciErr sciErr;

    Rhs = Max(Rhs, 0);
    CheckRhs(0, 1) ;
    CheckLhs(0, 1) ;

    if (Rhs == 0)
    {
        int iErr = 0;
        double *dDate = getCurrentDateAsDoubleVector(&iErr);

        if (iErr)
        {
            Scierror(999, _("%s: An error occurred.\n"), fname);
            if (dDate)
            {
                FREE(dDate);
                dDate = NULL;
            }
            return 0;
        }

        if (dDate)
        {
            sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 1, NB_ELEMNT_ARRAY_GETDATE, dDate);
            FREE(dDate);
            dDate = NULL;

            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }
        else
        {
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    else /* Rhs == 1 */
    {
        int *piAddressVarOne = 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;
        }

        if (isStringType(pvApiCtx, piAddressVarOne))
        {
            if (isScalar(pvApiCtx, piAddressVarOne))
            {
                double dTime = 0.;

                char *pStr = NULL;
                if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &pStr) != 0)
                {
                    Scierror(999, _("%s: No more memory.\n"), fname);
                    return 0;
                }

                if (strcmp(pStr, "s") != 0)
                {
                    freeAllocatedSingleString(pStr);
                    pStr = NULL;
                    Scierror(999, _("%s: Wrong value for input argument #%d: '%s' expected.\n"), fname, 1, "s");
                    return 0;
                }

                freeAllocatedSingleString(pStr);
                pStr = NULL;

                dTime = getCurrentDateAsUnixTimeConvention();

                if (createScalarDouble(pvApiCtx, Rhs + 1, dTime) != 0)
                {
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                    return 0;
                }
            }
            else
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
                return 0;
            }
        }
        else if (isDoubleType(pvApiCtx, piAddressVarOne))
        {
            if (isEmptyMatrix(pvApiCtx, piAddressVarOne))
            {
                if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
                {
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                    return 0;
                }
            }
            else if (!isVarComplex(pvApiCtx, piAddressVarOne))
            {
                int iErr = 0;
                double *dValues = NULL;
                double *dResults = NULL;
                int m = 0, n = 0;
                int nbElements = 0;
                int i = 0;
                sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m, &n, &dValues);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
                    return 0;
                }

                nbElements = m * n;

                for (i = 0; i < nbElements; i++)
                {
                    if (dValues[i] < 0.)
                    {
                        Scierror(999, _("%s: Wrong value for input argument #%d: Must be > %d.\n"), fname, 1, 0);
                        return 0;
                    }
                }

                dResults = getConvertedDateAsMatrixOfDouble(dValues, nbElements, &iErr);

                if (iErr == 2)
                {
                    FREE(dResults);
                    Scierror(999, _("%s: An error occurred.\n"), fname);
                    return 0;
                }

                if (dResults == NULL)
                {
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                    return 0;
                }


                sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, nbElements, NB_ELEMNT_ARRAY_GETDATE, dResults);

                FREE(dResults);
                dResults = NULL;

                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                    return 0;
                }
            }
            else
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: A real expected.\n"), fname, 1);
                return 0;
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: Integer or '%s' expected.\n"), fname, 1, "s");
            return 0;
        }
    }

    LhsVar(1) = Rhs + 1;
    PutLhsVar();
    return 0;
}