OSDN Git Service

Environment.cpp: Copy env_GetMyDocuments() and env_GetWindowsDirectory() from WinMerg...
[winmerge-jp/winmerge-jp.git] / Src / Environment.cpp
1 /** 
2  * @file  Environment.cpp
3  *
4  * @brief Environment related routines.
5  */
6
7 #define POCO_NO_UNWINDOWS 1
8 #include "Environment.h"
9 #include <windows.h>
10 #include <shlobj.h>
11 #include <cassert>
12 #include <sstream>
13 #include <Poco/Path.h>
14 #include <Poco/Process.h>
15 #include "paths.h"
16 #include "unicoder.h"
17
18 using Poco::Path;
19 using Poco::Process;
20
21 /**
22  * @brief Temp path.
23  * Static string used by env_GetTempPath() for storing temp path.
24  */
25 static String strTempPath;
26 static String strProgPath;
27
28 void env_SetTempPath(const String& path)
29 {
30         strTempPath = paths_AddTrailingSlash(paths_GetLongPath(path));
31         paths_CreateIfNeeded(strTempPath);
32 }
33
34 /** 
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?
40  */
41 String env_GetTempPath()
42 {
43         if (strTempPath.empty())
44         {
45                 strTempPath = env_GetSystemTempPath();
46                 if (strTempPath.empty())
47                         return strTempPath;
48
49                 paths_CreateIfNeeded(strTempPath);
50         }
51         return strTempPath;
52 }
53
54 /**
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.
60  */
61 String env_GetTempFileName(const String& lpPathName, const String& lpPrefixString, int * pnerr)
62 {
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);
67         if (!rtn)
68         {
69                 int err = GetLastError();
70                 if (pnerr)
71                         *pnerr = err;
72                 return _T("");
73         }
74         return buffer;
75 }
76
77 String env_GetTempChildPath()
78 {
79         String path;
80         do
81         {
82                 path = paths_ConcatPath(env_GetTempPath(), string_format(_T("%08x"), rand()));
83         } while (paths_IsDirectory(path) || !paths_CreateIfNeeded(path));
84         return path;
85 }
86
87 void env_SetProgPath(const String& path)
88 {
89         strProgPath = paths_AddTrailingSlash(path);
90 }
91
92 String env_GetProgPath()
93 {
94         if (strProgPath.empty())
95         {
96                 TCHAR temp[MAX_PATH] = {0};
97                 GetModuleFileName(NULL, temp, MAX_PATH);
98                 strProgPath = paths_GetPathOnly(temp);
99         }
100         return strProgPath;
101 }
102
103 /**
104  * @brief Get Windows directory.
105  * @return Windows directory.
106  */
107 String env_GetWindowsDirectory()
108 {
109         TCHAR path[MAX_PATH];
110         path[0] = _T('\0');
111         GetWindowsDirectory(path, MAX_PATH);
112         return path;
113 }
114
115 /**
116  * @brief Return User's My Documents Folder.
117  * This function returns full path to User's My Documents -folder.
118  * @return Full path to My Documents -folder.
119  */
120 String env_GetMyDocuments()
121 {
122         TCHAR path[MAX_PATH];
123         path[0] = _T('\0');
124         SHGetSpecialFolderPath(NULL, path, CSIDL_MYDOCUMENTS, FALSE);
125         return path;
126 }
127
128 /**
129  * @brief Return unique string for the instance.
130  * This function formats an unique string for WinMerge instance. The string
131  * is quaranteed to be unique for instance asking it.
132  * @param [in] name Additional name used as part of the string.
133  * @return Unique string for the instance.
134  */
135 String env_GetPerInstanceString(const String& name)
136 {
137         std::basic_stringstream<TCHAR> stream;
138         stream << name << Process::id();
139         return stream.str();
140 }
141
142 /**
143  * @brief Get system temporary directory.
144  * @return System temporary director.
145  */
146 String env_GetSystemTempPath()
147 {
148         try
149         {
150                 return ucr::toTString(Path::temp());
151         }
152         catch (...)
153         {
154                 return _T("");
155         }
156 }
157
158 static bool launchProgram(const String& sCmd, WORD wShowWindow)
159 {
160         STARTUPINFO stInfo = {0};
161         stInfo.cb = sizeof(STARTUPINFO);
162         stInfo.dwFlags = STARTF_USESHOWWINDOW;
163         stInfo.wShowWindow = wShowWindow;
164         PROCESS_INFORMATION processInfo;
165         BOOL retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
166                 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
167                 &stInfo, &processInfo);
168         if (!retVal)
169                 return false;
170         CloseHandle(processInfo.hThread);
171         CloseHandle(processInfo.hProcess);
172         return true;
173 }
174
175 /**
176  * @brief Load registry keys from .reg file if existing .reg file
177  */
178 bool env_LoadRegistryFromFile(const String& sRegFilePath)
179 {
180         if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
181                 return false;
182         return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE);
183 }
184
185 /** 
186  * @brief Save registry keys to .reg file if existing .reg file
187  */
188 bool env_SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir)
189 {
190         if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
191                 return false;
192         DeleteFile(sRegFilePath.c_str());
193         return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE);
194 }