1 /* Copyright (c) 2007 Eric Scott Albright
\r
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
4 * of this software and associated documentation files (the "Software"), to deal
\r
5 * in the Software without restriction, including without limitation the rights
\r
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
7 * copies of the Software, and to permit persons to whom the Software is
\r
8 * furnished to do so, subject to the following conditions:
\r
10 * The above copyright notice and this permission notice shall be included in
\r
11 * all copies or substantial portions of the Software.
\r
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
22 #ifndef __ENCHANTTESTFIXTURE
\r
23 #define __ENCHANTTESTFIXTURE
\r
25 #if defined(_MSC_VER)
\r
30 #define WIN32_LEAN_AND_MEAN
\r
31 #include <windows.h>
\r
32 #include <shlwapi.h>
\r
39 struct EnchantTestFixture
\r
41 std::string savedRegistryHomeDir;
\r
42 std::string savedUserRegistryModuleDir;
\r
43 std::string savedMachineRegistryModuleDir;
\r
44 std::string savedUserRegistryConfigDir;
\r
45 std::string savedMachineRegistryConfigDir;
\r
48 EnchantTestFixture()
\r
50 CleanUpFiles(); //just in case we stopped the process in the middle.
\r
51 MoveEnchantUserFilesOutOfTheWay();
\r
54 savedRegistryHomeDir = GetRegistryHomeDir();
\r
55 ClearRegistryHomeDir();
\r
57 savedUserRegistryModuleDir = GetUserRegistryModuleDir();
\r
58 ClearUserRegistryModuleDir();
\r
60 savedMachineRegistryModuleDir = GetMachineRegistryModuleDir();
\r
61 ClearMachineRegistryModuleDir();
\r
63 savedUserRegistryConfigDir = GetUserRegistryConfigDir();
\r
64 ClearUserRegistryConfigDir();
\r
66 savedMachineRegistryConfigDir = GetMachineRegistryConfigDir();
\r
67 ClearMachineRegistryConfigDir();
\r
72 ~EnchantTestFixture(){
\r
74 if(savedRegistryHomeDir.empty()) {
\r
75 ClearRegistryHomeDir();
\r
78 SetRegistryHomeDir(savedRegistryHomeDir);
\r
81 if(savedUserRegistryModuleDir.empty()) {
\r
82 ClearUserRegistryModuleDir();
\r
85 SetUserRegistryModuleDir(savedUserRegistryModuleDir);
\r
88 if(savedMachineRegistryModuleDir.empty())
\r
90 ClearMachineRegistryModuleDir();
\r
93 SetMachineRegistryModuleDir(savedMachineRegistryModuleDir);
\r
96 if(savedUserRegistryConfigDir.empty())
\r
98 ClearUserRegistryConfigDir();
\r
101 SetUserRegistryConfigDir(savedUserRegistryConfigDir);
\r
104 if(savedMachineRegistryConfigDir.empty()) {
\r
105 ClearMachineRegistryConfigDir();
\r
108 SetMachineRegistryConfigDir(savedMachineRegistryConfigDir);
\r
112 RestoreEnchantUserFiles();
\r
115 void CleanUpFiles()
\r
117 //clean up personal dictionaries from home dir
\r
118 DeleteDirAndFiles(GetTempUserEnchantDir());
\r
119 DeleteDirAndFiles(AddToPath(GetDirectoryOfThisModule(), "lib"));
\r
120 DeleteDirAndFiles(AddToPath(GetDirectoryOfThisModule(), "share"));
\r
123 std::string GetTempUserEnchantDir()
\r
125 return GetEnchantHomeDirFromBase(GetDirectoryOfThisModule());
\r
128 void MoveEnchantUserFilesOutOfTheWay()
\r
130 GetFilesOutOfTheWay(GetEnchantHomeDirFromBase(g_get_user_config_dir()));
\r
131 GetFilesOutOfTheWay(GetEnchantHomeDirFromBase(g_get_home_dir()));
\r
134 void RestoreEnchantUserFiles()
\r
136 RestoreFiles(GetEnchantHomeDirFromBase(g_get_user_config_dir()));
\r
137 RestoreFiles(GetEnchantHomeDirFromBase(g_get_home_dir()));
\r
140 #define OUT_OF_THE_WAY ".real";
\r
142 void GetFilesOutOfTheWay(const std::string& dir)
\r
144 std::string toTheSideDir = dir + OUT_OF_THE_WAY;
\r
146 if(DirExists(dir) && !DirExists(toTheSideDir))
\r
148 MoveDir(dir, toTheSideDir);
\r
151 DeleteDirAndFiles(dir);
\r
154 void RestoreFiles(const std::string& dir)
\r
156 std::string toTheSideDir = dir + OUT_OF_THE_WAY;
\r
157 if(DirExists(toTheSideDir))
\r
159 DeleteDirAndFiles(dir);
\r
160 MoveDir(toTheSideDir, dir);
\r
164 std::string GetEnchantConfigDir()
\r
166 return AddToPath(AddToPath(GetDirectoryOfThisModule(), "share"), "enchant");
\r
169 static bool DirExists(const std::string& dir)
\r
171 return g_file_test(dir.c_str(), G_FILE_TEST_IS_DIR) != 0;
\r
174 static void MoveDir(const std::string& from, const std::string& to)
\r
176 int result = g_rename(from.c_str(), to.c_str());
\r
179 perror("failed (will retry)");
\r
181 result = g_rename(from.c_str(), to.c_str());
\r
183 assert(result == 0);
\r
186 perror("failed (giving up)");
\r
190 static void DeleteDirAndFiles(const std::string& dir)
\r
192 GDir* gdir = g_dir_open(dir.c_str(), 0, NULL);
\r
195 const gchar* filename;
\r
197 filename = g_dir_read_name(gdir);
\r
198 if(filename == NULL)
\r
202 std::string filepath = AddToPath(dir, filename);
\r
203 if(g_file_test(filepath.c_str(), G_FILE_TEST_IS_DIR)){
\r
204 DeleteDirAndFiles(filepath);
\r
207 DeleteFile(filepath);
\r
212 g_rmdir(dir.c_str());
\r
215 static std::string GetTemporaryFilename(const std::string & prefix){
\r
216 char* tempFileName = tempnam(".", prefix.c_str());
\r
217 std::string temp(tempFileName);
\r
218 free(tempFileName);
\r
222 static void CreateDirectory(const std::string& filepath)
\r
224 g_mkdir_with_parents(filepath.c_str(), S_IREAD | S_IWRITE | S_IEXEC);
\r
226 static void CreateFile(const std::string& filepath)
\r
228 int fh = g_creat(filepath.c_str(), _S_IREAD | _S_IWRITE);
\r
233 static void DeleteFile(const std::string& filepath)
\r
235 if(FileExists(filepath)){
\r
236 g_remove(filepath.c_str());
\r
239 static bool FileExists(const std::string& filepath)
\r
241 return(g_access(filepath.c_str(), 0)==0);
\r
244 std::string Convert(const std::wstring & ws)
\r
246 gchar* str = g_utf16_to_utf8((gunichar2*)ws.c_str(), (glong)ws.length(), NULL, NULL, NULL);
\r
247 std::string s(str);
\r
252 std::wstring Convert(const std::string & s)
\r
254 gunichar2* str = g_utf8_to_utf16(s.c_str(), (glong)s.length(), NULL, NULL, NULL);
\r
255 std::wstring ws((wchar_t*)str);
\r
260 std::string GetDirectoryOfThisModule()
\r
262 std::string result;
\r
263 #if defined(_WIN32)
\r
264 // should be in the same directory as our test fixture
\r
265 WCHAR szFilename[MAX_PATH];
\r
266 GetModuleFileName(NULL, (LPWSTR) &szFilename, sizeof(szFilename));
\r
267 PathRemoveFileSpec((LPWSTR)&szFilename);
\r
269 result = Convert(szFilename);
\r
270 #elif defined(ENABLE_BINRELOC)
\r
271 gchar* prefix = gbr_find_prefix(NULL);
\r
272 result = std::string(prefix);
\r
278 std::string GetRegistryHomeDir()
\r
280 return Convert(GetRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Home_Dir"));
\r
283 void SetRegistryHomeDir(const std::string & dir)
\r
285 SetRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Home_Dir", dir);
\r
288 void ClearRegistryHomeDir()
\r
290 ClearRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Home_Dir");
\r
293 std::string GetUserRegistryModuleDir()
\r
295 return Convert(GetRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Module_Dir"));
\r
299 void SetUserRegistryModuleDir(const std::string & dir)
\r
301 SetRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Module_Dir", dir);
\r
304 void ClearUserRegistryModuleDir()
\r
306 ClearRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Module_Dir");
\r
309 std::string GetMachineRegistryModuleDir()
\r
311 return Convert(GetRegistryValue(HKEY_LOCAL_MACHINE, L"Software\\Enchant\\Config", L"Module_Dir"));
\r
315 void SetMachineRegistryModuleDir(const std::string & dir)
\r
317 SetRegistryValue(HKEY_LOCAL_MACHINE, L"Software\\Enchant\\Config", L"Module_Dir", dir);
\r
320 void ClearMachineRegistryModuleDir()
\r
322 ClearRegistryValue(HKEY_LOCAL_MACHINE, L"Software\\Enchant\\Config", L"Module_Dir");
\r
325 std::string GetUserRegistryConfigDir()
\r
327 return Convert(GetRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Data_Dir"));
\r
330 void SetUserRegistryConfigDir(const std::string & dir)
\r
332 SetRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Data_Dir", dir);
\r
335 void ClearUserRegistryConfigDir()
\r
337 ClearRegistryValue(HKEY_CURRENT_USER, L"Software\\Enchant\\Config", L"Data_Dir");
\r
340 std::string GetMachineRegistryConfigDir()
\r
342 return Convert(GetRegistryValue(HKEY_LOCAL_MACHINE, L"Software\\Enchant\\Config", L"Data_Dir"));
\r
345 void SetMachineRegistryConfigDir(const std::string & dir)
\r
347 SetRegistryValue(HKEY_LOCAL_MACHINE, L"Software\\Enchant\\Config", L"Data_Dir", dir);
\r
350 void ClearMachineRegistryConfigDir()
\r
352 ClearRegistryValue(HKEY_LOCAL_MACHINE, L"Software\\Enchant\\Config", L"Data_Dir");
\r
355 std::wstring GetRegistryValue(HKEY baseKey, const std::wstring& key, const std::wstring& valueName)
\r
357 std::wstring result;
\r
360 DWORD dwSize = sizeof(data) * sizeof(WCHAR);
\r
363 if(RegOpenKeyEx(baseKey,
\r
365 0, KEY_WRITE, &hkey) == ERROR_SUCCESS)
\r
367 if(RegQueryValueEx(hkey,
\r
371 (LPBYTE)&data, &dwSize)
\r
373 result = std::wstring(data,dwSize);
\r
379 void SetRegistryValue(HKEY baseKey,
\r
380 const std::wstring& key,
\r
381 const std::wstring& valueName,
\r
382 const std::string& value)
\r
384 SetRegistryValue(baseKey, key, valueName, Convert(value));
\r
386 void SetRegistryValue(HKEY baseKey,
\r
387 const std::wstring& key,
\r
388 const std::wstring& valueName,
\r
389 const std::wstring& value)
\r
392 if(RegCreateKeyEx(baseKey,
\r
395 REG_OPTION_NON_VOLATILE,
\r
398 NULL) == ERROR_SUCCESS)
\r
400 RegSetValueEx(hkey,
\r
401 valueName.c_str(),
\r
404 (LPBYTE)value.c_str(),
\r
405 (DWORD)((value.length()+1)*sizeof(wchar_t)) );
\r
411 void ClearRegistryValue(HKEY baseKey,
\r
412 const std::wstring& key,
\r
413 const std::wstring& valueName)
\r
416 if(RegOpenKeyEx(baseKey,
\r
418 0, KEY_WRITE, &hkey) == ERROR_SUCCESS)
\r
420 RegDeleteValue(hkey, valueName.c_str());
\r
426 static std::string AddToPath(const std::string & path, const std::string & fileOrDirName)
\r
428 std::string result;
\r
429 gchar* filename = g_build_filename(path.c_str(), fileOrDirName.c_str(), NULL);
\r
430 result = std::string(filename);
\r
436 static std::string GetEnchantHomeDirFromBase(const std::string& basePath)
\r
438 #ifdef XP_TARGET_COCOA
\r
439 return AddToPath(AddToPath(AddToPath(basePath,"Library"),
\r
440 "Application Support"),
\r
442 #elif defined(_WIN32)
\r
443 return AddToPath(basePath,"enchant");
\r
445 return AddToPath(basePath,".enchant");
\r