2 * @file Environment.cpp
4 * @brief Environment related routines.
7 #define POCO_NO_UNWINDOWS 1
8 #include "Environment.h"
13 #include <Poco/Path.h>
14 #include <Poco/Process.h>
23 * Static string used by env_GetTempPath() for storing temp path.
25 static String strTempPath;
26 static String strProgPath;
28 void env_SetTempPath(const String& path)
30 strTempPath = paths_AddTrailingSlash(paths_GetLongPath(path));
31 paths_CreateIfNeeded(strTempPath);
35 * @brief Get folder for temporary files.
36 * This function returns system temp folder.
37 * @return Temp path, or empty string if error happened.
38 * @note Temp path is cached after first call.
39 * @todo Should we return NULL for error case?
41 String env_GetTempPath()
43 if (strTempPath.empty())
45 strTempPath = env_GetSystemTempPath();
46 if (strTempPath.empty())
49 paths_CreateIfNeeded(strTempPath);
55 * @brief Get filename for temporary file.
56 * @param [in] lpPathName Temporary file folder.
57 * @param [in] lpPrefixString Prefix to use for filename.
58 * @param [out] pnerr Error code if error happened.
59 * @return Full path for temporary file or empty string if error happened.
61 String env_GetTempFileName(const String& lpPathName, const String& lpPrefixString, int * pnerr)
63 TCHAR buffer[MAX_PATH] = {0};
64 if (lpPathName.length() > MAX_PATH-14)
65 return _T(""); // failure
66 int rtn = GetTempFileName(lpPathName.c_str(), lpPrefixString.c_str(), 0, buffer);
69 int err = GetLastError();
77 String env_GetTempChildPath()
82 path = paths_ConcatPath(env_GetTempPath(), string_format(_T("%08x"), rand()));
83 } while (paths_IsDirectory(path) || !paths_CreateIfNeeded(path));
87 void env_SetProgPath(const String& path)
89 strProgPath = paths_AddTrailingSlash(path);
92 String env_GetProgPath()
94 if (strProgPath.empty())
96 TCHAR temp[MAX_PATH] = {0};
97 GetModuleFileName(NULL, temp, MAX_PATH);
98 strProgPath = paths_GetPathOnly(temp);
104 * @brief Get Windows directory.
105 * @return Windows directory.
107 String env_GetWindowsDirectory()
109 TCHAR path[MAX_PATH];
111 GetWindowsDirectory(path, MAX_PATH);
116 * @brief Return User's My Documents Folder.
117 * This function returns full path to User's My Documents -folder.
118 * @return Full path to My Documents -folder.
120 String env_GetMyDocuments()
122 TCHAR path[MAX_PATH];
124 SHGetSpecialFolderPath(NULL, path, CSIDL_MYDOCUMENTS, FALSE);
129 * @brief Return unique string for the instance.
130 * This function formats an unique string for WinMerge instance. The string
131 * is quaranteed to be unique for instance asking it.
132 * @param [in] name Additional name used as part of the string.
133 * @return Unique string for the instance.
135 String env_GetPerInstanceString(const String& name)
137 std::basic_stringstream<TCHAR> stream;
138 stream << name << Process::id();
143 * @brief Get system temporary directory.
144 * @return System temporary director.
146 String env_GetSystemTempPath()
150 return ucr::toTString(Path::temp());
158 static bool launchProgram(const String& sCmd, WORD wShowWindow)
160 STARTUPINFO stInfo = {0};
161 stInfo.cb = sizeof(STARTUPINFO);
162 stInfo.dwFlags = STARTF_USESHOWWINDOW;
163 stInfo.wShowWindow = wShowWindow;
164 PROCESS_INFORMATION processInfo;
165 BOOL retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
166 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
167 &stInfo, &processInfo);
170 CloseHandle(processInfo.hThread);
171 CloseHandle(processInfo.hProcess);
176 * @brief Load registry keys from .reg file if existing .reg file
178 bool env_LoadRegistryFromFile(const String& sRegFilePath)
180 if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
182 return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE);
186 * @brief Save registry keys to .reg file if existing .reg file
188 bool env_SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir)
190 if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
192 DeleteFile(sRegFilePath.c_str());
193 return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE);