cd & chdir factorized
Allan CORNET [Fri, 25 Sep 2009 07:57:57 +0000 (09:57 +0200)]
scilab/CHANGES_5.2.X
scilab/modules/completion/src/c/getfilesdictionary.c
scilab/modules/fileio/includes/scicurdir.h
scilab/modules/fileio/macros/cd.sci [deleted file]
scilab/modules/fileio/sci_gateway/c/gw_fileio.c
scilab/modules/fileio/sci_gateway/c/sci_chdir.c
scilab/modules/fileio/sci_gateway/c/sci_findfiles.c
scilab/modules/fileio/sci_gateway/c/sci_pwd.c
scilab/modules/fileio/sci_gateway/fileio_gateway.xml
scilab/modules/fileio/src/c/scicurdir.c

index 65fb969..4494423 100644 (file)
@@ -128,6 +128,7 @@ fileio functions:
    + isdir manages matrix of strings as input argument
    + basename (primitive)
    + pathconvert (primitive)
+   + cd, chdir (primitive)
   
 Dynamic link:
 =============
index 0da2c49..afaed80 100644 (file)
@@ -55,14 +55,13 @@ char **getfilesdictionary(char *somechars,int *sizearray,BOOL fullpath)
                {
                        /* current path */
                        int ierr = 0;
-                       int lpath = 0;
-                       char *currentpath = NULL;
-
-                       scigetcwd(&currentpath,&lpath,&ierr);
+                       char *currentpath = scigetcwd(&ierr);
                        if (currentpath) 
                        {
                                strcpy(path, currentpath);
                                strcat(path, DIR_SEPARATOR);
+                               FREE(currentpath);
+                               currentpath = NULL;
                        }
                }
                else
index fc1f99a..79deedf 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) 2007 - INRIA - Sylvestre LEDRU
+ * Copyright (C) 2009 - DIGITEO - Allan CORNET
  * 
  * This file must be used under the terms of the CeCILL.
  * This source file is licensed as described in the file COPYING, which
 /**
  * Changes scilab current directory 
  * @param path : the path where Scilab should go
- * @param[out] err : the error code
+ * @return err : the error code
  */
-FILEIO_IMPEXP int scichdir(char *path,int *err);
+FILEIO_IMPEXP int scichdir(char *path);
+
+/**
+* Changes scilab current directory (wide string)
+* @param path : the path where Scilab should go
+* @return err : the error code
+*/
+FILEIO_IMPEXP int scichdirW(wchar_t *wcpath);
 
 /**
  * Get scilab current directory 
- * @param[out] path : the current path
- * @param[out] lpath : the length of the char * path
  * @param[out] err : the error code
+ * @return path : the current path
  */
-FILEIO_IMPEXP int scigetcwd(char **path,int *lpath,int *err);
+FILEIO_IMPEXP char * scigetcwd(int *err);
+
+/**
+* Get scilab current directory (wide string)
+* @param[out] err : the error code
+* @return path : the current path
+*/
+FILEIO_IMPEXP wchar_t * scigetcwdW(int *err);
 
 #endif /* __SCICURDIR_H__ */
 /*--------------------------------------------------------------------------*/
