2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Antoine ELIAS
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
17 #include "configvariable.hxx"
20 #include "context.hxx"
24 #include "sci_malloc.h"
26 #include "os_string.h"
27 #include "charEncoding.h"
33 #include "setenvvar.h"
34 #include "getshortpathname.h"
37 /*--------------------------------------------------------------------------*/
38 wchar_t* getSCIHOMEW(void)
40 return os_wcsdup(ConfigVariable::getSCIHOME().c_str());
42 /*--------------------------------------------------------------------------*/
43 char* getSCIHOME(void)
45 std::wstring tmpSCIHOME = ConfigVariable::getSCIHOME();
46 if (tmpSCIHOME == L"")
48 tmpSCIHOME = L"empty_SCIHOME";
50 return wide_string_to_UTF8(tmpSCIHOME.c_str());
52 /*--------------------------------------------------------------------------*/
53 void setSCIHOME(const char* _sci_home)
55 wchar_t* pstTemp = to_wide_string(_sci_home);
59 /*--------------------------------------------------------------------------*/
60 void setSCIHOMEW(const wchar_t* _sci_home)
62 //add SCI value in context as variable
63 types::String *pS = new types::String(_sci_home);
64 symbol::Context::getInstance()->put(symbol::Symbol(L"SCIHOME"), pS);
66 std::wstring sci_home(_sci_home);
67 ConfigVariable::setSCIHOME(sci_home);
70 /*--------------------------------------------------------------------------*/
71 wchar_t* computeSCIHOMEW(void)
73 char* pstTemp = computeSCIHOME();
74 wchar_t* pstReturn = to_wide_string(pstTemp);
78 /*--------------------------------------------------------------------------*/
80 char* computeSCIHOME(void)
82 #define BASEDIR "Scilab"
84 int buflen = PATH_MAX;
87 char USERPATHSCILAB[PATH_MAX];
88 char SCIHOMEPATH[PATH_MAX * 2];
89 char* SHORTUSERHOMESYSTEM = NULL;
91 char USERHOMESYSTEM[PATH_MAX];
93 BOOL bConverted = FALSE;
95 getenvc(&ierr, "APPDATA", USERHOMESYSTEM, &buflen, &iflag);
97 /* if APPDATA not found we try with USERPROFILE */
100 getenvc(&ierr, "USERPROFILE", USERHOMESYSTEM, &buflen, &iflag);
103 /* convert long path to short path format : remove some special characters */
104 SHORTUSERHOMESYSTEM = getshortpathname(USERHOMESYSTEM, &bConverted);
105 if (SHORTUSERHOMESYSTEM)
107 if (!isdir(SHORTUSERHOMESYSTEM))
109 /* last chance, we try to get default all users profile */
110 getenvc(&ierr, "ALLUSERSPROFILE", USERHOMESYSTEM, &buflen, &iflag);
113 delete []SHORTUSERHOMESYSTEM;
117 /* convert long path to short path format : remove some special characters */
118 SHORTUSERHOMESYSTEM = getshortpathname(USERHOMESYSTEM, &bConverted);
120 if ((!SHORTUSERHOMESYSTEM) || !isdir(SHORTUSERHOMESYSTEM))
122 if (SHORTUSERHOMESYSTEM)
124 delete []SHORTUSERHOMESYSTEM;
132 if (SHORTUSERHOMESYSTEM)
134 delete []SHORTUSERHOMESYSTEM;
139 /* checks that directory exists */
140 os_strcpy(USERHOMESYSTEM, SHORTUSERHOMESYSTEM);
141 if (SHORTUSERHOMESYSTEM)
143 delete []SHORTUSERHOMESYSTEM;
146 /* Set SCIHOME environment variable */
147 os_sprintf(USERPATHSCILAB, "%s%s%s", USERHOMESYSTEM, DIR_SEPARATOR, BASEDIR);
148 os_sprintf(SCIHOMEPATH, "%s%s%s", USERPATHSCILAB, DIR_SEPARATOR, SCI_VERSION_STRING);
150 /* creates directory if it does not exists */
151 if (!isdir(SCIHOMEPATH))
153 if (!isdir(USERPATHSCILAB))
155 createdirectory(USERPATHSCILAB);
158 if (createdirectory(SCIHOMEPATH))
161 return os_strdup(SCIHOMEPATH);
166 return os_strdup(SCIHOMEPATH);
172 char* computeSCIHOME(void)
174 #define BASEDIR ".Scilab"
176 int buflen = PATH_MAX;
178 char USERPATHSCILAB[PATH_MAX];
179 char USERHOMESYSTEM[PATH_MAX];
180 char SCIHOMEPATH[PATH_MAX * 2];
181 char HOME[] = "HOME";
183 getenvc(&ierr, HOME, USERHOMESYSTEM, &buflen, &iflag);
189 /* Set SCIHOME environment variable */
190 sprintf(USERPATHSCILAB, "%s%s%s", USERHOMESYSTEM, DIR_SEPARATOR, BASEDIR);
191 sprintf(SCIHOMEPATH, "%s%s%s", USERPATHSCILAB, DIR_SEPARATOR, SCI_VERSION_STRING);
193 /* creates directory if it does not exists */
194 if (!isdir(SCIHOMEPATH))
196 if (!isdir(USERPATHSCILAB))
198 createdirectory(USERPATHSCILAB);
201 if (createdirectory(SCIHOMEPATH))
203 return os_strdup(SCIHOMEPATH);
208 return os_strdup(SCIHOMEPATH);
215 /*--------------------------------------------------------------------------*/
216 char* getenvSCIHOME(void)
220 char *SciHome = new char[PATH_MAX];
224 getenvc(&ierr, "SCIHOME", SciHome, &lbuf, &iflag);
234 /*--------------------------------------------------------------------------*/
235 wchar_t* getenvSCIHOMEW(void)
237 char *SciHome = getenvSCIHOME();
238 wchar_t* pstTemp = to_wide_string(SciHome);
242 /*--------------------------------------------------------------------------*/
243 void putenvSCIHOMEW(const wchar_t* _sci_home)
245 char* pstTemp = wide_string_to_UTF8(_sci_home);
246 putenvSCIHOME(pstTemp);
251 void putenvSCIHOME(const char* _sci_home)
253 char *ShortPath = NULL;
254 char *CopyOfDefaultPath = NULL;
256 /* to be sure that it's unix 8.3 format */
257 /* c:/progra~1/scilab-5.0 */
258 BOOL bConvertOK = FALSE;
259 ShortPath = getshortpathname(_sci_home, &bConvertOK);
261 CopyOfDefaultPath = new char[strlen(_sci_home) + 1];
262 AntislashToSlash(ShortPath, CopyOfDefaultPath);
264 setenvc("SCIHOME", ShortPath);
266 delete[] CopyOfDefaultPath;
270 /*--------------------------------------------------------------------------*/
273 wchar_t* sci_home = computeSCIHOMEW();
274 setSCIHOMEW(sci_home);
275 putenvSCIHOMEW(sci_home);