rewrites isdir (remove workaround)
Allan CORNET [Sun, 20 Sep 2009 17:38:47 +0000 (19:38 +0200)]
scilab/CHANGES_5.2.X
scilab/modules/fileio/help/en_US/isdir.xml
scilab/modules/fileio/includes/expandPathVariable.h
scilab/modules/fileio/sci_gateway/c/sci_isdir.c
scilab/modules/fileio/sci_gateway/c/sci_isfile.c
scilab/modules/fileio/src/c/expandPathVariable.c

index eb2781c..b73bd10 100644 (file)
@@ -123,6 +123,7 @@ fileio functions:
 * Rewrite :
    + copyfile (primitive)
    + fileparts (primitive)
+   + isdir manages matrix of strings as input argument
   
 Dynamic link:
 =============
index bd5a157..37c6ba9 100644 (file)
@@ -29,7 +29,7 @@
       <varlistentry>
         <term>path</term>
         <listitem>
-          <para>a character string, the file pathname</para>
+          <para>a character string or a matrix of strings, the file pathname</para>
         </listitem>
       </varlistentry>
       <varlistentry>
index 4274b76..4e2f8b9 100644 (file)
 
 /**
 * expanded variables are: 
-* "SCI/" "sci/" "$SCI" "SCI\\" "sci\\"
-* "HOME/"  "home/"  "~/" "HOME\\"  "home\\"  "~\\" "$HOME"
-* "TMPDIR/" "tmpdir/" "TMPDIR\\" "tmpdir\\"  "$TMPDIR"
-*
-* replace order SCI, HOME , TMPDIR
+* "SCI"
+* "SCIHOME"
+* "HOME"
+* "TMPDIR"
 */
 
 /**
index e3f1b13..1be08df 100644 (file)
@@ -1,7 +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
  *
  */
 /*--------------------------------------------------------------------------*/ 
-#include <stdio.h>
-#include <string.h>
 #include "gw_fileio.h"
-#include "isdir.h"
 #include "stack-c.h"
 #include "MALLOC.h"
-#include "expandPathVariable.h"
-#include "Scierror.h"
 #include "localization.h"
-#include "PATH_MAX.h"
-#ifdef _MSC_VER
-#include "strdup_windows.h"
-#endif
+#include "api_common.h"
+#include "api_string.h"
+#include "api_boolean.h"
+#include "Scierror.h"
+#include "expandPathVariable.h"
+#include "isdir.h"
+#include "freeArrayOfString.h"
+#include "BOOL.h"
 /*--------------------------------------------------------------------------*/
 int sci_isdir(char *fname,unsigned long fname_len)
 {
-       int l1 = 0,n1 = 0,m1 = 0;
+       int *piAddressVarOne = NULL;
+       wchar_t **pStVarOne = NULL;
+       int *lenStVarOne = NULL;
+       int m1 = 0, n1 = 0;
+
+       BOOL *results = NULL;
+       int m_out = 0, n_out = 0;
+       int i = 0;
 
+       /* Check Input & Output parameters */
        CheckRhs(1,1);
        CheckLhs(1,1);
-       
-       if (! (GetType(1) == sci_strings))
+
+       getVarAddressFromPosition(1, &piAddressVarOne);
+
+       if (getVarType(piAddressVarOne) != sci_strings)
        {
-               Scierror(999,_("%s: Wrong type for input argument: A string expected.\n"),fname);
+               Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
                return 0;
        }
-       else
+
+       getVarDimension(piAddressVarOne, &m1, &n1);
+
+       lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1));
+       if (lenStVarOne == NULL)
        {
-               char *path = NULL, *myPath = NULL;
-               char *filename = NULL;
-               BOOL result = FALSE;
+               Scierror(999,_("%s : Memory allocation error.\n"),fname);
+               return 0;
+       }
 
