/*--------------------------------------------------------------------------*/ int getAllocatedNamedSingleWideString(void* _pvCtx, const char* _pstName, wchar_t** _pwstData) { SciErr sciErr = sciErrInit(); int iRows = 0; int iCols = 0; int iLen = 0; if (isNamedScalar(_pvCtx, _pstName) == 0 || isNamedStringType(_pvCtx, _pstName) == 0) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_SINGLE_WIDE_STRING, _("%s: Wrong type for input argument \"%s\": A single string expected.\n"), "getAllocatedNamedSingleWideString", _pstName); printError(&sciErr, 0); return sciErr.iErr; } sciErr = readNamedMatrixOfWideString(_pvCtx, _pstName, &iRows, &iCols, &iLen, NULL); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_SINGLE_WIDE_STRING, _("%s: Unable to get argument data"), "getAllocatedNamedSingleWideString"); printError(&sciErr, 0); return sciErr.iErr; } *_pwstData = (wchar_t*)MALLOC(sizeof(wchar_t) * (iLen + 1)); //+1 for null termination sciErr = readNamedMatrixOfWideString(_pvCtx, _pstName, &iRows, &iCols, &iLen, _pwstData); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_SINGLE_WIDE_STRING, _("%s: Unable to get argument data"), "getAllocatedNamedSingleWideString"); printError(&sciErr, 0); FREE(*_pwstData); return sciErr.iErr; } return 0; }
/*--------------------------------------------------------------------------*/ int getAllocatedNamedMatrixOfWideString(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, wchar_t*** _pwstData) { int iRows = 0; int iCols = 0; int* piLen = 0; SciErr sciErr = readNamedMatrixOfWideString(_pvCtx, _pstName, &iRows, &iCols, NULL, NULL); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_WIDE_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedNamedMatrixOfWideString"); printError(&sciErr, 0); return sciErr.iErr; } piLen = (int*)MALLOC(sizeof(int) **_piRows **_piCols); sciErr = readNamedMatrixOfWideString(_pvCtx, _pstName, &iRows, &iCols, piLen, NULL); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_WIDE_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedNamedMatrixOfWideString"); if (piLen) { FREE(piLen); piLen = NULL; } printError(&sciErr, 0); return sciErr.iErr; } *_pwstData = (wchar_t**)MALLOC(sizeof(wchar_t*) **_piRows **_piCols); for (int i = 0 ; i < *_piRows **_piCols ; i++) { *_pwstData[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (piLen[i] + 1));//+1 for null termination } sciErr = readNamedMatrixOfWideString(_pvCtx, _pstName, &iRows, &iCols, piLen, *_pwstData); if (piLen) { FREE(piLen); piLen = NULL; } if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_WIDE_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedNamedMatrixOfWideString"); printError(&sciErr, 0); for (int i = 0 ; i < *_piRows **_piCols ; i++) { FREE((*_pwstData)[i]); } FREE(*_pwstData); return sciErr.iErr; } return 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, ¤tDir); } 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; }
/*--------------------------------------------------------------------------*/ 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; }