OSDN Git Service

Fix import/export WinMerge.reg error. close #16
authorsdottaka <none@none>
Sat, 16 Mar 2013 22:46:38 +0000 (07:46 +0900)
committersdottaka <none@none>
Sat, 16 Mar 2013 22:46:38 +0000 (07:46 +0900)
Src/Environment.cpp
Src/Environment.h
Src/Merge.cpp
Testing/GoogleTest/Environment/Environemt_test.cpp [new file with mode: 0644]
Testing/GoogleTest/UnitTests/UnitTests.vcproj

index 0ebf367..9b23a31 100644 (file)
@@ -163,3 +163,41 @@ String env_GetSystemTempPath()
                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);
+}
index 6f181f7..e3863eb 100644 (file)
@@ -24,4 +24,7 @@ String env_GetSystemTempPath();
 
 String env_GetPerInstanceString(const String& name);
 
+bool env_LoadRegistryFromFile(const String& sRegFilePath);
+bool env_SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir);
+
 #endif // _ENVIRONMENT_H_
index b1f3273..fbd38a2 100644 (file)
 // $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"
@@ -270,33 +266,8 @@ BOOL CMergeApp::InitInstance()
 #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
 
@@ -539,40 +510,9 @@ void CMergeApp::OnUpdateViewLanguage(CCmdUI* pCmdUI)
 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();
diff --git a/Testing/GoogleTest/Environment/Environemt_test.cpp b/Testing/GoogleTest/Environment/Environemt_test.cpp
new file mode 100644 (file)
index 0000000..9cb08e8
--- /dev/null
@@ -0,0 +1,68 @@
+#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
index 2d08813..34e8c47 100644 (file)
                                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