-               GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
-               if ( n1==1 )
-               {
-                       path = cstk(l1);
-               }
-               else
-               {
-                       Scierror(999,_("%s: Wrong size for input argument: A string expected.\n"),fname);
-                       return 0;
-               }
+       pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1));
+       if (pStVarOne == NULL)
+       {
+               if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
+               Scierror(999,_("%s : Memory allocation error.\n"),fname);
+               return 0;
+       }
 
-               /* make sure the names are not too long */
-               
-               if( strlen(path) > PATH_MAX )
-               {
-                 Scierror(999,_("%s: Wrong size for input argument: Must be less than %d characters.\n"),fname,PATH_MAX);
-               }
-               
-               /* Crappy workaround because a / was added after SCI & ~ into 
-                * the Scilab macros
-                * cluni0 waits for SCI/ or ~/. It doesn't detect isdir("SCI")
-                */
-               if(strcmp(path,"SCI") == 0)
-               {
-                       myPath = strdup("SCI/");
-               }
-               if(strcmp(path,"~") == 0)
-               {
-                       myPath = strdup("~/");
-               }
-               /* End of the crappy workaround */
+       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;
+       }
 
-               if(myPath == NULL)
+       getMatrixOfWideString(piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
+
+       for (i = 0; i< m1 * n1; i++)
+       {
+               wchar_t *expandedPath = expandPathVariableW(pStVarOne[i]);
+               if (expandedPath)
                {
-                       /* Replaces SCI, ~, HOME by the real path */
-                       filename = expandPathVariable(path);
+                       results[i] = isdirW(expandedPath);
+                       FREE(expandedPath);
+                       expandedPath = NULL;
                }
                else
                {
-                       /* Replaces SCI, ~, HOME by the real path */
-                       filename = expandPathVariable(myPath);
-                       FREE(myPath);
-                       myPath = NULL;
-               }
-
-               if (filename)
-               {
-                       result = isdir(filename);
-                       FREE(filename);
-                       filename = NULL;
+                       results[i] = FALSE;
                }
-               m1 = 1;
-               n1 = 1;
-               CreateVar(Rhs+1,MATRIX_OF_BOOLEAN_DATATYPE, &m1, &n1 ,&l1); /* Create the space in the stack for result */
-               *istk(l1) = result; /* Copy result into the stack */
-               
-               LhsVar(1) = Rhs+1;
-               C2F(putlhsvar)();
-               
        }
+
+       if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
+       freeArrayOfWideString(pStVarOne, m1 * n1);
+
+       createMatrixOfBoolean(Rhs + 1, m1, n1, results);
+       LhsVar(1) = Rhs + 1;
+
+       if (results) {FREE(lenStVarOne); lenStVarOne = NULL;}
+
+       C2F(putlhsvar)();
        return 0;
-       
 }
 /*--------------------------------------------------------------------------*/
index 7f298b6..76b675b 100644 (file)
@@ -20,6 +20,7 @@
 #include "Scierror.h"
 #include "FileExist.h"
 #include "isdir.h"
+#include "expandPathVariable.h"
 #include "freeArrayOfString.h"
 #include "BOOL.h"
 /*--------------------------------------------------------------------------*/
@@ -76,7 +77,16 @@ int sci_isfile(char *fname,unsigned long fname_len)
 
        for (i=0;i< m1 * n1; i++)
        {
-               results[i] = !isdirW(pStVarOne[i]) && FileExistW(pStVarOne[i]);
+               wchar_t *expandedPath = expandPathVariableW(pStVarOne[i]);
+               if (expandedPath)
+               {
+                       results[i] = !isdirW(expandedPath) && FileExistW(expandedPath);
+                       FREE(expandedPath); expandedPath = NULL;
+               }
+               else
+               {
+                       results[i] = FALSE;
+               }
        }
 
        if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
index c49ba66..74c596e 100644 (file)
@@ -1,3 +1,4 @@
+/*--------------------------------------------------------------------------*/
 /*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2009 - DIGITEO - Allan CORNET
 #include "api_string.h"
 #include "api_common.h"
 /*--------------------------------------------------------------------------*/
