From fa173a333240954c958e8243ece96b911f419998 Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Thu, 25 Mar 2010 22:57:18 +0100 Subject: [PATCH] DeleteKey + DeleteSubKeys + Less verbose + Test script. --- Tests/PS/settings_test.simb | 17 +++++++ Units/MMLAddon/PSInc/Wrappers/settings.inc | 15 ++++++ Units/MMLAddon/PSInc/psexportedmethods.inc | 3 ++ Units/MMLAddon/settings.pas | 55 ++++++++++++++++++++++ Units/MMLAddon/settingssandbox.pas | 14 +++++- 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 Tests/PS/settings_test.simb diff --git a/Tests/PS/settings_test.simb b/Tests/PS/settings_test.simb new file mode 100644 index 0000000..1f257ab --- /dev/null +++ b/Tests/PS/settings_test.simb @@ -0,0 +1,17 @@ +program new; +begin +SettingsGetSetDefaultKeyValue('Kanker/wat', 'YO WAT'); +SettingsGetSetDefaultKeyValue('Kanker/wat2', 'YO WAT2'); +writeln(SettingsGetKeyValue('Kanker/wat')); + +if SettingsIsDirectory('Kanker') then + writeln('Kanker has at least one child!'); +if SettingsIsKey('Kanker/wat') then + writeln('wat exists!'); +SettingsDeleteKey('Kanker/wat'); +writeln(SettingsGetKeyValue('Kanker/wat2')); +writeln(SettingsGetKeyValue('Kanker/wat')); +SettingsDeleteSubKeys('Kanker'); +if SettingsIsKey('Kanker') then + writeln('kanker is a key now!'); +end. diff --git a/Units/MMLAddon/PSInc/Wrappers/settings.inc b/Units/MMLAddon/PSInc/Wrappers/settings.inc index 65ef150..2a2552d 100644 --- a/Units/MMLAddon/PSInc/Wrappers/settings.inc +++ b/Units/MMLAddon/PSInc/Wrappers/settings.inc @@ -61,3 +61,18 @@ begin setlength(KeyReturn, 0); end; +function SettingsDeleteKey(KeyName: String): Boolean; +begin + if CurrThread.Sett <> nil then + Result := CurrThread.Sett.DeleteKey(KeyName) + else + result := False; +end; + +function SettingsDeleteSubKeys(KeyName: String): Boolean; +begin + if CurrThread.Sett <> nil then + Result := CurrThread.Sett.DeleteSubKeys(KeyName) + else + result := False; +end; diff --git a/Units/MMLAddon/PSInc/psexportedmethods.inc b/Units/MMLAddon/PSInc/psexportedmethods.inc index d994d8e..ad851f6 100644 --- a/Units/MMLAddon/PSInc/psexportedmethods.inc +++ b/Units/MMLAddon/PSInc/psexportedmethods.inc @@ -372,3 +372,6 @@ AddFunction(@SettingsIsDirectory, 'function SettingsIsDirectory(KeyName: String) AddFunction(@SettingsGetKeyValue, 'function SettingsGetKeyValue(KeyName: String): String;'); AddFunction(@SettingsGetSetDefaultKeyValue, 'function SettingsGetSetDefaultKeyValue(KeyName, defVal: String): String;'); AddFunction(@SettingsListKeys, 'procedure SettingsListKeys(KeyName: String; var KeyReturn: TStringArray);'); + +AddFunction(@SettingsDeleteKey, 'function SettingsDeleteKey(KeyName: String): Boolean;'); +AddFunction(@SettingsDeleteSubKeys, 'function SettingsDeleteSubKeys(KeyName: String): Boolean;'); diff --git a/Units/MMLAddon/settings.pas b/Units/MMLAddon/settings.pas index 3652fd1..18055a2 100644 --- a/Units/MMLAddon/settings.pas +++ b/Units/MMLAddon/settings.pas @@ -77,9 +77,14 @@ type public function GetNodePath(Node: TTreeNode): String; function ListKeys(KeyName: String): TStringArray; + function KeyExists(KeyName: String): Boolean; function IsKey(KeyName: String): Boolean; function IsDirectory(KeyName: String): Boolean; + + function DeleteKey(KeyName: String): Boolean; + function DeleteSubKeys(KeyName: String): Boolean; + procedure SetKeyValue(KeyName: String; KeyValue: String); function CreateKey(KeyName: String; CreatePath: Boolean = False): Boolean; function GetKeyValue(KeyName: String): String; @@ -364,6 +369,56 @@ begin Exit(Res); end; +function TMMLSettings.DeleteKey(KeyName: String): Boolean; +var + Node, C: TTreeNode; +begin + if not isKey(KeyName) and not IsDirectory(KeyName) then + exit(false); + Node := WalkToNode(KeyName); + + if Node = nil then // This should not happen + exit; + + C := Node.GetFirstChild; + if C = nil then + begin + TSettingData(Node.Data).Free; + Node.Delete; + exit; + end; + + While C <> nil do + begin + TSettingData(C.Data).Free; + C := C.GetNextSibling; + end; + Node.DeleteChildren; + + TSettingData(Node.Data).Free; + Node.Delete; +end; + +function TMMLSettings.DeleteSubKeys(KeyName: String): Boolean; +var + Node, C: TTreeNode; +begin + if not isKey(KeyName) and not IsDirectory(KeyName) then + exit(false); + Node := WalkToNode(KeyName); + if Node = nil then // This should not happen + exit; + + C := Node.GetFirstChild; + While C <> nil do + begin + TSettingData(C.Data).Free; + C := C.GetNextSibling; + end; + + Node.DeleteChildren; +end; + { Clear the entire tree. Load from fileName. call GetSetDefaultKeyValue. } diff --git a/Units/MMLAddon/settingssandbox.pas b/Units/MMLAddon/settingssandbox.pas index c87d842..55af658 100644 --- a/Units/MMLAddon/settingssandbox.pas +++ b/Units/MMLAddon/settingssandbox.pas @@ -40,6 +40,9 @@ type function GetKeyValue(KeyName: String): String; function GetSetDefaultKeyValue(KeyName, defVal: String): String; function ListKeys(KeyName: String): TStringArray; + + function DeleteKey(KeyName: String): Boolean; + function DeleteSubKeys(KeyName: String): Boolean; public function GetPrefix: String; procedure SetPrefix(s: String); @@ -81,7 +84,6 @@ end; function TMMLSettingsSandbox.GetKeyValue(KeyName: String): String; begin - writeln('getkeyvalue'); exit(ST.GetKeyValue(Prefix + KeyName)) end; @@ -100,5 +102,15 @@ begin exit(ST.IsDirectory(Prefix + KeyName)) end; +function TMMLSettingsSandbox.DeleteKey(KeyName: String): Boolean; +begin + exit(ST.DeleteKey(Prefix + KeyName)); +end; + +function TMMLSettingsSandbox.DeleteSubKeys(KeyName: String): Boolean; +begin + exit(ST.DeleteSubKeys(Prefix + KeyName)); +end; + end.