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