-static wchar_t *SCI_words[]  = 
-{
-       L"SCI/" ,
-       L"SCI\\" ,
-       (wchar_t *) NULL
-};
-/*--------------------------------------------------------------------------*/
-static wchar_t *HOME_words[] = 
-{
-       L"HOME/" ,
-       L"home/" ,
-       L"~/" ,
-       L"HOME\\" ,
-       L"home\\" ,
-       L"~\\" ,
-       (wchar_t *) NULL
-};
-/*--------------------------------------------------------------------------*/
-static wchar_t *TMPDIR_words[]  = 
+struct VARIABLEALIAS
 {
-       L"TMPDIR/" ,
-       L"TMPDIR\\" ,
-       (wchar_t *) NULL
+       wchar_t *Alias;
+       wchar_t *VariableName;
 };
 /*--------------------------------------------------------------------------*/
-static wchar_t *SCIHOME_words[]  = 
+#define NB_ALIAS 6
+static struct VARIABLEALIAS VARIABLES_words[NB_ALIAS] =
 {
-       L"SCIHOME/" ,
-       L"SCIHOME\\",
-       (wchar_t *) NULL
+       {L"SCIHOME", L"SCIHOME"},
+       {L"SCI", L"SCI"},
+       {L"~", L"home"},
+       {L"HOME", L"home"},
+       {L"home", L"home"},
+       {L"TMPDIR", L"TMPDIR"}
 };
 /*--------------------------------------------------------------------------*/
-static wchar_t *getVariableValueDefinedInScilab(char *varname);
-static wchar_t *findAliasInString(wchar_t *wcStr, wchar_t *wcAlias);
-static wchar_t *replaceAliasInString(wchar_t *wcValue, wchar_t *wcBegin);
+static wchar_t *getVariableValueDefinedInScilab(wchar_t *wcVarName);
 static wchar_t *convertFileSeparators(wchar_t *wcStr);
 /*--------------------------------------------------------------------------*/
