Example #1
0
/*--------------------------------------------------------------------------*/
int checkNamedVarType(void *_pvCtx, const char *_pstName, int _iType)
{
    int iType = 0;

    SciErr sciErr = getNamedVarType(_pvCtx, _pstName, &iType);
    if (sciErr.iErr)
    {
        return 0;
    }

    if (iType == _iType)
    {
        return 1;
    }

    return 0;
}
Example #2
0
/*--------------------------------------------------------------------------*/
int sci_chdir(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    wchar_t *pStVarOne = NULL;
    int iType1	= 0;
    int lenStVarOne = 0;
    int m1 = 0, n1 = 0;

    wchar_t *expandedPath = NULL;

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

    if (Rhs == 0)
    {
        pStVarOne = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(L"home") + 1));
        if (pStVarOne)
        {
            wcscpy(pStVarOne, L"home");
        }
    }
    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, &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;
        }

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

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

        pStVarOne = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne + 1));
        if (pStVarOne == NULL)
        {
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

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

    expandedPath = expandPathVariableW(pStVarOne);
    if (pStVarOne)
    {
        FREE(pStVarOne);
        pStVarOne = NULL;
    }

    if (expandedPath)
    {
        /* get value of PWD scilab variable (compatiblity scilab 4.x) */
        if (wcscmp(expandedPath, L"PWD") == 0)
        {
            sciErr = getNamedVarType(pvApiCtx, "PWD", &iType1);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                Scierror(999, _("%s: Can not read named argument %s.\n"), fname, "PWD");
                return 0;
            }

            if (iType1 == sci_strings)
            {
                wchar_t *VARVALUE = NULL;
                int VARVALUElen = 0;
                int m = 0, n = 0;
                sciErr = readNamedMatrixOfWideString(pvApiCtx, "PWD", &m, &n, &VARVALUElen, &VARVALUE);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    Scierror(999, _("%s: Can not read named argument %s.\n"), fname, "PWD");
                    return 0;
                }

                if ( (m == 1) && (n == 1) )
                {
                    VARVALUE = (wchar_t*)MALLOC(sizeof(wchar_t) * (VARVALUElen + 1));
                    if (VARVALUE)
                    {
                        readNamedMatrixOfWideString(pvApiCtx, "PWD", &m, &n, &VARVALUElen, &VARVALUE);
                        FREE(expandedPath);
                        expandedPath = VARVALUE;
                    }
                }
            }
        }

        if (strcmp(fname, "chdir") == 0) /* chdir output boolean */
        {
            BOOL *bOutput = (BOOL*)MALLOC(sizeof(BOOL));

            int ierr = scichdirW(expandedPath);

            if (ierr)
            {
                bOutput[0] = FALSE;
            }
            else
            {
                bOutput[0] = TRUE;
            }

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

            LhsVar(1) = Rhs + 1;

            if (bOutput)
            {
                FREE(bOutput);
                bOutput = NULL;
            }

            PutLhsVar();
        }
        else /* cd output string current path */
        {
            if ( isdirW(expandedPath) || (wcscmp(expandedPath, L"/") == 0) ||
                    (wcscmp(expandedPath, L"\\") == 0) )
            {
                int ierr = scichdirW(expandedPath);
                wchar_t *currentDir = scigetcwdW(&ierr);
                if ( (ierr == 0) && currentDir)
                {
                    sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, 1, 1, &currentDir);
                }
                else
                {
                    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;
                if (currentDir)
                {
                    FREE(currentDir);
                    currentDir = NULL;
                }
                PutLhsVar();
            }
            else
            {
                char *path = wide_string_to_UTF8(expandedPath);
                if (path)
                {
                    Scierror(998, _("%s: Cannot go to directory %s\n"), fname, path);
                    FREE(path);
                    path = NULL;
                }
                else
                {
                    Scierror(998, _("%s: Cannot go to directory.\n"), fname);
                }
            }
        }

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

    return 0;
}
Example #3
0
/*--------------------------------------------------------------------------*/
wchar_t *getVariableValueDefinedInScilab(wchar_t *wcVarName)
{
    wchar_t *VARVALUE = NULL;
    char *varname = NULL;
    int iType	= 0;

    if (wcVarName)
    {
        varname = wide_string_to_UTF8(wcVarName);
        if (varname)
        {
            SciErr sciErr = getNamedVarType(pvApiCtx, varname, &iType);
            if (sciErr.iErr)
            {
                return NULL;
            }

            if (iType == sci_strings)
            {

                int VARVALUElen = 0;
                int m = 0, n = 0;

                sciErr = readNamedMatrixOfWideString(pvApiCtx, varname, &m, &n, &VARVALUElen, &VARVALUE);
                if (sciErr.iErr)
                {
                    return NULL;
                }

                if ( (m == 1) && (n == 1) )
                {
                    VARVALUE = (wchar_t*)MALLOC(sizeof(wchar_t) * (VARVALUElen + 1));
                    if (VARVALUE)
                    {
                        BOOL bConvLong = FALSE;
                        wchar_t *LongName = NULL;
                        sciErr = readNamedMatrixOfWideString(pvApiCtx, varname, &m, &n, &VARVALUElen, &VARVALUE);
                        if (sciErr.iErr)
                        {
                            FREE(VARVALUE);
                            VARVALUE = NULL;
                            return 0;
                        }
                        LongName = getlongpathnameW(VARVALUE, &bConvLong);
                        if (LongName)
                        {
                            FREE(VARVALUE);
                            VARVALUE = LongName;
                        }
                    }
                }
            }
        }
        if (varname)
        {
            FREE(varname);
            varname = NULL;
        }
    }
    return VARVALUE;
}