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)
31 if (!paths_EndsWithSlash(strTempPath))
33 paths_CreateIfNeeded(strTempPath);
37 * @brief Get folder for temporary files.
38 * This function returns system temp folder.
39 * @return Temp path, or empty string if error happened.
40 * @note Temp path is cached after first call.
41 * @todo Should we return NULL for error case?
43 String env_GetTempPath()
45 if (strTempPath.empty())
47 strTempPath = env_GetSystemTempPath();
48 if (strTempPath.empty())
51 paths_CreateIfNeeded(strTempPath);
57 * @brief Get filename for temporary file.
58 * @param [in] lpPathName Temporary file folder.
59 * @param [in] lpPrefixString Prefix to use for filename.
60 * @param [out] pnerr Error code if error happened.
61 * @return Full path for temporary file or empty string if error happened.
63 String env_GetTempFileName(const String& lpPathName, const String& lpPrefixString, int * pnerr)
65 TCHAR buffer[MAX_PATH] = {0};
66 if (lpPathName.length() > MAX_PATH-14)
67 return _T(""); // failure
68 int rtn = GetTempFileName(lpPathName.c_str(), lpPrefixString.c_str(), 0, buffer);
71 int err = GetLastError();
79 String env_GetTempChildPath()
84 path = paths_ConcatPath(env_GetTempPath(), string_format(_T("%08x"), rand()));
85 } while (paths_IsDirectory(path) || !paths_CreateIfNeeded(path));
89 void env_SetProgPath(const String& path)
92 if (!paths_EndsWithSlash(strProgPath))
96 String env_GetProgPath()
98 if (strProgPath.empty())
100 TCHAR temp[MAX_PATH] = {0};
101 GetModuleFileName(NULL, temp, MAX_PATH);
102 strProgPath = paths_GetPathOnly(temp);
108 * @brief Get Windows directory.
109 * @return Windows directory.
111 String env_GetWindowsDirectory()
113 TCHAR buf[MAX_PATH] = {0};
114 GetWindowsDirectory(buf, MAX_PATH);
119 * @brief Return User's My Documents Folder.
120 * This function returns full path to User's My Documents -folder.
121 * @return Full path to My Documents -folder.
123 String env_GetMyDocuments()
129 HRESULT rv = SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl);
132 TCHAR szPath[MAX_PATH] = {0};
133 if (SHGetPathFromIDList(pidl, szPath))
138 SHGetMalloc(&pMalloc);
146 * @brief Return unique string for the instance.
147 * This function formats an unique string for WinMerge instance. The string
148 * is quaranteed to be unique for instance asking it.
149 * @param [in] name Additional name used as part of the string.
150 * @return Unique string for the instance.
152 String env_GetPerInstanceString(const String& name)
154 std::basic_stringstream<TCHAR> stream;
155 stream << name << Process::id();
160 * @brief Get system temporary directory.
161 * @return System temporary director.
163 String env_GetSystemTempPath()
167 return ucr::toTString(Path::temp());
175 static bool launchProgram(const String& sCmd, WORD wShowWindow)
177 STARTUPINFO stInfo = {0};
178 stInfo.cb = sizeof(STARTUPINFO);
179 stInfo.dwFlags = STARTF_USESHOWWINDOW;
180 stInfo.wShowWindow = wShowWindow;
181 PROCESS_INFORMATION processInfo;
182 BOOL retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
183 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
184 &stInfo, &processInfo);
187 CloseHandle(processInfo.hThread);
188 CloseHandle(processInfo.hProcess);
193 * @brief Load registry keys from .reg file if existing .reg file
195 bool env_LoadRegistryFromFile(const String& sRegFilePath)
197 if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
199 return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE);
203 * @brief Save registry keys to .reg file if existing .reg file
205 bool env_SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir)
207 if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
209 DeleteFile(sRegFilePath.c_str());
210 return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE);