Exemplo n.º 1
0
/*******************************************************************************
   Interface for MATIO function called Mat_Open
   Scilab function name : matfile_open
*******************************************************************************/
int sci_matfile_open(char *fname, unsigned long fname_len)
{
    int nbRow = 0, nbCol = 0;
    mat_t *matfile;
    int fileIndex = 0;
    char * filename  = NULL;
    char * optionStr = NULL;
    int option = 0, var_type;
    int * filename_addr = NULL, * option_addr = NULL, * version_addr = NULL;
    char * versionStr = NULL;
    int version = MAT_FT_MAT5; // By default, use MAtlab 5 files
    SciErr sciErr;

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

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

    if (var_type == sci_strings)
    {
        getAllocatedSingleString(pvApiCtx, filename_addr, &filename);
        sciErr = getVarDimension(pvApiCtx, filename_addr, &nbRow, &nbCol);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }

        if (nbCol != 1)
        {
            Scierror(999, _("%s: Wrong size for first input argument: A string expected.\n"), fname);
            freeAllocatedSingleString(filename);
            return FALSE;
        }
    }
    else
    {
        Scierror(999, _("%s: Wrong type for first input argument: A string expected.\n"), fname);
        freeAllocatedSingleString(filename);
        return FALSE;
    }

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

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

        if (var_type == sci_strings)
        {
            getAllocatedSingleString(pvApiCtx, option_addr, &optionStr);
            sciErr = getVarDimension(pvApiCtx, option_addr, &nbRow, &nbCol);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 0;
            }

            if (nbCol != 1)
            {
                Scierror(999, _("%s: Wrong size for second input argument: A string expected.\n"), fname);
                freeAllocatedSingleString(filename);
                freeAllocatedSingleString(optionStr);

                return FALSE;
            }

            if (strcmp(optionStr, "r") == 0)
            {
                option = MAT_ACC_RDONLY;
            }
            else if (strcmp(optionStr, "w") == 0)
            {
                option = MAT_ACC_RDWR;
            }
            else
            {
                Scierror(999, _("%s: Wrong value for second input argument: 'r' or 'w' expected.\n"), fname);
                freeAllocatedSingleString(filename);
                freeAllocatedSingleString(optionStr);

                return FALSE;
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for second input argument: A string expected.\n"), fname);
            freeAllocatedSingleString(filename);
            freeAllocatedSingleString(optionStr);

            return FALSE;
        }
    }
    else
    {
        /* Default option value */
        option = MAT_ACC_RDONLY;
    }

    if (Rhs >= 3)
    {
        sciErr = getVarAddressFromPosition(pvApiCtx, 3, &version_addr);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }

        sciErr = getVarType(pvApiCtx, version_addr, &var_type);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }
        printf("sci_strings %d %d\n", var_type, sci_strings);
        if (var_type == sci_strings)
        {
            getAllocatedSingleString(pvApiCtx, version_addr, &versionStr);
            sciErr = getVarDimension(pvApiCtx, version_addr, &nbRow, &nbCol);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 0;
            }
            if (nbCol != 1)
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
                freeAllocatedSingleString(filename);
                freeAllocatedSingleString(optionStr);
                freeAllocatedSingleString(versionStr);

                return FALSE;
            }

            if (strcmp(versionStr, "7.3") == 0)
            {
                version = MAT_FT_MAT73; // Matlab 7.3 file
            }
            else
            {
                version = MAT_FT_MAT5; // Default, Matlab 5 file
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3);
            return 0;
        }
    }

    if (option == MAT_ACC_RDWR) // Write, option = "w"
    {
        /* create a Matlab 5 or 7.3 file */
        matfile = Mat_CreateVer(filename, NULL, version);
    }
    else // Read, option = "r"
    {
        /* Try to open the file (as a Matlab 5 file) */
        matfile = Mat_Open(filename, option);
    }

    if (matfile == NULL) /* Opening failed */
    {
        /* Function returns -1 */
        fileIndex = -1;
    }

    if (matfile != NULL) /* Opening succeed */
    {
        /* Add the file to the manager */
        matfile_manager(MATFILEMANAGER_ADDFILE, &fileIndex, &matfile);
    }

    /* Return the index */
    createScalarDouble(pvApiCtx, Rhs + 1, (double)fileIndex);

    freeAllocatedSingleString(filename);
    freeAllocatedSingleString(optionStr);
    freeAllocatedSingleString(versionStr);

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

    return TRUE;
}
int sci_matfile_varreadnext(char *fname, void* pvApiCtx)
{
    mat_t *matfile = NULL;
    matvar_t *matvar = NULL;
    int fileIndex = 0;
    int returnedClass = 0, var_type;
    int * fd_addr = NULL;
    double tmp_dbl;
    SciErr sciErr;

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

    /* Input argument is the index of the file to read */

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

    if (var_type == sci_matrix)
    {
        getScalarDouble(pvApiCtx, fd_addr, &tmp_dbl);
        if (!isScalar(pvApiCtx, fd_addr))
        {
            Scierror(999, _("%s: Wrong size for first input argument: Single double expected.\n"), fname);
            return FALSE;
        }
        fileIndex = (int)tmp_dbl;
    }
    else
    {
        Scierror(999, _("%s: Wrong type for first input argument: Double expected.\n"), fname);
        return FALSE;
    }

    /* Gets the corresponding matfile */
    matfile_manager(MATFILEMANAGER_GETFILE, &fileIndex, &matfile);

    if (matfile == NULL)
    {
        Scierror(999, _("%s: Invalid file identifier.\n"), fname);
        return FALSE;
    }

    matvar = Mat_VarReadNext(matfile);
    if ((matvar == NULL) || (matvar->name == NULL))
    {
        /* Return empty name */
        createSingleString(pvApiCtx, Rhs + 1, "\0");
        LhsVar(1) = Rhs + 1;

        if (Lhs >= 2)
        {
            /* Return empty value */
            createEmptyMatrix(pvApiCtx, Rhs + 2);
            LhsVar(2) = Rhs + 2;
        }

        if (Lhs == 3)
        {
            /* Return error flag instead of variable class */
            createScalarDouble(pvApiCtx, Rhs + 3, NO_MORE_VARIABLES);
            LhsVar(3) = Rhs + 3;
        }

        PutLhsVar();

        return TRUE;
    }

    /* To be sure isComplex is 0 or 1 */
    matvar->isComplex =  matvar->isComplex != 0;

    /* Return the variable name */
    createSingleString(pvApiCtx, Rhs + 1, matvar->name);
    LhsVar(1) = Rhs + 1;

    returnedClass = matvar->class_type;

    if (Lhs >= 2)
    {
        /* Return the values */
        if (!CreateMatlabVariable(pvApiCtx, Rhs + 2, matvar, NULL, -1)) /* Could not Create Variable */
        {
            sciprint("Do not know how to read a variable of class %d.\n", matvar->class_type);
            returnedClass = UNKNOWN_VARIABLE_TYPE;
        }
        LhsVar(2) = Rhs + 2;
    }

    if (Lhs == 3)
    {
        /* Create class return value */
        createScalarDouble(pvApiCtx, Rhs + 3, returnedClass);
        LhsVar(3) = Rhs + 3;
    }

    Mat_VarFree(matvar);
    PutLhsVar();
    return TRUE;
}
Exemplo n.º 3
0
/*--------------------------------------------------------------------------*/
int sci_xget(char *fname, unsigned long fname_len)
{
    SciErr sciErr;

    int* piAddrl1 = NULL;
    char* l1 = NULL;
    int* piAddrl2 = NULL;
    double* l2 = NULL;
    char* l3 = NULL;

    int m1 = 0, m2 = 0, n2 = 0, i = 0;
    int one = 1;

    BOOL keyFound = FALSE;

    if (nbInputArgument(pvApiCtx) <= 0)
    {
        sci_demo(fname, fname_len);
        return 0;
    }

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

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

    // Retrieve a matrix of double at position 1.
    if (getAllocatedSingleString(pvApiCtx, piAddrl1, &l1))
    {
        Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1);
        return 1;
    }

    /* check if key is valid */
    for (i = 0; i < NUMSETFONC ; i++)
    {
        if (strcmp((l1), KeyTab_[i]) == 0)
        {
            keyFound = TRUE;
            break;
        }
    }

    if (!keyFound)
    {
        Scierror(999, _("%s: Unrecognized input argument: '%s'.\n"), fname, (l1));
        freeAllocatedSingleString(l1);
        return -1;
    }

    if (nbInputArgument(pvApiCtx) == 2)
    {
        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl2);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            freeAllocatedSingleString(l1);
            return 1;
        }

        // Retrieve a matrix of double at position 2.
        sciErr = getMatrixOfDouble(pvApiCtx, piAddrl2, &m2, &n2, &l2);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 2);
            freeAllocatedSingleString(l1);
            return 1;
        }

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

    }

    if (strcmp(l1, "fpf") == 0 || strcmp(l1, "auto clear") == 0)
    {
        int bufl;
        char buf[4096];
        /*     special case for global variables set */
        xgetg((l1), buf, &bufl, m1, bsiz);
        if (allocSingleString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, bufl * one, (const char **)&l3))
        {
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            freeAllocatedSingleString(l1);
            return 1;
        }

        strncpy((l3), buf, bufl);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "colormap") == 0)
    {
        int iObjUID = 0;
        // Force figure creation if none exists.
        getOrCreateDefaultSubwin();
        iObjUID = getCurrentFigure();

        get_color_map_property(pvApiCtx, iObjUID);

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "mark") == 0)
    {
        int iObjUID = getOrCreateDefaultSubwin();
        int iMarkStyle = 0;
        int* piMarkStyle = &iMarkStyle;
        int iMarkSize = 0;
        int* piMarkSize = &iMarkSize;
        double pdblResult[2];

        getGraphicObjectProperty(iObjUID, __GO_MARK_STYLE__, jni_int, (void**)&piMarkStyle);
        getGraphicObjectProperty(iObjUID, __GO_MARK_SIZE__, jni_int, (void**)&piMarkSize);
        pdblResult[0] = iMarkStyle;
        pdblResult[1] = iMarkSize;

        createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblResult);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "mark size") == 0)
    {
        int iObjUID = getOrCreateDefaultSubwin();
        get_mark_size_property(pvApiCtx, iObjUID);

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "line style") == 0)
    {
        get_line_style_property(pvApiCtx, getOrCreateDefaultSubwin());

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "clipping") == 0)
    {
        double *clipBox = NULL;
        int iObjUID = getOrCreateDefaultSubwin();

        getGraphicObjectProperty(iObjUID, __GO_CLIP_BOX__, jni_double_vector, (void **)&clipBox);

        createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 4, clipBox);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "font") == 0)
    {
        int iObjUID = getOrCreateDefaultSubwin();
        double dblFontSize = 0;
        double* pdblFontSize = &dblFontSize;
        int iFontStyle = 0;
        int* piFontStyle = &iFontStyle;
        double pdblResult[2];

        getGraphicObjectProperty(iObjUID, __GO_FONT_SIZE__, jni_double, (void **)&pdblFontSize);
        getGraphicObjectProperty(iObjUID, __GO_FONT_STYLE__, jni_int, (void**)&piFontStyle);

        pdblResult[0] = iFontStyle;
        pdblResult[1] = dblFontSize;


        createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblResult);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "font size") == 0)
    {
        double dblFontSize = 0;
        double* pdblFontSize = &dblFontSize;
        getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_FONT_SIZE__, jni_double, (void **)&pdblFontSize);

        createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dblFontSize);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "dashes") == 0)
    {
        int iLineStyle = 0;
        int* piLineStyle = &iLineStyle;

        getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_STYLE__, jni_int, (void**)&piLineStyle);

        createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iLineStyle);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "hidden3d") == 0)
    {
        get_hidden_color_property(pvApiCtx, getOrCreateDefaultSubwin());

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "window") == 0 || strcmp(l1, "figure") == 0)
    {
        int iFigureId = 0;
        int* piFigureId = &iFigureId;

        getOrCreateDefaultSubwin();
        getGraphicObjectProperty(getCurrentFigure(), __GO_ID__, jni_int, (void**)&piFigureId);
        createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iFigureId);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "thickness") == 0)
    {
        get_thickness_property(pvApiCtx, getOrCreateDefaultSubwin());

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "wdim") == 0 || strcmp(l1, "wpdim") == 0)
    {
        int *piFigureSize = NULL;
        double pdblFigureSize[2];

        getOrCreateDefaultSubwin();
        getGraphicObjectProperty(getCurrentFigure(), __GO_SIZE__, jni_int_vector, (void **) &piFigureSize);
        pdblFigureSize[0] = (double) piFigureSize[0];
        pdblFigureSize[1] = (double) piFigureSize[1];

        createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblFigureSize);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "wpos") == 0)
    {
        int *piFigurePosition = NULL;
        double pdblFigurePosition[2];

        getOrCreateDefaultSubwin();
        getGraphicObjectProperty(getCurrentFigure(), __GO_POSITION__, jni_int_vector, (void **) &piFigurePosition);
        pdblFigurePosition[0] = piFigurePosition[0];
        pdblFigurePosition[1] = piFigurePosition[1];

        createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblFigurePosition);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "viewport") == 0)
    {
        int* viewport = NULL;
        double pdblViewport[2];

        getOrCreateDefaultSubwin();
        getGraphicObjectProperty(getCurrentFigure(), __GO_VIEWPORT__, jni_int_vector, (void **)&viewport);
        pdblViewport[0] = viewport[0];
        pdblViewport[1] = viewport[1];

        createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblViewport);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "background") == 0)
    {
        get_background_property(pvApiCtx, getOrCreateDefaultSubwin());

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (  strcmp(l1, "color") == 0
               || strcmp(l1, "foreground") == 0
               || strcmp(l1, "pattern") == 0)
    {
        get_foreground_property(pvApiCtx, getOrCreateDefaultSubwin());

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "lastpattern") == 0)
    {
        int iNumColors = 0;
        int* piNumColors = &iNumColors;

        getOrCreateDefaultSubwin();
        getGraphicObjectProperty(getCurrentFigure(), __GO_COLORMAP_SIZE__, jni_int, (void**)&piNumColors);

        createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNumColors);

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "line mode") == 0)
    {
        int iLineMode = 0;
        int* lineMode = &iLineMode;

        getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_MODE__, jni_bool, (void **)&lineMode);

        createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iLineMode);

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "white") == 0)
    {
        int iNumColors = 0;
        int* piNumColors = &iNumColors;

        getOrCreateDefaultSubwin();
        getGraphicObjectProperty(getCurrentFigure(), __GO_COLORMAP_SIZE__, jni_int, (void**)&piNumColors);

        /* White is lqst colormap index + 2 */
        createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNumColors + 2);

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "wresize") == 0)
    {
        // autoresize property
        int iAutoResize = 0;
        int* piAutoResize =  &iAutoResize;

        getOrCreateDefaultSubwin();
        getGraphicObjectProperty(getCurrentFigure(), __GO_AUTORESIZE__, jni_bool, (void **)&piAutoResize);

        createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iAutoResize);

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "clipgrf") == 0)
    {
        /* clip_state : 0 = off, 1 = on */
        int iClipState = 0;
        int* piClipState = &iClipState;

        getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_CLIP_STATE__, jni_int, (void**)&piClipState);

        createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iClipState);
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else if (strcmp(l1, "clipoff") == 0)
    {
        int iClipState = 0;
        int* piClipState = &iClipState;

        getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_CLIP_STATE__, jni_int, (void**)&piClipState);

        /* clip_state : 0 = off, 1 = on */
        if (iClipState == 0)
        {
            createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1);
        }
        else
        {
            createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 0);
        }
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
    }
    else
    {
        Scierror(999, _("%s: Unrecognized input argument: '%s'.\n"), fname, (l1));
        freeAllocatedSingleString(l1);
        return -1;
    }

    freeAllocatedSingleString(l1);
    return 0;
}
Exemplo n.º 4
0
int sci_tbx_sum(char *fname) {
  SciErr sciErr;
  
  int *piAddressVarOne = NULL;
  double dVarOne = 0.0;
  
  int *piAddressVarTwo = NULL;
  double dVarTwo = 0.0;
  
  double dOut = 0.0;
  
  /* check that we have only 2 input arguments */
  /* check that we have only 1 output argument */
  CheckRhs(2,2);
  CheckLhs(1,1);
  
    /* get Address of inputs */
  sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
  if(sciErr.iErr)
  {
    printError(&sciErr, 0);
    return 0;
  }
  
  sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
  if(sciErr.iErr)
  {
    printError(&sciErr, 0);
    return 0;
  }
  
  /* check input type */
  if ( !isDoubleType(pvApiCtx, piAddressVarOne) )
  {
    Scierror(999,"%s: Wrong type for input argument #%d: A scalar expected.\n", fname, 1);
    return 0;
  }
  
  if ( !isDoubleType(pvApiCtx, piAddressVarTwo) )
  {
    Scierror(999,"%s: Wrong type for input argument #%d: A scalar expected.\n", fname, 2);
    return 0;
  }

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

  if ( getScalarDouble(pvApiCtx, piAddressVarTwo, &dVarTwo) )
  {
    Scierror(999,"%s: Wrong size for input argument #%d: A scalar expected.\n", fname, 2);
    return 0;
  }
  
  /* call c business function */  
  dOut = business_sum(dVarOne, dVarTwo);
  
  /* create result on stack */
  createScalarDouble(pvApiCtx, Rhs + 1, dOut);
  LhsVar(1) = Rhs + 1; 
}
Exemplo n.º 5
0
/*--------------------------------------------------------------------------*/
int sci_norm(char *fname, void* pvApiCtx)
{
    SciErr sciErr;
    // Arguments' addresses
    int *pAAddr         = NULL;
    int *pflagAddr      = NULL;
    // Arguments' values
    double *pA          = NULL;
    char *pflagChar     = NULL;
    doublecomplex *pAC  = NULL;
    double flagVal = 0;
    // Arguments' properties (type, dimensions, length)
    int iLen = 0;
    int iType = 0;
    int iRows = 0;
    int iCols = 0;
    // Return value
    double ret = 0;

    double RowsColsTemp = 0;
    int i = 0;
    int isMat = 0;
    int isComplex = 0;

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

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

    sciErr = getVarType(pvApiCtx, pAAddr, &iType); // Retrieving A type.
    if (iType != sci_matrix)
    {
        OverLoad(1);
        return 0;
    }

    if (isVarComplex(pvApiCtx, pAAddr))
    {
        sciErr = getComplexZMatrixOfDouble(pvApiCtx, pAAddr, &iRows, &iCols, &pAC);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(202, _("%s: Wrong type for argument #%d: Real or complex matrix expected.\n"), fname, 1);
            return 0;
        }

        isComplex = 1;
        for (i = 0; i < iRows * iCols; ++i) // Checking A for %inf, which is not supported by Lapack.
        {
            if (la_isinf(pAC[i].r) != 0 || la_isinf(pAC[i].i) != 0 || ISNAN(pAC[i].r) || ISNAN(pAC[i].i))
            {
                Scierror(264, _("%s: Wrong value for argument #%d: Must not contain NaN or Inf.\n"), fname, 1);
                return 0;
            }
        }
    }
    else
    {
        sciErr = getMatrixOfDouble(pvApiCtx, pAAddr, &iRows, &iCols, &pA);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(202, _("%s: Wrong type for argument #%d: Real or complex matrix expected.\n"), fname, 1);
            return 0;
        }

        for (i = 0 ; i < iRows * iCols ; i++) // Checking A for %inf, which is not supported by Lapack.
        {
            if (la_isinf(pA[i]) != 0 || ISNAN(pA[i]))
            {
                Scierror(264, _("%s: Wrong value for argument #%d: Must not contain NaN or Inf.\n"), fname, 1);
                return 0;
            }
        }
    }
    if (iRows == 0) // A = [] => returning 0.
    {
        createScalarDouble(pvApiCtx, Rhs + 1, 0);
        AssignOutputVariable(pvApiCtx, 1) = Rhs + 1;
        return 0;
    }

    if (iRows > 1 && iCols > 1) // If A is a matrix, only 1, 2 and %inf are allowed as second argument.
    {
        isMat = 1;
    }

    if (iRows == 1) // If iRows == 1, then transpose A to consider it like a vector.
    {
        RowsColsTemp = iRows;
        iRows = iCols;
        iCols = (int)RowsColsTemp;
    }

    if (Rhs == 1) // One argument => returning norm 2.
    {
        // Call normP() or normPC().
        if (isComplex)
        {
            ret = normPC(pAC, iRows, iCols, 2); // if A is a complex matrix, call the complex function.
        }
        else
        {
            ret = normP(pA, iRows, iCols, 2);
        }

        createScalarDouble(pvApiCtx, Rhs + 1, ret);
        AssignOutputVariable(pvApiCtx, 1) = Rhs + 1;
        return 0;
    }

    // Checking flag.
    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &pflagAddr); // Retrieving flag address.
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    sciErr = getVarType(pvApiCtx, pflagAddr, &iType); // Retrieving flag type.
    if (iType != sci_strings && iType != sci_matrix)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String or integer expected.\n"), fname, 2);
        return 0;
    }

    if (iType == sci_strings)
    {
        if (getAllocatedSingleString(pvApiCtx, pflagAddr, &pflagChar)) // Retrieving flag dimensions.
        {
            Scierror(205, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
            return 0;
        }

        iLen = (int)strlen(pflagChar);
        if (iLen != 3 && iLen != 1)
        {
            Scierror(116, _("%s: Wrong value for input argument #%d: %s, %s, %s, or %s expected.\n"), fname, 2, "i", "inf", "f", "fro");
            freeAllocatedSingleString(pflagChar);
            return 0;
        }

        if (strcmp(pflagChar, "inf") != 0 && strcmp(pflagChar, "i") != 0 &&
                strcmp(pflagChar, "fro") != 0 && strcmp(pflagChar, "f") != 0) // flag must be = "inf", "i", "fro" or "f".
        {
            Scierror(116, _("%s: Wrong value for input argument #%d: %s, %s, %s or %s expected.\n"), fname, 2, "i", "inf", "f", "fro");
            freeAllocatedSingleString(pflagChar);
            return 0;
        }

        if (isComplex)
        {
            ret = normStringC(pAC, iRows, iCols, pflagChar); // if A is a complex matrix, call the complex function.
        }
        else
        {
            ret = normString(pA, iRows, iCols, pflagChar); // flag is a string => returning the corresponding norm.
        }

        createScalarDouble(pvApiCtx, Rhs + 1, ret);
        AssignOutputVariable(pvApiCtx, 1) = Rhs + 1;
        freeAllocatedSingleString(pflagChar);
        return 0;
    }
    else
    {
        if (isVarComplex(pvApiCtx, pflagAddr))
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A real expected.\n"), fname, 2);
            return 0;
        }
        if (getScalarDouble(pvApiCtx, pflagAddr, &flagVal)) // Retrieving flag value & dimensions as a double.
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Wrong size for input argument #%d: A real scalar expected.\n"), fname, 2);
            return 0;
        }

        // Call the norm functions.
        if (la_isinf(flagVal) == 1 && flagVal > 0) // flag = %inf
        {
            if (isComplex)
            {
                ret = normStringC(pAC, iRows, iCols, "inf"); // if A is a complex matrix, call the complex function.
            }
            else
            {
                ret = normString(pA, iRows, iCols, "inf"); // The infinite norm is computed by normString().
            }

            createScalarDouble(pvApiCtx, Rhs + 1, ret);
            AssignOutputVariable(pvApiCtx, 1) = Rhs + 1;
            return 0;
        }
        else
        {
            if (isMat == 1 && flagVal != 1 && flagVal != 2 && la_isinf(flagVal) == 0)
            {
                Scierror(116, _("%s: Wrong value for input argument #%d: %s, %s, %s or %s expected.\n"), fname, 2, "1", "2", "inf", "-inf");
                return 0;
            }

            if (isComplex)
            {
                ret = normPC(pAC, iRows, iCols, flagVal); // if A is a complex matrix, call the complex function.
            }
            else
            {
                ret = normP(pA, iRows, iCols, flagVal); // flag is an integer => returning the corresponding norm.
            }

            createScalarDouble(pvApiCtx, Rhs + 1, ret);
            AssignOutputVariable(pvApiCtx, 1) = Rhs + 1;
            return 0;
        }
    }

    return 0;
}
//function to remove specified columns
int sci_sym_delete_cols(char *fname, unsigned long fname_len){
	
	// Error management variables
	SciErr sciErr1,sciErr2;
	double status=1.0;//assume error status
	double num;//variable to store the number of columns to be deleted obtained from user in scilab
	int count=0;//iterator variable
	int num_cols;//stores the number of columns in the loaded problem
	int iType= 0;//stores the datatype of matrix 
	int rows=0,columns=0;//integer variables to denote the number of rows and columns in the array denoting the column numbers to be deleted
	unsigned int *value=NULL;//pointer to integer array allocated dynamically having the indices to be deleted
	double *array_ptr=NULL;//double array pointer to the array denoting the column numbers to be deleted
	int *piAddressVarOne = NULL;//pointer used to access first and second arguments of the function
	int output=0;//output parameter for the symphony sym_delete_cols function
	CheckInputArgument(pvApiCtx, 1, 1);//Check we have exactly one argument as input or not
	CheckOutputArgument(pvApiCtx, 1, 1);//Check we have exactly one argument on output side or not

	//load address of 1st argument into piAddressVarOne
	sciErr2=getVarAddressFromPosition(pvApiCtx,1,&piAddressVarOne);

	
	if (sciErr2.iErr){
        printError(&sciErr2, 0);
        return 0;
	}

	//check if it is double type
	sciErr2 = getVarType(pvApiCtx, piAddressVarOne, &iType);
	if(sciErr2.iErr || iType != sci_matrix)
	{
		printError(&sciErr2, 0);
		return 0;
	}

		
	//getting the first argument as a double array
	sciErr2=getMatrixOfDouble(pvApiCtx,piAddressVarOne,&rows,&columns,&array_ptr);
	if (sciErr2.iErr){
        printError(&sciErr2, 0);
        return 0;
	}

	//dynamically allocate the integer array 
	value=(unsigned int *)malloc(sizeof(unsigned int)*columns);
	//store double values in the integer array by typecasting
	while(count<columns)
	{
		value[count]=(unsigned int)array_ptr[count];
		count++;
	}	
	sciprint("\n");

	//ensure that environment is active

	if(global_sym_env==NULL){
		sciprint("Error: Symphony environment not initialized. Please run 'sym_open()' first.\n");
		}
	else {
		int flag=0;//flag used for finding if the indices to be deleted are valid
		output=sym_get_num_cols(global_sym_env,&num_cols);//function to find the number of columns in the loaded problem
		if(output==FUNCTION_TERMINATED_ABNORMALLY)
		{
			Scierror(999, "An error occured. Has a problem been loaded?\n");
			free(value);//freeing the memory of the allocated pointer
			return 0;
		}
		for(count=0;count<columns;count++)//loop used to check if all the indices mentioned to be deleted are valid
		{
			if(value[count]<0   ||  value[count]>=num_cols){
				flag=1;
				break;
		}	
				
		}
		if(flag==1)
		{
			Scierror(999,"Not valid indices..\n");
			sciprint("valid indices are from 0 to %d",num_cols-1);
			free(value);//freeing the memory of the allocated pointer
			return 0;
		}
		//only when the number of columns to be deleted is lesser than the actual number of columns ,execution is proceeded with
		if(columns<=num_cols){
		output=sym_delete_cols(global_sym_env,(unsigned int)columns,value);//symphony function to delete the columns specified
		if(output==FUNCTION_TERMINATED_NORMALLY)
		{
			sciprint("Execution is successfull\n");
			status=0.0;
		}
		else if(output==FUNCTION_TERMINATED_ABNORMALLY)
		{
			Scierror(999,"Problem occured while deleting the columns,Are the column numbers correct?\n");
			sciprint("Function terminated abnormally\n");
			status=1.0;
		}
	}
		else{
			sciprint("These many number of variables dont exist in the problem\n");
			status=1.0;
		}
				
	}
	
	int e=createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx)+1,status);
	if (e){
		AssignOutputVariable(pvApiCtx, 1) = 0;
		free(value);//freeing the memory of the allocated pointer
		return 1;
		}

	AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
	ReturnArguments(pvApiCtx);
	free(value);//freeing the memory of the allocated pointer
	return 0;
	}
