Fix some valgrind issues
[scilab.git] / scilab / modules / preferences / src / c / getScilabPreference.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
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-en.txt
10  *
11  */
12
13 /*--------------------------------------------------------------------------*/
14 #include <stdlib.h>
15 #include <libxml/xpath.h>
16 #include <libxml/xmlreader.h>
17 #include "getScilabPreference.h"
18 #include "GetXmlFileEncoding.h"
19 #include "MALLOC.h"
20 #include "FileExist.h"
21 #include "stricmp.h"
22 #ifdef _MSC_VER
23 #include "strdup_windows.h"
24 #endif
25 #include "getshortpathname.h"
26 #include "BOOL.h"
27 #include "SCIHOME.h"
28
29 #define XCONF "%s/XConfiguration.xml"
30
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 /*--------------------------------------------------------------------------*/
40 const ScilabPreferences * getScilabPreferences()
41 {
42     getPrefs();
43     return &scilabPref;
44 }
45 /*--------------------------------------------------------------------------*/
46 void initPrefs()
47 {
48     scilabPref.heapSize = NULL;
49     scilabPref.adaptToDisplay = NULL;
50     scilabPref.columnsToDisplay = NULL;
51     scilabPref.linesToDisplay = NULL;
52     scilabPref.historySaveAfter = NULL;
53     scilabPref.historyFile = NULL;
54     scilabPref.historyLines = NULL;
55     scilabPref.historyEnable = NULL;
56     scilabPref.ieee = NULL;
57     scilabPref.format = NULL;
58     scilabPref.formatWidth = NULL;
59     scilabPref.language = NULL;
60 }
61 /*--------------------------------------------------------------------------*/
62 void reloadScilabPreferences()
63 {
64     clearScilabPreferences();
65     getPrefs();
66 }
67 /*--------------------------------------------------------------------------*/
68 void clearScilabPreferences()
69 {
70     if (isInit == 1)
71     {
72         if (scilabPref.heapSize)
73         {
74             FREE((void*)scilabPref.heapSize);
75         }
76         if (scilabPref.adaptToDisplay)
77         {
78             FREE((void*)scilabPref.adaptToDisplay);
79         }
80         if (scilabPref.columnsToDisplay)
81         {
82             FREE((void*)scilabPref.columnsToDisplay);
83         }
84         if (scilabPref.linesToDisplay)
85         {
86             FREE((void*)scilabPref.linesToDisplay);
87         }
88         if (scilabPref.historySaveAfter)
89         {
90             FREE((void*)scilabPref.historySaveAfter);
91         }
92         if (scilabPref.historyFile)
93         {
94             FREE((void*)scilabPref.historyFile);
95         }
96         if (scilabPref.historyLines)
97         {
98             FREE((void*)scilabPref.historyLines);
99         }
100         if (scilabPref.historyEnable)
101         {
102             FREE((void*)scilabPref.historyEnable);
103         }
104         if (scilabPref.ieee)
105         {
106             FREE((void*)scilabPref.ieee);
107         }
108         if (scilabPref.format)
109         {
110             FREE((void*)scilabPref.format);
111         }
112         if (scilabPref.formatWidth)
113         {
114             FREE((void*)scilabPref.formatWidth);
115         }
116         if (scilabPref.language)
117         {
118             FREE((void*)scilabPref.language);
119         }
120         initPrefs();
121     }
122     isInit = 0;
123 }
124 /*--------------------------------------------------------------------------*/
125 void getPrefs()
126 {
127     xmlDocPtr doc = NULL;
128     xmlXPathContextPtr xpathCtxt = NULL;
129     char * SCIHOME = NULL;
130     char * path = NULL;
131     BOOL bConvert = FALSE;
132     char * shortfilename_xml_conf = NULL;
133     char * attr = NULL;
134
135     if (!isInit)
136     {
137         initPrefs();
138
139         SCIHOME = getSCIHOME();
140         path = (char *)MALLOC(strlen(SCIHOME) + strlen(XCONF));
141
142         sprintf(path, XCONF, SCIHOME);
143         FREE(SCIHOME);
144
145         if (FileExist(path))
146         {
147             shortfilename_xml_conf = getshortpathname(path, &bConvert);
148             if (shortfilename_xml_conf)
149             {
150                 doc = xmlParseFile(shortfilename_xml_conf);
151                 FREE(shortfilename_xml_conf);
152                 shortfilename_xml_conf = NULL;
153             }
154             FREE(path);
155             path = NULL;
156         }
157
158         if (doc == NULL)
159         {
160             return;
161         }
162
163         if (stricmp(doc->encoding, "utf-8"))
164         {
165             return;
166         }
167
168         xpathCtxt = xmlXPathNewContext(doc);
169
170         if (xpathCtxt)
171         {
172             scilabPref.heapSize = strdup(getAttribute(doc, xpathCtxt, HEAPSIZE_XPATH));
173             scilabPref.adaptToDisplay = strdup(getAttribute(doc, xpathCtxt, ADAPTTODISPLAY_XPATH));
174             scilabPref.columnsToDisplay = strdup(getAttribute(doc, xpathCtxt, COLUMNSTODISPLAY_XPATH));
175             scilabPref.linesToDisplay = strdup(getAttribute(doc, xpathCtxt, LINESTODISPLAY_XPATH));
176             scilabPref.historySaveAfter = strdup(getAttribute(doc, xpathCtxt, HISTORYSAVEAFTER_XPATH));
177             scilabPref.historyFile = strdup(getAttribute(doc, xpathCtxt, HISTORYFILE_XPATH));
178             scilabPref.historyLines = strdup(getAttribute(doc, xpathCtxt, HISTORYLINES_XPATH));
179             scilabPref.historyEnable = strdup(getAttribute(doc, xpathCtxt, HISTORYENABLE_XPATH));
180             scilabPref.ieee = strdup(getAttribute(doc, xpathCtxt, IEEE_XPATH));
181             scilabPref.format = strdup(getAttribute(doc, xpathCtxt, FORMAT_XPATH));
182             scilabPref.formatWidth = strdup(getAttribute(doc, xpathCtxt, FORMATWIDTH_XPATH));
183             scilabPref.language = strdup(getAttribute(doc, xpathCtxt, LANGUAGE_XPATH));
184
185             xmlXPathFreeContext(xpathCtxt);
186         }
187         xmlFreeDoc(doc);
188
189         isInit = 1;
190     }
191 }
192 /*--------------------------------------------------------------------------*/
193 char * getAttribute(xmlDocPtr doc, xmlXPathContextPtr xpathCtxt, const char * xpath)
194 {
195     char * value = emptyAttribute;
196     xmlXPathObjectPtr xpathObj = xmlXPathEval((const xmlChar*)xpath, xpathCtxt);
197     if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeMax)
198     {
199         value = (char *)((xmlAttrPtr)xpathObj->nodesetval->nodeTab[0])->children->content;
200     }
201
202     if (xpathObj)
203     {
204         xmlXPathFreeObject(xpathObj);
205     }
206
207     return value;
208 }
209 /*--------------------------------------------------------------------------*/