* Bug 16365 fixed: median(m,'r'|'c') was wrong after 5dc990
[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  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 #include <string.h> /* strcmp */
16 #include <stdio.h> /* sprintf */
17 #include <stdlib.h> /* qsort */
18 #include "getfilesdictionary.h"
19 #include "PATH_MAX.h"
20 #include "scicurdir.h" /* scigetcwd */
21 #include "findfiles.h" /* findfiles */
22 #include "sci_malloc.h"
23 #include "expandPathVariable.h"
24 #include "machine.h"
25 #include "isdir.h"
26 /*--------------------------------------------------------------------------*/
27 static void mysplitpath(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
47         char pathname[PATH_MAX];
48         char filename[PATH_MAX];
49
50         char *pathextended = NULL;
51
52
53         mysplitpath(somechars, pathname, filename);
54
55         if ( strcmp(pathname, "") == 0 )
56         {
57             /* current path */
58             int ierr = 0;
59             char *currentpath = scigetcwd(&ierr);
60             if (currentpath)
61             {
62                 strcpy(path, currentpath);
63                 strcat(path, DIR_SEPARATOR);
64                 FREE(currentpath);
65                 currentpath = NULL;
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         pathextended = expandPathVariable(path);
86
87         if (pathextended)
88         {
89             dictionary = findfiles(pathextended, filespec, &sizeListReturned, FALSE);
90             FREE(pathextended);
91             pathextended = NULL;
92         }
93
94         if (fullpath)
95         {
96             dictionary = addPath(dictionary, sizeListReturned, path);
97         }
98
99         dictionary = addDirSeparator(dictionary, sizeListReturned, path);
100
101         *sizearray = sizeListReturned;
102
103         /* Add a NULL element at the end (to get number of items from JNI) */
104         if (dictionary != NULL)
105         {
106             dictionary = (char**)REALLOC(dictionary, sizeof(char*) * (sizeListReturned + 1));
107             dictionary[sizeListReturned] = NULL;
108             qsort(dictionary, *sizearray, sizeof dictionary[0], cmpfiles);
109         }
110     }
111     else
112     {
113         *sizearray = 0;
114     }
115     return dictionary;
116 }
117 /*--------------------------------------------------------------------------*/
118 static void mysplitpath(char *composite,  char *path,  char *fname)
119 {
120     if (composite && path && fname)
121     {
122         char *lastslash = NULL;
123         char *p2 = NULL;
124
125         lastslash = NULL;
126         p2 = composite;
127
128         while (*p2)
129         {
130 #ifdef _MSC_VER
131             if ( (*p2 == '/') || (*p2 == '\\') )
132             {
133                 lastslash = p2;
134             }
135 #else
136             if (*p2 == '/')
137             {
138                 lastslash = p2;
139             }
140 #endif
141             p2++;
142         }
143
144         if (lastslash != NULL)
145         {
146             strncpy(path, composite, 1 + (int)(lastslash - composite));
147             path[1 + (int)(lastslash - composite)] = '\0';
148             strcpy(fname, lastslash + 1);
149         }
150         else
151         {
152             strcpy(path, "");
153             strcpy(fname, composite);
154         }
155     }
156 }
157 /*--------------------------------------------------------------------------*/
158 static char **addPath(char **dictionary, int sizearray, char *path)
159 {
160     int i = 0;
161     for (i = 0; i < sizearray; i++)
162     {
163         char *newPath = NULL;
164         int newlength = (int)(strlen(dictionary[i]) + strlen(path) + 1);
165         newPath = (char *)MALLOC(sizeof(char) * (newlength));
166         sprintf(newPath, "%s%s", path, dictionary[i]);
167         FREE(dictionary[i]);
168         dictionary[i] = newPath;
169     }
170     return dictionary;
171 }
172 /*--------------------------------------------------------------------------*/
173 static char **addDirSeparator(char **dictionary, int sizearray, char *path)
174 {
175     int i = 0;
176     for (i = 0; i < sizearray; i++)
177     {
178         char *pathextended = NULL;
179         char fullpath[PATH_MAX * 2];
180
181         pathextended = expandPathVariable(path);
182         if (pathextended)
183         {
184             strcpy(fullpath, pathextended);
185             strcat(fullpath, dictionary[i]);
186         }
187         else
188         {
189             strcpy(fullpath, dictionary[i]);
190         }
191
192         if ( isdir(fullpath) && (dictionary[i][strlen(dictionary[i]) - 1] != DIR_SEPARATOR[0]) )
193         {
194             char *newPath = NULL;
195             int newlength = (int)(strlen(dictionary[i]) + strlen(DIR_SEPARATOR) + 1);
196             newPath = (char *)MALLOC(sizeof(char) * (newlength));
197             sprintf(newPath, "%s%s", dictionary[i], DIR_SEPARATOR);
198             FREE(dictionary[i]);
199             dictionary[i] = newPath;
200         }
201
202         if (pathextended)
203         {
204             FREE(pathextended);
205             pathextended = NULL;
206         }
207     }
208     return dictionary;
209 }
210 /*--------------------------------------------------------------------------*/