return _T("");
}
}
+
+static bool launchProgram(const String& sCmd, WORD wShowWindow)
+{
+ STARTUPINFO stInfo = {0};
+ stInfo.cb = sizeof(STARTUPINFO);
+ stInfo.dwFlags = STARTF_USESHOWWINDOW;
+ stInfo.wShowWindow = wShowWindow;
+ PROCESS_INFORMATION processInfo;
+ BOOL retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
+ NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
+ &stInfo, &processInfo);
+ if (!retVal)
+ return false;
+ CloseHandle(processInfo.hThread);
+ CloseHandle(processInfo.hProcess);
+ return true;
+}
+
+/**
+ * @brief Load registry keys from .reg file if existing .reg file
+ */
+bool env_LoadRegistryFromFile(const String& sRegFilePath)
+{
+ if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
+ return false;
+ return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE);
+}
+
+/**
+ * @brief Save registry keys to .reg file if existing .reg file
+ */
+bool env_SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir)
+{
+ if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE)
+ return false;
+ DeleteFile(sRegFilePath.c_str());
+ return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE);
+}
String env_GetPerInstanceString(const String& name);
+bool env_LoadRegistryFromFile(const String& sRegFilePath);
+bool env_SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir);
+
#endif // _ENVIRONMENT_H_
// $Id: Merge.cpp 6861 2009-06-25 12:11:07Z kimmov $
#include "stdafx.h"
-#define POCO_NO_UNWINDOWS 1
-#include <Poco/Process.h>
-#include <Poco/Format.h>
-#include <Poco/Exception.h>
#include "Constants.h"
#include "UnicodeString.h"
#include "unicoder.h"
#endif
#endif
- // Load registry file if existing
- String sRegPath = env_GetProgPath() + _T("\\WinMerge.reg");
- if (paths_DoesPathExist(sRegPath) == IS_EXISTING_FILE)
- {
- std::string sCmd, sa;
- std::vector<std::string> sArgs;
- if (SearchPath(NULL, _T("reg.exe"), NULL, 0, NULL, NULL) == 0)
- {
- sCmd = "reg.exe";
- sArgs.push_back("/s");
- }
- else
- {
- sCmd = "regedit.exe";
- sArgs.push_back("import");
- }
- Poco::format(sa, "\"%s\"", ucr::toUTF8(sRegPath));
- sArgs.push_back(sa);
- try
- {
- Poco::ProcessHandle handle(Poco::Process::launch(sCmd, sArgs));
- Poco::Process::wait(handle);
- }
- catch (Poco::SystemException)
- {
- }
- }
+ // Load registry keys from WinMerge.reg if existing WinMerge.reg
+ env_LoadRegistryFromFile(env_GetProgPath() + _T("\\WinMerge.reg"));
OptionsInit(); // Implementation in OptionsInit.cpp
int CMergeApp::ExitInstance()
{
charsets_cleanup();
- String sRegPath = env_GetProgPath() + _T("\\WinMerge.reg");
- if (paths_DoesPathExist(sRegPath) == IS_EXISTING_FILE)
- {
- DeleteFile(sRegPath.c_str());
- std::string sCmd, spath, sdir;
- std::vector<std::string> sArgs;
- if (SearchPath(NULL, _T("reg.exe"), NULL, 0, NULL, NULL) == 0)
- {
- sCmd = "reg.exe";
- sArgs.push_back("/s");
- sArgs.push_back("/e");
- Poco::format(spath, "\"%s\"", ucr::toUTF8(sRegPath));
- sArgs.push_back(spath);
- Poco::format(sdir, "HKEY_CURRENT_USER\\%s", ucr::toUTF8(f_RegDir));
- sArgs.push_back(sdir);
- }
- else
- {
- sCmd = "reg.exe";
- sArgs.push_back("export");
- Poco::format(sdir, "HKCU\\%s", ucr::toUTF8(f_RegDir));
- sArgs.push_back(sdir);
- Poco::format(spath, "\"%s\"", ucr::toUTF8(sRegPath));
- sArgs.push_back(spath);
- }
- try
- {
- Poco::ProcessHandle handle(Poco::Process::launch(sCmd, sArgs));
- Poco::Process::wait(handle);
- }
- catch (Poco::SystemException)
- {
- }
- }
+
+ // Save registry keys if existing WinMerge.reg
+ env_SaveRegistryToFile(env_GetProgPath() + _T("\\WinMerge.reg"), f_RegDir);
// Remove tempfolder
const String temp = env_GetTempPath();
--- /dev/null
+#include <gtest/gtest.h>
+#include "Environment.h"
+
+namespace
+{
+ // The fixture for testing paths functions.
+ class EnvironmentTest : public testing::Test
+ {
+ protected:
+ // You can remove any or all of the following functions if its body
+ // is empty.
+
+ EnvironmentTest()
+ {
+ // You can do set-up work for each test here.
+ }
+
+ virtual ~EnvironmentTest()
+ {
+ // You can do clean-up work that doesn't throw exceptions here.
+ }
+
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp()
+ {
+ // Code here will be called immediately after the constructor (right
+ // before each test).
+ }
+
+ virtual void TearDown()
+ {
+ // Code here will be called immediately after each test (right
+ // before the destructor).
+ }
+
+ // Objects declared here can be used by all tests in the test case for Foo.
+ };
+
+ TEST_F(EnvironmentTest, LoadRegistryFromFile)
+ {
+ EXPECT_EQ(false, env_LoadRegistryFromFile(_T("nonexistfile.reg")));
+
+ system(
+ "reg add HKCU\\Software\\Thingamahoochie\\test /v Path /t REG_EXPAND_SZ /d \"abc\" & "
+ "reg export HKCU\\Software\\Thingamahoochie\\test test.reg & "
+ "reg delete HKCU\\Software\\Thingamahoochie\\test /f");
+ EXPECT_EQ(true, env_LoadRegistryFromFile(_T("test.reg")));
+ EXPECT_EQ(0, system("reg query HKCU\\Software\\Thingamahoochie\\test /v Path | findstr abc"));
+ system("reg delete HKCU\\Software\\Thingamahoochie\\test /f");
+ remove("test.reg");
+ }
+
+ TEST_F(EnvironmentTest, SaveRegistry)
+ {
+ EXPECT_EQ(false, env_SaveRegistryToFile(_T("nonexistfile.reg"), _T("Software\\Thingamahoochie\\tes")));
+
+ system("reg add HKCU\\Software\\Thingamahoochie\\test /v Path /t REG_EXPAND_SZ /d \"abc\"");
+ system("echo. > test2.reg");
+ EXPECT_EQ(true, env_SaveRegistryToFile(_T("test2.reg"), _T("Software\\Thingamahoochie\\test")));
+ EXPECT_EQ(0, system("type test2.reg|findstr Path"));
+ system("reg delete HKCU\\Software\\Thingamahoochie\\test /f");
+ remove("test2.reg");
+ }
+
+
+} // namespace
RelativePath=".\..\DirItem\DirItem_test.cpp">\r
</File>\r
<File\r
+ RelativePath="..\Environment\Environemt_test.cpp">\r
+ </File>\r
+ <File\r
RelativePath="..\FileFilter\FileFilterHelper_test.cpp">\r
</File>\r
<File\r