Exemplo n.º 7
0
/*--------------------------------------------------------------------------*/
int sci_winqueryreg(char *fname, unsigned long l)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int *piAddressVarTwo = NULL;
    int *piAddressVarThree = NULL;

    char *pStrParamOne = NULL;
    char *pStrParamTwo = NULL;
    char *pStrParamThree = NULL;

    char *pStrOutput = NULL;
    int iOutput = 0;

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

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

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

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

        if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &pStrParamThree) != 0)
        {
            Scierror(999, _("%s: Memory allocation error.\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;
    }

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

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

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

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

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

    if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &pStrParamTwo) != 0)
    {
        if (pStrParamThree)
        {
            freeAllocatedSingleString(pStrParamThree);
            pStrParamThree = NULL;
        }
        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &pStrParamOne) != 0)
    {
        if (pStrParamThree)
        {
            freeAllocatedSingleString(pStrParamThree);
            pStrParamThree = NULL;
        }

        if (pStrParamTwo)
        {
            freeAllocatedSingleString(pStrParamTwo);
            pStrParamTwo = NULL;
        }

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

    if (Rhs == 3)
    {
        BOOL bKey = strcmp(pStrParamOne, "key") == 0;
        BOOL bValue = strcmp(pStrParamOne, "name") == 0;

        if (bValue || bKey)
        {
            int NumbersElm = 0;
            if (bValue)
            {
                WindowsQueryRegistryNumberOfValuesInList(pStrParamTwo, pStrParamThree, &NumbersElm);
            }
            else
            {
                WindowsQueryRegistryNumberOfKeysInList(pStrParamTwo, pStrParamThree, &NumbersElm);
            }

            if (NumbersElm)
            {
                BOOL bResult = FALSE;
#define MAX_ELMT_REGLIST 255
                char **ListKeysName = NULL;
                int i = 0;

                if (NumbersElm > MAX_ELMT_REGLIST)
                {
                    NumbersElm = MAX_ELMT_REGLIST;
                }
                ListKeysName = (char **)MALLOC(sizeof(char*) * NumbersElm);
                for (i = 0; i < NumbersElm; i++)
                {
                    ListKeysName[i] = NULL;
                }

                if (bValue)
                {
                    bResult = WindowsQueryRegistryValuesList(pStrParamTwo, pStrParamThree, NumbersElm, ListKeysName);
                }
                else
                {
                    bResult = WindowsQueryRegistryKeysList(pStrParamTwo, pStrParamThree, NumbersElm, ListKeysName);
                }

                if (bResult)
                {
                    int nOne = 1;
                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, NumbersElm, nOne, ListKeysName);
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0);
                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
                    }
                    else
                    {
                        LhsVar(1) = Rhs + 1;
                        PutLhsVar();
                    }
                }
                else
                {
                    Scierror(999, _("%s: Cannot open Windows registry.\n"), fname);
                }
                freeArrayOfString(ListKeysName, NumbersElm);
            }
            else
            {
                createEmptyMatrix(pvApiCtx, Rhs + 1);
                LhsVar(1) = Rhs + 1;
                PutLhsVar();
            }

            if (pStrParamThree)
            {
                freeAllocatedSingleString(pStrParamThree);
                pStrParamThree = NULL;
            }

            if (pStrParamTwo)
            {
                freeAllocatedSingleString(pStrParamTwo);
                pStrParamTwo = NULL;
            }

            if (pStrParamOne)
            {
                freeAllocatedSingleString(pStrParamOne);
                pStrParamOne = NULL;
            }
            return 0;
        }
    }

    pStrOutput = (char*)MALLOC(PATH_MAX * sizeof(char));
    if (pStrOutput)
    {
        BOOL OuputIsREG_SZ = FALSE;
        BOOL TestWinQuery = WindowsQueryRegistry(pStrParamOne, pStrParamTwo, pStrParamThree, pStrOutput, &iOutput, &OuputIsREG_SZ);
        if ( TestWinQuery )
        {
            if (OuputIsREG_SZ)
            {
                createSingleString(pvApiCtx, Rhs + 1, pStrOutput);
            }
            else
            {
                createScalarDouble(pvApiCtx, Rhs + 1, (double)iOutput);
            }

            LhsVar(1) = Rhs + 1;
            PutLhsVar();
        }
        else
        {
            Scierror(999, _("%s: Cannot query value of this type.\n"), fname);
        }

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

    if (pStrParamThree)
    {
        freeAllocatedSingleString(pStrParamThree);
        pStrParamThree = NULL;
    }

    if (pStrParamTwo)
    {
        freeAllocatedSingleString(pStrParamTwo);
        pStrParamTwo = NULL;
    }

    if (pStrParamOne)
    {
        freeAllocatedSingleString(pStrParamOne);
        pStrParamOne = NULL;
    }

    return 0;

}
int sci_sym_set_int_param(char *fname, unsigned long fname_len){
	
	// Error management variable
	SciErr sciErr1,sciErr2;
	double status=1.0;//assume error status
	double num;//to store and check the value obtained is pure unsigned integer
	int output;//output variable to store the return value of symphony set integer function
	int value;//to store the value of integer to be set
	int *piAddressVarOne = NULL;//pointer used to access first argument of the function
	int *piAddressVarTwo=NULL;//pointer used to access second argument of the function
	char variable_name[100];//string to hold the name of variable's value to be set
	char *ptr=variable_name;//pointer to point to address of the variable name
	CheckInputArgument(pvApiCtx, 2, 2);//Check we have exactly two argument as input or not
	CheckOutputArgument(pvApiCtx, 1, 1);//Check we have exactly no argument on output side or not

	//load address of 1st argument into piAddressVarOne
	sciErr1 = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
	sciErr2 = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
	//check whether there is an error or not.
	if (sciErr1.iErr){
        printError(&sciErr1, 0);
        return 0;
	}
	if (sciErr2.iErr){
        printError(&sciErr2, 0);
        return 0;
	}
	
	
	//read the value in that pointer pointing to variable name
	int err1=getAllocatedSingleString(pvApiCtx, piAddressVarOne, &ptr);
	//read the value of the variable to be set as a double value
	int err2=getScalarDouble(pvApiCtx, piAddressVarTwo, &num);

	//check for the integrity of integer value obtained
	if((double)(unsigned int)num!=(double)num)
		return 0;
	else
		value=(unsigned int)num;//if the value passed is an integer ,store it as an unsigned integer in value variable 

	//ensure that environment is active
	if(global_sym_env==NULL){
		sciprint("Error: Symphony environment not initialized. Please run 'sym_open()' first.\n");
		}
	else {
		output=sym_set_int_param(global_sym_env,ptr,value);//symphony function to set the variable name pointed by the ptr pointer to the integer value stored in value variable.
		if(output==FUNCTION_TERMINATED_NORMALLY){
		sciprint("setting of integer parameter function executed successfully\n");
		status=0.0;	
		}
		else if(output==FUNCTION_TERMINATED_ABNORMALLY){
			sciprint("setting of integer parameter was unsuccessful.....check your parameter and value\n");
			status=1.0;
		}
		else
			sciprint("\nerror while executing the setting integer function...check your parameter and value!!\n");
		
		}
	
	int e=createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx)+1,status);
	if (e){
		AssignOutputVariable(pvApiCtx, 1) = 0;
		return 1;
		}

	AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
	//ReturnArguments(pvApiCtx);

	return 0;
	}
