int write_boolean(char *fname, unsigned long fname_len) { SciErr sciErr; int i, j; //first variable info : boolean matrix of boolean 3 x 4 int iRows1 = 3; int iCols1 = 4; int* pboolOutput1 = NULL; //second variable info : boolean matrix of boolean 4 x 6 int iRows2 = 4; int iCols2 = 6; int* pboolOutput2 = NULL; /************************ * First variable * ************************/ //alloc array of data in OS memory pboolOutput1 = (int*)malloc(sizeof(int) * iRows1 * iCols1); //fill array with incremental values //[ %t %f %t %f // %t %f %t %f // %t %f %t %f ] for (i = 0 ; i < iRows1 ; i++) { for (j = 0 ; j < iCols1 ; j++) { pboolOutput1[i + iRows1 * j] = (i * iCols1 + j) % 2 == FALSE; } } //can be written in a single loop //for(i = 0 ; i < iRows1 * iCols1; i++) //{ // pboolOutput1[i] = i % 2 == FALSE; //} //create a variable from a existing data array sciErr = createMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iRows1, iCols1, pboolOutput1); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //after creation, we can free memory. free(pboolOutput1); /************************* * Second variable * *************************/ //reserve space in scilab memory and fill it sciErr = allocMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 2, iRows2, iCols2, &pboolOutput2); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //fill array with incremental values //[ %f %t %f %t %f %t // %f %t %f %t %f %t // %f %t %f %t %f %t // %f %t %f %t %f %t ] for (i = 0 ; i < iRows2 ; i++) { for (j = 0 ; j < iCols2 ; j++) { pboolOutput2[i + iRows2 * j] = (i * iCols2 + j) % 2 == TRUE; } } //can be written in a single loop //for(i = 0 ; i < iRows2 * iCols2; i++) //{ // pboolOutput2[i] = i % 2 == TRUE; //} // /!\ DO NOT FREE MEMORY, in this case, it's the Scilab memory //assign allocated variables to Lhs position AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; return 0; }
/*--------------------------------------------------------------------------*/ int sci_isdir(char *fname, unsigned long fname_len) { SciErr sciErr; int *piAddressVarOne = NULL; wchar_t **pStVarOne = NULL; int iType = 0; int *lenStVarOne = NULL; int m1 = 0, n1 = 0; BOOL *results = NULL; int i = 0; /* Check Input & Output parameters */ CheckRhs(1, 1); CheckLhs(1, 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, &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) { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1); return 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; } lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } results = (BOOL*)MALLOC(sizeof(BOOL) * (m1 * n1)); if (results == NULL) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pStVarOne, m1 * n1); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL); if (sciErr.iErr) { printError(&sciErr, 0); FREE(results); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); if (pStVarOne == NULL) { FREE(lenStVarOne); lenStVarOne = NULL; Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } for (i = 0; i < m1 * n1; i++) { pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1)); if (pStVarOne[i] == NULL) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { freeArrayOfWideString(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++) { wchar_t *expandedPath = expandPathVariableW(pStVarOne[i]); if (expandedPath) { results[i] = isdirW(expandedPath); FREE(expandedPath); expandedPath = NULL; } else { results[i] = FALSE; } } if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pStVarOne, m1 * n1); sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m1, n1, results); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; if (results) { FREE(results); results = NULL; } PutLhsVar(); return 0; }
/* ==================================================================== */ int sci_foo(char *fname, void* pvApiCtx, unsigned long fname_len) { // Error management variable SciErr sciErr; ////////// Variables declaration ////////// int m1 = 0, n1 = 0; int *piAddressVarOne = NULL; double *matrixOfDouble = NULL; double *newMatrixOfDouble = NULL; int m2 = 0, n2 = 0; int *piAddressVarTwo = NULL; int *matrixOfBoolean = NULL; int *newMatrixOfBoolean = NULL; int i = 0; ////////// Check the number of input and output arguments ////////// /* --> [c, d] = foo(a, b) */ /* check that we have only 2 input arguments */ /* check that we have only 2 output argument */ CheckInputArgument(pvApiCtx, 2, 2) ; CheckOutputArgument(pvApiCtx, 2, 2) ; ////////// Manage the first input argument (double) ////////// /* get Address of inputs */ sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } /* Check that the first input argument is a real matrix (and not complex) */ if ( !isDoubleType(pvApiCtx, piAddressVarOne) || isVarComplex(pvApiCtx, piAddressVarOne) ) { Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), fname, 1); return 0; } /* get matrix */ sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &matrixOfDouble); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } ////////// Manage the second input argument (boolean) ////////// /* get Address of inputs */ sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if ( !isBooleanType(pvApiCtx, piAddressVarTwo) ) { Scierror(999, _("%s: Wrong type for input argument #%d: A boolean matrix expected.\n"), fname, 2); return 0; } /* get matrix */ sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2, &matrixOfBoolean); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } ////////// Check the consistency of the two input arguments ////////// if ((m1 != m2) | - (n1 != n2)) { Scierror(999, _("%s: Wrong size for input arguments: Same size expected.\n"), fname, 1); return 0; } newMatrixOfDouble = (double*)malloc(sizeof(double) * m1 * n1); ////////// Application code ////////// // Could be replaced by a call to a library for (i = 0; i < m1 * n1; i++) { /* For each element of the matrix, multiply by 2 */ newMatrixOfDouble[i] = matrixOfDouble[i] * 2; } newMatrixOfBoolean = (int*)malloc(sizeof(double) * m2 * n2); for (i = 0; i < m2 * n2; i++) { /* For each element of the matrix, invert the value */ newMatrixOfBoolean[i] = matrixOfBoolean[i] == TRUE ? FALSE : TRUE; } ////////// Create the output arguments ////////// /* Create the matrix as return of the function */ sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, newMatrixOfDouble); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } /* Create the matrix as return of the function */ sciErr = createMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 2, m2, n2, newMatrixOfBoolean); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } ////////// Return the output arguments to the Scilab engine ////////// AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; ReturnArguments(pvApiCtx); return 0; }
/*--------------------------------------------------------------------------*/ static int isasciiMatrix(char *fname, int *piAddressVarOne) { SciErr sciErr; int m1 = 0, n1 = 0; double *pdVarOne = NULL; 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 > 0) { BOOL *bOutputMatrix = NULL; sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &pdVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } bOutputMatrix = (BOOL*)MALLOC(sizeof(BOOL) * (m1 * n1)); if (bOutputMatrix) { int nbElems = m1 * n1; int i = 0; for (i = 0; i < nbElems; i++) { int iVal = (int)pdVarOne[i]; if (isascii(iVal)) { bOutputMatrix[i] = (int)TRUE; } else { bOutputMatrix[i] = (int)FALSE; } } sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m1, n1, bOutputMatrix); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (bOutputMatrix) { FREE(bOutputMatrix); bOutputMatrix = NULL; } LhsVar(1) = Rhs + 1; PutLhsVar(); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { /* returns [] */ m1 = 0; n1 = 0; sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, NULL); 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_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; }
/*--------------------------------------------------------------------------*/ static int isasciiStrings(char *fname, int *piAddressVarOne) { SciErr sciErr; int m1 = 0, n1 = 0; wchar_t **pwcStVarOne = NULL; int *lenStVarOne = NULL; 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; } lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne) { BOOL *bOutputMatrix = NULL; int i = 0; int lengthAllStrings = 0; sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL); 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; } pwcStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); for (i = 0; i < (m1 * n1); i++) { lengthAllStrings = lengthAllStrings + lenStVarOne[i]; pwcStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1)); if (pwcStVarOne[i] == NULL) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pwcStVarOne, m1 * n1); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pwcStVarOne); if (sciErr.iErr) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pwcStVarOne, m1 * n1); printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } bOutputMatrix = (BOOL*)MALLOC(sizeof(BOOL) * lengthAllStrings); if (bOutputMatrix) { int mOut = 0; int nOut = 0; int x = 0; for (i = 0; i < (m1 * n1); i++) { int j = 0; wchar_t* wcInput = pwcStVarOne[i]; int len = (int)wcslen(wcInput); for (j = 0; j < len; j++) { if (iswascii(wcInput[j])) { bOutputMatrix[x] = (int)TRUE; } else { bOutputMatrix[x] = (int)FALSE; } x++; } } mOut = 1; nOut = lengthAllStrings; sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, mOut, nOut, bOutputMatrix); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pwcStVarOne, m1 * n1); if (bOutputMatrix) { FREE(bOutputMatrix); bOutputMatrix = NULL; } LhsVar(1) = Rhs + 1; PutLhsVar(); } else { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pwcStVarOne, m1 * n1); Scierror(999, _("%s: Memory allocation error.\n"), fname); } } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } return 0; }
int CreateBooleanVariable(void *pvApiCtx, int iVar, matvar_t *matVariable, int * parent, int item_position) { int nbRow = 0, nbCol = 0; int *piDims = NULL; int * intPtr = NULL; double * dblPtr = NULL; int K = 0; SciErr sciErr; if (matVariable->rank == 2) /* 2-D array */ { nbRow = (int)matVariable->dims[0]; nbCol = (int)matVariable->dims[1]; if (nbRow * nbCol != 0) { if ((intPtr = (int*) MALLOC(sizeof(int) * nbRow * nbCol)) == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateBooleanVariable"); return FALSE; } for (K = 0; K < nbRow * nbCol; K++) { intPtr[K] = ((unsigned char*)matVariable->data)[K]; } if (parent == NULL) { sciErr = createMatrixOfBoolean(pvApiCtx, iVar, nbRow, nbCol, intPtr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } } else { sciErr = createMatrixOfBooleanInList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, intPtr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } } FREE(intPtr); } else { if ((dblPtr = (double *)MALLOC(sizeof(double) * nbRow * nbCol)) == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateBooleanVariable"); return FALSE; } for (K = 0; K < nbRow * nbCol; K++) { dblPtr[K] = ((unsigned char*)matVariable->data)[K]; } if (parent == NULL) { sciErr = createMatrixOfDouble(pvApiCtx, iVar, nbRow, nbCol, dblPtr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } } else { sciErr = createMatrixOfDoubleInList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, dblPtr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } } FREE(dblPtr); } } else /* Multi-dimension array -> Scilab HyperMatrix */ { piDims = (int*) MALLOC(matVariable->rank * sizeof(int)); if (piDims == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateBooleanVariable"); return FALSE; } for (K = 0; K < matVariable->rank; K++) { piDims[K] = (int)matVariable->dims[K]; } CreateHyperMatrixVariable(pvApiCtx, iVar, MATRIX_OF_BOOLEAN_DATATYPE, NULL, &matVariable->rank, piDims, (double*)matVariable->data, NULL, parent, item_position); FREE(piDims); } return TRUE; }
/*----------------------------------------------------------------------------*/ int sci_isdigit(char *fname, unsigned long fname_len) { SciErr sciErr; int *piAddressVarOne = NULL; CheckRhs(1, 1); CheckLhs(1, 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; } if (!isScalar(pvApiCtx, piAddressVarOne)) { Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1); return 0; } if (!isStringType(pvApiCtx, piAddressVarOne)) { Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1); } else { wchar_t *pStVarOne = NULL; if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &pStVarOne) != 0) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } else { int valuesSize = 0; BOOL *values = IsDigitW(pStVarOne, &valuesSize); freeAllocatedSingleWideString(pStVarOne); pStVarOne = NULL; if (values) { int m1 = 1; int n1 = valuesSize; sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m1, n1, (const int *)values); FREE(values); values = NULL; if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { createEmptyMatrix(pvApiCtx, Rhs + 1); } LhsVar(1) = Rhs + 1; PutLhsVar(); } } return 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; }
/*--------------------------------------------------------------------------*/ 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; }