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(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 buf[MAX_PATH] = {0};
110 GetWindowsDirectory(buf, MAX_PATH);
115 * @brief Return User's My Documents Folder.
116 * This function returns full path to User's My Documents -folder.
117 * @return Full path to My Documents -folder.
119 String env_GetMyDocuments()
125 HRESULT rv = SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl);
128 TCHAR szPath[MAX_PATH] = {0};
129 if (SHGetPathFromIDList(pidl, szPath))
134 SHGetMalloc(&pMalloc);
142 * @brief Return unique string for the instance.
143 * This function formats an unique string for WinMerge instance. The string
144 * is quaranteed to be unique for instance asking it.
145 * @param [in] name Additional name used as part of the string.
146 * @return Unique string for the instance.
148 String env_GetPerInstanceString(const String& name)
150 std::basic_stringstream<TCHAR> stream;
151 stream << name << Process::id();
156 * @brief Get system temporary directory.
157 * @return System temporary director.
159 String env_GetSystemTempPath()
163 return ucr::toTString(Path::temp());
171 static bool launchProgram(const String& sCmd, WORD wShowWindow)
173 STARTUPINFO stInfo = {0};
174 stInfo.cb = sizeof(STARTUPINFO);
175 stInfo.dwFlags = STARTF_USESHOWWINDOW;
176 stInfo.wShowWindow = wShowWindow;
177 PROCESS_INFORMATION processInfo;
178 BOOL retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
179 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
180 &stInfo, &processInfo);
183 CloseHandle(processInfo.hThread);
184 CloseHandle(processInfo.hProcess);
189 * @brief Load registry keys from .reg file if existing .reg file
191 bool env_LoadRegistryFromFile(const String& sRegFilePath)
193 if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
195 return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE);
199 * @brief Save registry keys to .reg file if existing .reg file
201 bool env_SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir)
203 if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
205 DeleteFile(sRegFilePath.c_str());
206 return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE);