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