Massive indent of all codes:
[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 == '\\') )
129             {
130                 lastslash = p2;
131             }
132 #else
133             if (*p2 == '/')
134             {
135                 lastslash = p2;
136             }
137 #endif
138             p2++;
139         }
140
141         if (lastslash != NULL)
142         {
143             strncpy(path, composite, 1 + (int)(lastslash - composite));
144             path[1 + (int)(lastslash - composite)] = '\0';
145             strcpy(fname, lastslash + 1);
146         }
147         else
148         {
149             strcpy(path, "");
150             strcpy(fname, composite);
151         }
152     }
153 }
154 /*--------------------------------------------------------------------------*/
155 static char **addPath(char **dictionary, int sizearray, char *path)
156 {
157     int i = 0;
158     for (i = 0; i < sizearray; i++)
159     {
160         char *newPath = NULL;
161         int newlength = (int)(strlen(dictionary[i]) + strlen(path) + 1);
162         newPath = (char *)MALLOC(sizeof(char) * (newlength));
163         sprintf(newPath, "%s%s", path, dictionary[i]);
164         FREE(dictionary[i]);
165         dictionary[i] = newPath;
166     }
167     return dictionary;
168 }
169 /*--------------------------------------------------------------------------*/
170 static char **addDirSeparator(char **dictionary, int sizearray, char *path)
171 {
172     int i = 0;
173     for (i = 0; i < sizearray; i++)
174     {
175         char *pathextended = NULL;
176         char fullpath[PATH_MAX * 2];
177
178         pathextended = expandPathVariable(path);
179         if (pathextended)
180         {
181             strcpy(fullpath, pathextended);
182             strcat(fullpath, dictionary[i]);
183         }
184         else
185         {
186             strcpy(fullpath, dictionary[i]);
187         }
188
189         if ( isdir(fullpath) && (dictionary[i][strlen(dictionary[i]) - 1] != DIR_SEPARATOR[0]) )
190         {
191             char *newPath = NULL;
192             int newlength = (int)(strlen(dictionary[i]) + strlen(DIR_SEPARATOR) + 1);
193             newPath = (char *)MALLOC(sizeof(char) * (newlength));
194             sprintf(newPath, "%s%s", dictionary[i], DIR_SEPARATOR);
195             FREE(dictionary[i]);
196             dictionary[i] = newPath;
197         }
198
199         if (pathextended)
200         {
201             FREE(pathextended);
202             pathextended = NULL;
203         }
204     }
205     return dictionary;
206 }
207 /*--------------------------------------------------------------------------*/