/*--------------------------------------------------------------------------*/ BOOL removedirW(wchar_t *pathW) { if (isdirW(pathW)) { #ifdef _MSC_VER DeleteDirectory(pathW); #else char *path = wide_string_to_UTF8(pathW); if (path) { DeleteDirectory(path); FREE(path); path = NULL; } #endif if (!isdirW(pathW)) return TRUE; } return FALSE; }
/*--------------------------------------------------------------------------*/ BOOL isdir(const char * path) { BOOL bOK = FALSE; #ifndef _MSC_VER struct stat buf; if (path == NULL) { return FALSE; } if (stat(path, &buf) == 0 && S_ISDIR(buf.st_mode)) { bOK = TRUE; } #else wchar_t *wcpath = to_wide_string((char*)path); if (wcpath == NULL) { return FALSE; } bOK = isdirW(wcpath); FREE(wcpath); #endif return bOK; }
/*--------------------------------------------------------------------------*/ 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_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_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; }