Localization (fileio)
[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 "gw_fileio.h"
15 #include "findfiles.h"
16 #include "stack-c.h"
17 #include "MALLOC.h"
18 #include "localization.h"
19 #include "../../../core/src/c/scicurdir.h" /* C2F(scigetcwd) */
20 #include "Scierror.h"
21 #include "cluni0.h"
22 /*--------------------------------------------------------------------------*/
23 #define DEFAULT_FILESPEC "*.*"
24 /*--------------------------------------------------------------------------*/
25 int C2F(sci_findfiles)(char *fname,unsigned long fname_len)
26 {
27         static int l1,n1,m1;
28         char pathextented[PATH_MAX];
29         int out_n = 0;
30         char *path=NULL;
31         char *filespec=NULL;
32         char **FilesList=NULL;
33         int sizeListReturned=0;
34         BOOL needtofreefilespec = FALSE;
35         BOOL needtofreepath = FALSE;
36
37         Rhs=Max(Rhs,0);
38         CheckRhs(0,2) ;
39         CheckLhs(0,1) ;
40
41         switch(Rhs)
42         {
43         default: case 0:
44                 {
45                         int ierr=0;
46                         int lpath=0;
47
48                         C2F(scigetcwd)(&path,&lpath,&ierr);
49                         needtofreepath = TRUE;
50
51                         if (ierr)
52                         {
53                                 Scierror(999,_("%s: Error while trying to retrieve the name of the current directory.\n"),fname);
54                                 return 0;
55                         }
56                         else
57                         {
58                                 filespec=(char *)MALLOC(sizeof(char)*(strlen(DEFAULT_FILESPEC)+1));
59                                 strcpy(filespec,DEFAULT_FILESPEC);
60                                 needtofreefilespec = TRUE;
61                         }
62                 }
63                 break;
64
65         case 1:
66                 {
67                         if (GetType(1) == sci_strings)
68                         {
69                                 GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
70                                 path=cstk(l1);
71                                 needtofreepath = FALSE;
72
73                                 filespec=(char *)MALLOC(sizeof(char)*(strlen(DEFAULT_FILESPEC)+1));
74                                 strcpy(filespec,DEFAULT_FILESPEC);
75                                 needtofreefilespec = TRUE;
76                         }
77                         else
78                         {
79                                 Scierror(999,_("%s: Wrong type for input argument #%d: String expected.\n"),fname,1);
80                                 return 0;
81                         }
82
83                 }
84                 break;
85
86         case 2:
87                 {
88                         if ( (GetType(1) == sci_strings) && (GetType(2) == sci_strings) )
89                         {
90                                 GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
91                                 path=cstk(l1);
92                                 needtofreepath = FALSE;
93
94                                 GetRhsVar(2,STRING_DATATYPE,&m1,&n1,&l1);
95                                 filespec=cstk(l1);
96                                 needtofreefilespec = FALSE;
97                         }
98                         else
99                         {
100                                 Scierror(999,_("%s: Wrong type for input arguments: Strings expected.\n"),fname); 
101                                 return 0;
102                         }
103                 }
104                 break;
105         }
106
107         C2F(cluni0)(path,pathextented,&out_n,(long)strlen(path),PATH_MAX);
108         FilesList=findfiles(pathextented,filespec,&sizeListReturned);
109         if ( (filespec) && needtofreefilespec ) {FREE(filespec);filespec = NULL;}
110         if ( (path) && needtofreepath ) {FREE(path);path=NULL;}
111
112         if (FilesList)
113         {
114                 int ncol,nrow;
115
116                 ncol=1;
117                 nrow=sizeListReturned;
118
119                 CreateVarFromPtr(Rhs+1,MATRIX_OF_STRING_DATATYPE, &nrow, &ncol, FilesList);
120                 LhsVar(1) = Rhs+1;
121         }
122         else
123         {
124                 n1=0;m1=0;l1=0;
125                 CreateVarFromPtr(Rhs+ 1,MATRIX_OF_DOUBLE_DATATYPE,&n1,&m1,&l1);
126                 LhsVar(1) = Rhs+1;
127         }
128
129         if (FilesList)
130         {
131                 int i=0;
132                 for (i=0;i<sizeListReturned;i++)
133                 {
134                         if (FilesList[i])
135                         {
136                                 FREE(FilesList[i]);
137                                 FilesList[i]=NULL;
138                         }
139                 }
140                 FREE(FilesList);
141                 FilesList=NULL;
142         }
143
144         C2F(putlhsvar)();
145         return 0;
146 }
147 /*--------------------------------------------------------------------------*/