1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-24 18:22:25 -05:00

Final, I think.

git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@378 3f818213-9676-44b0-a9b4-5e4c4e03d09d
This commit is contained in:
Wizzup? 2010-01-06 19:06:20 +00:00
parent aa5fb8bda8
commit e4b17843d5
3 changed files with 198 additions and 191 deletions

View File

@ -39,7 +39,7 @@
<Filename Value="project1.lpr"/> <Filename Value="project1.lpr"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="project1"/> <UnitName Value="project1"/>
<UsageCount Value="35"/> <UsageCount Value="39"/>
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="unit1.pas"/> <Filename Value="unit1.pas"/>
@ -47,10 +47,10 @@
<ComponentName Value="Form1"/> <ComponentName Value="Form1"/>
<ResourceBaseClass Value="Form"/> <ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/> <UnitName Value="Unit1"/>
<CursorPos X="32" Y="174"/> <CursorPos X="1" Y="24"/>
<TopLine Value="151"/> <TopLine Value="5"/>
<EditorIndex Value="0"/> <EditorIndex Value="0"/>
<UsageCount Value="35"/> <UsageCount Value="39"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit1> </Unit1>
<Unit2> <Unit2>
@ -84,10 +84,10 @@
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="settings"/> <UnitName Value="settings"/>
<CursorPos X="1" Y="194"/> <CursorPos X="32" Y="483"/>
<TopLine Value="168"/> <TopLine Value="453"/>
<EditorIndex Value="1"/> <EditorIndex Value="1"/>
<UsageCount Value="33"/> <UsageCount Value="37"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit5> </Unit5>
<Unit6> <Unit6>
@ -95,18 +95,14 @@
<UnitName Value="updater"/> <UnitName Value="updater"/>
<CursorPos X="2" Y="88"/> <CursorPos X="2" Y="88"/>
<TopLine Value="55"/> <TopLine Value="55"/>
<EditorIndex Value="3"/>
<UsageCount Value="16"/> <UsageCount Value="16"/>
<Loaded Value="True"/>
</Unit6> </Unit6>
<Unit7> <Unit7>
<Filename Value="../../../../Documents/lazarus/lcl/comctrls.pp"/> <Filename Value="../../../../Documents/lazarus/lcl/comctrls.pp"/>
<UnitName Value="ComCtrls"/> <UnitName Value="ComCtrls"/>
<CursorPos X="77" Y="1920"/> <CursorPos X="3" Y="2190"/>
<TopLine Value="1902"/> <TopLine Value="2172"/>
<EditorIndex Value="2"/>
<UsageCount Value="13"/> <UsageCount Value="13"/>
<Loaded Value="True"/>
</Unit7> </Unit7>
<Unit8> <Unit8>
<Filename Value="../../../../Documents/lazarus/lcl/include/treeview.inc"/> <Filename Value="../../../../Documents/lazarus/lcl/include/treeview.inc"/>
@ -124,10 +120,10 @@
<Unit10> <Unit10>
<Filename Value="../../../../Documents/fpc/rtl/objpas/strutils.pp"/> <Filename Value="../../../../Documents/fpc/rtl/objpas/strutils.pp"/>
<UnitName Value="strutils"/> <UnitName Value="strutils"/>
<CursorPos X="1" Y="176"/> <CursorPos X="70" Y="25"/>
<TopLine Value="1"/> <TopLine Value="1"/>
<EditorIndex Value="4"/> <EditorIndex Value="2"/>
<UsageCount Value="16"/> <UsageCount Value="18"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit10> </Unit10>
<Unit11> <Unit11>
@ -147,123 +143,123 @@
<JumpHistory Count="30" HistoryIndex="29"> <JumpHistory Count="30" HistoryIndex="29">
<Position1> <Position1>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="173" Column="1" TopLine="161"/> <Caret Line="40" Column="21" TopLine="22"/>
</Position1> </Position1>
<Position2> <Position2>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="184" Column="3" TopLine="166"/> <Caret Line="325" Column="30" TopLine="306"/>
</Position2> </Position2>
<Position3> <Position3>
<Filename Value="unit1.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="154" Column="1" TopLine="137"/> <Caret Line="49" Column="61" TopLine="23"/>
</Position3> </Position3>
<Position4> <Position4>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="76" Column="3" TopLine="56"/> <Caret Line="242" Column="6" TopLine="215"/>
</Position4> </Position4>
<Position5> <Position5>
<Filename Value="unit1.pas"/> <Filename Value="settings.pas"/>
<Caret Line="65" Column="32" TopLine="39"/> <Caret Line="235" Column="31" TopLine="213"/>
</Position5> </Position5>
<Position6> <Position6>
<Filename Value="unit1.pas"/> <Filename Value="settings.pas"/>
<Caret Line="42" Column="6" TopLine="42"/> <Caret Line="14" Column="46" TopLine="1"/>
</Position6> </Position6>
<Position7> <Position7>
<Filename Value="unit1.pas"/> <Filename Value="settings.pas"/>
<Caret Line="70" Column="2" TopLine="56"/> <Caret Line="288" Column="26" TopLine="270"/>
</Position7> </Position7>
<Position8> <Position8>
<Filename Value="unit1.pas"/> <Filename Value="settings.pas"/>
<Caret Line="75" Column="8" TopLine="62"/> <Caret Line="337" Column="1" TopLine="319"/>
</Position8> </Position8>
<Position9> <Position9>
<Filename Value="unit1.pas"/> <Filename Value="settings.pas"/>
<Caret Line="80" Column="1" TopLine="52"/> <Caret Line="248" Column="38" TopLine="224"/>
</Position9> </Position9>
<Position10> <Position10>
<Filename Value="unit1.pas"/> <Filename Value="settings.pas"/>
<Caret Line="79" Column="3" TopLine="51"/> <Caret Line="42" Column="28" TopLine="23"/>
</Position10> </Position10>
<Position11> <Position11>
<Filename Value="unit1.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="173" Column="19" TopLine="143"/> <Caret Line="169" Column="51" TopLine="141"/>
</Position11> </Position11>
<Position12> <Position12>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="38" Column="24" TopLine="24"/> <Caret Line="251" Column="63" TopLine="1"/>
</Position12> </Position12>
<Position13> <Position13>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="326" Column="45" TopLine="294"/> <Caret Line="23" Column="53" TopLine="1"/>
</Position13> </Position13>
<Position14> <Position14>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="314" Column="13" TopLine="296"/> <Caret Line="194" Column="12" TopLine="176"/>
</Position14> </Position14>
<Position15> <Position15>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="40" Column="21" TopLine="22"/> <Caret Line="224" Column="9" TopLine="209"/>
</Position15> </Position15>
<Position16> <Position16>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="325" Column="30" TopLine="306"/> <Caret Line="40" Column="21" TopLine="22"/>
</Position16> </Position16>
<Position17> <Position17>
<Filename Value="unit1.pas"/> <Filename Value="settings.pas"/>
<Caret Line="49" Column="61" TopLine="23"/> <Caret Line="32" Column="21" TopLine="12"/>
</Position17> </Position17>
<Position18> <Position18>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="242" Column="6" TopLine="215"/> <Caret Line="98" Column="22" TopLine="88"/>
</Position18> </Position18>
<Position19> <Position19>
<Filename Value="settings.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="235" Column="31" TopLine="213"/> <Caret Line="48" Column="69" TopLine="1"/>
</Position19> </Position19>
<Position20> <Position20>
<Filename Value="settings.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="14" Column="46" TopLine="1"/> <Caret Line="105" Column="29" TopLine="85"/>
</Position20> </Position20>
<Position21> <Position21>
<Filename Value="settings.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="288" Column="26" TopLine="270"/> <Caret Line="110" Column="16" TopLine="85"/>
</Position21> </Position21>
<Position22> <Position22>
<Filename Value="settings.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="337" Column="1" TopLine="319"/> <Caret Line="113" Column="29" TopLine="85"/>
</Position22> </Position22>
<Position23> <Position23>
<Filename Value="settings.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="248" Column="38" TopLine="224"/> <Caret Line="206" Column="7" TopLine="180"/>
</Position23> </Position23>
<Position24> <Position24>
<Filename Value="settings.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="42" Column="28" TopLine="23"/> <Caret Line="25" Column="60" TopLine="14"/>
</Position24> </Position24>
<Position25> <Position25>
<Filename Value="unit1.pas"/> <Filename Value="settings.pas"/>
<Caret Line="169" Column="51" TopLine="141"/> <Caret Line="509" Column="65" TopLine="481"/>
</Position25> </Position25>
<Position26> <Position26>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="251" Column="63" TopLine="1"/> <Caret Line="464" Column="5" TopLine="448"/>
</Position26> </Position26>
<Position27> <Position27>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="23" Column="53" TopLine="1"/> <Caret Line="471" Column="1" TopLine="451"/>
</Position27> </Position27>
<Position28> <Position28>
<Filename Value="settings.pas"/> <Filename Value="settings.pas"/>
<Caret Line="194" Column="12" TopLine="176"/> <Caret Line="481" Column="19" TopLine="453"/>
</Position28> </Position28>
<Position29> <Position29>
<Filename Value="settings.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="224" Column="9" TopLine="209"/> <Caret Line="79" Column="1" TopLine="72"/>
</Position29> </Position29>
<Position30> <Position30>
<Filename Value="settings.pas"/> <Filename Value="unit1.pas"/>
<Caret Line="40" Column="21" TopLine="22"/> <Caret Line="26" Column="2" TopLine="8"/>
</Position30> </Position30>
</JumpHistory> </JumpHistory>
</ProjectOptions> </ProjectOptions>

