cd & chdir factorized
[scilab.git] / scilab / modules / completion / src / c / getfilesdictionary.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Allan CORNET
4  * 
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at    
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 #include <string.h> /* strcmp */
13 #include <stdio.h> /* sprintf */
14 #include <stdlib.h> /* qsort */
15 #include "stack-def.h"
16 #include "getfilesdictionary.h"
17 #include "PATH_MAX.h"
18 #include "scicurdir.h" /* scigetcwd */
19 #include "findfiles.h" /* findfiles */
20 #include "MALLOC.h"
21 #include "expandPathVariable.h"
22 #include "machine.h"
23 #include "isdir.h"
24 #include "stack-def.h"
25 /*--------------------------------------------------------------------------*/ 
26 static void splitpath(char *composite,  char *path,  char *fname);
27 static char **addPath(char **dictionary, int sizearray, char *path);
28 static char **addDirSeparator(char **dictionary, int sizearray, char *path);
29 /*--------------------------------------------------------------------------*/ 
30 static int cmpfiles( const void *a ,const void *b)
31 {
32         return strcmp(*(const char **)a, *(const char **)b );
33 }
34 /*--------------------------------------------------------------------------*/ 
35 char **getfilesdictionary(char *somechars,int *sizearray,BOOL fullpath)
36 {
37         char **dictionary = NULL;
38
39         if (somechars)
40         {
41                 int sizeListReturned = 0;
42                 char path[PATH_MAX];
43                 
44                 char filespec[PATH_MAX];
45
46                 char pathname[PATH_MAX];
47                 char filename[PATH_MAX];
48
49                 char *pathextended = NULL;
50                 int out_n = 0;
51
52                 splitpath(somechars,pathname,filename);
53
54                 if ( strcmp(pathname,"")==0 )
55                 {
56                         /* current path */
57                         int ierr = 0;
58                         char *currentpath = scigetcwd(&ierr);
59                         if (currentpath) 
60                         {
61                                 strcpy(path, currentpath);
62                                 strcat(path, DIR_SEPARATOR);
63                                 FREE(currentpath);
64                                 currentpath = NULL;
65                         }
66                 }
67                 else
68                 {
69                         /* we have a path */
70                         strcpy(path,pathname);
71                 }
72
73                 if ( strcmp(filename,"")==0 )
74                 {
75                         /* no filename */
76                         strcpy(filespec,"*");
77                 }
78                 else
79                 {
80                         /* we have the beginning of a filename */
81                         sprintf(filespec,"%s*",filename);
82                 }
83
84                 pathextended = expandPathVariable(path);
85                 
86                 if (pathextended)
87                 {
88                         dictionary = findfiles(pathextended, filespec, &sizeListReturned);
89                         FREE(pathextended);
90                         pathextended = NULL;
91                 }
92
93                 if (fullpath)
94                 {
95                         dictionary = addPath(dictionary, sizeListReturned, path);
96                 }
97
98                 dictionary = addDirSeparator(dictionary, sizeListReturned, path);
99
100                 *sizearray = sizeListReturned;
101
102         /* Add a NULL element at the end (to get number of items from JNI) */
103         if (dictionary != NULL)
104         {
105                         dictionary = (char**)REALLOC(dictionary,sizeof(char*)*(sizeListReturned+1));
106                         dictionary[sizeListReturned] = NULL;
107                         qsort(dictionary, sizeof dictionary / sizeof dictionary[0], sizeof dictionary[0], cmpfiles);
108                 }
109         }
110         else
111         {
112                 *sizearray = 0;
113         }
114         return dictionary;
115 }
116 /*--------------------------------------------------------------------------*/ 
117 static void splitpath(char *composite,  char *path,  char *fname)
118 {
119         if (composite && path && fname)
120         {
121                 char *lastslash = NULL;
122                 char *p2 = NULL;
123
124                 lastslash = NULL;
125                 p2 = composite;
126
127                 while (*p2)  
128                 {
129                 #ifdef _MSC_VER
130                         if ( (*p2 == '/') || (*p2 == '\\') ) lastslash = p2;
131                 #else
132                         if (*p2 == '/') lastslash = p2;
133                 #endif
134                         p2++;
135                 }
136
137                 if (lastslash != NULL)  
138                 {
139                         strncpy(path, composite, 1 + (int)(lastslash - composite));
140                         path[1 + (int)(lastslash - composite)]='\0';
141                         strcpy(fname, lastslash+1);
142                 }
143                 else 
144                 {
145                         strcpy(path, "");
146                         strcpy(fname, composite);
147                 }
148         }
149 }
150 /*--------------------------------------------------------------------------*/
151 static char **addPath(char **dictionary, int sizearray, char *path)
152 {
153         int i = 0;
154         for (i = 0;i < sizearray;i++)
155         {
156                 char *newPath = NULL;
157                 int newlength = (int)(strlen(dictionary[i]) + strlen(path) + 1);
158                 newPath = (char *)MALLOC(sizeof(char)*(newlength));
159                 sprintf(newPath,"%s%s",path,dictionary[i]);
160                 FREE(dictionary[i]);
161                 dictionary[i] = newPath;
162         }
163         return dictionary;
164 }
165 /*--------------------------------------------------------------------------*/
166 static char **addDirSeparator(char **dictionary, int sizearray, char *path)
167 {
168         int i = 0;
169         for (i = 0;i < sizearray;i++)
170         {
171                 char *pathextended = NULL;
172                 char fullpath[PATH_MAX * 2];
173
174                 pathextended = expandPathVariable(path);
175                 if (pathextended)
176                 {
177                         strcpy(fullpath, pathextended);
178                         strcat(fullpath, dictionary[i]);
179                 }
180                 else
181                 {
182                         strcpy(fullpath, dictionary[i]);
183                 }
184
185                 if ( isdir(fullpath) && (dictionary[i][strlen(dictionary[i])-1] != DIR_SEPARATOR[0]) )
186                 {
187                         char *newPath = NULL;
188                         int newlength = (int)(strlen(dictionary[i]) + strlen(DIR_SEPARATOR) + 1);
189                         newPath = (char *)MALLOC(sizeof(char)*(newlength));
190                         sprintf(newPath,"%s%s",dictionary[i],DIR_SEPARATOR);
191                         FREE(dictionary[i]);
192                         dictionary[i] = newPath;
193                 }
194         }
195         return dictionary;
196 }
197 /*--------------------------------------------------------------------------*/