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