View File

@ -5,7 +5,7 @@ unit settings;
interface interface
uses uses
Classes, SysUtils, ComCtrls; Classes, SysUtils, ComCtrls, xmlread, xmlwrite, DOM;
@ -34,6 +34,13 @@ type
function WalkToNode(KeyName: String): TTreeNode; function WalkToNode(KeyName: String): TTreeNode;
function GetNodePath(Node: TTreeNode): String; 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 public
function ListKeys(KeyName: String): TStringArray; function ListKeys(KeyName: String): TStringArray;
function KeyExists(KeyName: String): Boolean; function KeyExists(KeyName: String): Boolean;
@ -44,6 +51,10 @@ type
function GetKeyValue(KeyName: String): String; function GetKeyValue(KeyName: String): String;
function GetSetDefaultKeyValue(KeyName, defVal: String): String; function GetSetDefaultKeyValue(KeyName, defVal: String): String;
public
procedure LoadFromXML(fileName: String);
procedure SaveToXML(fileName: String);
end; end;
implementation implementation
@ -75,6 +86,54 @@ begin
inherited; inherited;
end; 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; function TMMLSettings.KeyNameToKeys(KeyName: String): TStringArray;
// yay for SRL! // yay for SRL!
function srl_Explode(str, del: string): TStringArray; function srl_Explode(str, del: string): TStringArray;
@ -126,13 +185,12 @@ begin
inc(i); inc(i);
if i = length(s) then if i = length(s) then
break; break;
n := N.GetFirstChild; N := N.GetFirstChild;
end else end else
n := n.GetNextSibling; N := N.GetNextSibling;
end; end;
Result := N; Result := N;
end; end;
function TMMLSettings.GetNodePath(Node: TTreeNode): String; function TMMLSettings.GetNodePath(Node: TTreeNode): String;
@ -279,6 +337,7 @@ begin
if path[0] <> nParent.Text then if path[0] <> nParent.Text then
begin begin
writeln('First key doesn''t match. First key should always match'); writeln('First key doesn''t match. First key should always match');
exit(false);
end; end;
for i := 0 to length(Path) - 2 do for i := 0 to length(Path) - 2 do
begin begin
@ -358,5 +417,73 @@ begin
end; end;
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. end.

