1 |
torben |
2671 |
(* ***** BEGIN LICENSE BLOCK *****
|
2 |
|
|
* Version: MPL 1.1
|
3 |
|
|
*
|
4 |
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
5 |
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
6 |
|
|
* the License. You may obtain a copy of the License at
|
7 |
|
|
* http://www.mozilla.org/MPL/
|
8 |
|
|
*
|
9 |
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
10 |
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
11 |
|
|
* for the specific language governing rights and limitations under the
|
12 |
|
|
* License.
|
13 |
|
|
*
|
14 |
|
|
* The Original Code is TurboPower SysTools
|
15 |
|
|
*
|
16 |
|
|
* The Initial Developer of the Original Code is
|
17 |
|
|
* TurboPower Software
|
18 |
|
|
*
|
19 |
|
|
* Portions created by the Initial Developer are Copyright (C) 1996-2002
|
20 |
|
|
* the Initial Developer. All Rights Reserved.
|
21 |
|
|
*
|
22 |
|
|
* Contributor(s):
|
23 |
|
|
*
|
24 |
|
|
* ***** END LICENSE BLOCK ***** *)
|
25 |
|
|
|
26 |
|
|
unit ExTreeU;
|
27 |
|
|
|
28 |
|
|
interface
|
29 |
|
|
|
30 |
|
|
uses
|
31 |
|
|
Windows, Messages, SysUtils, Classes, Graphics, Controls,
|
32 |
|
|
Forms, Dialogs, StdCtrls,
|
33 |
|
|
|
34 |
|
|
StConst, StBase, StTree;
|
35 |
|
|
|
36 |
|
|
type
|
37 |
|
|
S10 = String[10];
|
38 |
|
|
S15 = String[15];
|
39 |
|
|
|
40 |
|
|
PersonRecord = record
|
41 |
|
|
First : S10;
|
42 |
|
|
Last : S15;
|
43 |
|
|
Age : Integer;
|
44 |
|
|
end;
|
45 |
|
|
PPersonRecord = ^PersonRecord;
|
46 |
|
|
|
47 |
|
|
TSTDlg = class(TForm)
|
48 |
|
|
CreateBtn: TButton;
|
49 |
|
|
ClearBtn: TButton;
|
50 |
|
|
LB1: TListBox;
|
51 |
|
|
Label1: TLabel;
|
52 |
|
|
Label2: TLabel;
|
53 |
|
|
Label3: TLabel;
|
54 |
|
|
Edit1: TEdit;
|
55 |
|
|
Edit2: TEdit;
|
56 |
|
|
Edit3: TEdit;
|
57 |
|
|
InsertBtn: TButton;
|
58 |
|
|
DeleteBtn: TButton;
|
59 |
|
|
FindBtn: TButton;
|
60 |
|
|
SearchBtn: TButton;
|
61 |
|
|
LoadBtn: TButton;
|
62 |
|
|
SaveBtn: TButton;
|
63 |
|
|
OD1: TOpenDialog;
|
64 |
|
|
SD1: TSaveDialog;
|
65 |
|
|
procedure FormActivate(Sender: TObject);
|
66 |
|
|
procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
67 |
|
|
procedure CreateBtnClick(Sender: TObject);
|
68 |
|
|
procedure ClearBtnClick(Sender: TObject);
|
69 |
|
|
procedure InsertBtnClick(Sender: TObject);
|
70 |
|
|
procedure DeleteBtnClick(Sender: TObject);
|
71 |
|
|
procedure FindBtnClick(Sender: TObject);
|
72 |
|
|
procedure SearchBtnClick(Sender: TObject);
|
73 |
|
|
procedure LB1DblClick(Sender: TObject);
|
74 |
|
|
procedure SaveBtnClick(Sender: TObject);
|
75 |
|
|
procedure LoadBtnClick(Sender: TObject);
|
76 |
|
|
procedure FormCreate(Sender: TObject);
|
77 |
|
|
private
|
78 |
|
|
{ Private declarations }
|
79 |
|
|
public
|
80 |
|
|
{ Public declarations }
|
81 |
|
|
procedure SetBusy(B : Boolean);
|
82 |
|
|
procedure FillListBox;
|
83 |
|
|
procedure FillControls(PR : PersonRecord);
|
84 |
|
|
function GetControls(var PR : PersonRecord) : Boolean;
|
85 |
|
|
procedure UpdateButtons(TOK : Boolean);
|
86 |
|
|
end;
|
87 |
|
|
|
88 |
|
|
const
|
89 |
|
|
MaxElem = 3000;
|
90 |
|
|
|
91 |
|
|
var
|
92 |
|
|
STDlg: TSTDlg;
|
93 |
|
|
FirstA : array[0..7] of S10;
|
94 |
|
|
LastA : array[0..7] of S15;
|
95 |
|
|
MyTree : TStTree;
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
implementation
|
99 |
|
|
|
100 |
|
|
{$R *.DFM}
|
101 |
|
|
|
102 |
|
|
function MyLoadData(Reader : TReader) : Pointer; far;
|
103 |
|
|
begin
|
104 |
|
|
GetMem(Result,SizeOf(PersonRecord));
|
105 |
|
|
with PersonRecord(Result^), Reader do
|
106 |
|
|
begin
|
107 |
|
|
First := ReadString;
|
108 |
|
|
Last := ReadString;
|
109 |
|
|
Age := ReadInteger;
|
110 |
|
|
end;
|
111 |
|
|
end;
|
112 |
|
|
|
113 |
|
|
procedure MyStoreData(Writer : TWriter; Data : Pointer); far;
|
114 |
|
|
var
|
115 |
|
|
PR : PersonRecord;
|
116 |
|
|
begin
|
117 |
|
|
PR := PersonRecord(Data^);
|
118 |
|
|
with Writer do
|
119 |
|
|
begin
|
120 |
|
|
WriteString(PR.First);
|
121 |
|
|
WriteString(PR.Last);
|
122 |
|
|
WriteInteger(PR.Age);
|
123 |
|
|
end;
|
124 |
|
|
end;
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
procedure MyDisposeData(Data : Pointer); far;
|
128 |
|
|
begin
|
129 |
|
|
FreeMem(Data, SizeOf(PersonRecord));
|
130 |
|
|
end;
|
131 |
|
|
|
132 |
|
|
function MySortTree(Data1, Data2 : Pointer) : Integer; far;
|
133 |
|
|
var
|
134 |
|
|
R1 : PPersonRecord absolute Data1;
|
135 |
|
|
R2 : PPersonRecord absolute Data2;
|
136 |
|
|
begin
|
137 |
|
|
Result := CompareText(R1^.Last, R2^.Last);
|
138 |
|
|
if Result = 0 then
|
139 |
|
|
CompareText(R1^.First, R2^.First);
|
140 |
|
|
if Result = 0 then
|
141 |
|
|
Result := (R1^.Age - R2^.Age);
|
142 |
|
|
end;
|
143 |
|
|
|
144 |
|
|
function MyTreeWalker(Contariner : TStContainer;
|
145 |
|
|
Node : TStNode;
|
146 |
|
|
OtherData : Pointer) : Boolean; far;
|
147 |
|
|
var
|
148 |
|
|
R : PersonRecord;
|
149 |
|
|
S : String;
|
150 |
|
|
begin
|
151 |
|
|
R := PersonRecord(Node.Data^);
|
152 |
|
|
S := R.Last + ', ' + R.First + ', ' + IntToStr(R.Age);
|
153 |
|
|
STDlg.LB1.Items.Add(S);
|
154 |
|
|
Result := True;
|
155 |
|
|
end;
|
156 |
|
|
|
157 |
|
|
function MyTreeSearcher(Contariner : TStContainer;
|
158 |
|
|
Node : TStNode;
|
159 |
|
|
OtherData : Pointer) : Boolean; far;
|
160 |
|
|
|
161 |
|
|
var
|
162 |
|
|
S : string;
|
163 |
|
|
R1 : PersonRecord;
|
164 |
|
|
R2 : PPersonRecord absolute OtherData;
|
165 |
|
|
begin
|
166 |
|
|
R1 := PersonRecord(Node.Data^);
|
167 |
|
|
if (CompareText(R1.Last, R2^.Last) = 0) then
|
168 |
|
|
begin
|
169 |
|
|
S := 'Match: ' + R1.First + ' ' + R1.Last + ', ' + IntToStr(R1.Age);
|
170 |
|
|
if MessageDlg(S,mtInformation,[mbOK,mbCancel],0) = mrCancel then
|
171 |
|
|
Result := False
|
172 |
|
|
else
|
173 |
|
|
Result := True;
|
174 |
|
|
end else
|
175 |
|
|
Result := True;
|
176 |
|
|
end;
|
177 |
|
|
|
178 |
|
|
procedure TSTDlg.SetBusy(B : Boolean);
|
179 |
|
|
begin
|
180 |
|
|
if B then
|
181 |
|
|
Screen.Cursor := crHourGlass
|
182 |
|
|
else
|
183 |
|
|
Screen.Cursor := crDefault;
|
184 |
|
|
end;
|
185 |
|
|
|
186 |
|
|
procedure TSTDlg.FillListBox;
|
187 |
|
|
begin
|
188 |
|
|
LB1.Clear;
|
189 |
|
|
LB1.Perform(WM_SETREDRAW,0,0);
|
190 |
|
|
SetBusy(True);
|
191 |
|
|
|
192 |
|
|
MyTree.Iterate(MyTreeWalker,True,nil);
|
193 |
|
|
|
194 |
|
|
LB1.Perform(WM_SETREDRAW,1,0);
|
195 |
|
|
LB1.Update;
|
196 |
|
|
SetBusy(False);
|
197 |
|
|
end;
|
198 |
|
|
|
199 |
|
|
procedure TSTDlg.FillControls(PR : PersonRecord);
|
200 |
|
|
begin
|
201 |
|
|
Edit1.Text := PR.First;
|
202 |
|
|
Edit2.Text := PR.Last;
|
203 |
|
|
Edit3.Text := IntToStr(PR.Age);
|
204 |
|
|
end;
|
205 |
|
|
|
206 |
|
|
function TSTDlg.GetControls(var PR : PersonRecord) : Boolean;
|
207 |
|
|
var
|
208 |
|
|
I,
|
209 |
|
|
Code : Integer;
|
210 |
|
|
begin
|
211 |
|
|
Result := False;
|
212 |
|
|
if (Edit1.Text = '') OR
|
213 |
|
|
(Edit2.Text = '') OR
|
214 |
|
|
(Edit3.Text = '') then
|
215 |
|
|
Exit;
|
216 |
|
|
|
217 |
|
|
PR.First := Edit1.Text;
|
218 |
|
|
PR.Last := Edit2.Text;
|
219 |
|
|
|
220 |
|
|
Val(Edit3.Text,I,Code);
|
221 |
|
|
if (Code <> 0) then
|
222 |
|
|
Exit
|
223 |
|
|
else
|
224 |
|
|
PR.Age := I;
|
225 |
|
|
Result := True;
|
226 |
|
|
end;
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
procedure TSTDlg.UpdateButtons(TOK : Boolean);
|
230 |
|
|
begin
|
231 |
|
|
ClearBtn.Enabled := TOK;
|
232 |
|
|
InsertBtn.Enabled := TOK;
|
233 |
|
|
DeleteBtn.Enabled := TOK;
|
234 |
|
|
FindBtn.Enabled := TOK;
|
235 |
|
|
SearchBtn.Enabled := TOK;
|
236 |
|
|
SaveBtn.Enabled := TOK;
|
237 |
|
|
end;
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
procedure TSTDlg.FormCreate(Sender: TObject);
|
241 |
|
|
begin
|
242 |
|
|
RegisterClasses([TStTree,TStTreeNode]);
|
243 |
|
|
UpdateButtons(False);
|
244 |
|
|
end;
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
procedure TSTDlg.FormActivate(Sender: TObject);
|
248 |
|
|
begin
|
249 |
|
|
FirstA[0] := 'Fred';
|
250 |
|
|
FirstA[1] := 'Mike';
|
251 |
|
|
FirstA[2] := 'Barney';
|
252 |
|
|
FirstA[3] := 'Horatio';
|
253 |
|
|
FirstA[4] := 'Mickey';
|
254 |
|
|
FirstA[5] := 'Arthur';
|
255 |
|
|
FirstA[6] := 'Santa';
|
256 |
|
|
FirstA[7] := 'John Q. ';
|
257 |
|
|
|
258 |
|
|
LastA[0] := 'Flintstone';
|
259 |
|
|
LastA[1] := 'Hammer';
|
260 |
|
|
LastA[2] := 'Rubble';
|
261 |
|
|
LastA[3] := 'Hornblower';
|
262 |
|
|
LastA[4] := 'Spilane';
|
263 |
|
|
LastA[5] := 'Miller';
|
264 |
|
|
LastA[6] := 'Claus';
|
265 |
|
|
LastA[7] := 'Public';
|
266 |
|
|
end;
|
267 |
|
|
|
268 |
|
|
procedure TSTDlg.FormClose(Sender: TObject; var Action: TCloseAction);
|
269 |
|
|
begin
|
270 |
|
|
MyTree.Free;
|
271 |
|
|
end;
|
272 |
|
|
|
273 |
|
|
procedure TSTDlg.CreateBtnClick(Sender: TObject);
|
274 |
|
|
var
|
275 |
|
|
I : Integer;
|
276 |
|
|
PR : PPersonRecord;
|
277 |
|
|
TN : TStTreeNode;
|
278 |
|
|
begin
|
279 |
|
|
if Assigned(MyTree) then
|
280 |
|
|
MyTree.Free;
|
281 |
|
|
|
282 |
|
|
UpdateButtons(False);
|
283 |
|
|
MyTree:= TStTree.Create(TStTreeNode);
|
284 |
|
|
|
285 |
|
|
MyTree.Compare := MySortTree;
|
286 |
|
|
MyTree.DisposeData := MyDisposeData;
|
287 |
|
|
MyTree.LoadData := MyLoadData;
|
288 |
|
|
MyTree.StoreData := MyStoreData;
|
289 |
|
|
|
290 |
|
|
SetBusy(True);
|
291 |
|
|
for I := 0 to MaxElem-1 do
|
292 |
|
|
begin
|
293 |
|
|
if (I mod 250 = 0) then Randomize;
|
294 |
|
|
GetMem(PR, SizeOf(PersonRecord));
|
295 |
|
|
with PR^ do
|
296 |
|
|
repeat
|
297 |
|
|
First := FirstA[Random(8)];
|
298 |
|
|
Last := LastA[Random(8)];
|
299 |
|
|
Age := Random(10000);
|
300 |
|
|
|
301 |
|
|
{search for duplicate entry, if found - don't try to add}
|
302 |
|
|
TN := MyTree.Find(PR);
|
303 |
|
|
if TN = nil then
|
304 |
|
|
MyTree.Insert(PR);
|
305 |
|
|
until TN = nil;
|
306 |
|
|
end;
|
307 |
|
|
FillListBox;
|
308 |
|
|
SetBusy(False);
|
309 |
|
|
UpdateButtons(True);
|
310 |
|
|
end;
|
311 |
|
|
|
312 |
|
|
procedure TSTDlg.ClearBtnClick(Sender: TObject);
|
313 |
|
|
begin
|
314 |
|
|
MyTree.Clear;
|
315 |
|
|
LB1.Clear;
|
316 |
|
|
Edit1.Text := '';
|
317 |
|
|
Edit2.Text := '';
|
318 |
|
|
Edit3.Text := '';
|
319 |
|
|
end;
|
320 |
|
|
|
321 |
|
|
procedure TSTDlg.InsertBtnClick(Sender: TObject);
|
322 |
|
|
var
|
323 |
|
|
PR : PPersonRecord;
|
324 |
|
|
begin
|
325 |
|
|
GetMem(PR, SizeOf(PersonRecord));
|
326 |
|
|
if NOT (GetControls(PR^)) then
|
327 |
|
|
begin
|
328 |
|
|
FreeMem(PR, SizeOf(PersonRecord));
|
329 |
|
|
ShowMessage('One or more fields invalid');
|
330 |
|
|
Exit;
|
331 |
|
|
end else
|
332 |
|
|
begin
|
333 |
|
|
MyTree.Insert(PR);
|
334 |
|
|
FillListBox;
|
335 |
|
|
end;
|
336 |
|
|
end;
|
337 |
|
|
|
338 |
|
|
procedure TSTDlg.DeleteBtnClick(Sender: TObject);
|
339 |
|
|
var
|
340 |
|
|
PR : PersonRecord;
|
341 |
|
|
TN : TStTreeNode;
|
342 |
|
|
begin
|
343 |
|
|
if NOT (GetControls(PR)) then
|
344 |
|
|
begin
|
345 |
|
|
ShowMessage('One or more invalid entry fields');
|
346 |
|
|
Exit;
|
347 |
|
|
end;
|
348 |
|
|
TN := MyTree.Find(@PR);
|
349 |
|
|
if (TN <> nil) then
|
350 |
|
|
begin
|
351 |
|
|
MyTree.Delete(@PR);
|
352 |
|
|
FillListBox;
|
353 |
|
|
end else
|
354 |
|
|
ShowMessage('Record not found');
|
355 |
|
|
end;
|
356 |
|
|
|
357 |
|
|
procedure TSTDlg.FindBtnClick(Sender: TObject);
|
358 |
|
|
var
|
359 |
|
|
PR : PersonRecord;
|
360 |
|
|
TN : TStTreeNode;
|
361 |
|
|
begin
|
362 |
|
|
if NOT (GetControls(PR)) then
|
363 |
|
|
begin
|
364 |
|
|
ShowMessage('One or more invalid entry fields');
|
365 |
|
|
Exit;
|
366 |
|
|
end;
|
367 |
|
|
|
368 |
|
|
TN := MyTree.Find(@PR);
|
369 |
|
|
if (TN <> nil) then
|
370 |
|
|
ShowMessage('Record was found');
|
371 |
|
|
end;
|
372 |
|
|
|
373 |
|
|
procedure TSTDlg.SearchBtnClick(Sender: TObject);
|
374 |
|
|
var
|
375 |
|
|
PR : PersonRecord;
|
376 |
|
|
begin
|
377 |
|
|
PR.Last := Edit2.Text;
|
378 |
|
|
MyTree.Iterate(MyTreeSearcher, True, @PR);
|
379 |
|
|
end;
|
380 |
|
|
|
381 |
|
|
procedure TSTDlg.LB1DblClick(Sender: TObject);
|
382 |
|
|
var
|
383 |
|
|
I,
|
384 |
|
|
L : Integer;
|
385 |
|
|
PR : PersonRecord;
|
386 |
|
|
S : string;
|
387 |
|
|
TN : TStTreeNode;
|
388 |
|
|
|
389 |
|
|
begin
|
390 |
|
|
S := LB1.Items[LB1.ItemIndex];
|
391 |
|
|
L := Length(S);
|
392 |
|
|
I := pos(',', S);
|
393 |
|
|
|
394 |
|
|
PR.Last := S;
|
395 |
|
|
Delete(PR.Last, I, L-I+1);
|
396 |
|
|
Delete(S, 1, I+1);
|
397 |
|
|
|
398 |
|
|
PR.First := S;
|
399 |
|
|
L := Length(PR.First);
|
400 |
|
|
I := pos(',', PR.First);
|
401 |
|
|
|
402 |
|
|
Delete(PR.First, I, L-I+1);
|
403 |
|
|
Delete(S, 1, I+1);
|
404 |
|
|
PR.Age := StrToInt(S);
|
405 |
|
|
|
406 |
|
|
TN := MyTree.Find(@PR);
|
407 |
|
|
if TN <> nil then
|
408 |
|
|
begin
|
409 |
|
|
MyTree.Delete(@PR);
|
410 |
|
|
FillListBox;
|
411 |
|
|
end;
|
412 |
|
|
end;
|
413 |
|
|
|
414 |
|
|
procedure TSTDlg.LoadBtnClick(Sender: TObject);
|
415 |
|
|
begin
|
416 |
|
|
if OD1.Execute then
|
417 |
|
|
begin
|
418 |
|
|
if (NOT Assigned(MyTree)) then
|
419 |
|
|
begin
|
420 |
|
|
UpdateButtons(False);
|
421 |
|
|
MyTree:= TStTree.Create(TStTreeNode);
|
422 |
|
|
MyTree.Compare := MySortTree;
|
423 |
|
|
MyTree.DisposeData := MyDisposeData;
|
424 |
|
|
MyTree.LoadData := MyLoadData;
|
425 |
|
|
MyTree.StoreData := MyStoreData;
|
426 |
|
|
end;
|
427 |
|
|
|
428 |
|
|
MyTree.Clear;
|
429 |
|
|
MyTree.LoadFromFile(OD1.FileName);
|
430 |
|
|
FillListBox;
|
431 |
|
|
UpdateButtons(True);
|
432 |
|
|
end;
|
433 |
|
|
end;
|
434 |
|
|
|
435 |
|
|
procedure TSTDlg.SaveBtnClick(Sender: TObject);
|
436 |
|
|
begin
|
437 |
|
|
if SD1.Execute then
|
438 |
|
|
MyTree.StoreToFile(SD1.FileName);
|
439 |
|
|
end;
|
440 |
|
|
|
441 |
|
|
end.
|