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