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 #include "createdirectory.h"
43 /*--------------------------------------------------------------------------*/
44 wchar_t* getSCIHOMEW(void)
46 return os_wcsdup(ConfigVariable::getSCIHOME().c_str());
48 /*--------------------------------------------------------------------------*/
49 char* getSCIHOME(void)
51 std::wstring tmpSCIHOME = ConfigVariable::getSCIHOME();
52 if (tmpSCIHOME == L"")
54 tmpSCIHOME = L"empty_SCIHOME";
56 return wide_string_to_UTF8(tmpSCIHOME.c_str());
58 /*--------------------------------------------------------------------------*/
59 void setSCIHOME(const char* _sci_home)
61 wchar_t* pstTemp = to_wide_string(_sci_home);
65 /*--------------------------------------------------------------------------*/
66 void setSCIHOMEW(const wchar_t* _sci_home)
68 //add SCI value in context as variable
69 types::String *pS = new types::String(_sci_home);
70 symbol::Context::getInstance()->put(symbol::Symbol(L"SCIHOME"), pS);
72 std::wstring sci_home(_sci_home);
73 ConfigVariable::setSCIHOME(sci_home);
76 /*--------------------------------------------------------------------------*/
77 wchar_t* computeSCIHOMEW(void)
79 char* pstTemp = computeSCIHOME();
80 wchar_t* pstReturn = to_wide_string(pstTemp);
84 /*--------------------------------------------------------------------------*/
86 char* computeSCIHOME(void)
88 #define BASEDIR "Scilab"
90 int buflen = PATH_MAX;
93 char USERPATHSCILAB[PATH_MAX];
94 char SCIHOMEPATH[PATH_MAX * 2];
95 char* SHORTUSERHOMESYSTEM = NULL;
97 char USERHOMESYSTEM[PATH_MAX];
99 BOOL bConverted = FALSE;
101 getenvc(&ierr, "APPDATA", USERHOMESYSTEM, &buflen, &iflag);
103 /* if APPDATA not found we try with USERPROFILE */
106 getenvc(&ierr, "USERPROFILE", USERHOMESYSTEM, &buflen, &iflag);
109 /* convert long path to short path format : remove some special characters */
110 SHORTUSERHOMESYSTEM = getshortpathname(USERHOMESYSTEM, &bConverted);
111 if (SHORTUSERHOMESYSTEM)
113 if (!isdir(SHORTUSERHOMESYSTEM))
115 /* last chance, we try to get default all users profile */
116 getenvc(&ierr, "ALLUSERSPROFILE", USERHOMESYSTEM, &buflen, &iflag);
119 delete []SHORTUSERHOMESYSTEM;
123 /* convert long path to short path format : remove some special characters */
124 SHORTUSERHOMESYSTEM = getshortpathname(USERHOMESYSTEM, &bConverted);
126 if ((!SHORTUSERHOMESYSTEM) || !isdir(SHORTUSERHOMESYSTEM))
128 if (SHORTUSERHOMESYSTEM)
130 delete []SHORTUSERHOMESYSTEM;
138 if (SHORTUSERHOMESYSTEM)
140 delete []SHORTUSERHOMESYSTEM;
145 /* checks that directory exists */
146 os_strcpy(USERHOMESYSTEM, SHORTUSERHOMESYSTEM);
147 if (SHORTUSERHOMESYSTEM)
149 delete []SHORTUSERHOMESYSTEM;
152 /* Set SCIHOME environment variable */
153 os_sprintf(USERPATHSCILAB, "%s%s%s", USERHOMESYSTEM, DIR_SEPARATOR, BASEDIR);
154 os_sprintf(SCIHOMEPATH, "%s%s%s", USERPATHSCILAB, DIR_SEPARATOR, SCI_VERSION_STRING);
156 /* creates directory if it does not exists */
157 if (!isdir(SCIHOMEPATH))
159 if (!isdir(USERPATHSCILAB))
161 createdirectory(USERPATHSCILAB);
164 if (createdirectory(SCIHOMEPATH))
167 return os_strdup(SCIHOMEPATH);
172 return os_strdup(SCIHOMEPATH);
178 char* computeSCIHOME(void)
180 #define BASEDIR ".Scilab"
182 int buflen = PATH_MAX;
184 char USERPATHSCILAB[PATH_MAX];
185 char USERHOMESYSTEM[PATH_MAX];
186 char SCIHOMEPATH[PATH_MAX * 2];
187 char HOME[] = "HOME";
189 getenvc(&ierr, HOME, USERHOMESYSTEM, &buflen, &iflag);
195 /* Set SCIHOME environment variable */
196 sprintf(USERPATHSCILAB, "%s%s%s", USERHOMESYSTEM, DIR_SEPARATOR, BASEDIR);
197 sprintf(SCIHOMEPATH, "%s%s%s", USERPATHSCILAB, DIR_SEPARATOR, SCI_VERSION_STRING);
199 /* creates directory if it does not exists */
200 if (!isdir(SCIHOMEPATH))
202 if (!isdir(USERPATHSCILAB))
204 createdirectory(USERPATHSCILAB);
207 if (createdirectory(SCIHOMEPATH))
209 return os_strdup(SCIHOMEPATH);
214 return os_strdup(SCIHOMEPATH);
221 /*--------------------------------------------------------------------------*/
222 char* getenvSCIHOME(void)
226 char *SciHome = new char[PATH_MAX];
230 getenvc(&ierr, "SCIHOME", SciHome, &lbuf, &iflag);
240 /*--------------------------------------------------------------------------*/
241 wchar_t* getenvSCIHOMEW(void)
243 char *SciHome = getenvSCIHOME();
244 wchar_t* pstTemp = to_wide_string(SciHome);
248 /*--------------------------------------------------------------------------*/
249 void putenvSCIHOMEW(const wchar_t* _sci_home)
251 char* pstTemp = wide_string_to_UTF8(_sci_home);
252 putenvSCIHOME(pstTemp);
257 void putenvSCIHOME(const char* _sci_home)
259 char *ShortPath = NULL;
260 char *CopyOfDefaultPath = NULL;
262 /* to be sure that it's unix 8.3 format */
263 /* c:/progra~1/scilab-5.0 */
264 BOOL bConvertOK = FALSE;
265 ShortPath = getshortpathname(_sci_home, &bConvertOK);
267 CopyOfDefaultPath = new char[strlen(_sci_home) + 1];
268 AntislashToSlash(ShortPath, CopyOfDefaultPath);
270 setenvc("SCIHOME", ShortPath);
272 delete[] CopyOfDefaultPath;
275 /*--------------------------------------------------------------------------*/
276 static bool createDirectoryRecursively(std::wstring path)
282 pos = path.find_first_of(L"\\/", pos + 1);
283 if (CreateDirectoryW(path.substr(0, pos).c_str(), NULL) == FALSE)
285 DWORD d = GetLastError();
286 if (d == ERROR_PATH_NOT_FOUND)
291 } while (pos != std::string::npos);
294 char* file_path = wide_string_to_UTF8(path.data());
297 for (p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/'))
300 if (mkdir(file_path, 777) == -1)
316 /*--------------------------------------------------------------------------*/
319 wchar_t* sci_home = getSCIHOMEW();
320 if (wcscmp(sci_home, L"") == 0)
323 sci_home = computeSCIHOMEW();
327 if (createDirectoryRecursively(sci_home) == false)
329 sciprint("Unable to create SCIHOME in `%ls`.\n", sci_home);
330 sciprint("Back to normal behaviour.\n");
332 sci_home = computeSCIHOMEW();
336 setSCIHOMEW(sci_home);
337 putenvSCIHOMEW(sci_home);