2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
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
13 /*--------------------------------------------------------------------------*/
15 #include <libxml/xpath.h>
16 #include <libxml/xmlreader.h>
17 #include "getScilabPreference.h"
18 #include "GetXmlFileEncoding.h"
20 #include "FileExist.h"
23 #include "strdup_windows.h"
25 #include "getshortpathname.h"
29 #define XCONF "%s/XConfiguration.xml"
31 /*--------------------------------------------------------------------------*/
32 static unsigned char isInit = 0;
33 static ScilabPreferences scilabPref;
34 static char * emptyAttribute = "";
35 /*--------------------------------------------------------------------------*/
36 static void getPrefs();
37 static char * getAttribute(xmlDocPtr doc, xmlXPathContextPtr xpathCtxt, const char * xpath);
38 static void initPrefs();
39 static void getDocAndCtxt(xmlDocPtr * doc, xmlXPathContextPtr * xpathCtxt);
40 /*--------------------------------------------------------------------------*/
41 const ScilabPreferences * getScilabPreferences()
46 /*--------------------------------------------------------------------------*/
49 scilabPref.heapSize = NULL;
50 scilabPref.adaptToDisplay = NULL;
51 scilabPref.columnsToDisplay = NULL;
52 scilabPref.linesToDisplay = NULL;
53 scilabPref.historySaveAfter = NULL;
54 scilabPref.historyFile = NULL;
55 scilabPref.historyLines = NULL;
56 scilabPref.historyEnable = NULL;
57 scilabPref.ieee = NULL;
58 scilabPref.format = NULL;
59 scilabPref.formatWidth = NULL;
60 scilabPref.language = NULL;
61 scilabPref.startup_dir_use = NULL;
62 scilabPref.startup_dir_default = NULL;
63 scilabPref.startup_dir_previous = NULL;
65 /*--------------------------------------------------------------------------*/
66 void reloadScilabPreferences()
68 clearScilabPreferences();
71 /*--------------------------------------------------------------------------*/
72 void clearScilabPreferences()
76 if (scilabPref.heapSize)
78 FREE((void*)scilabPref.heapSize);
80 if (scilabPref.adaptToDisplay)
82 FREE((void*)scilabPref.adaptToDisplay);
84 if (scilabPref.columnsToDisplay)
86 FREE((void*)scilabPref.columnsToDisplay);
88 if (scilabPref.linesToDisplay)
90 FREE((void*)scilabPref.linesToDisplay);
92 if (scilabPref.historySaveAfter)
94 FREE((void*)scilabPref.historySaveAfter);
96 if (scilabPref.historyFile)
98 FREE((void*)scilabPref.historyFile);
100 if (scilabPref.historyLines)
102 FREE((void*)scilabPref.historyLines);
104 if (scilabPref.historyEnable)
106 FREE((void*)scilabPref.historyEnable);
110 FREE((void*)scilabPref.ieee);
112 if (scilabPref.format)
114 FREE((void*)scilabPref.format);
116 if (scilabPref.formatWidth)
118 FREE((void*)scilabPref.formatWidth);
120 if (scilabPref.language)
122 FREE((void*)scilabPref.language);
124 if (scilabPref.startup_dir_use)
126 FREE((void*)scilabPref.startup_dir_use);
128 if (scilabPref.startup_dir_default)
130 FREE((void*)scilabPref.startup_dir_default);
132 if (scilabPref.startup_dir_previous)
134 FREE((void*)scilabPref.startup_dir_previous);
140 /*--------------------------------------------------------------------------*/
145 xmlDocPtr doc = NULL;
146 xmlXPathContextPtr xpathCtxt = NULL;
150 getDocAndCtxt(&doc, &xpathCtxt);
156 scilabPref.heapSize = strdup(getAttribute(doc, xpathCtxt, HEAPSIZE_XPATH));
157 scilabPref.adaptToDisplay = strdup(getAttribute(doc, xpathCtxt, ADAPTTODISPLAY_XPATH));
158 scilabPref.columnsToDisplay = strdup(getAttribute(doc, xpathCtxt, COLUMNSTODISPLAY_XPATH));
159 scilabPref.linesToDisplay = strdup(getAttribute(doc, xpathCtxt, LINESTODISPLAY_XPATH));
160 scilabPref.historySaveAfter = strdup(getAttribute(doc, xpathCtxt, HISTORYSAVEAFTER_XPATH));
161 scilabPref.historyFile = strdup(getAttribute(doc, xpathCtxt, HISTORYFILE_XPATH));
162 scilabPref.historyLines = strdup(getAttribute(doc, xpathCtxt, HISTORYLINES_XPATH));
163 scilabPref.historyEnable = strdup(getAttribute(doc, xpathCtxt, HISTORYENABLE_XPATH));
164 scilabPref.ieee = strdup(getAttribute(doc, xpathCtxt, IEEE_XPATH));
165 scilabPref.format = strdup(getAttribute(doc, xpathCtxt, FORMAT_XPATH));
166 scilabPref.formatWidth = strdup(getAttribute(doc, xpathCtxt, FORMATWIDTH_XPATH));
167 scilabPref.language = strdup(getAttribute(doc, xpathCtxt, LANGUAGE_XPATH));
168 scilabPref.startup_dir_use = strdup(getAttribute(doc, xpathCtxt, STARTUP_DIR_USE_XPATH));
169 scilabPref.startup_dir_default = strdup(getAttribute(doc, xpathCtxt, STARTUP_DIR_DEFAULT_XPATH));
170 scilabPref.startup_dir_previous = strdup(getAttribute(doc, xpathCtxt, STARTUP_DIR_PREVIOUS_XPATH));
172 xmlXPathFreeContext(xpathCtxt);
178 /*--------------------------------------------------------------------------*/
179 char * getAttribute(xmlDocPtr doc, xmlXPathContextPtr xpathCtxt, const char * xpath)
181 char * value = emptyAttribute;
182 xmlXPathObjectPtr xpathObj = xmlXPathEval((const xmlChar*)xpath, xpathCtxt);
183 if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeMax)
185 value = (char *)((xmlAttrPtr)xpathObj->nodesetval->nodeTab[0])->children->content;
190 xmlXPathFreeObject(xpathObj);
195 /*--------------------------------------------------------------------------*/
196 void getDocAndCtxt(xmlDocPtr * doc, xmlXPathContextPtr * xpathCtxt)
198 char * SCIHOME = NULL;
200 BOOL bConvert = FALSE;
201 char * shortfilename_xml_conf = NULL;
204 SCIHOME = getSCIHOME();
205 path = (char *)MALLOC(strlen(SCIHOME) + strlen(XCONF));
207 sprintf(path, XCONF, SCIHOME);
212 shortfilename_xml_conf = getshortpathname(path, &bConvert);
213 if (shortfilename_xml_conf)
215 *doc = xmlParseFile(shortfilename_xml_conf);
216 FREE(shortfilename_xml_conf);
217 shortfilename_xml_conf = NULL;
229 if (stricmp((*doc)->encoding, "utf-8"))
237 *xpathCtxt = xmlXPathNewContext(*doc);
238 if (*xpathCtxt == NULL)
244 /*--------------------------------------------------------------------------*/
245 char * getPrefAttributeValue(const char * xpath, const char * attribute)
247 xmlDocPtr doc = NULL;
248 xmlXPathContextPtr xpathCtxt = NULL;
251 unsigned int xlen = 0;
252 unsigned int alen = 0;
253 char * query = (char *)MALLOC((xlen + alen + 2 + 1) * sizeof(char));
255 if (!xpath || !attribute)
260 xlen = (unsigned int) strlen(xpath);
261 alen = (unsigned int) strlen(attribute);
263 getDocAndCtxt(&doc, &xpathCtxt);
271 sprintf(query, "%s/@%s", xpath, attribute);
272 query[xlen + alen + 2] = '\0';
274 ret = strdup(getAttribute(doc, xpathCtxt, (const xmlChar*)query));
278 xmlXPathFreeContext(xpathCtxt);
283 /*--------------------------------------------------------------------------*/
284 char ** getPrefAttributesValues(const char * xpath, const char ** attributes, const unsigned int attrLen)
286 xmlDocPtr doc = NULL;
287 xmlXPathContextPtr xpathCtxt = NULL;
288 xmlXPathObjectPtr xpathObj = NULL;
291 if (!xpath || !attributes || !attrLen)
296 getDocAndCtxt(&doc, &xpathCtxt);
302 ret = (char**)MALLOC(sizeof(char*) * attrLen);
303 xpathObj = xmlXPathEval((const xmlChar*)xpath, xpathCtxt);
304 if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeMax)
307 xmlNode * node = (xmlNode*)xpathObj->nodesetval->nodeTab[0];
308 for (i = 0; i < (int)attrLen; i++)
310 xmlAttr * attrs = xmlHasProp(node, (const xmlChar *)attributes[i]);
313 ret[i] = strdup((const char *)attrs->children->content);
318 for (j = 0; j < i; j++)
331 xmlXPathFreeObject(xpathObj);
334 xmlXPathFreeContext(xpathCtxt);
339 /*--------------------------------------------------------------------------*/
340 void setPrefAttributesValues(const char * xpath, const char ** kv, const unsigned int kvLen)
342 xmlDocPtr doc = NULL;
343 xmlXPathContextPtr xpathCtxt = NULL;
344 xmlXPathObjectPtr xpathObj = NULL;
345 char * SCIHOME = NULL;
347 BOOL bConvert = FALSE;
348 char * shortfilename_xml_conf = NULL;
350 if (!xpath || !kv || !kvLen)
355 getDocAndCtxt(&doc, &xpathCtxt);
361 xpathObj = xmlXPathEval((const xmlChar*)xpath, xpathCtxt);
362 if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeMax)
365 xmlNode * node = (xmlNode*)xpathObj->nodesetval->nodeTab[0];
366 for (i = 0; i < (int)kvLen / 2; i++)
368 xmlAttr * attrs = xmlHasProp(node, (const xmlChar *)kv[2 * i]);
371 attrs->children->content = xmlStrdup((const xmlChar *)kv[2 * i + 1]);
382 xmlXPathFreeObject(xpathObj);
385 xmlXPathFreeContext(xpathCtxt);
388 xmlThrDefIndentTreeOutput(1);
390 SCIHOME = getSCIHOME();
391 path = (char *)MALLOC(strlen(SCIHOME) + strlen(XCONF));
393 sprintf(path, XCONF, SCIHOME);
398 shortfilename_xml_conf = getshortpathname(path, &bConvert);
399 if (shortfilename_xml_conf)
401 xmlSaveFormatFile(shortfilename_xml_conf, doc, 2);
402 FREE(shortfilename_xml_conf);
403 shortfilename_xml_conf = NULL;