/*--------------------------------------------------------------------------*/ int sci_tempname(char *fname, void* pvApiCtx) { SciErr sciErr; wchar_t *wcprefix = NULL; wchar_t *wcTempFilename = NULL; //Rhs = Max(Rhs, 0); CheckRhs(0, 1); CheckLhs(1, 1); if (Rhs == 0) { wcprefix = (wchar_t *)MALLOC(sizeof(wchar_t) * (wcslen(DEFAULT_PREFIX) + 1)); wcscpy(wcprefix, DEFAULT_PREFIX); } else { 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 (!isScalar(pvApiCtx, piAddressVarOne)) { Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 1); return 0; } if (isStringType(pvApiCtx, piAddressVarOne)) { if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &wcprefix)) { if (wcprefix) { freeAllocatedSingleWideString(wcprefix); } Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } #if _MSC_VER if (wcslen(wcprefix) > 3) { freeAllocatedSingleWideString(wcprefix); Scierror(999, _("%s: Wrong size for input argument #%d: A string (3 characters max.) expected.\n"), fname, 1); return 0; } #endif } else { freeAllocatedSingleWideString(wcprefix); Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1); return 0; } } wcTempFilename = createtempfilenameW(wcprefix, TRUE); freeAllocatedSingleWideString(wcprefix); if (wcTempFilename == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (createSingleWideString(pvApiCtx, Rhs + 1, wcTempFilename)) { FREE(wcTempFilename); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } FREE(wcTempFilename); LhsVar(1) = Rhs + 1; PutLhsVar(); return 0; }
/*--------------------------------------------------------------------------*/ int sci_movefile(char *fname, unsigned long fname_len) { SciErr sciErr; int *piAddressVarOne = NULL; wchar_t *pStVarOne = NULL; wchar_t *pStVarOneExpanded = NULL; int *piAddressVarTwo = NULL; wchar_t *pStVarTwo = NULL; wchar_t *pStVarTwoExpanded = NULL; /* Check Input & Output parameters */ CheckRhs(2, 2); CheckLhs(1, 2); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (!isStringType(pvApiCtx, piAddressVarOne)) { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\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; } 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)) { if (pStVarOne) { FREE(pStVarOne); pStVarOne = NULL; } Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2); return 0; } if (!isScalar(pvApiCtx, piAddressVarTwo)) { if (pStVarOne) { FREE(pStVarOne); pStVarOne = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2); return 0; } if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &pStVarOne)) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (getAllocatedSingleWideString(pvApiCtx, piAddressVarTwo, &pStVarTwo)) { if (pStVarOne) { freeAllocatedSingleWideString(pStVarOne); pStVarOne = NULL; } Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } pStVarOneExpanded = expandPathVariableW(pStVarOne); pStVarTwoExpanded = expandPathVariableW(pStVarTwo); freeAllocatedSingleWideString(pStVarTwo); pStVarTwo = NULL; freeAllocatedSingleWideString(pStVarOne); pStVarOne = NULL; if (isdirW(pStVarOneExpanded) || FileExistW(pStVarOneExpanded)) { int ierrMove = 0; if (isdirW(pStVarOneExpanded)) { /* move a directory into a directory */ ierrMove = MoveDirectoryFunction(pStVarTwoExpanded, pStVarOneExpanded); } else if (FileExistW(pStVarOneExpanded)) { if (isdirW(pStVarTwoExpanded)) { /* move file into a existing directory */ /* copy file into a existing directory */ wchar_t *filename = getFilenameWithExtensionForMove(pStVarOneExpanded); if (filename) { #define FORMAT_FULLFILENAME "%s/%s" wchar_t *destFullFilename = NULL; /* remove last file separator if it exists */ if ((pStVarTwoExpanded[wcslen(pStVarTwoExpanded) - 1] == L'\\') || (pStVarTwoExpanded[wcslen(pStVarTwoExpanded) - 1] == L'/')) { pStVarTwoExpanded[wcslen(pStVarTwoExpanded) - 1] = L'\0'; } destFullFilename = (wchar_t *) MALLOC(sizeof(wchar_t) * ((int)wcslen(pStVarTwoExpanded) + (int)wcslen(filename) + (int)wcslen(L"/") + 1)); wcscpy(destFullFilename, pStVarTwoExpanded); wcscat(destFullFilename, L"/"); wcscat(destFullFilename, filename); ierrMove = MoveFileFunction(destFullFilename, pStVarOneExpanded); FREE(filename); filename = NULL; FREE(destFullFilename); destFullFilename = NULL; } else { if (pStVarOneExpanded) { FREE(pStVarOneExpanded); pStVarOneExpanded = NULL; } if (pStVarTwoExpanded) { FREE(pStVarTwoExpanded); pStVarTwoExpanded = NULL; } Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { /* move a file into a file */ ierrMove = MoveFileFunction(pStVarTwoExpanded, pStVarOneExpanded); } } else { if (pStVarOneExpanded) { FREE(pStVarOneExpanded); pStVarOneExpanded = NULL; } if (pStVarTwo) { FREE(pStVarTwoExpanded); pStVarTwoExpanded = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: A valid filename or directory expected.\n"), fname, 1); return 0; } returnMoveFileResultOnStack(ierrMove, fname); } else { Scierror(999, _("%s: Wrong value for input argument #%d: A valid filename or directory expected.\n"), fname, 1); } if (pStVarOneExpanded) { FREE(pStVarOneExpanded); pStVarOneExpanded = NULL; } if (pStVarTwoExpanded) { FREE(pStVarTwoExpanded); pStVarTwoExpanded = NULL; } return 0; }
/*----------------------------------------------------------------------------*/ 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_archive_compress(char *fname, unsigned long fname_len) { SciErr sciErr; int *piAddressVarOne = NULL; char *pStVarOne = NULL; char *pStVarOneExpanded = NULL; int *piAddressVarTwo = NULL; char **pStVarTwo = NULL; int m1 = 0; int n1 = 0; int i = 0; int *piAddressVarThree = NULL; char *pStVarThree = NULL; char **file_list = NULL; /* Check Input & Output parameters */ CheckRhs(3, 3); CheckLhs(1, 2); int result = 0; int *error = 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) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: A 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, 1); return 0; } if (isDoubleType(pvApiCtx, piAddressVarTwo)) { if (isEmptyMatrix(pvApiCtx, piAddressVarTwo)) { if (createEmptyMatrix(pvApiCtx, Rhs + 2)) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1); return 0; } LhsVar(1) = Rhs + 1; PutLhsVar(); return 0; } if (isStringType(pvApiCtx, piAddressVarTwo) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 2); return 0; } if (getAllocatedMatrixOfString(pvApiCtx, piAddressVarTwo, &m1, &n1, &pStVarTwo)) { freeAllocatedMatrixOfString(m1, n1, pStVarTwo); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if (isStringType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &pStVarOne)) { if (pStVarOne) { freeAllocatedSingleWideString(pStVarOne); } Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &pStVarThree)) { if (pStVarThree) { freeAllocatedSingleWideString(pStVarThree); } freeAllocatedSingleString(pStVarOne); freeAllocatedSingleString(pStVarTwo); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } int size = 0; while(i < (m1*n1)) { size++; if(file_list!=NULL) { file_list = (char**)REALLOC(file_list,sizeof(char*)*(size)); } else { file_list = (char**)MALLOC(sizeof(char*)*(size)); } file_list[size-1] = (char*)MALLOC(sizeof(char)*512); strcpy(file_list[size-1],expandPathVariable(pStVarTwo[i])); i++; } i = 0; if(strlen(pStVarThree)>2) { Scierror(999, _("%s: Sorry unrecognised format type.\n"), fname); return 0; } while(i<strlen(pStVarThree)) { if(pStVarThree[i]!='Z' && pStVarThree[i]!='j' && pStVarThree[i]!='y' && pStVarThree[i]!='z') { Scierror(999, _("%s: Sorry unrecognised format type.\n"), fname); return 0; } i++; } result = archive_compress(pStVarOne,file_list,size,pStVarThree,&error); if(error == 1) { Scierror(999, _("%s: Sorry the file could not be opened.\n"), fname); return 0; } if(error == 2) { Scierror(999, _("%s: Sorry the file header could not be read\n"), fname); return 0; } if(error == 3) { Scierror(999, _("%s: Sorry the file header could not be written.\n"), fname); return 0; } freeAllocatedSingleString(pStVarOne); freeAllocatedSingleString(pStVarTwo); createScalarInteger32(pvApiCtx, Rhs + 1, result); 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; }