b3b8c2049e81be189e93142458579f1c201ddc7d
[scilab.git] / scilab / modules / fileio / sci_gateway / c / sci_findfiles.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Allan CORNET
4  * ...
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 /*--------------------------------------------------------------------------*/
14 #include <string.h>
15 #include "gw_fileio.h"
16 #include "findfiles.h"
17 #include "stack-c.h"
18 #include "MALLOC.h"
19 #include "localization.h"
20 #include "scicurdir.h" /* scigetcwd */
21 #include "Scierror.h"
22 #include "expandPathVariable.h"
23 #include "PATH_MAX.h"
24 #ifdef _MSC_VER
25 #include "strdup_windows.h"
26 #endif
27 #include "freeArrayOfString.h"
28 /*--------------------------------------------------------------------------*/
29 #define DEFAULT_FILESPEC "*.*"
30 /*--------------------------------------------------------------------------*/
31 int sci_findfiles(char *fname,unsigned long fname_len)
32 {
33         static int l1 = 0, n1 = 0, m1 = 0;
34         char *pathextented = NULL;
35         int out_n = 0;
36         char *path = NULL;
37         char *filespec = NULL;
38         char **FilesList = NULL;
39         int sizeListReturned = 0;
40         BOOL needtofreefilespec = FALSE;
41
42         Rhs = Max(Rhs,0);
43         CheckRhs(0,2) ;
44         CheckLhs(0,1) ;
45
46         switch(Rhs)
47         {
48         default: case 0:
49                 {
50                         int ierr = 0;
51                         int lpath = 0;
52
53                         scigetcwd(&path,&lpath,&ierr);
54
55                         if (ierr)
56                         {
57                                 Scierror(999,_("%s: Error while trying to retrieve the name of the current directory.\n"),fname);
58                                 return 0;
59                         }
60                         else
61                         {
62                                 filespec = strdup(DEFAULT_FILESPEC);
63                                 needtofreefilespec = TRUE;
64                         }
65                 }
66                 break;
67
68         case 1:
69                 {
70                         if (GetType(1) == sci_strings)
71                         {
72                                 GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
73                                 path = cstk(l1);
74                                 filespec = strdup(DEFAULT_FILESPEC);
75                         }
76                         else
77                         {
78                                 Scierror(999,_("%s: Wrong type for input argument #%d: String expected.\n"),fname,1);
79                                 return 0;
80                         }
81
82                 }
83                 break;
84
85         case 2:
86                 {
87                         if ( (GetType(1) == sci_strings) && (GetType(2) == sci_strings) )
88                         {
89                                 GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
90                                 path = cstk(l1);
91
92                                 GetRhsVar(2,STRING_DATATYPE,&m1,&n1,&l1);
93                                 filespec = cstk(l1);
94                                 needtofreefilespec = FALSE;
95                         }
96                         else
97                         {
98                                 Scierror(999,_("%s: Wrong type for input arguments: Strings expected.\n"),fname); 
99                                 return 0;
100                         }
101                 }
102                 break;
103         }
104
105         pathextented = expandPathVariable(path);
106         FilesList = findfiles(pathextented, filespec, &sizeListReturned);
107         if (pathextented) {FREE(pathextented); pathextented = NULL;}
108         if (needtofreefilespec) { if (filespec) FREE(filespec); filespec = NULL;}
109
110         if (FilesList)
111         {
112                 int ncol = 0, nrow = 0;
113
114                 ncol = 1;
115                 nrow = sizeListReturned;
116
117                 CreateVarFromPtr(Rhs+1,MATRIX_OF_STRING_DATATYPE, &nrow, &ncol, FilesList);
118                 LhsVar(1) = Rhs+1;
119         }
120         else
121         {
122                 n1 = 0; m1 = 0; l1 = 0;
123                 CreateVarFromPtr(Rhs+ 1,MATRIX_OF_DOUBLE_DATATYPE,&n1,&m1,&l1);
124                 LhsVar(1) = Rhs+1;
125         }
126
127         freeArrayOfString(FilesList,sizeListReturned);
128
129         C2F(putlhsvar)();
130         return 0;
131 }
132 /*--------------------------------------------------------------------------*/