Exemplo n.º 9
0
/*--------------------------------------------------------------------------*/
int C2F(sci_funcprot)(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    CheckLhs(1, 1);
    CheckRhs(0, 1);

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

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

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

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

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

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

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

        ilevel = (int) dVarOne;

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

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

            LhsVar(1) = Rhs + 1;
            PutLhsVar();
        }
    }
    return 0;
}
Exemplo n.º 10
0
/*--------------------------------------------------------------------------*/
int C2F(sci_lasterror)(char *fname,unsigned long fname_len)
{
    SciErr sciErr;
    BOOL bClearLastError = TRUE;
    int NbLines = 0;
    const char **errorMessage = NULL;

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

    if (Rhs == 1)
    {
        int *piAddressVarOne = NULL;

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

        if (isBooleanType(pvApiCtx, piAddressVarOne))
        {
            if (isScalar(pvApiCtx, piAddressVarOne))
            {
                getScalarBoolean(pvApiCtx, piAddressVarOne, &bClearLastError);
            }
            else
            {
                Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 1);
            }
        }
        else
        {
            Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 1);
        }
    }

    errorMessage = getLastErrorMessage(&NbLines);

    if ((NbLines <= 0) || (errorMessage == NULL))
    {
        createEmptyMatrix(pvApiCtx, Rhs + 1);
    }
    else
    {
        sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, NbLines, 1, (char**)errorMessage);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    LhsVar(1) = Rhs + 1;

    if (Lhs >= 2)
    {
        double dLastErrorValue = (double) getLastErrorValue();
        createScalarDouble(pvApiCtx, Rhs + 2, dLastErrorValue);
        LhsVar(2) = Rhs + 2;
    }

    if (Lhs >= 3)
    {
        double dLinePosition = (double)  getLastErrorLinePosition();
        createScalarDouble(pvApiCtx, Rhs + 3, dLinePosition);
        LhsVar(3) = Rhs + 3;
    }

    if (Lhs == 4)
    {
        createSingleString(pvApiCtx, Rhs + 4, (char*)getLastErrorFunctionName());
        LhsVar(4) = Rhs + 4;
    }

    if (bClearLastError)
    {
        clearLastError();
    }

    PutLhsVar();

    return 0;
}
Exemplo n.º 11
0
/*--------------------------------------------------------------------------*/
int sci_getdate(char *fname, unsigned long fname_len)
{
    SciErr sciErr;

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

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

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

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

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

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

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

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

                freeAllocatedSingleString(pStr);
                pStr = NULL;

                dTime = getCurrentDateAsUnixTimeConvention();

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

                nbElements = m * n;

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

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

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

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


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

                FREE(dResults);
                dResults = NULL;

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

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