diff --git a/scilab/modules/fileio/macros/cd.sci b/scilab/modules/fileio/macros/cd.sci
deleted file mode 100644 (file)
index a13fc4b..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) INRIA
-// Copyright (C) DIGITEO - 2009 - Allan CORNET
-//
-// This file must be used under the terms of the CeCILL.
-// This source file is licensed as described in the file COPYING, which
-// you should have received as part of this distribution.  The terms
-// are also available at
-// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
-
-function path=cd(path)
-  if (argn(2) == 1) then
-    if type(path) <> 10 then 
-      error(999,msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),'cd',1));
-    end 
-    
-    if size(path,'*') <> 1 then 
-      error(999,msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),'cd',1));
-    end 
-    
-       path = stripblanks(path);
-       
-    if (path == 'PWD') then 
-      path = evstr(path);
-    end
-    
-    if or(path==['/' '\' '..']) then
-      chdir(path);
-    else
-      if ~isdir(path) then
-        error(msprintf(gettext("%s: Cannot go to directory %s\n"),'cd',path),998);
-      end
-      chdir(path);
-    end 
-  else 
-    chdir(home);
-  end
-  path = pwd();
-endfunction
index 92ba7e9..a391366 100644 (file)
@@ -59,7 +59,8 @@ static gw_generic_table Tab[]={
        {sci_fileparts,"fileparts"},
        {sci_movefile,"movefile"},
        {sci_basename,"basename"},
-       {sci_pathconvert,"pathconvert"}
+       {sci_pathconvert,"pathconvert"},
+       {sci_chdir,"cd"}
        };
 /*--------------------------------------------------------------------------*/ 
 int gw_fileio(void)
index 1cbd858..39f9310 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) 2006 - INRIA - Allan CORNET
+ * Copyright (C) 2009 - DIGITEO - Allan CORNET
  * 
  * This file must be used under the terms of the CeCILL.
  * This source file is licensed as described in the file COPYING, which
  * are also available at    
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
- */
-#include <stdio.h>
+/*--------------------------------------------------------------------------*/
 #include "gw_fileio.h"
 #include "stack-c.h"
 #include "Scierror.h"
 #include "scicurdir.h"
 #include "localization.h"
 #include "expandPathVariable.h"
-#include "PATH_MAX.h"
 #include "MALLOC.h"
+#include "localization.h"
+#include "api_common.h"
+#include "api_string.h"
+#include "api_double.h"
+#include "api_boolean.h"
+#include "isdir.h"
+#include "charEncoding.h"
 /*--------------------------------------------------------------------------*/
 int sci_chdir(char *fname,unsigned long fname_len)
 {
+       int *piAddressVarOne = NULL;
+       wchar_t *pStVarOne = NULL;
+       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) || (GetType(1) == sci_strings) )
+       if (Rhs == 0)
        {
-               int ierr = 1;
-               int l1 = 0, n1 = 0,m1 = 0;
-               char shortpath[PATH_MAX];
-               char *path = NULL;
-               int out_n = 0;
+               pStVarOne = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(L"home")+1));
+               if (pStVarOne)
+               {
+                       wcscpy(pStVarOne, L"home");
+               }
+       }
+       else
+       {
+               getVarAddressFromPosition(1, &piAddressVarOne);
+
+               if ( getVarType(piAddressVarOne) != sci_strings )
+               {
+                       Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
+                       return 0;
+               }
 
-               if (Rhs == 0)
+               getMatrixOfWideString(piAddressVarOne,&m1,&n1,&lenStVarOne,&pStVarOne);
+               if ( (m1 != n1) && (n1 != 1) ) 
                {
-                       strcpy(shortpath, "home/");
+                       Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
+                       return 0;
                }
-               else
+
+               pStVarOne = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarOne + 1));
+               if (pStVarOne == NULL)
                {
-                       GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
-                       strcpy(shortpath,cstk(l1));
+                       Scierror(999,_("%s : Memory allocation error.\n"),fname);
+                       return 0;
                }
 
-               path = expandPathVariable(shortpath);
-               if (path)
+               getMatrixOfWideString(piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
+       }
+
+       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)
                {
-                       scichdir(path, &ierr);
-                       FREE(path);
-                       path = NULL;
+                       if (getNamedVarType("PWD") == sci_strings)
+                       {
+                               wchar_t *VARVALUE = NULL;
+                               int VARVALUElen = 0;
+                               int m = 0, n = 0;
+                               if (readNamedMatrixOfWideString("PWD", &m, &n, &VARVALUElen, &VARVALUE) == 0)
+                               {
+                                       if ( (m == 1) && (n == 1) )
+                                       {
+                                               VARVALUE = (wchar_t*)MALLOC(sizeof(wchar_t)*(VARVALUElen + 1));
+                                               if (VARVALUE)
+                                               {
+                                                       readNamedMatrixOfWideString("PWD", &m, &n, &VARVALUElen, &VARVALUE);
+                                                       FREE(expandedPath);
+                                                       expandedPath = VARVALUE;
+                                               }
+                                       }
+                               }
+                       }
                }
 
-               n1=1;
-               CreateVar(Rhs+1,MATRIX_OF_BOOLEAN_DATATYPE, &n1,&n1,&l1);
+               if (strcmp(fname, "chdir") == 0) /* chdir output boolean */
+               {       
+                       BOOL *bOuput = (BOOL*)MALLOC(sizeof(BOOL));
 
-               if (ierr == 0) *istk(l1)=(int)(TRUE);
-               else *istk(l1)=(int)(FALSE);
+                       int ierr = scichdirW(expandedPath);
 
-               LhsVar(1)=Rhs+1;
-               C2F(putlhsvar)();
+                       if (ierr) bOuput[0] = FALSE;
+                       else bOuput[0] = TRUE; 
+
+                       createMatrixOfBoolean(Rhs + 1, 1, 1, bOuput);
+
+                       LhsVar(1) = Rhs + 1;
+                       C2F(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)
+                               {
+                                       createMatrixOfWideString(Rhs + 1, 1, 1, &currentDir);
+                               }
+                               else
+                               {
+                                       createMatrixOfDouble(Rhs + 1, 0, 0, NULL);
+                               }
+
+                               LhsVar(1) = Rhs + 1;
+                               C2F(putlhsvar)();
+
+                               if (currentDir) {FREE(currentDir); currentDir = NULL;}
+                       }
+                       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: Wrong type for input argument #%d: String expected.\n"),fname, 1);
+               Scierror(999,_("%s : Memory allocation error.\n"),fname);
        }
