/*--------------------------------------------------------------------------*/
int getAllocatedSingleString(void* _pvCtx, int* _piAddress, char** _pstData)
{
    SciErr sciErr = sciErrInit();
    int iRows = 0;
    int iCols = 0;
    int iLen = 0;

    if (isScalar(_pvCtx, _piAddress) == 0 || isStringType(_pvCtx, _piAddress) == 0)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_SINGLE_STRING, _("%s: Wrong type for input argument #%d: A single string expected.\n"), "getAllocatedSingleString", getRhsFromAddress(_pvCtx, _piAddress));
        printError(&sciErr, 0);
        return sciErr.iErr;
    }

    sciErr = getMatrixOfString(_pvCtx, _piAddress, &iRows, &iCols, &iLen, NULL);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_SINGLE_STRING, _("%s: Unable to get argument data"), "getAllocatedSingleString");
        printError(&sciErr, 0);
        return sciErr.iErr;
    }

    *_pstData = (char*)MALLOC(sizeof(char) * (iLen + 1)); //+1 for null termination

    sciErr = getMatrixOfString(_pvCtx, _piAddress, &iRows, &iCols, &iLen, _pstData);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_SINGLE_STRING, _("%s: Unable to get argument data"), "getAllocatedSingleString");
        printError(&sciErr, 0);
        FREE(*_pstData);
        return sciErr.iErr;
    }

    return 0;
}
/* ========================================================================== */
int sci_gpuLoadDynLib(char* fname)
{
    int row         = 0;
    int col         = 0;
    int* piLen      = NULL;
    char** pstData  = NULL;
    int* pstrA      = NULL;
    int inputType_A = 0;
    int  i          = 0;

    CheckRhs(1, 1);
    CheckLhs(0, 1);

    // get lib
    getVarAddressFromPosition(pvApiCtx,1,&pstrA);
    getVarType(pvApiCtx, pstrA, &inputType_A);
    getMatrixOfString(pvApiCtx, pstrA, &row, &col, NULL, NULL);
    piLen = (int*)malloc(sizeof(int) * row * col);
    getMatrixOfString(pvApiCtx, pstrA, &row, &col, piLen, NULL);
    pstData = (char**)malloc(sizeof(char*) * row * col);
    for(i = 0 ; i < row * col ; i++)
    {
        pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));
    }
    getMatrixOfString(pvApiCtx, pstrA, &row, &col, piLen, pstData);

    // open lib
    printf("%s\n", *pstData);
    Sci_dlopen(*pstData);

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

    return 0;
}
Beispiel #3
0
/**
 * Read a single string on the stack.
 *
 * @param _pvCtx private api context (opaque structure)
 * @param rhsPosition the position on the stack.
 * @param[out] out the read value.
 * @param fname the function name used for the call.
 * @return status of the operation (<> 0 on error)
 */
