2e641004109b269fc5cb09d074c8ff5fbfb18338
[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 <libxml/xpath.h>
8 #include <libxml/xmlreader.h>
9 #include "getmodules.h"
10 #include "machine.h"
11 #include "MALLOC.h"
12 #include "setgetSCIpath.h"
13 #include "string.h"
14 #include "sciprint.h"
15 #include "GetXmlFileEncoding.h"
16 /*-----------------------------------------------------------------------------------*/ 
17 #define basenamemodulesfile "etc/modules.xml" 
18 /*-----------------------------------------------------------------------------------*/ 
19 extern BOOL FileExist(char *filename);
20 /*-----------------------------------------------------------------------------------*/ 
21 static struct MODULESLIST *ScilabModules=NULL;
22 /*-----------------------------------------------------------------------------------*/ 
23 static BOOL ReadModulesFile(void);
24 static BOOL AppendModules(char *filename);
25 static BOOL VerifyModule(char *ModuleName);
26 /*-----------------------------------------------------------------------------------*/ 
27 struct MODULESLIST *getmodules(void)
28 {
29         if (ScilabModules==NULL)
30         {
31                 ScilabModules=(struct MODULESLIST *)MALLOC(sizeof(struct MODULESLIST));
32                 ReadModulesFile();
33         }
34         return ScilabModules;
35 }
36 /*-----------------------------------------------------------------------------------*/ 
37 BOOL DisposeModulesInfo(void)
38 {
39         BOOL bOK=FALSE;
40         if (ScilabModules)
41         {
42                 int i=0;
43                 for (i=0;i<ScilabModules->numberofModules;i++)
44                 {
45                         if (ScilabModules->ModuleList[i])
46                         {
47                                 FREE(ScilabModules->ModuleList[i]);
48                                 ScilabModules->ModuleList[i]=NULL;
49                         }
50                 }
51                 if (ScilabModules->ModuleList)
52                 {
53                         FREE(ScilabModules->ModuleList);
54                         ScilabModules->ModuleList=NULL;
55                 }
56                 ScilabModules->numberofModules=0;
57                 FREE(ScilabModules);
58                 ScilabModules=NULL;
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                 AppendModules(ModulesFilename);
85                 FREE(ModulesFilename);
86                 ModulesFilename=NULL;
87         }
88         else
89         {
90                 sciprint("Cannot load the module declaration file : %s.\n",ModulesFilename);
91                 FREE(ModulesFilename);
92                 ModulesFilename=NULL;
93                 return FALSE;
94         }
95         return bOK;
96 }
97 /*-----------------------------------------------------------------------------------*/ 
98 static BOOL VerifyModule(char *ModuleName)
99 {
100         BOOL bOK=FALSE;
101         char *SciPath=NULL;
102         char *FullPathModuleName=NULL;
103         
104
105         SciPath=getSCIpath();
106         if (SciPath==NULL)
107         {
108                 sciprint("The SCI environment variable is not set\n");
109                 return FALSE;
110         }
111
112         FullPathModuleName=(char*)MALLOC((strlen(SciPath)+strlen("%s/modules/%s/etc/%s.start")+(strlen(ModuleName)*2)+1)*sizeof(char));
113         sprintf(FullPathModuleName,"%s/modules/%s/etc/%s.start",SciPath,ModuleName,ModuleName);
114         FREE(SciPath);
115         SciPath=NULL;
116
117         /* ajouter d'autres tests d'existences */
118
119         if (FileExist(FullPathModuleName))
120         {
121                 bOK=TRUE;
122         }
123         FREE(FullPathModuleName);
124         FullPathModuleName=NULL;
125
126         return bOK;
127 }
128 /*-----------------------------------------------------------------------------------*/ 
129 static BOOL AppendModules(char *xmlfilename)
130 {
131         BOOL bOK = FALSE;
132         if ( FileExist(xmlfilename) )
133         {
134                 char *encoding=GetXmlFileEncoding(xmlfilename);
135
136                 /* Don't care about line return / empty line */
137                 xmlKeepBlanksDefault(0);
138                 /* check if the XML file has been encoded with utf8 (unicode) or not */
139                 if ( (strcmp("utf-8", encoding)!=0) || (strcmp("UTF-8", encoding)==0) )
140                 {
141                         xmlDocPtr doc;
142                         xmlXPathContextPtr xpathCtxt = NULL;
143                         xmlXPathObjectPtr xpathObj = NULL;
144                         char *name=NULL;
145                         int activate=0;
146                         int indice=0;
147
148                         doc = xmlParseFile (xmlfilename);
149
150                         if (doc == NULL) 
151                         {
152                                 printf("Error: could not parse file %s\n", xmlfilename);
153                                 if (encoding) {FREE(encoding);encoding=NULL;}
154                                 return bOK;
155                         }
156
157                         xpathCtxt = xmlXPathNewContext(doc);
158                         xpathObj = xmlXPathEval((const xmlChar*)"//module_list/module", xpathCtxt);
159
160                         if(xpathObj && xpathObj->nodesetval->nodeMax) 
161                         {
162                                 /* the Xpath has been understood and there are node */
163                                 int     i;
164                                 for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)
165                                 {
166
167                                         xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->properties;
168                                         /* Get the properties of <module>  */
169                                         while (attrib != NULL)
170                                         {
171                                                 /* loop until when have read all the attributes */
172                                                 if (xmlStrEqual (attrib->name, (const xmlChar*) "name"))
173                                                 { 
174                                                         /* we found the tag name */
175                                                         const char *str=(const char*)attrib->children->content;
176                                                         name=(char*)MALLOC(sizeof(char)*(strlen((const char*)str)+1));
177                                                         strcpy(name,str);
178                                                 }
179                                                 else if (xmlStrEqual (attrib->name, (const xmlChar*) "activate"))
180                                                 { 
181                                                         /* we found the tag activate */
182                                                         const char *str=(const char*)attrib->children->content;
183                                                         activate=atoi(str);
184                                                 }
185                                                 attrib = attrib->next;
186                                         }
187
188                                         if ( (name) && (strlen(name) > 0) && (activate) )
189                                         {
190                                                 if ( VerifyModule(name) )
191                                                 {
192                                                         if (ScilabModules->ModuleList) ScilabModules->ModuleList=(char**)REALLOC(ScilabModules->ModuleList,sizeof(char*)*(indice+1)); 
193                                                         else ScilabModules->ModuleList=(char**)MALLOC(sizeof(char*)*(indice+1)); 
194
195                                                         ScilabModules->numberofModules=indice+1;
196                                                         
197                                                         ScilabModules->ModuleList[indice]=(char*)MALLOC(sizeof(char)*(strlen(name)+1));
198                                                         sprintf(ScilabModules->ModuleList[indice],"%s",name);
199                                                         indice++;
200                                                 }
201                                                 else
202                                                 {
203                                                         sciprint("%s module not found.\n",name);
204                                                 }
205                                         }
206                                         if (name) {FREE(name);name = NULL;}
207                                         activate = 0;
208                                 }
209                                 bOK = TRUE;
210                         }
211                 }
212                 else
213                 {
214                         printf("Error : Not a valid module file %s (encoding not 'utf-8') Encoding '%s' found\n", xmlfilename, encoding);
215                 }
216                 if (encoding) {FREE(encoding);encoding=NULL;}
217         }
218         return bOK;
219 }
220 /*-----------------------------------------------------------------------------------*/