diff --git a/Projects/settings/project1.lpi b/Projects/settings/project1.lpi index 9604d45..a5b996e 100644 --- a/Projects/settings/project1.lpi +++ b/Projects/settings/project1.lpi @@ -39,7 +39,7 @@ - + @@ -47,10 +47,10 @@ - - + + - + @@ -84,10 +84,10 @@ - - + + - + @@ -95,18 +95,14 @@ - - - - - + + - @@ -124,10 +120,10 @@ - + - - + + @@ -147,123 +143,123 @@ - + - + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - + - + - + - + - + - + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - + - + - - + + - - + + diff --git a/Projects/settings/settings.pas b/Projects/settings/settings.pas index 1c1bab0..7ea14eb 100644 --- a/Projects/settings/settings.pas +++ b/Projects/settings/settings.pas @@ -5,7 +5,7 @@ unit settings; interface uses - Classes, SysUtils, ComCtrls; + Classes, SysUtils, ComCtrls, xmlread, xmlwrite, DOM; @@ -34,6 +34,13 @@ type function WalkToNode(KeyName: String): TTreeNode; function GetNodePath(Node: TTreeNode): String; + procedure InternalLoadFromXML(XMLDoc: TXMLDocument); + procedure WriteXMLData(n: TTreeNode; + XMLNode: TDOMNode; XMLDoc: TXMLDocument; + var XMLChild: TDOMNode; var C: Integer); + procedure WalkTree(Node: TTreeNode; XMLNode: TDOMNode; XMLDoc: TXMLDocument; + var C: Integer); + public function ListKeys(KeyName: String): TStringArray; function KeyExists(KeyName: String): Boolean; @@ -44,6 +51,10 @@ type function GetKeyValue(KeyName: String): String; function GetSetDefaultKeyValue(KeyName, defVal: String): String; + public + procedure LoadFromXML(fileName: String); + procedure SaveToXML(fileName: String); + end; implementation @@ -75,6 +86,54 @@ begin inherited; end; +procedure TMMLSettings.InternalLoadFromXML(XMLDoc: TXMLDocument); +var + iNode: TDOMNode; + + procedure ProcessNode(Node: TDOMNode; TreeNode: TTreeNode); + var + cNode: TDOMNode; + s: string; + d: TSettingData; + + begin + if Node = nil then Exit; // Stops if reached a leaf + + // Adds a node to the tree + if (Node.NodeType = 3) then + s := 'Data' + else + s := Node.NodeName; + + TreeNode := Nodes.AddChild(TreeNode, s); + if (Node.NodeType = 3) then + begin + d := TSettingData.Create; + D.Val := Node.NodeValue; + TreeNode.Data := D; + + TreeNode.Text := 'Value'; + end; + // Goes to the child node + cNode := Node.FirstChild; + + // Processes all child nodes + while cNode <> nil do + begin + ProcessNode(cNode, TreeNode); + cNode := cNode.NextSibling; + end; + end; + +begin + iNode := XMLDoc.DocumentElement; + while iNode <> nil do + begin + ProcessNode(iNode, nil); // Recursive + iNode := iNode.NextSibling; + end; +end; + function TMMLSettings.KeyNameToKeys(KeyName: String): TStringArray; // yay for SRL! function srl_Explode(str, del: string): TStringArray; @@ -126,13 +185,12 @@ begin inc(i); if i = length(s) then break; - n := N.GetFirstChild; + N := N.GetFirstChild; end else - n := n.GetNextSibling; + N := N.GetNextSibling; end; Result := N; - end; function TMMLSettings.GetNodePath(Node: TTreeNode): String; @@ -279,6 +337,7 @@ begin if path[0] <> nParent.Text then begin writeln('First key doesn''t match. First key should always match'); + exit(false); end; for i := 0 to length(Path) - 2 do begin @@ -358,5 +417,73 @@ begin end; end; +procedure TMMLSettings.LoadFromXML(fileName: String); +var + Doc: TXMLDocument; +begin + ReadXMLFile(Doc, fileName); + InternalLoadFromXML(Doc); + Doc.Free; +end; + +procedure TMMLSettings.WriteXMLData(n: TTreeNode; + XMLNode: TDOMNode; XMLDoc: TXMLDocument; + var XMLChild: TDOMNode; var C: Integer); + +var + DDataNode, DataNode: TDOMNode; + +begin + if n.Text = 'Value' then + begin + XMLChild := XMLDoc.CreateTextNode(TSettingData(N.Data).Val); + end else + begin + XMLChild := XMLDoc.CreateElement(n.Text); + end; + Inc(C); + XMLNode.AppendChild(XMLChild); +end; + +procedure TMMLSettings.WalkTree(Node: TTreeNode; XMLNode: TDOMNode; XMLDoc: TXMLDocument; + var C: Integer); +var + N: TTreeNode; + XMLChild: TDOMNode; + +begin + N := Node.GetFirstChild; + + while assigned(n) do + begin + WriteXMLData(n, XMLNode, XMLDoc, XMLChild, C); + + WalkTree(n, XMLChild, XMLDoc, C); + n := n.GetNextSibling; + end; +end; + +procedure TMMLSettings.SaveToXML(fileName: String); +var + XMLDoc: TXMLDocument; + RootNode: TDOMNode; + C: Integer; +begin + XMLDoc := TXMLDocument.Create; + + RootNode := XMLDoc.CreateElement('Settings'); + XMLDoc.AppendChild(RootNode); + + RootNode := XMLDoc.DocumentElement; + + C := 0; + if Nodes.GetFirstNode <> nil then + WalkTree(Nodes.GetFirstNode, RootNode, XMLDoc, C); + + WriteXMLFile(XMLDoc, fileName); + + XMLDoc.Free; +end; + end. diff --git a/Projects/settings/unit1.pas b/Projects/settings/unit1.pas index fcbe453..b5a5de7 100644 --- a/Projects/settings/unit1.pas +++ b/Projects/settings/unit1.pas @@ -6,7 +6,7 @@ interface uses Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, - ComCtrls, StdCtrls, XMLRead, XMLWrite, DOM; + ComCtrls, StdCtrls; type @@ -22,8 +22,6 @@ type private { private declarations } - procedure LoadFromXML(tree: TTreeView; XMLDoc: TXMLDocument); - procedure SaveToXML(s: String); public { public declarations } end; @@ -35,123 +33,6 @@ implementation uses settings; - -procedure WriteXMLData(n: TTreeNode; - XMLNode: TDOMNode; XMLDoc: TXMLDocument; - var XMLChild: TDOMNode; var C: Integer); - -var - DDataNode, DataNode: TDOMNode; - -begin - if n.Text = 'Value' then - begin - XMLChild := XMLDoc.CreateTextNode(TSettingData(N.Data).Val); - end else - begin - XMLChild := XMLDoc.CreateElement(n.Text); - end; - Inc(C); - XMLNode.AppendChild(XMLChild); -end; - -procedure WalkTree(Node: TTreeNode; XMLNode: TDOMNode; XMLDoc: TXMLDocument; - var C: Integer); -var - N: TTreeNode; - XMLChild: TDOMNode; - -begin - N := Node.GetFirstChild; - - while assigned(n) do - begin - WriteXMLData(n, XMLNode, XMLDoc, XMLChild, C); - - WalkTree(n, XMLChild, XMLDoc, C); - n := n.GetNextSibling; - end; -end; - -procedure TForm1.SaveToXML(s: String); -var - XMLDoc: TXMLDocument; - RootNode: TDOMNode; - C: Integer; - -begin - XMLDoc := TXMLDocument.Create; - - RootNode := XMLDoc.CreateElement('Settings'); - XMLDoc.AppendChild(RootNode); - - RootNode := XMLDoc.DocumentElement; - - C := 0; - if TreeView1.Items.GetFirstNode <> nil then - WalkTree(TreeView1.Items.GetFirstNode, RootNode, XMLDoc, C); - - { writeln(TreeView1.Items.TopLvlCount); - for i := 0 to TreeView1.Items.TopLvlCount -1 do - begin - WriteXMLData(TreeView1.Items.TopLvlItems[i], RootNode, XMLDoc, XMLChild, C); - WalkTree(TreeView1.Items.TopLvlItems[i], XMLChild, XMLDoc, C); - end; } - - WriteXMLFile(XMLDoc, s); - - XMLDoc.Free; -end; - -procedure TForm1.LoadFromXML(tree: TTreeView; XMLDoc: TXMLDocument); -var - iNode: TDOMNode; - - procedure ProcessNode(Node: TDOMNode; TreeNode: TTreeNode); - var - cNode: TDOMNode; - s: string; - d: TSettingData; - - begin - if Node = nil then Exit; // Stops if reached a leaf - - // Adds a node to the tree - if (Node.NodeType = 3) then - s := 'Data' - else - s := Node.NodeName; - - TreeNode := tree.Items.AddChild(TreeNode, s); - if (Node.NodeType = 3) then - begin - d := TSettingData.Create; - D.Val := Node.NodeValue; - TreeNode.Data := D; - - TreeNode.Text := 'Value'; - end; - // Goes to the child node - cNode := Node.FirstChild; - - // Processes all child nodes - while cNode <> nil do - begin - ProcessNode(cNode, TreeNode); - cNode := cNode.NextSibling; - end; - end; - -begin - iNode := XMLDoc.DocumentElement; - while iNode <> nil do - begin - ProcessNode(iNode, nil); // Recursive - iNode := iNode.NextSibling; - end; -end; - - { TForm1 } procedure TForm1.SaveButtonClick(Sender: TObject); @@ -184,23 +65,26 @@ begin ss:=ss +'/' + s[0]; end; } + sett.SaveToXML('settings.xml'); sett.Free; - SaveToXML('settings.xml'); + //SaveToXML('settings.xml'); end; procedure TForm1.LoadButtonClick(Sender: TObject); var - doc: TXMLDocument; + s: TMMLSettings; begin if not FileExists('settings.xml') then begin TreeView1.Items.Clear; - SaveToXML('settings.xml'); + s := TMMLSettings.Create(TreeView1.Items); + s.SaveToXML('settings.xml'); + s.Free; end; - ReadXMLFile(doc, 'settings.xml'); TreeView1.Items.Clear; - LoadFromXML(TreeView1, doc); + s := TMMLSettings.Create(TreeView1.Items); + s.LoadFromXML('settings.xml'); end; initialization