int readSingleString(void* _pvCtx, int rhsPosition, char** out, const char* fname)
{
    int* argumentPointer = NULL;
    int rowsArgument = 0;
    int colsArgument = 0;
    int lenArgument = 0;
    char* value = NULL;

    *out = NULL;
    SciErr sciErr;

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

    sciErr = getMatrixOfString(_pvCtx, argumentPointer, &rowsArgument,
                               &colsArgument, NULL, NULL);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, rhsPosition);
        return -1;
    }

    if (rowsArgument != 1 || colsArgument != 1)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, rhsPosition);
        return -1;
    }

    sciErr = getMatrixOfString(_pvCtx, argumentPointer, &rowsArgument,
                               &colsArgument, &lenArgument, NULL);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, rhsPosition);
        return -1;
    }

    value = (char*) MALLOC(sizeof(char) * (lenArgument + 1)); //+ 1 for null termination
    value[lenArgument] = '\0';
    sciErr = getMatrixOfString(_pvCtx, argumentPointer, &rowsArgument,
                               &colsArgument, &lenArgument, &value);
    if (sciErr.iErr)
    {
        FREE(value);
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, rhsPosition);
        return -1;
    }

    *out = value;
    return 0;
}
/*--------------------------------------------------------------------------*/
int getAllocatedMatrixOfString(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, char*** _pstData)
{
    SciErr sciErr = getMatrixOfString(_pvCtx, _piAddress, _piRows, _piCols, NULL, NULL);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedMatrixOfString");
        printError(&sciErr, 0);
        return sciErr.iErr;
    }

    int* piLen = (int*)MALLOC(sizeof(int) **_piRows **_piCols);

    sciErr = getMatrixOfString(_pvCtx, _piAddress, _piRows, _piCols, piLen, NULL);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedMatrixOfString");
        printError(&sciErr, 0);
        if (piLen)
        {
            FREE(piLen);
            piLen = NULL;
        }
        return sciErr.iErr;
    }

    *_pstData = (char**)MALLOC(sizeof(char*) **_piRows **_piCols);
    for (int i = 0 ; i < *_piRows **_piCols ; i++)
    {
        (*_pstData)[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1));//+1 for null termination
    }

    sciErr = getMatrixOfString(_pvCtx, _piAddress, _piRows, _piCols, piLen, *_pstData);
    if (piLen)
    {
        FREE(piLen);
        piLen = NULL;
    }
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedMatrixOfString");
        printError(&sciErr, 0);
        for (int i = 0 ; i < *_piRows **_piCols ; i++)
        {
            FREE((*_pstData)[i]);
        }
        FREE(*_pstData);
        return sciErr.iErr;
    }

    return 0;
}
static bool export_strings(int _iH5File, int *_piVar, char* _pstName)
{
    int iRet = 0;
    int* piLen = NULL;
    char** pstData = NULL;
    int piDims[2];

    SciErr sciErr = getMatrixOfString(pvApiCtx, _piVar, &piDims[0], &piDims[1], NULL, NULL);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return false;
    }

    piLen = (int*)MALLOC(piDims[0] * piDims[1] * sizeof(int));
    sciErr = getMatrixOfString(pvApiCtx, _piVar, &piDims[0], &piDims[1], piLen, NULL);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return false;
    }

    pstData = (char**)MALLOC(sizeof(char*) * piDims[0] * piDims[1]);
    for (int i = 0 ; i < piDims[0] * piDims[1] ; i++)
    {
        pstData[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1));// for null termination
    }

    sciErr = getMatrixOfString(pvApiCtx, _piVar, &piDims[0], &piDims[1], piLen, pstData);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return false;
    }

    iRet = writeStringMatrix(_iH5File, _pstName, 2, piDims, pstData);

    if (iRet)
    {
        return false;
    }

    char pstMsg[512];
    sprintf(pstMsg, "string (%d x %d)", piDims[0], piDims[1]);
    print_type(pstMsg);

    freeArrayOfString(pstData, piDims[0] * piDims[1]);
    return true;
}
Beispiel #6
0
// This function returns a pointer to a function or a Scilab pointer to a function
// The choice is performed on the type of the given Scilab parameter
voidf GetFunctionPtr(char * name, int n, FTAB Table[], voidf scifun, int * ifunc, int * lhs, int * rhs) 
{
  int type, rep, m_tmp, n_tmp, i;
  int * tmp_addr = NULL;
  int * pi_len = NULL;
  char ** pst_strings = NULL;
  voidf f;
  SciErr _SciErr;

  _SciErr = getVarAddressFromPosition(pvApiCtx, n, &tmp_addr); GLCPD_ERROR_RETURN_NULL;
  _SciErr = getVarType(pvApiCtx, tmp_addr, &type); GLCPD_ERROR_RETURN_NULL;

  switch(type) 
    {
    case sci_strings: 
      _SciErr = getMatrixOfString(pvApiCtx, tmp_addr, &n_tmp, &m_tmp, NULL, NULL); GLCPD_ERROR_RETURN_NULL;
      pi_len = (int *)MALLOC(n_tmp*m_tmp*sizeof(int));
      pst_strings = (char **)MALLOC(n_tmp*m_tmp*sizeof(char *));
      _SciErr = getMatrixOfString(pvApiCtx, tmp_addr, &n_tmp, &m_tmp, pi_len, NULL); GLCPD_ERROR_RETURN_NULL;
      for(i=0;i<n_tmp*m_tmp;i++) pst_strings[i] = (char *)MALLOC((pi_len[i]+1)*sizeof(char));
      _SciErr = getMatrixOfString(pvApiCtx, tmp_addr, &n_tmp, &m_tmp, pi_len, pst_strings); GLCPD_ERROR_RETURN_NULL;

      f = SetFunction(pst_strings[0], &rep, Table);

      if (pst_strings) freeArrayOfString(pst_strings, n_tmp*m_tmp);
      if (pi_len) FREE(pi_len);

      if (rep==1)
	{
	  Scierror(999,"Function not found is %s\n", name);
	  return (voidf)0;
	}

      return f ;

    case sci_c_function: 
      GetRhsVar(n, "f", lhs, rhs, ifunc);
      return (voidf)scifun ;
      
    default: 
      Scierror(999,"Wrong parameter in %s ! (number %d)\r\n",name,n);
      return (voidf)0;
    }
}
/*--------------------------------------------------------------------------*/
SciErr readNamedMatrixOfString(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piLength, char** _pstStrings)
{
    int* piAddr = NULL;

    SciErr sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_READ_NAMED_STRING, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfString", _pstName);
        return sciErr;
    }

    sciErr = getMatrixOfString(_pvCtx, piAddr, _piRows, _piCols, _piLength, _pstStrings);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_READ_NAMED_STRING, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfString", _pstName);
        return sciErr;
    }

    return sciErr;
}
Beispiel #8
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 #9
0
/*--------------------------------------------------------------------------*/
int sci_typename_two_rhs(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int m1 = 0, n1 = 0;
    int iType1			= 0;
    int *piAddressVarOne = NULL;
    char *pStVarOne = NULL;
    int lenStVarOne = 0;

    int m2 = 0, n2 = 0;
    int iType2			= 0;
    int *piAddressVarTwo = NULL;
    double *pdVarTwo = NULL;

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


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


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

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

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

    if ( iType2 != sci_matrix )
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), fname, 2);
        return 0;
    }

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

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

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

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

    pStVarOne = (char*)MALLOC(sizeof(char) * (lenStVarOne + 1));
    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }


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

        ierr = addNamedType(pStVarOne, (int)pdVarTwo[0]);

        switch (ierr)
        {
            case -1 :
                Scierror(999, _("%s: '%s' already exists.\n"), fname, pStVarOne);
                break;
            case 0:
                LhsVar(1) = 0;
                PutLhsVar();
                break;
                break;

            case 1:
            case 3:
                SciError(224);
                break;

            case 2:
                SciError(225);
                break;

            default:
                /* never here */
                Scierror(999, _("%s: Unknown Error.\n"), fname);
                break;
        }

        FREE(pStVarOne);
        pStVarOne = NULL;
    }
    else
    {
        Scierror(999, _("%s: No more memory.\n"), fname);
    }
    return 0;
}
Beispiel #10
0
/*--------------------------------------------------------------------------*/
int sci_setenv(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int m1 = 0, n1 = 0;
    int *piAddressVarOne = NULL;
    int iType1 = 0;
    char *pStVarOne = NULL;
    int lenStVarOne = 0;

    int m2 = 0, n2 = 0;
    int *piAddressVarTwo = NULL;
    int iType2 = 0;
    char *pStVarTwo = NULL;
    int lenStVarTwo = 0;

    int m_out1 = 0, n_out1 = 0;

    int result = 0;

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

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

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

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

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

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

    if (iType2  != sci_strings )
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
        return 0;
    }

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

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

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

    if ( (m2 != n2) && (n2 != 1) )
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
        return 0;
    }

    pStVarOne = (char*)MALLOC(sizeof(char) * (lenStVarOne + 1));
    if (pStVarOne)
    {
        sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
            return 0;
        }

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

    pStVarTwo = (char*)MALLOC(sizeof(char) * (lenStVarTwo + 1));
    if (pStVarTwo)
    {
        sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
            return 0;
        }

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

    result = setenvc(pStVarOne, pStVarTwo);

    FREE(pStVarOne);
    pStVarOne = NULL;
    FREE(pStVarTwo);
    pStVarTwo = NULL;

    m_out1 = 1;
    n_out1 = 1;
    sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m_out1, n_out1, &result);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    LhsVar(1) = Rhs + 1;

    PutLhsVar();
    return 0;
}
/* ==================================================================== */
int sci_edf_set_patientname(char *fname)
{
  SciErr sciErr;
  
  int m1 = 0, n1 = 0;
  int *piAddressVarOne = NULL;
  int* piLenVarOne = NULL;
  double *pdVarOne = NULL;
  int iType1 = 0;  
  
  int m2 = 0, n2 = 0;
  int *piAddressVarTwo = NULL;
  int* piLenVarTwo = NULL;
   char **stringData  = NULL;
  int iType2 = 0;  
  

  int m_out = 0, n_out = 0;
  double *dOut = NULL;

  int i;
  int handle;

  /* --> result = csum(3,8)
  /* check that we have only 2 parameters input */
  /* check that we have only 1 parameters output */
  CheckInputArgument(pvApiCtx,2,2) ;
  CheckOutputArgument(pvApiCtx,1,1) ;   
  
  /* get Address of inputs */
  sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
  if(sciErr.iErr)
  {
    printError(&sciErr, 0);
    return 0;
  }
  
  sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
  if(sciErr.iErr)
  {
    printError(&sciErr, 0);
    return 0;
  }
  
  

  /* check input type */
  sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
  if(sciErr.iErr)
  {
    printError(&sciErr, 0);
    return 0;
  } 
   
  if ( iType1 != sci_matrix )
  {
    Scierror(999,"%s: Wrong type for input argument #%d: A integer expected.\n",fname,1);
    return 0;
  }

  sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
  if(sciErr.iErr)
  {
    printError(&sciErr, 0);
    return 0;
  }   
  
  if ( iType2 != sci_strings )
  {
  	Scierror(999,"%s: Wrong type for input argument #%d: A string expected.\n",fname,2);
  	return 0;
  }




  sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne,&m1,&n1,&pdVarOne);
  if(sciErr.iErr)
  {
    printError(&sciErr, 0);
    return 0;
  }


  
 /* get string */
    sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo,&m2, &n2, NULL, NULL);
  if(sciErr.iErr)
  {
    printError(&sciErr, 0);
    return 0;
  }

    piLenVarTwo = (int*)malloc(sizeof(int) * m2 * n2);
   sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &m2, &n2, piLenVarTwo, NULL);
                if(sciErr.iErr)
                {
                        printError(&sciErr, 0);
                        return 0;
                }

                stringData = (char**)malloc(sizeof(char*) * m2 * n2);
                for(i = 0 ; i < n2 * m2 ; i++)
                {
                        stringData[i] = (char*)malloc(sizeof(char) * (piLenVarTwo[i] + 1));//+ 1 for null termination
                }

                sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &m2, &n2, piLenVarTwo, stringData);
                if(sciErr.iErr)
                {
                        printError(&sciErr, 0);
                        return 0;
                }




  
  /* check size */
  if ( (m1 != 1) || (n1 != 1) ) 
  {
  	Scierror(999,"%s: Wrong size for input argument #%d: A scalar expected.\n",fname,1);
  	return 0;
  }
  if ( (m2 !=1) || (n2 !=1) ) 
  {
  	Scierror(999,"%s: Wrong size for input argument #%d: A single stringr expected.\n",fname,2);
  	return 0;
  }



  
  /* call c function csum */
//  csum(&pdVarOne[0],&pdVarTwo[0],&dOut);


if ( edf_set_patientname(pdVarOne[0],  stringData[0]) <0)
{
     Scierror(999,"Could not write string\n");
    return 0;
}
  

  m_out = 1;  n_out = 1;

  dOut =  (double*)malloc(sizeof(double) * m_out*n_out);
 // CreateVar(1, MATRIX_OF_DOUBLE_DATATYPE, &m_out, &n_out, &dout);
  dOut[0]=0;  
  /* create result on stack */
  createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m_out, n_out, dOut);
 
  free(dOut);
  AssignOutputVariable(pvApiCtx,1) = nbInputArgument(pvApiCtx) + 1; 
  
  /* This function put on scilab stack, the lhs variable
  which are at the position lhs(i) on calling stack */
  /* You need to add PutLhsVar here because WITHOUT_ADD_PUTLHSVAR 
  was defined and equal to %t */
  /* without this, you do not need to add PutLhsVar here */
  ReturnArguments(pvApiCtx);
  
  return 0;
}
int read_string(char *fname,unsigned long fname_len)
{
	SciErr sciErr;
	int i,j;
	int iLen		= 0;
	//variable info
	int iRows		= 0;
	int iCols		= 0;
	int* piAddr		= NULL;
	int* piLen		= NULL;
	char** pstData	= NULL;
	//output variable
	int iRowsOut	= 1;
	int iColsOut	= 1;
	char* pstOut	= NULL;
	//check input and output arguments

    CheckInputArgument(pvApiCtx, 1, 1);
    CheckOutputArgument(pvApiCtx, 1, 1);

	//get variable address
	sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	//fisrt call to retrieve dimensions
	sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	piLen = (int*)malloc(sizeof(int) * iRows * iCols);

	//second call to retrieve length of each string
	sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	pstData = (char**)malloc(sizeof(char*) * iRows * iCols);
	for(i = 0 ; i < iRows * iCols ; i++)
	{
		pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
	}

	//third call to retrieve data
	sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	//computer length of all strings
	for(i = 0 ; i < iRows * iCols ; i++)
	{
		iLen += piLen[i];
	}

	//alloc output variable
	pstOut = (char*)malloc(sizeof(char) * (iLen + iRows * iCols));
	//initialize string to 0x00
	memset(pstOut, 0x00, sizeof(char) * (iLen + iRows * iCols));

	//concat input strings in output string
	for(i = 0 ; i < iRows ; i++)
	{
		for(j = 0 ; j < iCols ; j++)
		{
			int iCurLen = strlen(pstOut);
			if(iCurLen)
			{
				strcat(pstOut, " ");
			}
			strcpy(pstOut + strlen(pstOut), pstData[j * iRows + i]);
		}
	}

	//create new variable
	sciErr = createMatrixOfString(pvApiCtx, InputArgument + 1, iRowsOut, iColsOut, &pstOut);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	//free memory
	free(piLen);

	for(i = 0 ; i < iRows * iCols ; i++)
	{
		free(pstData[i]);
	}

	free(pstData);
	free(pstOut);
	AssignOutputVariable(1) = InputArgument + 1;
	return 0;
}
Beispiel #13
0
int sci_nearfloat(char *fname, void* pvApiCtx)
{
    SciErr sciErr;
    int i;

    int* piAddr1				= NULL;
    int iRows1					= 0;
    int iCols1					= 0;
    int iType1					= 0;
    int* piLen					= NULL;
    char **pstData			= NULL;

    int* piAddr2				= NULL;
    int iRows2					= 0;
    int iCols2					= 0;
    int iType2					= 0;
    double *pdblReal		= NULL;

    double dblMode			= 0;

    double *pdblRealRet	= NULL;


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

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    sciErr = getVarType(pvApiCtx, piAddr1, &iType1);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    sciErr = getVarType(pvApiCtx, piAddr2, &iType2);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    if (iType1 != sci_strings)
    {
        //Err = 1;
        SciError(55);
        return 0;
    }

    if (iType2 != sci_matrix)
    {
        //Err = 2;
        SciError(53);
        return 0;
    }

    sciErr = getMatrixOfString(pvApiCtx, piAddr1, &iRows1, &iCols1, NULL, NULL);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    if (iRows1 != 1 || iCols1 != 1)
    {
        return 1;
    }

    piLen = (int*)malloc(sizeof(int) * iRows1 * iCols1);
    sciErr = getMatrixOfString(pvApiCtx, piAddr1, &iRows1, &iCols1, piLen, NULL);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        free(piLen);
        return 0;
    }

    pstData	= (char**)malloc(sizeof(char*) * iRows1 * iCols1);
    for (i = 0 ; i < iRows1 * iCols1 ; i++)
    {
        pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+1 for null termination
    }

    sciErr = getMatrixOfString(pvApiCtx, piAddr1, &iRows1, &iCols1, piLen, pstData);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        free(piLen);
        for (i = 0 ; i < iRows1 * iCols1 ; i++)
        {
            free(pstData[i]);
        }
        free(pstData);
        return 0;
    }

    sciErr = getMatrixOfDouble(pvApiCtx, piAddr2, &iRows2, &iCols2, &pdblReal);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        free(piLen);
        for (i = 0 ; i < iRows1 * iCols1 ; i++)
        {
            free(pstData[i]);
        }
        free(pstData);
        return 0;
    }

    if (strcmp(pstData[0], "succ") == 0)
    {
        dblMode = 1.0;
    }
    else if (strcmp(pstData[0], "pred") == 0)
    {
        dblMode = -1.0;
    }
    else
    {
        SciError(999);
        free(piLen);
        for (i = 0 ; i < iRows1 * iCols1 ; i++)
        {
            free(pstData[i]);
        }
        free(pstData);
        return 0;
    }

    sciErr = allocMatrixOfDouble(pvApiCtx, Rhs + 1, iRows2, iCols2, &pdblRealRet);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        free(piLen);
        for (i = 0 ; i < iRows1 * iCols1 ; i++)
        {
            free(pstData[i]);
        }
        free(pstData);
        return 0;
    }

    for (i = 0 ; i < iRows2 * iCols2 ; i++)
    {
        pdblRealRet[i] = dblNearFloat(pdblReal[i], dblMode);
    }

    free(piLen);
    for (i = 0 ; i < iRows1 * iCols1 ; i++)
    {
        free(pstData[i]);
    }
    free(pstData);

    LhsVar(1) = Rhs + 1;
    PutLhsVar();
    return 0;
}
Beispiel #14
0
matvar_t *GetCharVariable(int iVar, const char *name, int * parent, int item_position)
{
    char * dataAdr = NULL;
    int rank = 0, i = 0, j = 0;
    int *dims = NULL;
    matvar_t *createdVar = NULL;
    int* piLen = NULL;
    char** pstData = NULL;
    char* pstMatData = NULL;
    int * piAddr = NULL;
    int * item_addr = NULL;
    int var_type;
    int saveDim = 0; /* Used to save old dimension before restoring it */
    SciErr sciErr;

    if (parent==NULL)
    {
        sciErr = getVarAddressFromPosition(pvApiCtx, iVar, &piAddr);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            return NULL;
        }
        sciErr = getVarType(pvApiCtx, piAddr, &var_type);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            return NULL;
        }
    }
    else
    {
        sciErr = getListItemAddress(pvApiCtx, parent, item_position, &item_addr);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            return NULL;
        }
        sciErr = getVarType(pvApiCtx, item_addr, &var_type);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            return NULL;
        }
    }

    if(var_type == sci_strings) /* 2-D array */
    {
        rank = 2;
        if ((dims = (int*)MALLOC(sizeof(int) * rank)) == NULL)
        {
            Scierror(999, _("%s: No more memory.\n"), "GetCharVariable");
            return NULL;
        }

        if (parent==NULL)
        {
            // First call to retrieve dimensions
            sciErr = getMatrixOfString(pvApiCtx, piAddr, &dims[0], &dims[1], NULL, NULL);
            if(sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 0;
            }
            piLen = (int *)MALLOC(dims[0] * dims[1] * sizeof(int));
            // Second call to retrieve length of each string
            sciErr = getMatrixOfString(pvApiCtx, piAddr, &dims[0], &dims[1], piLen, NULL);
            if(sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 0;
            }
            pstData = (char**)MALLOC(sizeof(char*) * dims[0] * dims[1]);
            for(i = 0 ; i < dims[0] * dims[1] ; i++)
            {
                pstData[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1)); //+ 1 for null termination
            }
            // Third call to retrieve data
            sciErr = getMatrixOfString(pvApiCtx, piAddr, &dims[0], &dims[1], piLen, pstData);
            if(sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 0;
            }
            dataAdr = strdup(pstData[0]);
        }
        else
        {
            // First call to retrieve dimensions
            sciErr  = getMatrixOfStringInList(pvApiCtx, parent, item_position, &dims[0], &dims[1], NULL, NULL);
            if(sciErr.iErr)
            {
                printError(&sciErr, 0);
                return NULL;
            }
            piLen = (int *)MALLOC(dims[0] * dims[1] * sizeof(int));
            // Second call to retrieve length of each string
            sciErr = getMatrixOfStringInList(pvApiCtx, parent, item_position, &dims[0], &dims[1], piLen, NULL);
            if(sciErr.iErr)
            {
                printError(&sciErr, 0);
                return NULL;
            }
            pstData = (char**)MALLOC(sizeof(char*) * dims[0] * dims[1]);
            for(i = 0 ; i < dims[0] * dims[1] ; i++)
            {
                pstData[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1)); //+ 1 for null termination
            }
            // Third call to retrieve data
            sciErr = getMatrixOfStringInList(pvApiCtx, parent, item_position, &dims[0], &dims[1], piLen, pstData);
            if(sciErr.iErr)
            {
                printError(&sciErr, 0);
                return NULL;
            }
            dataAdr = strdup(pstData[0]);
        }

        if (dims[0] == 0) /* Empty character string */
        {
            createdVar = Mat_VarCreate(name, MAT_C_CHAR, MAT_T_UINT8, rank, dims, pstData[0], 0);
        }
        else if (dims[0]*dims[1] == 1) /* Scalar character string */
        {
            saveDim = dims[1];
            dims[1] = piLen[0];
            createdVar = Mat_VarCreate(name, MAT_C_CHAR, MAT_T_UINT8, rank, dims, pstData[0], 0);
            dims[1] = saveDim;
        }
        else /* More than one character string -> save as a Cell */
        {
            if (dims[0] == 1)
            {
                /* TODO: Should be saved as a cell */
                Scierror(999, _("%s: Row array of strings saving is not implemented.\n"), "GetCharVariable");
                freeArrayOfString(pstData, dims[0]*dims[1]);
                FREE(dims);
                FREE(dataAdr);
                FREE(piLen);
                return NULL;
            }
            else if (dims[1] == 1)
            {
                /* Check that all strings have the same length */
                for (i = 0 ; i < dims[0] ; i++)
                {
                    if (piLen[0] != piLen[i])
                    {
                        /* TODO: Should be saved as a cell */
                        Scierror(999, _("%s: Column array of strings with different lengths saving is not implemented.\n"), "GetCharVariable");
                        freeArrayOfString(pstData, dims[0]*dims[1]);
                        FREE(dims);
                        FREE(dataAdr);
                        FREE(piLen);
                        return NULL;
                    }
                }

                /* Reorder characters */
                pstMatData = (char*)MALLOC(sizeof(char) * dims[0] * piLen[0]);
                for (i = 0 ; i < dims[0] ; i++)
                {
                    for (j = 0 ; j < piLen[0] ; j++)
                    {
                        pstMatData[i+j*dims[0]] = pstData[i][j];
                    }
                }

                /* Save the variable */
                saveDim = dims[1];
                dims[1] = piLen[0];
                createdVar = Mat_VarCreate(name, MAT_C_CHAR, MAT_T_UINT8, rank, dims, pstMatData, 0);
                dims[1] = saveDim;

                freeArrayOfString(pstData, dims[0]*dims[1]); /* FREE now because dimensions are changed just below */
                FREE(pstMatData);
                FREE(dims);
                FREE(dataAdr);
                FREE(piLen);
            }
            else
            {
                /* TODO: Should be saved as a cell */
                Scierror(999, _("%s: 2D array of strings saving is not implemented.\n"), "GetCharVariable");
                freeArrayOfString(pstData, dims[0]*dims[1]);
                FREE(dims);
                FREE(dataAdr);
                FREE(piLen);
                return NULL;
            }
        }
    }
    else
    {
        Scierror(999, _("%s: Wrong type for first input argument: String matrix expected.\n"), "GetCharVariable");
        freeArrayOfString(pstData, dims[0]*dims[1]);
        FREE(dims);
        FREE(dataAdr);
        FREE(piLen);
        return NULL;
    }

    return createdVar;
}
Beispiel #15
0
/*--------------------------------------------------------------------------*/
int sci_javaclasspath(char *fname, unsigned long fname_len)
{
    int *piAddressVarOne = NULL;
    int iType = 0;
    SciErr sciErr;

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

    if (Rhs == 0)
    {
        int nbRow = 0;
        int nbCol = 1;
        char **Strings = NULL;

        Strings = getClasspath(&nbRow);
        createMatrixOfString(pvApiCtx, Rhs + 1, nbRow, nbCol, Strings);

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
        freeArrayOfString(Strings, nbRow * nbCol);
    }
    else
    {
        sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
            return 0;
        }

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

        if ( iType == sci_strings )
        {
            char **pStVarOne = NULL;
            int *lenStVarOne = NULL;
            static int n1 = 0, m1 = 0;
            int i = 0;

            /* get dimensions */
            sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
                return 0;
            }

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

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

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

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

            for (i = 0; i < m1 * n1 ; i++)
            {
                if (!addToClasspath(pStVarOne[i], STARTUP))
                {
                    Scierror(999, _("%s: Could not add URL to system classloader : %s.\n"), fname, pStVarOne[i]);
                    freeArrayOfString(pStVarOne, m1 * n1);
                    return 0;
                }
            }
            LhsVar(1) = 0;
            PutLhsVar();
            freeArrayOfString(pStVarOne, m1 * n1);
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1);
        }
    }

    return 0;
}
Beispiel #16
0
/*--------------------------------------------------------------------------*/
int sci_dos(char *fname, void* pvApiCtx)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int iType1	= 0;
    wchar_t *pStVarOne = NULL;

    char **Output = NULL;
    int numberoflines = 0;
    BOOL ECHOMODE = FALSE;

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

    if (Rhs == 2)
    {
        int *piAddressVarTwo = NULL;
        int m2 = 0, n2 = 0;
        int iType2 = 0;
        char *pStVarTwo = NULL;
        int lenStVarTwo = 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;
        }

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

        if (iType2  != sci_strings )
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
            return 0;
        }

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

        if ( (m2 != n2) && (n2 != 1) )
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname, 2);
            return 0;
        }

        pStVarTwo = (char*)MALLOC(sizeof(char) * (lenStVarTwo + 1));
        if (pStVarTwo)
        {
            sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
                return 0;
            }

            if ( strcmp(pStVarTwo, "-echo") )
            {
                FREE(pStVarTwo);
                pStVarTwo = NULL;
                Scierror(999, _("%s: Wrong value for input argument #%d: '%s' expected.\n"), fname, 2, "-echo");
                return 0;
            }
            else
            {
                ECHOMODE = TRUE;
            }
        }
        else
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }
    }

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

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

    if (iType1  != sci_strings )
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
        return 0;
    }

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

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

    double exitCode = 0.;
    BOOL DetachProcessOption = FALSE;
    BOOL *StatusExit = NULL;

    DetachProcessOption = DetectDetachProcessInCommandLine(pStVarOne);
    exitCode = (double)spawncommand(pStVarOne, DetachProcessOption);
    freeAllocatedSingleWideString(pStVarOne);

    StatusExit = (BOOL*)MALLOC(sizeof(BOOL));

    if (DetachProcessOption)
    {
        if (strlen((const char *)(pipeSpawnErr.OutputBuffer)))
        {
            /* StdErr will be "Output" */
            *StatusExit = FALSE;
            Output = CreateOuput(&pipeSpawnErr, DetachProcessOption);
            numberoflines = pipeSpawnErr.NumberOfLines;
        }
        else
        {
            /* StdOut will be "Output" */
            *StatusExit = TRUE;
            Output = CreateOuput(&pipeSpawnOut, DetachProcessOption);
            numberoflines = pipeSpawnOut.NumberOfLines;
        }
    }
    else
    {
        char FileTMPDir[PATH_MAX + 16];
        BOOL bConvert = FALSE;

        char *TMPDirLong = getTMPDIR();
        char *TMPDirShort = getshortpathname(TMPDirLong, &bConvert);

        sprintf(FileTMPDir, "%s\\DOS.OK", TMPDirLong);
        FREE(TMPDirLong);
        TMPDirLong = NULL;
        FREE(TMPDirShort);
        TMPDirShort = NULL;

        if (FileExist(FileTMPDir))
        {
            DeleteFile(FileTMPDir);
            /* StdOut will be "Output" */
            *StatusExit = TRUE;
            Output = CreateOuput(&pipeSpawnOut, DetachProcessOption);
            numberoflines = pipeSpawnOut.NumberOfLines;
        }
        else
        {
            /* StdErr will be "Output" */
            *StatusExit = FALSE;
            Output = CreateOuput(&pipeSpawnErr, DetachProcessOption);
            numberoflines = pipeSpawnErr.NumberOfLines;
        }
    }

    if (ECHOMODE)
    {
        PrintOuput(Output, numberoflines);
    }

    if (Lhs == 1)
    {
        int m_out = 1, n_out = 1;
        sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m_out, n_out, StatusExit);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        LhsVar(1) = Rhs + 1;
    }
    else
    {
        int m_out2 = 1;
        int n_out2 = 1;

        if (Output && Output[0])
        {
            int m_out1 = numberoflines;
            int n_out1 = 1;
            sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m_out1, n_out1, Output);
        }
        else
        {
            /* returns [] */
            int m_out1 = 0;
            int n_out1 = 0;
            sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out1, n_out1, NULL);
        }

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

        LhsVar(1) = Rhs + 1;

        sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 2, m_out2, n_out2, StatusExit);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        LhsVar(2) = Rhs + 2;
    }

    if (Lhs > 2)
    {
        int m_out3 = 1, n_out3 = 1;
        sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 3, m_out3, n_out3, &exitCode);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        LhsVar(3) = Rhs + 3;
    }
    if (StatusExit)
    {
        FREE(StatusExit);
        StatusExit = NULL;
    }

    freeArrayOfString(Output, numberoflines);

    ClosePipeInfo(pipeSpawnOut);
    ClosePipeInfo(pipeSpawnErr);

    PutLhsVar();

    return 0;
}
Beispiel #17
0
/*--------------------------------------------------------------------------*/
int C2F(sci_execstr)(char *fname,unsigned long fname_len)
{
	if ( isRecursionCallToFunction() )
	{
		C2F(intexecstr)(fname, fname_len);
	}
	else
	{
		SciErr sciErr;

		int *piAddressVarOne = NULL;
		int iType1 = 0;

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

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

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

		/* execstr([])*/
		if (iType1 == sci_matrix)
		{
			int m1 = 0, n1 = 0;

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

			if ((m1 == n1) && (m1 == 0)) /* [] */
			{
				sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 0, 0, NULL);
				if(sciErr.iErr)
				{
					printError(&sciErr, 0);
                    Scierror(999,_("%s: Memory allocation error.\n"), fname);
					return 0;
				}

				LhsVar(1) = Rhs + 1;

				PutLhsVar();
				return 0;
			}
			else
			{
				Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
				return 0;
			}
		}

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

		if (Rhs > 1)
		{
			int m2 = 0, n2 = 0;
			int *piAddressVarTwo = NULL;
			char *pStVarTwo = NULL;
			int lenStVarTwo = 0;
			int iType2 = 0;

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

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

			if (iType2 != sci_strings)
			{
				Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
				return 0;
			}

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

			if (m2 * n2 != 1)
			{
				Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,2);
				return 0;
			}

			pStVarTwo = (char*)MALLOC(sizeof(char)*(lenStVarTwo + 1));
			if (pStVarTwo)
			{
				sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo,&m2,&n2,&lenStVarTwo,&pStVarTwo);
				if(sciErr.iErr)
				{
					printError(&sciErr, 0);
                    Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
					return 0;
				}

				if (strcmp(pStVarTwo,ERRCATCH_KEYWORD))
				{
					Scierror(999,_("%s: Wrong value for input argument #%d: 'errcatch' expected.\n"),fname,2);
					FREE(pStVarTwo);
					pStVarTwo = NULL;
					return 0;
				}

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

		if (Rhs > 2)
		{
			int m3 = 0, n3 = 0;
			int *piAddressVarThree = NULL;
			char *pStVarThree = NULL;
			int lenStVarThree = 0;
			int iType3 = 0;

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

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

			if (iType3 != sci_strings)
			{
				Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,3);
				return 0;
			}

			sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree,&m3,&n3,&lenStVarThree, NULL);
			if(sciErr.iErr)
			{
				printError(&sciErr, 0);
                Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
				return 0;
			}

			if (m3 * n3 != 1)
			{
				Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,3);
				return 0;
			}

			pStVarThree = (char*)MALLOC(sizeof(char)*(lenStVarThree + 1));
			if (pStVarThree)
			{
				sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree,&m3,&n3,&lenStVarThree,&pStVarThree);
				if(sciErr.iErr)
				{
					printError(&sciErr, 0);
                    Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
					return 0;
				}

				if ( strcmp(pStVarThree, MESSAGE_KEYWORD) && strcmp(pStVarThree, MESSAGE_DEFAULT_KEYWORD) )
				{
					Scierror(999,_("%s: Wrong value for input argument #%d: 'm' or 'n' expected.\n"),fname,3);
					FREE(pStVarThree);
					pStVarThree = NULL;
					return 0;
				}

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

		C2F(intexecstr)(fname, fname_len);
	}
	return 0;
}
extern "C" int sci_sdpa_read_prob(char * fname)
{
  int nb_vars, nb_constr, i, j, k, Index;
  struct blockmatrix pC;
  double * pa = NULL;
  struct constraintmatrix * pconstraints = NULL;
  struct sparseblock * tmp_block = NULL;
  int printlevel = 0, ret = 0, res = 0;
  char ** filename = NULL;
  int * filename_address = NULL, filename_rows, filename_cols, * filename_length = NULL;
  int * printlevel_address = NULL, printlevel_rows, printlevel_cols;
  double * printlevel_value = NULL, * c_out = NULL, * b_out = NULL;
  int * c_out_address = NULL, * a_out_address = NULL;
  int * constraint_address = NULL;
  int constraint_block_rows, constraint_block_cols, constraint_nb_item;
  double * block_value = NULL;
  int minrhs = 1, maxrhs = 2;
  int minlhs = 4, maxlhs = 4;
  SciErr sciErr;

  CheckRhs(minrhs,maxrhs);
  CheckLhs(minlhs,maxlhs);

  ///////////////////////
  // Read the filename //
  ///////////////////////

  sciErr = getVarAddressFromPosition(pvApiCtx,FILENAME_IN,&filename_address); CSDP_ERROR;
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, NULL, NULL); CSDP_ERROR;
  if (filename_rows*filename_cols!=1)
    {
      Scierror(999,"%s: an unique string is requested for the filename\n",fname);
      return 0;
    }
  filename_length = (int *)MALLOC(filename_rows*filename_cols*sizeof(int));
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, filename_length, NULL); CSDP_ERROR;
  filename = (char **)MALLOC(filename_rows*filename_cols*sizeof(char *));
  for(i=0;i<filename_rows*filename_cols;i++)
    {
      filename[i] = (char *)MALLOC((filename_length[i]+1)*sizeof(char));
    }
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, filename_length, filename); CSDP_ERROR;

  /////////////////////
  // Read printlevel //
  /////////////////////

  if (Rhs==2)
    {
      sciErr = getVarAddressFromPosition(pvApiCtx,PRINTLEVEL_IN, &printlevel_address); CSDP_ERROR;
      sciErr = getMatrixOfDouble(pvApiCtx,printlevel_address, &printlevel_rows, &printlevel_cols, &printlevel_value); CSDP_ERROR;
      printlevel = (int)*printlevel_value;
    }
  else
    {
      printlevel = 0;
    }

  ///////////////////////////
  // Read the sdpa problem //
  ///////////////////////////

  ret =  read_prob(filename[0], &nb_vars, &nb_constr, &pC, &pa, &pconstraints, printlevel);