View File

@ -6,7 +6,7 @@ interface
uses uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
ComCtrls, StdCtrls, XMLRead, XMLWrite, DOM; ComCtrls, StdCtrls;
type type
@ -22,8 +22,6 @@ type
private private
{ private declarations } { private declarations }
procedure LoadFromXML(tree: TTreeView; XMLDoc: TXMLDocument);
procedure SaveToXML(s: String);
public public
{ public declarations } { public declarations }
end; end;
@ -35,123 +33,6 @@ implementation
uses uses
settings; 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 } { TForm1 }
procedure TForm1.SaveButtonClick(Sender: TObject); procedure TForm1.SaveButtonClick(Sender: TObject);
@ -184,23 +65,26 @@ begin
ss:=ss +'/' + s[0]; ss:=ss +'/' + s[0];
end; } end; }
sett.SaveToXML('settings.xml');
sett.Free; sett.Free;
SaveToXML('settings.xml'); //SaveToXML('settings.xml');
end; end;
procedure TForm1.LoadButtonClick(Sender: TObject); procedure TForm1.LoadButtonClick(Sender: TObject);
var var
doc: TXMLDocument; s: TMMLSettings;
begin begin
if not FileExists('settings.xml') then if not FileExists('settings.xml') then
begin begin
TreeView1.Items.Clear; TreeView1.Items.Clear;
SaveToXML('settings.xml'); s := TMMLSettings.Create(TreeView1.Items);
s.SaveToXML('settings.xml');
s.Free;
end; end;
ReadXMLFile(doc, 'settings.xml');
TreeView1.Items.Clear; TreeView1.Items.Clear;
LoadFromXML(TreeView1, doc); s := TMMLSettings.Create(TreeView1.Items);
s.LoadFromXML('settings.xml');
end; end;
initialization initialization