-#define expandPath(PathStr, VariableNames, VariableValue) \
-i = 0; \
-while(VariableNames[i] != NULL) \
-{ \
-       wcTmp = findAliasInString(PathStr, VariableNames[i]); \
-       if (wcTmp) \
-       { \
-               if (VariableValue) \
-               { \
-                       wchar_t * replaced = replaceAliasInString(VariableValue, wcTmp); \
-                       if (SCIDIR) {FREE(SCIDIR); SCIDIR = NULL;} \
-                       if (TMPDIR) {FREE(TMPDIR); TMPDIR = NULL;} \
-                       if (HOMEDIR) {FREE(HOMEDIR); HOMEDIR = NULL;} \
-                       if (SCIHOMEDIR) {FREE(SCIHOMEDIR); SCIHOMEDIR = NULL;} \
-                       return replaced; \
-               } \
-       } \
-       i++; \
-} \
-/*--------------------------------------------------------------------------*/
 wchar_t *expandPathVariableW(wchar_t *wcstr)
 {
-       int i = 0;
-
        wchar_t *wcexpanded = NULL;
-       wchar_t *wcTmp = NULL;
-       
-       wchar_t *SCIDIR = NULL;
-       wchar_t *HOMEDIR = NULL;
-       wchar_t *SCIHOMEDIR = NULL;
-       wchar_t *TMPDIR = NULL;
+       if (wcstr)
+       {
+               int i = 0;
+               int lenStr = (int)wcslen(wcstr);
 
-       /* search SCIHOME Alias and expand */
-       SCIHOMEDIR = getVariableValueDefinedInScilab("SCIHOME");
-       expandPath(wcstr, SCIHOME_words, SCIHOMEDIR)
+               for (i = 0; i < NB_ALIAS; i++)
+               {
+                       int lenAlias = 0;
 
-       /* search SCI Alias and expand */
-       SCIDIR = getVariableValueDefinedInScilab("SCI");
-       expandPath(wcstr, SCI_words, SCIDIR)
+                       /* input is ALIAS without subdirectory */
+                       if (wcscmp(VARIABLES_words[i].Alias, wcstr) == 0)
+                       {
+                               wchar_t *wcexpanded = getVariableValueDefinedInScilab(VARIABLES_words[i].VariableName);
+                               if (wcexpanded)
+                               {
+                                        return convertFileSeparators(wcexpanded);
+                               }
+                       }
+
+                       lenAlias = (int)wcslen(VARIABLES_words[i].Alias);
 
-       /* search HOME Alias and expand */
-       HOMEDIR = getVariableValueDefinedInScilab("home");
-       expandPath(wcstr, HOME_words, HOMEDIR)
+                       if (lenStr > lenAlias)
+                       {
+                               wchar_t *wcBegin = (wchar_t *)MALLOC(sizeof(wchar_t) * (lenAlias + 1));
+                               if (wcBegin)
+                               {
+                                       wcsncpy(wcBegin, wcstr, lenAlias);
+                                       wcBegin[lenAlias] = 0;
 
-       /* search TMPDIR Alias and expand */
-       TMPDIR = getVariableValueDefinedInScilab("TMPDIR");
-       expandPath(wcstr, TMPDIR_words, TMPDIR)
+                                       if (wcscmp(wcBegin,VARIABLES_words[i].Alias)== 0 )
+                                       {
+                                               if ( (wcstr[lenAlias] == L'/') || (wcstr[lenAlias] == L'\\') )
+                                               {
+                                                       wchar_t * newBegin = getVariableValueDefinedInScilab(VARIABLES_words[i].VariableName);
+                                                       if (newBegin)
+                                                       {
+                                                               int lengthnewBegin = (int)wcslen(newBegin);
+                                                               wcexpanded = (wchar_t *)MALLOC(sizeof(wchar_t)* (lengthnewBegin + (int)wcslen(&wcstr[lenAlias]) + 1));
+                                                               if (wcexpanded)
+                                                               {
+                                                                       wcscpy(wcexpanded, newBegin);
+                                                                       wcscat(wcexpanded, &wcstr[lenAlias]);
+                                                                       FREE(wcBegin); wcBegin = NULL;
+                                                                       FREE(newBegin); newBegin = NULL;
+                                                                       return convertFileSeparators(wcexpanded);
+                                                               }
+                                                               FREE(newBegin); newBegin = NULL;
+                                                       }
+                                               }
+                                       }
+                                       FREE(wcBegin);wcBegin = NULL;
+                               }
+                       }
+               }
 
-       if (SCIDIR) {FREE(SCIDIR); SCIDIR = NULL;}
-       if (TMPDIR) {FREE(TMPDIR); TMPDIR = NULL;}
-       if (HOMEDIR) {FREE(HOMEDIR); HOMEDIR = NULL;}
-       if (SCIHOMEDIR) {FREE(SCIHOMEDIR); SCIHOMEDIR = NULL;}
-       
-       /* Variables not founded returns a copy of input */
-       wcexpanded = (wchar_t*)MALLOC(sizeof(wchar_t)* ((int)wcslen(wcstr) + 1));
-       wcscpy(wcexpanded, wcstr);
-       return convertFileSeparators(wcexpanded);
+               /* Variables not founded returns a copy of input */
+               wcexpanded = (wchar_t*)MALLOC(sizeof(wchar_t)* ((int)wcslen(wcstr) + 1));
+               if (wcexpanded) 
+               {
+                       wcscpy(wcexpanded, wcstr);
+                       return convertFileSeparators(wcexpanded);
+               }
+       }
+       return wcexpanded;
 }
 /*--------------------------------------------------------------------------*/
 char *expandPathVariable(char* str)
@@ -134,44 +128,38 @@ char *expandPathVariable(char* str)
        return expanded;
 }
 /*--------------------------------------------------------------------------*/
-static wchar_t *findAliasInString(wchar_t *wcStr, wchar_t *wcAlias)
+wchar_t *getVariableValueDefinedInScilab(wchar_t *wcVarName)
 {
-       if (wcStr && wcAlias)
+       wchar_t *VARVALUE = NULL;
+       char *varname = NULL;
+
+       if (wcVarName)
        {
-               int lenwcAlias = (int)wcslen(wcAlias);
-               int lenwcStr = (int)wcslen(wcStr);
-               if (lenwcStr >= lenwcAlias)
+               varname = wide_string_to_UTF8(wcVarName);
+               if (varname)
                {
-                       wchar_t *wcBegin = (wchar_t *)MALLOC(sizeof(wchar_t) * (lenwcAlias + 1));
-                       wcsncpy(wcBegin, wcStr, lenwcAlias);
-                       wcBegin[lenwcAlias] = 0;
-                       if (wcscmp(wcBegin, wcAlias) == 0)
+                       if (getNamedVarType(varname) == sci_strings)
                        {
-                               FREE(wcBegin); wcBegin = NULL;
-                               return(&wcStr[lenwcAlias]);
+                               int VARVALUElen = 0;
+                               int m = 0, n = 0;
+                               if (readNamedMatrixOfWideString(varname, &m, &n, &VARVALUElen, &VARVALUE) == 0)
+                               {
+                                       if ( (m == 1) && (n == 1) )
+                                       {
+                                               VARVALUE = (wchar_t*)MALLOC(sizeof(wchar_t)*(VARVALUElen + 1));
+                                               if (VARVALUE)
+                                               {
+                                                       readNamedMatrixOfWideString(varname, &m, &n, &VARVALUElen, &VARVALUE);
+                                               }
+                                       }
+                               }
                        }
-                       FREE(wcBegin); wcBegin = NULL;
                }
        }
-       return NULL;
-}
-/*--------------------------------------------------------------------------*/
-static wchar_t *replaceAliasInString(wchar_t *wcValue, wchar_t *wcBegin)
-{
-       wchar_t *wcexpanded = NULL;
-       int lenwcBegin = (int)wcslen(wcBegin);
-       int lenwcValue = (int)wcslen(wcValue);
-
-       wcexpanded = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenwcBegin + lenwcValue + 1 + 1));
-
-       wcscpy(wcexpanded, wcValue);
-       wcscat(wcexpanded, L"/");
-       wcscat(wcexpanded, wcBegin);
-
-       return convertFileSeparators(wcexpanded);
+       return VARVALUE;
 }
 /*--------------------------------------------------------------------------*/
-static wchar_t *convertFileSeparators(wchar_t *wcStr)
+wchar_t *convertFileSeparators(wchar_t *wcStr)
 {
        if (wcStr)
        {
@@ -187,25 +175,3 @@ static wchar_t *convertFileSeparators(wchar_t *wcStr)
        return wcStr;
 }
 /*--------------------------------------------------------------------------*/
-wchar_t *getVariableValueDefinedInScilab(char *varname)
-{
-       wchar_t *VARVALUE = NULL;
-       if (getNamedVarType(varname) == sci_strings)
-       {
-               int VARVALUElen = 0;
-               int m = 0, n = 0;
-               if (readNamedMatrixOfWideString(varname, &m, &n, &VARVALUElen, &VARVALUE) == 0)
-               {
-                       if ( (m == 1) && (n == 1) )
-                       {
-                               VARVALUE = (wchar_t*)MALLOC(sizeof(wchar_t)*(VARVALUElen + 1));
-                               if (VARVALUE)
-                               {
-                                       readNamedMatrixOfWideString(varname, &m, &n, &VARVALUElen, &VARVALUE);
-                               }
-                       }
-               }
-       }
-       return VARVALUE;
-}
-/*--------------------------------------------------------------------------*/