bug 3884
[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 "getfilesdictionary.h"
16 #include "PATH_MAX.h"
17 #include "scicurdir.h" /* scigetcwd */
18 #include "findfiles.h" /* findfiles */
19 #include "MALLOC.h"
20 #include "cluni0.h"
21 #include "machine.h"
22 #include "isdir.h"
23 #include "charEncoding.h"
24 /*--------------------------------------------------------------------------*/ 
25 static void splitpath(char *composite,  char *path,  char *fname);
26 static char **addPath(char **dictionary, int sizearray, char *path);
27 static char **addDirSeparator(char **dictionary, int sizearray, char *path);
28 /*--------------------------------------------------------------------------*/ 
29 static int cmpfiles( const void *a ,const void *b)
30 {
31         return strcmp(*(const char **)a, *(const char **)b );
32 }
33 /*--------------------------------------------------------------------------*/ 
34 char **getfilesdictionary(char *somechars,int *sizearray,BOOL fullpath)
35 {
36         char **dictionary = NULL;
37
38         if (somechars)
39         {
40                 int sizeListReturned = 0;
41                 char path[PATH_MAX];
42                 
43                 char filespec[PATH_MAX];
44
45                 char pathname[PATH_MAX];
46                 char filename[PATH_MAX];
47
48                 char pathextended[PATH_MAX];
49                 int out_n = 0;
50
51                 splitpath(somechars,pathname,filename);
52
53                 if ( strcmp(pathname,"")==0 )
54                 {
55                         /* current path */
56                         int ierr = 0;
57                         int lpath = 0;
58                         char *currentpath = NULL;
59
60                         scigetcwd(&currentpath,&lpath,&ierr);
61                         if (currentpath) 
62                         {
63                                 strcpy(path, currentpath);
64                                 strcat(path, DIR_SEPARATOR);
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                 C2F(cluni0)(path,pathextended,&out_n,(long)strlen(path),PATH_MAX);
85                 dictionary = findfiles(pathextended, UTFToLocale(filespec), &sizeListReturned);
86                 if (fullpath)
87                 {
88                         dictionary = addPath(dictionary, sizeListReturned, path);
89                 }
90
91                 dictionary = addDirSeparator(dictionary, sizeListReturned, path);
92
93                 *sizearray = sizeListReturned;
94
95         /* Add a NULL element at the end (to get number of items from JNI) */
96         if (dictionary != NULL)
97         {
98                         dictionary = (char**)REALLOC(dictionary,sizeof(char*)*(sizeListReturned+1));
99                         dictionary[sizeListReturned] = NULL;
100                         qsort(dictionary, sizeof dictionary / sizeof dictionary[0], sizeof dictionary[0], cmpfiles);
101                 }
102         }
103         else
104         {
105                 *sizearray = 0;
106         }
107         return dictionary;
108 }
109 /*--------------------------------------------------------------------------*/ 
110 static void splitpath(char *composite,  char *path,  char *fname)
111 {
112         if (composite && path && fname)
113         {
114                 char *lastslash = NULL;
115                 char *p2 = NULL;
116
117                 lastslash = NULL;
118                 p2 = composite;
119
120                 while (*p2)  
121                 {
122                 #ifdef _MSC_VER
123                         if ( (*p2 == '/') || (*p2 == '\\') ) lastslash = p2;
124                 #else
125                         if (*p2 == '/') lastslash = p2;
126                 #endif
127                         p2++;
128                 }
129
130                 if (lastslash != NULL)  
131                 {
132                         strncpy(path, composite, 1 + (int)(lastslash - composite));
133                         path[1 + (int)(lastslash - composite)]='\0';
134                         strcpy(fname, lastslash+1);
135                 }
136                 else 
137                 {
138                         strcpy(path, "");
139                         strcpy(fname, composite);
140                 }
141         }
142 }
143 /*--------------------------------------------------------------------------*/
144 static char **addPath(char **dictionary, int sizearray, char *path)
145 {
146         int i = 0;
147         for (i = 0;i < sizearray;i++)
148         {
149                 char *newPath = NULL;
150                 int newlength = (int)(strlen(dictionary[i]) + strlen(path) + 1);
151                 newPath = (char *)MALLOC(sizeof(char)*(newlength));
152                 sprintf(newPath,"%s%s",path,dictionary[i]);
153                 FREE(dictionary[i]);
154                 dictionary[i] = newPath;
155         }
156         return dictionary;
157 }
158 /*--------------------------------------------------------------------------*/
159 static char **addDirSeparator(char **dictionary, int sizearray, char *path)
160 {
161         int i = 0;
162         for (i = 0;i < sizearray;i++)
163         {
164                 int out_n = 0;
165
166                 char pathextended[PATH_MAX];
167                 char fullpath[PATH_MAX * 2];
168
169                 C2F(cluni0)(path, pathextended, &out_n,( long)strlen(path), PATH_MAX);
170
171                 strcpy(fullpath, pathextended);
172                 strcat(fullpath, dictionary[i]);
173
174                 if ( isdir(fullpath) && (dictionary[i][strlen(dictionary[i])-1] != DIR_SEPARATOR[0]) )
175                 {
176                         char *newPath = NULL;
177                         int newlength = (int)(strlen(dictionary[i]) + strlen(DIR_SEPARATOR) + 1);
178                         newPath = (char *)MALLOC(sizeof(char)*(newlength));
179                         sprintf(newPath,"%s%s",dictionary[i],DIR_SEPARATOR);
180                         FREE(dictionary[i]);
181                         dictionary[i] = newPath;
182                 }
183         }
184         return dictionary;
185 }
186 /*--------------------------------------------------------------------------*/