2 * @file Environment.cpp
4 * @brief Environment related routines.
7 #define POCO_NO_UNWINDOWS 1
8 #include "Environment.h"
12 #include <Poco/Path.h>
13 #include <Poco/Process.h>
25 * Static string used by GetTemporaryPath() for storing temp path.
27 static String strTempPath;
28 static String strProgPath;
30 void SetTemporaryPath(const String& path)
32 strTempPath = paths::AddTrailingSlash(paths::GetLongPath(path));
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 GetTemporaryPath()
45 if (strTempPath.empty())
47 strTempPath = 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 GetTemporaryFileName(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 GetTempChildPath()
84 path = paths::ConcatPath(GetTemporaryPath(), strutils::format(_T("%08x"), rand()));
85 } while (paths::IsDirectory(path) || !paths::CreateIfNeeded(path));
89 void SetProgPath(const String& path)
91 strProgPath = paths::AddTrailingSlash(path);
96 if (strProgPath.empty())
98 TCHAR temp[MAX_PATH] = {0};
99 GetModuleFileName(NULL, temp, MAX_PATH);
100 strProgPath = paths::GetPathOnly(temp);
106 * @brief Get Windows directory.
107 * @return Windows directory.
109 String GetWindowsDirectory()
111 TCHAR path[MAX_PATH];
113 ::GetWindowsDirectory(path, MAX_PATH);
118 * @brief Return User's My Documents Folder.
119 * This function returns full path to User's My Documents -folder.
120 * @return Full path to My Documents -folder.
122 String GetMyDocuments()
124 TCHAR path[MAX_PATH];
126 SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, path);
131 * @brief Return unique string for the instance.
132 * This function formats an unique string for WinMerge instance. The string
133 * is quaranteed to be unique for instance asking it.
134 * @param [in] name Additional name used as part of the string.
135 * @return Unique string for the instance.
137 String GetPerInstanceString(const String& name)
139 std::basic_stringstream<TCHAR> stream;
140 stream << name << Process::id();
145 * @brief Get system temporary directory.
146 * @return System temporary director.
148 String GetSystemTempPath()
152 return ucr::toTString(Path::temp());
160 static bool launchProgram(const String& sCmd, WORD wShowWindow)
162 STARTUPINFO stInfo = { sizeof(STARTUPINFO) };
163 stInfo.dwFlags = STARTF_USESHOWWINDOW;
164 stInfo.wShowWindow = wShowWindow;
165 PROCESS_INFORMATION processInfo;
166 BOOL retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
167 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
168 &stInfo, &processInfo);
171 CloseHandle(processInfo.hThread);
172 CloseHandle(processInfo.hProcess);
177 * @brief Load registry keys from .reg file if existing .reg file
179 bool LoadRegistryFromFile(const String& sRegFilePath)
181 if (paths::DoesPathExist(sRegFilePath) != paths::IS_EXISTING_FILE)
183 return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE);
187 * @brief Save registry keys to .reg file if existing .reg file
189 bool SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir)
191 if (paths::DoesPathExist(sRegFilePath) != paths::IS_EXISTING_FILE)
193 DeleteFile(sRegFilePath.c_str());
194 return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE);