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