#ifdef DEBUG
  printf("DEBUG: read file %s, return code = %d nb_vars = %d nb_constr = %d\n", filename[0], ret, nb_vars, nb_constr);
#endif

  ///////////////
  // Process C //
  ///////////////

  sciErr = createList(pvApiCtx,C_OUT,pC.nblocks,&c_out_address); CSDP_ERROR;

#ifdef DEBUG
  printf("DEBUG: process c: %d blocks\n", pC.nblocks);
#endif

  for(i=0;i<pC.nblocks;i++)
    {
#ifdef DEBUG
      printf("DEBUG: processing block %d: blockssize = %d blockcat = %d\n", i+1, pC.blocks[i+1].blocksize, pC.blocks[i+1].blockcategory);
#endif
      if (pC.blocks[i+1].blockcategory==MATRIX)
	{
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, C_OUT, c_out_address, i+1, pC.blocks[i+1].blocksize, pC.blocks[i+1].blocksize, &c_out); CSDP_ERROR;
	  
	  for(j=0;j<pC.blocks[i+1].blocksize;j++)
	    {
	      for(k=0;k<pC.blocks[i+1].blocksize;k++)
		{
		  c_out[j + k*pC.blocks[i+1].blocksize] = pC.blocks[i+1].data.mat[ijtok(j+1,k+1,pC.blocks[i+1].blocksize)];
#ifdef DEBUG
		  printf("DEBUG: MATRIX - c_out_m[%d][%d] = %f - Index = %d\n",j,k,c_out[j + k*pC.blocks[i+1].blocksize], ijtok(j+1,k+1,pC.blocks[i+1].blocksize));
#endif
		}
	    }
	}
      else if (pC.blocks[i+1].blockcategory==DIAG)
	{
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, C_OUT, c_out_address, i+1, pC.blocks[i+1].blocksize, 1, &c_out); CSDP_ERROR;
	  
	  for(j=0;j<pC.blocks[i+1].blocksize;j++)
	    {
	      c_out[j] = pC.blocks[i+1].data.vec[j+1];
#ifdef DEBUG
	      printf("DEBUG: DIAG - c_out_d[%d] = %f\n",j,c_out[j]);
#endif
	    }	  
	}
      else
	{
	  Scierror(999,"%s: wrong blockcat type PACKEDMATRIX\n",fname);
	  return 0;
	}
    }

  ///////////////
  // Process A //
  ///////////////

