1 /*-----------------------------------------------------------------------------------*/
4 /*-----------------------------------------------------------------------------------*/
7 #include <libxml/xpath.h>
8 #include <libxml/xmlreader.h>
9 #include "getmodules.h"
12 #include "setgetSCIpath.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)
29 if (ScilabModules==NULL)
31 ScilabModules=(struct MODULESLIST *)MALLOC(sizeof(struct MODULESLIST));
36 /*-----------------------------------------------------------------------------------*/
37 BOOL DisposeModulesInfo(void)
43 for (i=0;i<ScilabModules->numberofModules;i++)
45 if (ScilabModules->ModuleList[i])
47 FREE(ScilabModules->ModuleList[i]);
48 ScilabModules->ModuleList[i]=NULL;
51 if (ScilabModules->ModuleList)
53 FREE(ScilabModules->ModuleList);
54 ScilabModules->ModuleList=NULL;
56 ScilabModules->numberofModules=0;
63 /*-----------------------------------------------------------------------------------*/
64 static BOOL ReadModulesFile(void)
67 char *ModulesFilename=NULL;
73 sciprint("The SCI environment variable is not set\n");
77 ModulesFilename=(char*)MALLOC((strlen(SciPath)+strlen("/")+strlen(basenamemodulesfile)+1)*sizeof(char));
78 sprintf(ModulesFilename,"%s/%s",SciPath,basenamemodulesfile);
82 if (FileExist(ModulesFilename))
84 AppendModules(ModulesFilename);
85 FREE(ModulesFilename);
90 sciprint("Cannot load the module declaration file : %s.\n",ModulesFilename);
91 FREE(ModulesFilename);
97 /*-----------------------------------------------------------------------------------*/
98 static BOOL VerifyModule(char *ModuleName)
102 char *FullPathModuleName=NULL;
105 SciPath=getSCIpath();
108 sciprint("The SCI environment variable is not set\n");
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);
117 /* ajouter d'autres tests d'existences */
119 if (FileExist(FullPathModuleName))
123 FREE(FullPathModuleName);
124 FullPathModuleName=NULL;
128 /*-----------------------------------------------------------------------------------*/
129 static BOOL AppendModules(char *xmlfilename)
132 if ( FileExist(xmlfilename) )
134 char *encoding=GetXmlFileEncoding(xmlfilename);
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) )
142 xmlXPathContextPtr xpathCtxt = NULL;
143 xmlXPathObjectPtr xpathObj = NULL;
148 doc = xmlParseFile (xmlfilename);
152 printf("Error: could not parse file %s\n", xmlfilename);
153 if (encoding) {FREE(encoding);encoding=NULL;}
157 xpathCtxt = xmlXPathNewContext(doc);
158 xpathObj = xmlXPathEval((const xmlChar*)"//module_list/module", xpathCtxt);
160 if(xpathObj && xpathObj->nodesetval->nodeMax)
162 /* the Xpath has been understood and there are node */
164 for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)
167 xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->properties;
168 /* Get the properties of <module> */
169 while (attrib != NULL)
171 /* loop until when have read all the attributes */
172 if (xmlStrEqual (attrib->name, (const xmlChar*) "name"))
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));
179 else if (xmlStrEqual (attrib->name, (const xmlChar*) "activate"))
181 /* we found the tag activate */
182 const char *str=(const char*)attrib->children->content;
185 attrib = attrib->next;
188 if ( (name) && (strlen(name) > 0) && (activate) )
190 if ( VerifyModule(name) )
192 if (ScilabModules->ModuleList) ScilabModules->ModuleList=(char**)REALLOC(ScilabModules->ModuleList,sizeof(char*)*(indice+1));
193 else ScilabModules->ModuleList=(char**)MALLOC(sizeof(char*)*(indice+1));
195 ScilabModules->numberofModules=indice+1;
197 ScilabModules->ModuleList[indice]=(char*)MALLOC(sizeof(char)*(strlen(name)+1));
198 sprintf(ScilabModules->ModuleList[indice],"%s",name);
203 sciprint("%s module not found.\n",name);
206 if (name) {FREE(name);name = NULL;}
214 printf("Error : Not a valid module file %s (encoding not 'utf-8') Encoding '%s' found\n", xmlfilename, encoding);
216 if (encoding) {FREE(encoding);encoding=NULL;}
220 /*-----------------------------------------------------------------------------------*/