1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-12-22 15:28:50 -05:00

DeleteKey + DeleteSubKeys + Less verbose + Test script.

This commit is contained in:
Merlijn Wajer 2010-03-25 22:57:18 +01:00
parent eaeb1669c8
commit fa173a3332
5 changed files with 103 additions and 1 deletions

View File

@ -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.

View File

@ -61,3 +61,18 @@ begin
setlength(KeyReturn, 0); setlength(KeyReturn, 0);
end; 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;

View File

@ -372,3 +372,6 @@ AddFunction(@SettingsIsDirectory, 'function SettingsIsDirectory(KeyName: String)
AddFunction(@SettingsGetKeyValue, 'function SettingsGetKeyValue(KeyName: String): String;'); AddFunction(@SettingsGetKeyValue, 'function SettingsGetKeyValue(KeyName: String): String;');
AddFunction(@SettingsGetSetDefaultKeyValue, 'function SettingsGetSetDefaultKeyValue(KeyName, defVal: String): String;'); AddFunction(@SettingsGetSetDefaultKeyValue, 'function SettingsGetSetDefaultKeyValue(KeyName, defVal: String): String;');
AddFunction(@SettingsListKeys, 'procedure SettingsListKeys(KeyName: String; var KeyReturn: TStringArray);'); AddFunction(@SettingsListKeys, 'procedure SettingsListKeys(KeyName: String; var KeyReturn: TStringArray);');
AddFunction(@SettingsDeleteKey, 'function SettingsDeleteKey(KeyName: String): Boolean;');
AddFunction(@SettingsDeleteSubKeys, 'function SettingsDeleteSubKeys(KeyName: String): Boolean;');

View File

@ -77,9 +77,14 @@ type
public public
function GetNodePath(Node: TTreeNode): String; function GetNodePath(Node: TTreeNode): String;
function ListKeys(KeyName: String): TStringArray; function ListKeys(KeyName: String): TStringArray;
function KeyExists(KeyName: String): Boolean; function KeyExists(KeyName: String): Boolean;
function IsKey(KeyName: String): Boolean; function IsKey(KeyName: String): Boolean;
function IsDirectory(KeyName: String): Boolean; function IsDirectory(KeyName: String): Boolean;
function DeleteKey(KeyName: String): Boolean;
function DeleteSubKeys(KeyName: String): Boolean;
procedure SetKeyValue(KeyName: String; KeyValue: String); procedure SetKeyValue(KeyName: String; KeyValue: String);
function CreateKey(KeyName: String; CreatePath: Boolean = False): Boolean; function CreateKey(KeyName: String; CreatePath: Boolean = False): Boolean;
function GetKeyValue(KeyName: String): String; function GetKeyValue(KeyName: String): String;
@ -364,6 +369,56 @@ begin
Exit(Res); Exit(Res);
end; 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. Clear the entire tree. Load from fileName. call GetSetDefaultKeyValue.
} }

View File

@ -40,6 +40,9 @@ type
function GetKeyValue(KeyName: String): String; function GetKeyValue(KeyName: String): String;
function GetSetDefaultKeyValue(KeyName, defVal: String): String; function GetSetDefaultKeyValue(KeyName, defVal: String): String;
function ListKeys(KeyName: String): TStringArray; function ListKeys(KeyName: String): TStringArray;
function DeleteKey(KeyName: String): Boolean;
function DeleteSubKeys(KeyName: String): Boolean;
public public
function GetPrefix: String; function GetPrefix: String;
procedure SetPrefix(s: String); procedure SetPrefix(s: String);
@ -81,7 +84,6 @@ end;
function TMMLSettingsSandbox.GetKeyValue(KeyName: String): String; function TMMLSettingsSandbox.GetKeyValue(KeyName: String): String;
begin begin
writeln('getkeyvalue');
exit(ST.GetKeyValue(Prefix + KeyName)) exit(ST.GetKeyValue(Prefix + KeyName))
end; end;
@ -100,5 +102,15 @@ begin
exit(ST.IsDirectory(Prefix + KeyName)) exit(ST.IsDirectory(Prefix + KeyName))
end; 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. end.