#ifdef DEBUG
  printf("DEBUG: process a - nb_constr = %d\n",nb_constr);
#endif
  sciErr = createList(pvApiCtx,A_OUT,nb_constr,&a_out_address); CSDP_ERROR;

  for(i=0;i<nb_constr;i++)
    {
#ifdef DEBUG
      printf("DEBUG: processing constraint %d\n", i+1);
#endif
      // constraint_nb_item: nb blocks for constraint i+1
      Index = 0;
      tmp_block = pconstraints[i+1].blocks;
      while (tmp_block)
	{
	  tmp_block = tmp_block->next;
	  Index++;
	}
      constraint_nb_item = Index;

      sciErr = createListInList(pvApiCtx,A_OUT, a_out_address, i+1, constraint_nb_item, &constraint_address); CSDP_ERROR;

      Index = 0;
      tmp_block = pconstraints[i+1].blocks;
      for(j=0;j<constraint_nb_item;j++)
	{
#ifdef DEBUG
	  printf("DEBUG: processing block %d: nb_block = %d blocksize = %d numentries %d\n", j+1, constraint_nb_item, tmp_block->blocksize, tmp_block->numentries);
	  printf("DEBUG:                      blocknum = %d constraintnum = %d issparse = %d\n", tmp_block->blocknum, tmp_block->constraintnum, tmp_block->issparse);
#endif
	  constraint_block_rows = tmp_block->blocksize;
	  constraint_block_cols = tmp_block->blocksize;
#ifdef DEBUG
	  int ii;
	  for(ii=0;ii<tmp_block->numentries;ii++)
	    {
	      printf("entries[%d] = %f\n",ii+1,tmp_block->entries[ii+1]);
	    }
#endif
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, A_OUT, constraint_address, j+1, constraint_block_rows, constraint_block_cols, &block_value); CSDP_ERROR;
	  if (res)
	    {
	      Scierror(999,"%s: error while allocating a matrix A on the stack\n", fname);
	      return 0;
	    }
	  for(k=0;k<constraint_block_rows*constraint_block_cols;k++) block_value[k] = 0.0;

	  for(k=0;k<tmp_block->numentries;k++)
	    {
	      block_value[(tmp_block->iindices[k+1]-1) + (tmp_block->jindices[k+1]-1)*tmp_block->blocksize] = tmp_block->entries[k+1];
#ifdef DEBUG
	      printf("a_out[%d][%d] = %f\n",tmp_block->iindices[k+1],tmp_block->jindices[k+1],
			block_value[(tmp_block->iindices[k+1]-1)+(tmp_block->jindices[k+1]-1)*tmp_block->blocksize]);
#endif
	    }
	  tmp_block = tmp_block->next;
	}
    }

  ///////////////
  // Process B //
  ///////////////

