浏览代码

Refactoring ImportHostKeys

Source commit: f7be1dd37173a14e3bc1a4a4c445bf79e469c907
Martin Prikryl 7 年之前
父节点
当前提交
15310afcbf
共有 2 个文件被更改,包括 18 次插入37 次删除
  1. 17 35
      source/core/SessionData.cpp
  2. 1 2
      source/core/SessionData.h

+ 17 - 35
source/core/SessionData.cpp

@@ -4685,53 +4685,35 @@ THierarchicalStorage * __fastcall TStoredSessionList::CreateHostKeysStorageForWr
 }
 //---------------------------------------------------------------------------
 void __fastcall TStoredSessionList::ImportHostKeys(
-  const UnicodeString SourceKey, TStoredSessionList * Sessions,
-  bool OnlySelected)
+  const UnicodeString & SourceKey, TStoredSessionList * Sessions, bool OnlySelected)
 {
-  TRegistryStorage * SourceStorage = NULL;
-  THierarchicalStorage * TargetStorage = NULL;
-  TStringList * KeyList = NULL;
-  try
+  std::auto_ptr<THierarchicalStorage> TargetStorage(CreateHostKeysStorageForWritting());
+  std::auto_ptr<THierarchicalStorage> SourceStorage(new TRegistryStorage(SourceKey));
+
+  if ((TargetStorage.get() != NULL) &&
+      SourceStorage->OpenRootKey(false))
   {
-    TargetStorage = CreateHostKeysStorageForWritting();
-    SourceStorage = new TRegistryStorage(SourceKey);
-    KeyList = new TStringList();
+    std::auto_ptr<TStringList> KeyList(new TStringList());
+    SourceStorage->GetValueNames(KeyList.get());
 
-    if ((TargetStorage != NULL) &&
-        SourceStorage->OpenRootKey(false))
+    DebugAssert(Sessions != NULL);
+    for (int Index = 0; Index < Sessions->Count; Index++)
     {
-      SourceStorage->GetValueNames(KeyList);
-
-      TSessionData * Session;
-      UnicodeString HostKeyName;
-      DebugAssert(Sessions != NULL);
-      for (int Index = 0; Index < Sessions->Count; Index++)
+      TSessionData * Session = Sessions->Sessions[Index];
+      if (!OnlySelected || Session->Selected)
       {
-        Session = Sessions->Sessions[Index];
-        if (!OnlySelected || Session->Selected)
+        UnicodeString HostKeyName = PuttyMungeStr(FORMAT(L"@%d:%s", (Session->PortNumber, Session->HostNameExpanded)));
+        for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
         {
-          HostKeyName = PuttyMungeStr(FORMAT(L"@%d:%s", (Session->PortNumber, Session->HostNameExpanded)));
-          UnicodeString KeyName;
-          for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
+          UnicodeString KeyName = KeyList->Strings[KeyIndex];
+          if (EndsText(HostKeyName, KeyName))
           {
-            KeyName = KeyList->Strings[KeyIndex];
-            int P = KeyName.Pos(HostKeyName);
-            if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
-            {
-              TargetStorage->WriteStringRaw(KeyName,
-                SourceStorage->ReadStringRaw(KeyName, L""));
-            }
+            TargetStorage->WriteStringRaw(KeyName, SourceStorage->ReadStringRaw(KeyName, L""));
           }
         }
       }
     }
   }
-  __finally
-  {
-    delete SourceStorage;
-    delete TargetStorage;
-    delete KeyList;
-  }
 }
 //---------------------------------------------------------------------------
 void __fastcall TStoredSessionList::ImportSelectedKnownHosts(TStoredSessionList * Sessions)

+ 1 - 2
source/core/SessionData.h

@@ -698,8 +698,7 @@ public:
   __property TSessionData * DefaultSettings  = { read=FDefaultSettings, write=SetDefaultSettings };
 
   static void __fastcall ImportHostKeys(
-    const UnicodeString SourceKey, TStoredSessionList * Sessions,
-    bool OnlySelected);
+    const UnicodeString & SourceKey, TStoredSessionList * Sessions, bool OnlySelected);
   static void __fastcall ImportSelectedKnownHosts(TStoredSessionList * Sessions);
 
 private: