Scicos tests: fix scicos.tst to load Scicos libs correctly
[scilab.git] / scilab / modules / core / src / c / readGateway.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008 - 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.1-en.txt
10  *
11  */
12
13 /*--------------------------------------------------------------------------*/
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <libxml/xpath.h>
18 #include <libxml/xmlreader.h>
19 #include "readGateway.h"
20 #include "sci_path.h"
21 #include "scilabDefaults.h"
22 #include "FileExist.h"
23 #include "with_module.h"
24 #include "sci_malloc.h"
25 #include "GetXmlFileEncoding.h"
26 #include "localization.h"
27 #include "os_string.h"
28 #include "getshortpathname.h"
29 #include "BOOL.h"
30 /*--------------------------------------------------------------------------*/
31 static char *getModuleXmlFilename(char *modulename);
32 static struct gateway_struct *readGatewayXmlFile(char *filenameXml);
33 /*--------------------------------------------------------------------------*/
34 struct gateway_struct *readGateway(char *modulename)
35 {
36     struct gateway_struct *content = NULL;
37
38     //if (modulename)
39     //{
40     //  char *XmlFile = getModuleXmlFilename(modulename);
41     //  if (XmlFile)
42     //  {
43     //          content = readGatewayXmlFile(XmlFile);
44     //          FREE(XmlFile);
45     //          XmlFile = NULL;
46     //  }
47     //  else
48     //  {
49     //          /* module exists but without gateway */
50     //          if ( with_module(modulename) )
51     //          {
52     //                  content = (struct gateway_struct *)MALLOC(sizeof(struct gateway_struct));
53     //                  if (content)
54     //                  {
55     //                          content->dimLists = 0;
56     //                          content->gatewayIdList = NULL;
57     //                          content->primitivesList = NULL;
58     //                          content->primiviteIdList = NULL;
59     //                  }
60     //          }
61     //          else /* not exist */
62     //          {
63     //                  content = NULL;
64     //          }
65     //  }
66     //}
67
68     return content;
69 }
70 /*--------------------------------------------------------------------------*/
71 static struct gateway_struct *readGatewayXmlFile(char *filenameXml)
72 {
73     struct gateway_struct *gateway = NULL;
74
75     char *encoding = GetXmlFileEncoding(filenameXml);
76
77     /* Don't care about line return / empty line */
78     xmlKeepBlanksDefault(0);
79
80     /* check if the XML file has been encoded with utf8 (unicode) or not */
81     if ( (strcmp("utf-8", encoding) != 0) || (strcmp("UTF-8", encoding) == 0) )
82     {
83         xmlDocPtr doc = NULL;
84         xmlXPathContextPtr xpathCtxt = NULL;
85         xmlXPathObjectPtr xpathObj = NULL;
86
87         int GATEWAY_ID = 0;
88         int PRIMITIVE_ID = 0;
89         char *PRIMITIVE_NAME = NULL;
90
91         {
92             BOOL bConvert = FALSE;
93             char *shortfilenameXml = getshortpathname(filenameXml, &bConvert);
94             if (shortfilenameXml)
95             {
96                 doc = xmlParseFile (shortfilenameXml);
97                 FREE(shortfilenameXml);
98                 shortfilenameXml = NULL;
99             }
100         }
101
102         if (doc == NULL)
103         {
104             fprintf(stderr, _("Error: could not parse file %s\n"), filenameXml);
105             if (encoding)
106             {
107                 FREE(encoding);
108                 encoding = NULL;
109             }
110             return NULL;
111         }
112
113         gateway = (struct gateway_struct *)MALLOC(sizeof(struct gateway_struct));
114         if (gateway == NULL)
115         {
116             fprintf(stderr, _("Error: Memory allocation.\n"));
117             if (encoding)
118             {
119                 FREE(encoding);
120                 encoding = NULL;
121             }
122             return NULL;
123         }
124         else
125         {
126             gateway->dimLists = 0;
127             gateway->gatewayIdList = NULL;
128             gateway->primitivesList = NULL;
129             gateway->primiviteIdList = NULL;
130         }
131
132         xpathCtxt = xmlXPathNewContext(doc);
133         xpathObj = xmlXPathEval((const xmlChar*)"//GATEWAY/PRIMITIVE", xpathCtxt);
134
135         if (xpathObj && xpathObj->nodesetval->nodeMax)
136         {
137             /* the Xpath has been understood and there are node */
138             int i;
139             for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
140             {
141                 xmlAttrPtr attrib = xpathObj->nodesetval->nodeTab[i]->properties;
142                 /* Get the properties of <PRIMITIVE>  */
143                 while (attrib != NULL)
144                 {
145                     /* loop until when have read all the attributes */
146                     if (xmlStrEqual (attrib->name, (const xmlChar*) "gatewayId"))
147                     {
148                         /* we found the tag gatewayId */
149                         const char *str = (const char*)attrib->children->content;
150                         GATEWAY_ID = atoi(str);
151                     }
152                     else if (xmlStrEqual (attrib->name, (const xmlChar*)"primitiveId"))
153                     {
154                         /* we found the tag primitiveId */
155                         const char *str = (const char*)attrib->children->content;
156                         PRIMITIVE_ID = atoi(str);
157                     }
158                     else if (xmlStrEqual (attrib->name, (const xmlChar*)"primitiveName"))
159                     {
160                         /* we found the tag primitiveName */
161                         const char *str = (const char*)attrib->children->content;
162                         PRIMITIVE_NAME = os_strdup(str);
163                     }
164                     attrib = attrib->next;
165                 }
166
167                 if ( (GATEWAY_ID != 0) && (PRIMITIVE_ID != 0) && (PRIMITIVE_NAME) )
168                 {
169                     if (strlen(PRIMITIVE_NAME) > 0)
170                     {
171                         gateway->dimLists++;
172                         if (gateway->gatewayIdList)
173                         {
174                             gateway->gatewayIdList = (int*)REALLOC(gateway->gatewayIdList,
175                                                                    sizeof(int*) * gateway->dimLists);
176                         }
177                         else
178                         {
179                             gateway->gatewayIdList = (int*)MALLOC(sizeof(int) * gateway->dimLists);
180                         }
181
182                         if (gateway->primitivesList)
183                         {
184                             gateway->primitivesList = (char **)REALLOC(gateway->primitivesList,
185                                                       sizeof(char**) * gateway->dimLists);
186                         }
187                         else
188                         {
189                             gateway->primitivesList = (char **)MALLOC(sizeof(char*) * gateway->dimLists);
190                         }
191
192                         if (gateway->primiviteIdList)
193                         {
194                             gateway->primiviteIdList = (int*)REALLOC(gateway->primiviteIdList,
195                                                        sizeof(int*) * gateway->dimLists);
196                         }
197                         else
198                         {
199                             gateway->primiviteIdList = (int*)MALLOC(sizeof(int) * gateway->dimLists);
200                         }
201
202                         if (gateway->gatewayIdList)
203                         {
204                             gateway->gatewayIdList[gateway->dimLists - 1] = GATEWAY_ID;
205                         }
206
207                         if (gateway->primitivesList)
208                         {
209                             gateway->primitivesList[gateway->dimLists - 1] = os_strdup(PRIMITIVE_NAME);
210                         }
211
212                         if (gateway->primiviteIdList)
213                         {
214                             gateway->primiviteIdList[gateway->dimLists - 1] = PRIMITIVE_ID;
215                         }
216                     }
217                 }
218                 if (PRIMITIVE_NAME)
219                 {
220                     FREE(PRIMITIVE_NAME);
221                     PRIMITIVE_NAME = NULL;
222                 }
223                 GATEWAY_ID = 0;
224                 PRIMITIVE_ID = 0;
225             }
226         }
227         else
228         {
229             fprintf(stderr, _("Error: Not a valid gateway file %s (should start with <GATEWAY> and contain <PRIMITIVE gatewayId='' primitiveId='' primitiveName=''>)\n"), filenameXml);
230         }
231
232         if (xpathObj)
233         {
234             xmlXPathFreeObject(xpathObj);
235         }
236         if (xpathCtxt)
237         {
238             xmlXPathFreeContext(xpathCtxt);
239         }
240         xmlFreeDoc (doc);
241     }
242     else
243     {
244         fprintf(stderr, _("Error: Not a valid gateway file %s (encoding not 'utf-8') Encoding '%s' found\n"), filenameXml, encoding);
245     }
246
247     if (encoding)
248     {
249         FREE(encoding);
250         encoding = NULL;
251     }
252
253     return gateway;
254 }
255 /*--------------------------------------------------------------------------*/
256 static char *getModuleXmlFilename(char *modulename)
257 {
258     char *filename_module = NULL;
259
260     if (modulename)
261     {
262         char *SciPath = NULL;
263         SciPath = getSCI();
264         if (SciPath)
265         {
266             int length_filename_module = 0;
267
268             length_filename_module = (int)strlen(FORMATGATEWAYFILENAME) +
269                                      (int)strlen(SciPath) + ((int)strlen(modulename) * 2) + 3;
270
271             filename_module = (char*)MALLOC((length_filename_module + 1) *
272                                             sizeof(char));
273
274             if (filename_module)
275             {
276                 sprintf(filename_module, FORMATGATEWAYFILENAME,
277                         SciPath, modulename, modulename);
278
279                 /* file doesn't exist */
280                 if ( !FileExist(filename_module) )
281                 {
282                     FREE(filename_module);
283                     filename_module = NULL;
284                 }
285             }
286             FREE(SciPath);
287             SciPath = NULL;
288         }
289     }
290     return filename_module;
291 }
292 /*--------------------------------------------------------------------------*/