#ifdef DEBUG
  printf("DEBUG: process B\n");
#endif
  sciErr = allocMatrixOfDouble(pvApiCtx, B_OUT, nb_constr, 1, &b_out); CSDP_ERROR;
  if (res)
    {
      Scierror(999,"%s: error while allocating a vector on the stack\n", fname);
      return 0;
    }
  for(i=0;i<nb_constr;i++) 
    {
      b_out[i] = pa[i+1];
#ifdef DEBUG
      printf("a[%d] = %f\n", i, b_out[i]);
#endif
    }

  ////////////////////
  // Process status //
  ////////////////////

#ifdef DEBUG
  printf("DEBUG: process status\n");
#endif
  sciErr = allocMatrixOfDouble(pvApiCtx, STATUS_OUT, 1, 1, &b_out); CSDP_ERROR;
  *b_out = (double)ret;
#ifdef DEBUG
  printf("DEBUG: status = %d,%d\n",*b_out,ret);
#endif

  LhsVar(1) = C_OUT;
  LhsVar(2) = A_OUT;
  LhsVar(3) = B_OUT;
  LhsVar(4) = STATUS_OUT;

  return 0;
}
Beispiel #19
0
/*--------------------------------------------------------------------------*/
SciErr getProcessMode(void *_pvCtx, int _iPos, int *_piAddRef, int *_piMode)
{
    SciErr sciErr;
    sciErr.iErr = 0;
    sciErr.iMsgCount = 0;
    int iRows1 = 0;
    int iCols1 = 0;
    int iRows2 = 0;
    int iCols2 = 0;
    int iType2 = 0;
    int iMode = 0;
    int *piAddr2 = NULL;

    sciErr = getVarDimension(_pvCtx, _piAddRef, &iRows1, &iCols1);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument dimension"), "getProcessMode");
        return sciErr;
    }

    //sciprint("getProcessMode ->");
    sciErr = getinternalVarAddress(_pvCtx, _iPos, &piAddr2);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get variable address"), "getProcessMode");
        return sciErr;
    }

    sciErr = getVarType(_pvCtx, piAddr2, &iType2);
    if (sciErr.iErr)
    {
        addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument type"), "getProcessMode");
        return sciErr;
    }

    if (iType2 == sci_matrix && !isVarComplex(_pvCtx, piAddr2))
    {
        double *pdblReal2 = NULL;

        sciErr = getMatrixOfDouble(_pvCtx, piAddr2, &iRows2, &iCols2, &pdblReal2);
        if (sciErr.iErr)
        {
            addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument data"), "getProcessMode");
            return sciErr;
        }

        if (iRows2 != 1 || iCols2 != 1)
        {
            addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Wrong size for argument %d: (%d,%d) expected.\n"), "getProcessMode", _iPos, 1,
                            1);
            return sciErr;
        }

        iMode = (int)pdblReal2[0];
    }
    else if (iType2 == sci_strings)
    {
        int iLen = 0;
        char *pstMode[1] = { "" };

        sciErr = getVarDimension(_pvCtx, piAddr2, &iRows2, &iCols2);
        if (sciErr.iErr)
        {
            addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument dimension"), "getProcessMode");
            return sciErr;
        }

        if (iRows2 != 1 || iCols2 != 1)
        {
            addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Wrong size for argument %d: (%d,%d) expected.\n"), "getProcessMode", _iPos, 1,
                            1);
            return sciErr;
        }

        sciErr = getMatrixOfString(_pvCtx, piAddr2, &iRows2, &iCols2, &iLen, NULL);
        if (sciErr.iErr)
        {
            addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument data"), "getProcessMode");
            return sciErr;
        }

        pstMode[0] = (char *)MALLOC(sizeof(char) * (iLen + 1)); //+1 for null termination
        sciErr = getMatrixOfString(_pvCtx, piAddr2, &iRows2, &iCols2, &iLen, pstMode);
        if (sciErr.iErr)
        {
            addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Unable to get argument data"), "getProcessMode");
            return sciErr;
        }

        iMode = (int)pstMode[0][0];
        FREE(pstMode[0]);
    }
    else
    {
        addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Wrong type for input argument #%d: A string or a scalar expected.\n"),
                        "getProcessMode", _iPos);
        return sciErr;
    }

    if (iMode == ROW_LETTER || iMode == BY_ROWS)
    {
        *_piMode = BY_ROWS;
    }
    else if (iMode == COL_LETTER || iMode == BY_COLS)
    {
        *_piMode = BY_COLS;
    }
    else if (iMode == STAR_LETTER || iMode == BY_ALL)
    {
        *_piMode = BY_ALL;
    }
    else if (iMode == MTLB_LETTER || iMode == BY_MTLB)
    {
        *_piMode = 0;
        if (iRows1 > 1)
        {
            *_piMode = 1;
        }
        else if (iCols1 > 1)
        {
            *_piMode = 2;
        }
    }
    else
    {
        addErrorMessage(&sciErr, API_ERROR_GET_PROCESSMODE, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), "getProcessMode",
                        _iPos, "'*', 'r', 'c', 'm', '0', '1', '2'", "-1");
        return sciErr;
    }
    return sciErr;
}
Beispiel #20
0
/*--------------------------------------------------------------------------*/
int sci_fprintfMat(char *fname,unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int m1 = 0, n1 = 0;
    int iType1 = 0;

    int *piAddressVarTwo = NULL;
    int m2 = 0, n2 = 0;
    int iType2 = 0;

    fprintfMatError ierr = FPRINTFMAT_ERROR;

    char *filename = NULL;
    char *expandedFilename = NULL;
    char **textAdded = NULL;
    char *Format = NULL;
    double *dValues = NULL;
    char *separator = NULL;
    int m4n4 = 0;
    int i = 0;

    Nbvars = 0;
    CheckRhs(1,5);
    CheckLhs(1,1);

    if (Rhs >= 3)
    {
        int *piAddressVarThree = NULL;
        int iType3	= 0;
        int m3 = 0, n3 = 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;
        }

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

        if (iType3 != sci_strings)
        {
            Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3);
            return 0;
        }

        sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);

        if ( (m3 != n3) && (n3 != 1) ) 
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &Format))
        {
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    else
    {
        Format = strdup(DEFAULT_FPRINTFMAT_FORMAT);
    }

    if ( Rhs >= 4 )
    {
        int *piAddressVarFour = NULL;
        int *lengthStrings = NULL;
        int iType4	= 0;
        int m4 = 0, n4 = 0;

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

        sciErr = getVarType(pvApiCtx, piAddressVarFour, &iType4);
        if(sciErr.iErr)
        {
            if (Format) {FREE(Format); Format = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        if (iType4 != sci_strings)
        {
            if (Format) {FREE(Format); Format = NULL;}
            Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 4);
            return 0;
        }

        sciErr = getVarDimension(pvApiCtx, piAddressVarFour, &m4, &n4);
        if(sciErr.iErr)
        {
            if (Format) {FREE(Format); Format = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        if  (! ((m4 == 1) || (n4 == 1)))
        {
            if (Format) {FREE(Format); Format = NULL;}
            Scierror(999,_("%s: Wrong size for input argument #%d.\n"), fname, 4);
            return 0;
        }

        lengthStrings = (int*)MALLOC(sizeof(int) * (m4 * n4));
        if (lengthStrings == NULL)
        {
            if (Format) {FREE(Format); Format = NULL;}
            Scierror(999,_("%s: Memory allocation error.\n"),fname);
            return 0;
        }

        // get lengthStrings value
        sciErr = getMatrixOfString(pvApiCtx, piAddressVarFour, &m4, &n4, lengthStrings, NULL);
        if(sciErr.iErr)
        {
            if (Format) {FREE(Format); Format = NULL;}
            if (lengthStrings) {FREE(lengthStrings); lengthStrings = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        textAdded = (char**)MALLOC(sizeof(char*) * (m4 * n4));
        if (textAdded == NULL)
        {
            if (Format) {FREE(Format); Format = NULL;}
            if (lengthStrings) {FREE(lengthStrings); lengthStrings = NULL;}
            Scierror(999,_("%s: Memory allocation error.\n"),fname);
            return 0;
        }

        for (i = 0; i < (m4 * n4); i++)
        {
            textAdded[i] = (char*)MALLOC(sizeof(char) * (lengthStrings[i] + 1));
            if (textAdded[i] == NULL)
            {
                freeArrayOfString(textAdded, m4 * n4);
                if (Format) {FREE(Format); Format = NULL;}
                if (lengthStrings) {FREE(lengthStrings); lengthStrings = NULL;}
                Scierror(999,_("%s: Memory allocation error.\n"),fname);
                return 0;
            }
        }

        // get textAdded
        sciErr = getMatrixOfString(pvApiCtx, piAddressVarFour, &m4, &n4, lengthStrings, textAdded);
        if (lengthStrings) {FREE(lengthStrings); lengthStrings = NULL;}
        if(sciErr.iErr)
        {
            freeArrayOfString(textAdded, m4 * n4);
            if (Format) {FREE(Format); Format = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        m4n4 = m4 * n4;
    }

    if (Rhs > 4)
    {
        int *piAddressVarFive = NULL;
        int iType5	= 0;
        int m5 = 0, n5 = 0;

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

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

        if (iType5 != sci_strings)
        {
            Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 5);
            return 0;
        }

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

        if ( (m5 != n5) && (n5 != 1) ) 
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 5);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarFive, &separator))
        {
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    else
    {
        separator = strdup(DEFAULT_FPRINTFMAT_SEPARATOR);
    }

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

    sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
    if(sciErr.iErr)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
        return 0;
    }

    if (iType2 != sci_matrix)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Wrong type for input argument #%d: Matrix of floating point numbers expected.\n"), fname, 2);
        return 0;
    }

    if (isVarComplex(pvApiCtx, piAddressVarTwo))
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Wrong type for input argument #%d: Real values expected.\n"), fname, 2);
        return 0;
    }

    sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &dValues);
    if(sciErr.iErr)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
        return 0;
    }

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
    if(sciErr.iErr)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
    if(sciErr.iErr)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    if (iType1 != sci_strings)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
        return 0;
    }

    sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);

    if ( (m1 != n1) && (n1 != 1) ) 
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &filename))
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    expandedFilename = expandPathVariable(filename);
    ierr = fprintfMat(expandedFilename, Format, separator, dValues, m2, n2,textAdded, m4n4);
    if (expandedFilename) {FREE(expandedFilename); expandedFilename = NULL;}
    if (textAdded) freeArrayOfString(textAdded, m4n4);
    if (Format) {FREE(Format); Format = NULL;}
    if (separator){FREE(separator); separator = NULL;}

    switch(ierr)
    {
    case FPRINTFMAT_NO_ERROR:
        {
            LhsVar(1) = 0;
            if (filename) {FREE(filename); filename = NULL;}
            PutLhsVar();
        }
        break;
    case FPRINTFMAT_FOPEN_ERROR:
        {
            Scierror(999,_("%s: can not open file %s.\n"), fname, filename);
        }
        break;
    case FPRINTMAT_FORMAT_ERROR:
        {
            Scierror(999,_("%s: Invalid format.\n"), fname);
        }
        break;
    default:
    case FPRINTFMAT_ERROR:
        {
            Scierror(999,_("%s: error.\n"), fname);
        }
        break;
    }

    if (filename) {FREE(filename); filename = NULL;}
    return 0;
}
Beispiel #21
0
/*--------------------------------------------------------------------------*/
int sci_fftw_flags(char *fname, unsigned long fname_len)
{
    /* declaration of variables to store scilab parameters address */
    static int m1 = 0, n1 = 0;

    char **Str1 = NULL;
    char **Str3 = NULL;

    unsigned int uiVar1 = 0;
    int* piDataOut = NULL;
    int* piAddr1 = NULL;
    int* piLen = NULL;
    int iType = 0;

    /* please update me ! */
    static int nb_flag = 22;
    static char *Str[] =
    {
        /* documented flags */
        "FFTW_MEASURE",
        "FFTW_DESTROY_INPUT",
        "FFTW_UNALIGNED",
        "FFTW_CONSERVE_MEMORY",
        "FFTW_EXHAUSTIVE",
        "FFTW_PRESERVE_INPUT",
        "FFTW_PATIENT",
        "FFTW_ESTIMATE",

        /* undocumented beyond-guru flags */
        "FFTW_ESTIMATE_PATIENT",
        "FFTW_BELIEVE_PCOST",
        "FFTW_NO_DFT_R2HC",
        "FFTW_NO_NONTHREADED",
        "FFTW_NO_BUFFERING",
        "FFTW_NO_INDIRECT_OP",
        "FFTW_ALLOW_LARGE_GENERIC",
        "FFTW_NO_RANK_SPLITS",
        "FFTW_NO_VRANK_SPLITS",
        "FFTW_NO_VRECURSE",
        "FFTW_NO_SIMD",
        "FFTW_NO_SLOW",
        "FFTW_NO_FIXED_RADIX_LARGE_N",
        "FFTW_ALLOW_PRUNING"
    };

    static unsigned flagt[] =
    {
        /* documented flags */
        FFTW_MEASURE,
        FFTW_DESTROY_INPUT,
        FFTW_UNALIGNED,
        FFTW_CONSERVE_MEMORY,
        FFTW_EXHAUSTIVE,
        FFTW_PRESERVE_INPUT,
        FFTW_PATIENT,
        FFTW_ESTIMATE,

        /* undocumented beyond-guru flags */
        FFTW_ESTIMATE_PATIENT,
        FFTW_BELIEVE_PCOST,
        FFTW_NO_DFT_R2HC,
        FFTW_NO_NONTHREADED,
        FFTW_NO_BUFFERING,
        FFTW_NO_INDIRECT_OP,
        FFTW_ALLOW_LARGE_GENERIC,
        FFTW_NO_RANK_SPLITS,
        FFTW_NO_VRANK_SPLITS,
        FFTW_NO_VRECURSE,
        FFTW_NO_SIMD,
        FFTW_NO_SLOW,
        FFTW_NO_FIXED_RADIX_LARGE_N,
        FFTW_ALLOW_PRUNING
    };

    unsigned flagv = 0;

    int i = 0, j = 0;

    SciErr sciErr;
    CheckInputArgument(pvApiCtx, 0, 1);

    if (nbInputArgument(pvApiCtx) == 0)
    {
        // nothing
    }
    else
    {
        //get variable address of the input argument
        sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 1;
        }

        getVarType(pvApiCtx, piAddr1, &iType);
        switch (iType)
        {
            case sci_ints:
            {
                /* int */
                int iPrecision = 0;
                int* pi32Data = NULL;
                unsigned int* pui32Data = NULL;

                getMatrixOfIntegerPrecision(pvApiCtx, piAddr1, &iPrecision);
                if (iPrecision != SCI_INT32 && iPrecision != SCI_UINT32)
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A int32 expected.\n"), fname, 1);
                    return 1;
                }

                if (iPrecision == SCI_INT32)
                {
                    sciErr = getMatrixOfInteger32(pvApiCtx, piAddr1, &m1, &n1, pi32Data);
                    uiVar1 = (unsigned int)pi32Data[0];
                }
                else
                {
                    sciErr = getMatrixOfUnsignedInteger32(pvApiCtx, piAddr1, &m1, &n1, pui32Data);
                    uiVar1 = pui32Data[0];
                }

                if (sciErr.iErr)
                {
                    Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
                    printError(&sciErr, 0);
                    return 1;
                }
                break;
            }
            case sci_matrix:
            {
                /* double */
                double* pdblData = NULL;
                sciErr = getMatrixOfDouble(pvApiCtx, piAddr1, &m1, &n1, &pdblData);
                if (sciErr.iErr)
                {
                    Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
                    printError(&sciErr, 0);
                    return 1;
                }

                uiVar1 = (unsigned int)pdblData[0];
                break;
            }
            case sci_strings:
            {
                /* string */
                //fisrt call to retrieve dimensions
                sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, NULL, NULL);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 1;
                }

                piLen = (int*)malloc(sizeof(int) * m1 * n1);

                //second call to retrieve length of each string
                sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, NULL);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 1;
                }

                Str1 = (char**)malloc(sizeof(char*) * m1 * n1);
                for (i = 0 ; i < m1 * n1 ; i++)
                {
                    Str1[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
                }

                //third call to retrieve data
                sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, Str1);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 1;
                }

                for (j = 0; j < m1 * n1; j++)
                {
                    for (i = 0; i < nb_flag; i++)
                    {
                        if (strcmp(Str1[j], Str[i]) == 0)
                        {
                            break;
                        }
                    }

                    if (i == nb_flag)
                    {
                        freeArrayOfString(Str1, m1 * n1);
                        Scierror(999, _("%s: Wrong values for input argument #%d: FFTW flag expected.\n"), fname, 1);
                        return 0;
                    }
                    else
                    {
                        if (i > 0)
                        {
                            flagv = ( flagv | (1U << (i - 1)) );
                        }
                    }
                }

                uiVar1 = (unsigned int)flagv;
                freeArrayOfString(Str1, m1 * n1);
                m1 = 1;
                n1 = 1;
                break;
            }
            default:
                Scierror(53, _("%s: Wrong type for input argument #%d.\n"), fname, 1);
                return 1;
        }

        CheckDims(1, m1, n1, 1, 1);
        setCurrentFftwFlags(uiVar1);
    }

    /* return value of Sci_Plan.flags in position 2 */
    sciErr = allocMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + 2, 1, 1, &piDataOut);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: No more memory.\n"), fname);
        return 1;
    }

    piDataOut[0] = (int) getCurrentFftwFlags();

    /*Test for only FFTW_MEASURE*/
    if (getCurrentFftwFlags() == 0)
    {
        j = 1;
        if ((Str3 = (char **)MALLOC(sizeof(char *))) == NULL)
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 1;
        }

        Str3[0] = strdup(Str[0]);
        if (Str3[0] == NULL)
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 1;
        }
    }
    else
    {
        j = 0;
        for (i = 1; i < nb_flag; i++)
        {
            if ((getCurrentFftwFlags() & flagt[i]) == flagt[i])
            {
                j++;
                if (Str3)
                {
                    Str3 = (char **)REALLOC(Str3, sizeof(char *) * j);
                }
                else
                {
                    Str3 = (char **)MALLOC(sizeof(char *) * j);
                }

                if ( Str3 == NULL)
                {
                    Scierror(999, _("%s: No more memory.\n"), fname);
                    return 1;
                }

                Str3[j - 1] = strdup(Str[i]);
                if (Str3[j - 1] == NULL)
                {
                    freeArrayOfString(Str3, j);
                    Scierror(999, _("%s: No more memory.\n"), fname);
                    return 1;
                }
            }
        }
    }

    /* Create the string matrix as return of the function */
    sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 3, j, 1, Str3);
    freeArrayOfString(Str3, j); // Data have been copied into Scilab memory
    if (sciErr.iErr)
    {
        freeArrayOfString(Str3, j); // Make sure everything is cleanup in case of error
        printError(&sciErr, 0);
        return 1;
    }

    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 2;
    AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 3;
    ReturnArguments(pvApiCtx);
    return 0;
}
Beispiel #22
0
/* Set fftw wisdom
*
* Scilab Syntax :
*   -->set_fftw_wisdom(txt);
*
* Input : a scilab string matrix
*
* Output : Nothing or an error messsage if
*          wisdom can't be read by fftw
*
*/
int sci_set_fftw_wisdom(char *fname, void* pvApiCtx)
{
    SciErr sciErr;
    char **Str1 = NULL;
    char *Str = NULL;
    int m1 = 0, n1 = 0;
    int len = 0, k = 0;
    int j = 0;
    int i = 0;
    int* piAddr1 = NULL;
    int* piLen = NULL;

    if (withMKL())
    {
        Scierror(999, _("%s: MKL fftw library does not implement wisdom functions yet.\n"), fname);
        return 0;
    }

    CheckInputArgument(pvApiCtx, 1, 1);

    //get variable address
    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

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

    //fisrt call to retrieve dimensions
    sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, NULL, NULL);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    piLen = (int*)MALLOC(sizeof(int) * m1 * n1);

    //second call to retrieve length of each string
    sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, NULL);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    Str1 = (char**)MALLOC(sizeof(char*) * m1 * n1);
    for (i = 0 ; i < m1 * n1 ; i++)
    {
        Str1[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
    }

    //third call to retrieve data
    sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, Str1);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    for (j = 0; j < m1 * n1; j++)
    {
        len += (int)strlen(Str1[j]) + 1;

        if (Str)
        {
            Str = (char *)REALLOC(Str, sizeof(char) * (len));
        }
        else
        {
            Str = (char *)MALLOC(sizeof(char) * (len));
        }

        if (Str == NULL)
        {
            freeArrayOfString(Str1, m1 * n1);
            Scierror(999, _("%s: Cannot allocate more memory.\n"), fname);
            return 1;
        }

        for (i = 0; i < (int)strlen(Str1[j]); i++)
        {
            Str[k + i] = Str1[j][i];
        }
        Str[k + strlen(Str1[j])] = '\n';
        k += (int)strlen(Str1[j]) + 1;
    }
    Str[k - 1] = '\0';

    freeArrayOfString(Str1, m1 * n1);

    if (!(call_fftw_import_wisdom_from_string(Str)))
    {
        FREE(Str);
        Str = NULL;
        Scierror(999, _("%s: Wrong value for input argument #%d: a valid wisdom expected.\n"), fname, 1);
        return 1;
    }
    FREE(Str);
    Str = NULL;

    AssignOutputVariable(pvApiCtx, 1) = 0;
    ReturnArguments(pvApiCtx);
    return 0;
}
int get_string_info(void* _pvCtx, int _iRhs, int* _piParent, int *_piAddr, int _iItemPos)
{
    SciErr sciErr;
    int i;
    int iRows       = 0;
    int iCols       = 0;
    int* piLen      = NULL;
    char **pstData  = NULL;

    if (_iItemPos == 0)
    {
        //Not in list
        sciErr = getMatrixOfString(_pvCtx, _piAddr, &iRows, &iCols, NULL, NULL);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }

        piLen = (int*)MALLOC(sizeof(int) * iRows * iCols);
        sciErr = getMatrixOfString(_pvCtx, _piAddr, &iRows, &iCols, piLen, NULL);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }

        pstData = (char**)MALLOC(sizeof(char*) * iRows * iCols);

        for (i = 0 ; i < iRows * iCols ; i++)
        {
            pstData[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
        }

        sciErr = getMatrixOfString(_pvCtx, _piAddr, &iRows, &iCols, piLen, pstData);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }
    }
    else
    {
        sciErr = getMatrixOfStringInList(_pvCtx, _piParent, _iItemPos, &iRows, &iCols, NULL, NULL);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }

        piLen = (int*)MALLOC(sizeof(int) * iRows * iCols);

        sciErr = getMatrixOfStringInList(_pvCtx, _piParent, _iItemPos, &iRows, &iCols, piLen, NULL);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }

        pstData = (char**)MALLOC(sizeof(char*) * iRows * iCols);

        for (i = 0 ; i < iRows * iCols ; i++)
        {
            pstData[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
        }

        sciErr = getMatrixOfStringInList(_pvCtx, _piParent, _iItemPos, &iRows, &iCols, piLen, pstData);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }
    }
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    for (i = 0 ; i < iRows * iCols ; i++)
    {
        FREE(pstData[i]);
    }

    FREE(pstData);
    FREE(piLen);

    insert_indent();
    sciprint("Strings (%d x %d)\n", iRows, iCols);
    return 0;;
}