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;
230 if (stricmp((*doc)->encoding, "utf-8"))
238 *xpathCtxt = xmlXPathNewContext(*doc);
239 if (*xpathCtxt == NULL)
245 /*--------------------------------------------------------------------------*/
246 char * getPrefAttributeValue(const char * xpath, const char * attribute)
248 xmlDocPtr doc = NULL;
249 xmlXPathContextPtr xpathCtxt = NULL;
252 unsigned int xlen = 0;
253 unsigned int alen = 0;
254 char * query = (char *)MALLOC((xlen + alen + 2 + 1) * sizeof(char));
256 if (!xpath || !attribute)
261 xlen = (unsigned int) strlen(xpath);
262 alen = (unsigned int) strlen(attribute);
264 getDocAndCtxt(&doc, &xpathCtxt);
272 sprintf(query, "%s/@%s", xpath, attribute);
273 query[xlen + alen + 2] = '\0';
275 ret = strdup(getAttribute(doc, xpathCtxt, (const xmlChar*)query));
279 xmlXPathFreeContext(xpathCtxt);
284 /*--------------------------------------------------------------------------*/
285 char ** getPrefAttributesValues(const char * xpath, const char ** attributes, const unsigned int attrLen)
287 xmlDocPtr doc = NULL;
288 xmlXPathContextPtr xpathCtxt = NULL;
289 xmlXPathObjectPtr xpathObj = NULL;
292 if (!xpath || !attributes || !attrLen)
297 getDocAndCtxt(&doc, &xpathCtxt);
303 ret = (char**)MALLOC(sizeof(char*) * attrLen);
304 xpathObj = xmlXPathEval((const xmlChar*)xpath, xpathCtxt);
305 if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeMax)
308 xmlNode * node = (xmlNode*)xpathObj->nodesetval->nodeTab[0];
309 for (i = 0; i < (int)attrLen; i++)
311 xmlAttr * attrs = xmlHasProp(node, (const xmlChar *)attributes[i]);
314 ret[i] = strdup((const char *)attrs->children->content);
319 for (j = 0; j < i; j++)
332 xmlXPathFreeObject(xpathObj);
335 xmlXPathFreeContext(xpathCtxt);
340 /*--------------------------------------------------------------------------*/
341 void setPrefAttributesValues(const char * xpath, const char ** kv, const unsigned int kvLen)
343 xmlDocPtr doc = NULL;
344 xmlXPathContextPtr xpathCtxt = NULL;
345 xmlXPathObjectPtr xpathObj = NULL;
346 char * SCIHOME = NULL;
348 BOOL bConvert = FALSE;
349 char * shortfilename_xml_conf = NULL;
351 if (!xpath || !kv || !kvLen)
356 getDocAndCtxt(&doc, &xpathCtxt);
362 xpathObj = xmlXPathEval((const xmlChar*)xpath, xpathCtxt);
363 if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeMax)
366 xmlNode * node = (xmlNode*)xpathObj->nodesetval->nodeTab[0];
367 for (i = 0; i < (int)kvLen / 2; i++)
369 xmlAttr * attrs = xmlHasProp(node, (const xmlChar *)kv[2 * i]);
372 attrs->children->content = xmlStrdup((const xmlChar *)kv[2 * i + 1]);
383 xmlXPathFreeObject(xpathObj);
386 xmlXPathFreeContext(xpathCtxt);
389 xmlThrDefIndentTreeOutput(1);
391 SCIHOME = getSCIHOME();
392 path = (char *)MALLOC(strlen(SCIHOME) + strlen(XCONF));
394 sprintf(path, XCONF, SCIHOME);
399 shortfilename_xml_conf = getshortpathname(path, &bConvert);
400 if (shortfilename_xml_conf)
402 xmlSaveFormatFile(shortfilename_xml_conf, doc, 2);
403 FREE(shortfilename_xml_conf);
404 shortfilename_xml_conf = NULL;