2 * @file Environment.cpp
4 * @brief Environment related routines.
8 #define POCO_NO_UNWINDOWS 1
9 #include "Environment.h"
11 #pragma warning (push) // prevent "warning C4091: 'typedef ': ignored on left of 'tagGPFIDL_FLAGS' when no variable is declared"
12 #pragma warning (disable:4091) // VC bug when using XP enabled toolsets.
16 #include <Poco/Path.h>
17 #include <Poco/Process.h>
29 * Static string used by GetTemporaryPath() for storing temp path.
31 static String strTempPath;
32 static String strProgPath;
34 void SetTemporaryPath(const String& path)
36 strTempPath = paths::AddTrailingSlash(paths::GetLongPath(path));
37 paths::CreateIfNeeded(strTempPath);
41 * @brief Get folder for temporary files.
42 * This function returns system temp folder.
43 * @return Temp path, or empty string if error happened.
44 * @note Temp path is cached after first call.
45 * @todo Should we return `nullptr` for error case?
47 String GetTemporaryPath()
49 if (strTempPath.empty())
51 strTempPath = GetSystemTempPath();
52 if (strTempPath.empty())
55 paths::CreateIfNeeded(strTempPath);
61 * @brief Get filename for temporary file.
62 * @param [in] lpPathName Temporary file folder.
63 * @param [in] lpPrefixString Prefix to use for filename.
64 * @param [out] pnerr Error code if error happened.
65 * @return Full path for temporary file or empty string if error happened.
67 String GetTemporaryFileName(const String& lpPathName, const String& lpPrefixString, int * pnerr /*= nullptr*/)
69 TCHAR buffer[MAX_PATH] = {0};
70 if (lpPathName.length() > MAX_PATH-14)
71 return _T(""); // failure
72 int rtn = ::GetTempFileName(lpPathName.c_str(), lpPrefixString.c_str(), 0, buffer);
75 int err = GetLastError();
83 String GetTempChildPath()
88 path = paths::ConcatPath(GetTemporaryPath(), strutils::format(_T("%08x"), rand()));
89 } while (paths::IsDirectory(path) || !paths::CreateIfNeeded(path));
93 void SetProgPath(const String& path)
95 strProgPath = paths::AddTrailingSlash(path);
100 if (strProgPath.empty())
102 TCHAR temp[MAX_PATH] = {0};
103 GetModuleFileName(nullptr, temp, MAX_PATH);
104 strProgPath = paths::GetPathOnly(temp);
110 * @brief Get Windows directory.
111 * @return Windows directory.
113 String GetWindowsDirectory()
115 TCHAR path[MAX_PATH];
117 ::GetWindowsDirectory(path, MAX_PATH);
122 * @brief Return User's My Documents Folder.
123 * This function returns full path to User's My Documents -folder.
124 * @return Full path to My Documents -folder.
126 String GetMyDocuments()
128 TCHAR path[MAX_PATH];
130 SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, 0, path);
135 * @brief Return unique string for the instance.
136 * This function formats an unique string for WinMerge instance. The string
137 * is quaranteed to be unique for instance asking it.
138 * @param [in] name Additional name used as part of the string.
139 * @return Unique string for the instance.
141 String GetPerInstanceString(const String& name)
143 std::basic_stringstream<TCHAR> stream;
144 stream << name << Process::id();
149 * @brief Get system temporary directory.
150 * @return System temporary director.
152 String GetSystemTempPath()
156 return ucr::toTString(Path::temp());
164 static bool launchProgram(const String& sCmd, WORD wShowWindow)
166 STARTUPINFO stInfo = { sizeof(STARTUPINFO) };
167 stInfo.dwFlags = STARTF_USESHOWWINDOW;
168 stInfo.wShowWindow = wShowWindow;
169 PROCESS_INFORMATION processInfo;
170 bool retVal = !!CreateProcess(nullptr, (LPTSTR)sCmd.c_str(),
171 nullptr, nullptr, FALSE, CREATE_DEFAULT_ERROR_MODE, nullptr, nullptr,
172 &stInfo, &processInfo);
175 CloseHandle(processInfo.hThread);
176 CloseHandle(processInfo.hProcess);
181 * @brief Load registry keys from .reg file if existing .reg file
183 bool LoadRegistryFromFile(const String& sRegFilePath)
185 if (paths::DoesPathExist(sRegFilePath) != paths::IS_EXISTING_FILE)
187 return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE);
191 * @brief Save registry keys to .reg file if existing .reg file
193 bool SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir)
195 if (paths::DoesPathExist(sRegFilePath) != paths::IS_EXISTING_FILE)
197 DeleteFile(sRegFilePath.c_str());
198 return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE);