浏览代码

Refactoring

Partially returning to the previous version of the code before 5.0, as the changed code seems unnecessarily complicated

Source commit: 5b4cb012b89ab1126100a22df7298ceeaa315f3d
Martin Prikryl 7 年之前
父节点
当前提交
4245e45bf3
共有 1 个文件被更改,包括 17 次插入14 次删除
  1. 17 14
      source/core/HierarchicalStorage.cpp

+ 17 - 14
source/core/HierarchicalStorage.cpp

@@ -160,35 +160,26 @@ UnicodeString __fastcall THierarchicalStorage::MungeKeyName(UnicodeString Key)
 //---------------------------------------------------------------------------
 bool __fastcall THierarchicalStorage::OpenSubKey(UnicodeString Key, bool CanCreate, bool Path)
 {
-  bool Result;
   UnicodeString MungedKey;
   if (Path)
   {
     DebugAssert(Key.IsEmpty() || (Key[Key.Length()] != L'\\'));
-    Result = true;
-    while (!Key.IsEmpty() && Result)
+    while (!Key.IsEmpty())
     {
       if (!MungedKey.IsEmpty())
       {
         MungedKey += L'\\';
       }
       MungedKey += MungeKeyName(CutToChar(Key, L'\\', false));
-      Result = DoOpenSubKey(MungedKey, CanCreate);
-    }
-
-    // hack to restore last opened key for registry storage
-    if (!Result)
-    {
-      FKeyHistory->Add(IncludeTrailingBackslash(CurrentSubKey+MungedKey));
-      CloseSubKey();
     }
   }
   else
   {
     MungedKey = MungeKeyName(Key);
-    Result = DoOpenSubKey(MungedKey, CanCreate);
   }
 
+  bool Result = DoOpenSubKey(MungedKey, CanCreate);
+
   if (Result)
   {
     FKeyHistory->Add(IncludeTrailingBackslash(CurrentSubKey+MungedKey));
@@ -520,9 +511,21 @@ void __fastcall TRegistryStorage::SetAccessMode(TStorageAccessMode value)
 //---------------------------------------------------------------------------
 bool __fastcall TRegistryStorage::DoOpenSubKey(const UnicodeString SubKey, bool CanCreate)
 {
-  if (FKeyHistory->Count > 0) FRegistry->CloseKey();
+  UnicodeString PrevPath;
+  bool WasOpened = (FRegistry->CurrentKey != NULL);
+  if (WasOpened)
+  {
+    PrevPath = FRegistry->CurrentPath;
+    DebugAssert(SamePaths(PrevPath, Storage + GetCurrentSubKeyMunged()));
+    FRegistry->CloseKey();
+  }
   UnicodeString K = ExcludeTrailingBackslash(Storage + CurrentSubKey + SubKey);
-  return FRegistry->OpenKey(K, CanCreate);
+  bool Result = FRegistry->OpenKey(K, CanCreate);
+  if (!Result && WasOpened)
+  {
+    FRegistry->OpenKey(PrevPath, false);
+  }
+  return Result;
 }
 //---------------------------------------------------------------------------
 void __fastcall TRegistryStorage::CloseSubKey()