76c5a31f6f1e3fab26303fe55d4de0cf1c243b72
[scilab.git] / scilab / modules / core / src / c / getmodules.c
1 /*-----------------------------------------------------------------------------------*/
2 /* INRIA 2006 */
3 /* Allan CORNET */
4 /*-----------------------------------------------------------------------------------*/ 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "getmodules.h"
8 #include "machine.h"
9 #include "MALLOC.h"
10 #include "setgetSCIpath.h"
11 #include "string.h"
12 #include "sciprint.h"
13 /*-----------------------------------------------------------------------------------*/ 
14 #define basenamemodulesfile "modules/modules" 
15 /*-----------------------------------------------------------------------------------*/ 
16 extern BOOL FileExist(char *filename);
17 /*-----------------------------------------------------------------------------------*/ 
18 static struct MODULESLIST *ScilabModules=NULL;
19 /*-----------------------------------------------------------------------------------*/ 
20 static BOOL ReadModulesFile(void);
21 static int GetNumberOfLinesInFile(char *filename);
22 static BOOL AppendModules(char *filename);
23 static void cleaningLine(char *source);
24 static BOOL VerifyModule(char *ModuleName);
25 /*-----------------------------------------------------------------------------------*/ 
26 struct MODULESLIST *getmodules(void)
27 {
28         if (ScilabModules==NULL)
29         {
30                 ScilabModules=(struct MODULESLIST *)MALLOC(sizeof(struct MODULESLIST));
31                 ReadModulesFile();
32         }
33         return ScilabModules;
34 }
35 /*-----------------------------------------------------------------------------------*/ 
36 BOOL DisposeModulesInfo(void)
37 {
38         BOOL bOK=FALSE;
39         if (ScilabModules)
40         {
41                 int i=0;
42                 for (i=0;i<ScilabModules->numberofModules;i++)
43                 {
44                         if (ScilabModules->ModuleList[i])
45                         {
46                                 FREE(ScilabModules->ModuleList[i]);
47                                 ScilabModules->ModuleList[i]=NULL;
48                         }
49                 }
50                 if (ScilabModules->ModuleList)
51                 {
52                         FREE(ScilabModules->ModuleList);
53                         ScilabModules->ModuleList=NULL;
54                 }
55                 ScilabModules->numberofModules=0;
56                 FREE(ScilabModules);
57                 ScilabModules=NULL;
58
59         }
60
61         return bOK;
62 }
63 /*-----------------------------------------------------------------------------------*/ 
64 static BOOL ReadModulesFile(void)
65 {
66         BOOL bOK=FALSE;
67         char *ModulesFilename=NULL;
68         char *SciPath=NULL;
69
70         SciPath=getSCIpath();
71         if (SciPath==NULL)
72         {
73                 sciprint("The SCI environment variable is not set\n");
74                 return FALSE;
75         }
76
77         ModulesFilename=(char*)MALLOC((strlen(SciPath)+strlen("/")+strlen(basenamemodulesfile)+1)*sizeof(char));
78         sprintf(ModulesFilename,"%s/%s",SciPath,basenamemodulesfile);
79         FREE(SciPath);
80         SciPath=NULL;
81         
82         if (FileExist(ModulesFilename))
83         {
84                 int NumberofLines=GetNumberOfLinesInFile(ModulesFilename);
85                 ScilabModules->ModuleList=(char**)MALLOC(sizeof(char*)*NumberofLines);
86                 ScilabModules->numberofModules=NumberofLines;
87                 AppendModules(ModulesFilename);
88                 FREE(ModulesFilename);
89                 ModulesFilename=NULL;
90         }
91         else
92         {
93                 sciprint("Cannot load the module declaration file : %s.\n",ModulesFilename);
94                 FREE(ModulesFilename);
95                 ModulesFilename=NULL;
96                 return FALSE;
97         }
98         return bOK;
99 }
100 /*-----------------------------------------------------------------------------------*/ 
101 static int GetNumberOfLinesInFile(char *filename)
102 {
103         #define LineMaxSize 1024
104         int ret=0;
105         FILE *pFile;
106
107         if (FileExist(filename))
108         {
109                 char Line[LineMaxSize];
110                 pFile=fopen(filename,"rt");
111                 while (fgets(Line, LineMaxSize,pFile))
112                 {
113                         if ( VerifyModule(Line) )
114                         {
115                                 ret++;
116                         }
117                 }
118                 fclose(pFile);
119         }
120
121         return ret;
122 }
123 /*-----------------------------------------------------------------------------------*/ 
124 static BOOL AppendModules(char *filename)
125 {
126         #define LineMaxSize 1024
127         BOOL bOK=FALSE;
128         char Line[LineMaxSize];
129         int i=0;
130         FILE *pFile;
131
132         pFile=fopen(filename,"rt");
133         while (fgets(Line, LineMaxSize,pFile))
134         {
135                 if ( VerifyModule(Line) )
136                 {
137                         ScilabModules->ModuleList[i]=(char*)MALLOC(sizeof(char)*(strlen(Line)+1));
138                         sprintf(ScilabModules->ModuleList[i],"%s",Line);
139                         i++;
140                 }
141                 else
142                 {
143                         if (Line[0]!=';') /* Starts with a ; => it is a comment */
144                         {
145                                 sciprint("%s module not found.\n",Line);
146                         }
147                 }
148         }
149         fclose(pFile);
150         return bOK;
151 }
152 /*-----------------------------------------------------------------------------------*/ 
153 static void cleaningLine(char *source)
154 {
155         int i=0;
156         for (i=0;i<(int)strlen(source);i++)
157         {
158                 if ( (source[i]==' ') || (source[i]=='\n') || (source[i]=='\r') )
159                 {
160                         source[i]='\0';
161                         break;
162                 }
163         }
164 }
165 /*-----------------------------------------------------------------------------------*/ 
166 static BOOL VerifyModule(char *ModuleName)
167 {
168         BOOL bOK=FALSE;
169         char *SciPath=NULL;
170         char *FullPathModuleName=NULL;
171
172         cleaningLine(ModuleName);
173
174         /* ';' is a comment into the declaration file (modules/modules) */
175         if (ModuleName[0]==';') return FALSE;
176
177         SciPath=getSCIpath();
178         if (SciPath==NULL)
179         {
180                 sciprint("The SCI environment variable is not set\n");
181                 return FALSE;
182         }
183
184         FullPathModuleName=(char*)MALLOC((strlen(SciPath)+strlen("%s/modules/%s/etc/%s.start")+(strlen(ModuleName)*2)+1)*sizeof(char));
185         sprintf(FullPathModuleName,"%s/modules/%s/etc/%s.start",SciPath,ModuleName,ModuleName);
186         FREE(SciPath);
187         SciPath=NULL;
188
189         /* ajouter d'autres tests d'existences */
190
191         if (FileExist(FullPathModuleName))
192         {
193                 bOK=TRUE;
194         }
195         FREE(FullPathModuleName);
196         FullPathModuleName=NULL;
197
198         return bOK;
199 }
200 /*-----------------------------------------------------------------------------------*/