+
        return 0;
 }
 /*--------------------------------------------------------------------------*/
index b3b8c20..f2b0e2c 100644 (file)
@@ -50,7 +50,7 @@ int sci_findfiles(char *fname,unsigned long fname_len)
                        int ierr = 0;
                        int lpath = 0;
 
-                       scigetcwd(&path,&lpath,&ierr);
+                       path = scigetcwd(&ierr);
 
                        if (ierr)
                        {
@@ -70,7 +70,7 @@ int sci_findfiles(char *fname,unsigned long fname_len)
                        if (GetType(1) == sci_strings)
                        {
                                GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
-                               path = cstk(l1);
+                               path = strdup(cstk(l1));
                                filespec = strdup(DEFAULT_FILESPEC);
                        }
                        else
@@ -87,7 +87,7 @@ int sci_findfiles(char *fname,unsigned long fname_len)
                        if ( (GetType(1) == sci_strings) && (GetType(2) == sci_strings) )
                        {
                                GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
-                               path = cstk(l1);
+                               path = strdup(cstk(l1));
 
                                GetRhsVar(2,STRING_DATATYPE,&m1,&n1,&l1);
                                filespec = cstk(l1);
@@ -103,6 +103,7 @@ int sci_findfiles(char *fname,unsigned long fname_len)
        }
 
        pathextented = expandPathVariable(path);
+       if (path) {FREE(path); path = NULL;}
        FilesList = findfiles(pathextented, filespec, &sizeListReturned);
        if (pathextented) {FREE(pathextented); pathextented = NULL;}
        if (needtofreefilespec) { if (filespec) FREE(filespec); filespec = NULL;}
index 07742fc..93f6bf6 100644 (file)
@@ -19,7 +19,6 @@
 int sci_pwd(char *fname,unsigned long fname_len)
 {
        int ierr = 0;
-       int lpath = 0;
        char *path = NULL;
 
        Rhs = Max(Rhs,0);
@@ -27,10 +26,11 @@ int sci_pwd(char *fname,unsigned long fname_len)
        CheckRhs(0,0);
        CheckLhs(0,1);
 
-       scigetcwd(&path,&lpath,&ierr);
+       path = scigetcwd(&ierr);
 
        if (ierr)
        {
+               if (path) {FREE(path); path = NULL;}
                Scierror(998,_("%s: An error occurred.\n"), fname);
                return 0;
        }
@@ -43,6 +43,8 @@ int sci_pwd(char *fname,unsigned long fname_len)
                CreateVarFromPtr(Rhs+1, STRING_DATATYPE, &m1, &n1, &path);
                LhsVar(1) = Rhs+1;
                C2F(putlhsvar)();
+
+               if (path) {FREE(path); path = NULL;}
        }
 
        return 0;
index f696183..6b456ea 100644 (file)
@@ -75,4 +75,5 @@
 <PRIMITIVE gatewayId="34" primitiveId="40" primitiveName="movefile" />
 <PRIMITIVE gatewayId="34" primitiveId="41" primitiveName="basename" />
 <PRIMITIVE gatewayId="34" primitiveId="42" primitiveName="pathconvert" />
+<PRIMITIVE gatewayId="34" primitiveId="43" primitiveName="cd" />
 </GATEWAY>
index 3293c80..ae9faa4 100644 (file)
@@ -2,6 +2,7 @@
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) INRIA
  * Copyright (C) ENPC
+ * Copyright (C) DIGITEO - 2009 - Allan CORNET
  *
  * This file must be used under the terms of the CeCILL.
  * This source file is licensed as described in the file COPYING, which
 #include "charEncoding.h"
 #include "warningmode.h"
 #include "PATH_MAX.h"
-static char cur_dir[PATH_MAX];
 /*--------------------------------------------------------------------------*/
