2 * @file Environment.cpp
4 * @brief Environment related routines.
7 #define POCO_NO_UNWINDOWS 1
8 #include "Environment.h"
10 #pragma warning (push) // prevent "warning C4091: 'typedef ': ignored on left of 'tagGPFIDL_FLAGS' when no variable is declared"
11 #pragma warning (disable:4091) // VC bug when using XP enabled toolsets.
15 #include <Poco/Path.h>
16 #include <Poco/Process.h>
28 * Static string used by GetTemporaryPath() for storing temp path.
30 static String strTempPath;
31 static String strProgPath;
33 void SetTemporaryPath(const String& path)
35 strTempPath = paths::AddTrailingSlash(paths::GetLongPath(path));
36 paths::CreateIfNeeded(strTempPath);
40 * @brief Get folder for temporary files.
41 * This function returns system temp folder.
42 * @return Temp path, or empty string if error happened.
43 * @note Temp path is cached after first call.
44 * @todo Should we return NULL for error case?
46 String GetTemporaryPath()
48 if (strTempPath.empty())
50 strTempPath = GetSystemTempPath();
51 if (strTempPath.empty())
54 paths::CreateIfNeeded(strTempPath);
60 * @brief Get filename for temporary file.
61 * @param [in] lpPathName Temporary file folder.
62 * @param [in] lpPrefixString Prefix to use for filename.
63 * @param [out] pnerr Error code if error happened.
64 * @return Full path for temporary file or empty string if error happened.
66 String GetTemporaryFileName(const String& lpPathName, const String& lpPrefixString, int * pnerr)
68 TCHAR buffer[MAX_PATH] = {0};
69 if (lpPathName.length() > MAX_PATH-14)
70 return _T(""); // failure
71 int rtn = ::GetTempFileName(lpPathName.c_str(), lpPrefixString.c_str(), 0, buffer);
74 int err = GetLastError();
82 String GetTempChildPath()
87 path = paths::ConcatPath(GetTemporaryPath(), strutils::format(_T("%08x"), rand()));
88 } while (paths::IsDirectory(path) || !paths::CreateIfNeeded(path));
92 void SetProgPath(const String& path)
94 strProgPath = paths::AddTrailingSlash(path);
99 if (strProgPath.empty())
101 TCHAR temp[MAX_PATH] = {0};
102 GetModuleFileName(NULL, temp, MAX_PATH);
103 strProgPath = paths::GetPathOnly(temp);
109 * @brief Get Windows directory.
110 * @return Windows directory.
112 String GetWindowsDirectory()
114 TCHAR path[MAX_PATH];
116 ::GetWindowsDirectory(path, MAX_PATH);
121 * @brief Return User's My Documents Folder.
122 * This function returns full path to User's My Documents -folder.
123 * @return Full path to My Documents -folder.
125 String GetMyDocuments()
127 TCHAR path[MAX_PATH];
129 SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, path);
134 * @brief Return unique string for the instance.
135 * This function formats an unique string for WinMerge instance. The string
136 * is quaranteed to be unique for instance asking it.
137 * @param [in] name Additional name used as part of the string.
138 * @return Unique string for the instance.
140 String GetPerInstanceString(const String& name)
142 std::basic_stringstream<TCHAR> stream;
143 stream << name << Process::id();
148 * @brief Get system temporary directory.
149 * @return System temporary director.
151 String GetSystemTempPath()
155 return ucr::toTString(Path::temp());
163 static bool launchProgram(const String& sCmd, WORD wShowWindow)
165 STARTUPINFO stInfo = { sizeof(STARTUPINFO) };
166 stInfo.dwFlags = STARTF_USESHOWWINDOW;
167 stInfo.wShowWindow = wShowWindow;
168 PROCESS_INFORMATION processInfo;
169 BOOL retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
170 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
171 &stInfo, &processInfo);
174 CloseHandle(processInfo.hThread);
175 CloseHandle(processInfo.hProcess);
180 * @brief Load registry keys from .reg file if existing .reg file
182 bool LoadRegistryFromFile(const String& sRegFilePath)
184 if (paths::DoesPathExist(sRegFilePath) != paths::IS_EXISTING_FILE)
186 return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE);
190 * @brief Save registry keys to .reg file if existing .reg file
192 bool SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir)
194 if (paths::DoesPathExist(sRegFilePath) != paths::IS_EXISTING_FILE)
196 DeleteFile(sRegFilePath.c_str());
197 return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE);