warnings x64
[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 "getfilesdictionary.h"
15 #include "PATH_MAX.h"
16 #include "../../../core/src/c/scicurdir.h" /* scigetcwd */
17 #include "findfiles.h" /* findfiles */
18 #include "MALLOC.h"
19 #include "cluni0.h"
20 #include "machine.h"
21 /*--------------------------------------------------------------------------*/ 
22 static void splitpath(char *composite,  char *path,  char *fname);
23 static char **addPath(char **dictionary, int sizearray, char *path);
24 /*--------------------------------------------------------------------------*/ 
25 char **getfilesdictionary(char *somechars,int *sizearray,BOOL fullpath)
26 {
27         char **dictionary = NULL;
28
29         if (somechars)
30         {
31                 int sizeListReturned = 0;
32                 char path[PATH_MAX];
33                 
34                 char filespec[PATH_MAX];
35
36                 char pathname[PATH_MAX];
37                 char filename[PATH_MAX];
38
39                 char pathextended[PATH_MAX];
40                 int out_n = 0;
41
42                 splitpath(somechars,pathname,filename);
43
44                 if ( strcmp(pathname,"")==0 )
45                 {
46                         /* current path */
47                         int ierr = 0;
48                         int lpath = 0;
49                         char *currentpath = NULL;
50
51                         scigetcwd(&currentpath,&lpath,&ierr);
52                         if (currentpath) 
53                         {
54                                 strcpy(path, currentpath);
55                                 strcat(path, DIR_SEPARATOR);
56                         }
57                 }
58                 else
59                 {
60                         /* we have a path */
61                         strcpy(path,pathname);
62                 }
63
64                 if ( strcmp(filename,"")==0 )
65                 {
66                         /* no filename */
67                         strcpy(filespec,"*");
68                 }
69                 else
70                 {
71                         /* we have the beginning of a filename */
72                         sprintf(filespec,"%s*",filename);
73                 }
74
75                 C2F(cluni0)(path,pathextended,&out_n,(long)strlen(path),PATH_MAX);
76                 dictionary = findfiles(pathextended, filespec, &sizeListReturned);
77                 if (fullpath)
78                 {
79                         dictionary = addPath(dictionary, sizeListReturned, path);
80                 }
81                 
82                 *sizearray = sizeListReturned;
83
84         /* Add a NULL element at the end (to get number of items from JNI) */
85         if (dictionary != NULL)
86         {
87                         dictionary = (char**)REALLOC(dictionary,sizeof(char*)*(sizeListReturned+1));
88                         dictionary[sizeListReturned] = NULL;
89                 }
90         }
91         else
92         {
93                 *sizearray = 0;
94         }
95         return dictionary;
96 }
97 /*--------------------------------------------------------------------------*/ 
98 static void splitpath(char *composite,  char *path,  char *fname)
99 {
100         if (composite && path && fname)
101         {
102                 char *lastslash = NULL;
103                 char *p2 = NULL;
104
105                 lastslash = NULL;
106                 p2 = composite;
107
108                 while (*p2)  
109                 {
110                 #ifdef _MSC_VER
111                         if ( (*p2 == '/') || (*p2 == '\\') ) lastslash = p2;
112                 #else
113                         if (*p2 == '/') lastslash = p2;
114                 #endif
115                         p2++;
116                 }
117
118                 if (lastslash != NULL)  
119                 {
120                         strncpy(path, composite, 1 + (int)(lastslash - composite));
121                         path[1 + (int)(lastslash - composite)]='\0';
122                         strcpy(fname, lastslash+1);
123                 }
124                 else 
125                 {
126                         strcpy(path, "");
127                         strcpy(fname, composite);
128                 }
129         }
130 }
131 /*--------------------------------------------------------------------------*/
132 static char **addPath(char **dictionary, int sizearray, char *path)
133 {
134         int i = 0;
135         for (i = 0;i < sizearray;i++)
136         {
137                 char *newPath = NULL;
138                 int newlength = (int)(strlen(dictionary[i]) + strlen(path) + 1);
139                 newPath = (char *)MALLOC(sizeof(char)*(newlength));
140                 sprintf(newPath,"%s%s",path,dictionary[i]);
141                 FREE(dictionary[i]);
142                 dictionary[i] = newPath;
143                 
144         }
145         return dictionary;
146 }
147 /*--------------------------------------------------------------------------*/