-int scichdir(char *path,int *err)
+int scichdirW(wchar_t *wcpath)
 {
-       *err=0;
-       if (path == (char*) NULL)
+#ifndef _MSC_VER
+       char *path = NULL;
+       if (wcpath == NULL)
        {
-               *cur_dir = '\0';
-               return (0);
+               return 1;
+       }
+
+       path = wide_string_to_UTF8(wcpath);
+       if (path == NULL)
+       {
+               return 1;
        }
 
-#ifndef _MSC_VER
        if (chdir(path) == -1)
        {
                if ( getWarningMode() ) sciprint(_("Can't go to directory %s.\n"), path);
-               *err=1;
+               if (path) {FREE(path); path = NULL;}
+               return 1;
        }
+
+       if (path) {FREE(path); path = NULL;}
+
 #else
-       /**/
+       if (wcpath == NULL)
        {
-               wchar_t *pwTemp = to_wide_string(path);
+               return 1;
+       }
 
-               if ( _wchdir(pwTemp) )
+       if ( _wchdir(wcpath) )
+       {
+               switch (errno)
                {
-                       switch (errno)
+               case ENOENT:
+                       {
+                               if ( getWarningMode() ) 
+                               {
+                                       char *path = wide_string_to_UTF8(wcpath);
+                                       if (path)
+                                       {
+                                               sciprint(_("Can't go to directory %s.\n"), path);
+                                               FREE(path);
+                                               path = NULL;
+                                       }
+                               }
+                       }
+                       break;
+               case EINVAL:
                        {
-                       case ENOENT:
-                               if ( getWarningMode() ) sciprint(_("Can't go to directory %s.\n"), path);
-                               break;
-                       case EINVAL:
                                if ( getWarningMode() ) sciprint(_("Invalid buffer.\n"));
-                               break;
-                       default:
+                       }
+                       break;
+               default:
+                       {
                                if ( getWarningMode() ) sciprint(_("Unknown error.\n"));
                        }
-                       *err = 1;
                }
-               FREE(pwTemp);
+               return 1;
        }
 #endif
        return 0;
 }
 /*--------------------------------------------------------------------------*/
-int scigetcwd(char **path,int *lpath,int *err)
+int scichdir(char *path)
 {
+       int ierr = 1;
+       wchar_t *wcpath = NULL;
+       if (path == NULL) return ierr;
+       wcpath = to_wide_string(path);
+       if (wcpath == NULL) return ierr;
+       ierr = scichdirW(wcpath);
+       FREE(wcpath);
+       wcpath = NULL;
+       return ierr;
+}
+/*--------------------------------------------------------------------------*/
+wchar_t * scigetcwdW(int *err)
+{
+       wchar_t *wcCurrentDir = NULL;
+       
 #ifndef _MSC_VER
-       if (GETCWD(cur_dir, PATH_MAX) == (char*) 0)
+       char currentDir[PATH_MAX + 1]:
+       if (GETCWD(cur_dir, PATH_MAX) == NULL)
+       {
+               if ( getWarningMode() ) sciprint(_("Can't get current directory.\n"));
+               *err = 1;
+       }
+       else
+       {
+               wcCurrentDir = to_wide_string(currentDir);
+               *err = 0;
+       }
 #else
-       wchar_t wcdir[PATH_MAX];
-       if ( _wgetcwd(wcdir, PATH_MAX) == (wchar_t*) 0 )
-#endif
+       wchar_t wcdir[PATH_MAX + 1];
+       if ( _wgetcwd(wcdir, PATH_MAX) == NULL )
        {
-               /* get current working dir */
                if ( getWarningMode() ) sciprint(_("Can't get current directory.\n"));
-               *cur_dir = '\0';
-               *lpath = 0;
                *err = 1;
        }
-  else
+       else
        {
-#ifdef _MSC_VER
-               char *tmpcdir = wide_string_to_UTF8(wcdir);
-               if (tmpcdir)
-               {
-                       strcpy(cur_dir,tmpcdir);
-                       FREE(tmpcdir);
-                       tmpcdir = NULL;
-                       *path = cur_dir;
-                       *lpath =(int)strlen(cur_dir);
-                       *err = 0;
-               }
-               else
+               wcCurrentDir = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(wcdir) + 1));
+               if (wcCurrentDir)
                {
-                       *cur_dir = '\0';
-                       *lpath=0;
-                       *err = 1;
+                       wcscpy(wcCurrentDir, wcdir);
                }
-#else
-       *path = cur_dir;
-       *lpath =(int)strlen(cur_dir);
-       *err = 0;
+       }
 #endif
+       return wcCurrentDir;
+}
+/*--------------------------------------------------------------------------*/
+char * scigetcwd(int *err)
+{
+       char *currentDir = NULL;
+       wchar_t *wcCurrentDir = scigetcwdW(err);
+       if (wcCurrentDir)
+       {
+               currentDir = wide_string_to_UTF8(wcCurrentDir);
+               FREE(wcCurrentDir);
+               wcCurrentDir = NULL;
        }
-       return 0;
+       return currentDir;
 }
 /*--------------------------------------------------------------------------*/