1 |
(* ***** 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 ExVarrU;
|
27 |
|
28 |
interface
|
29 |
|
30 |
uses
|
31 |
Windows, Messages, SysUtils, Classes, Graphics, Controls,
|
32 |
Forms, Dialogs, StdCtrls, Buttons, ExtCtrls,
|
33 |
|
34 |
StConst, StBase, StUtils, StVArr;
|
35 |
|
36 |
type
|
37 |
ARecord = record
|
38 |
X, Y : LongInt;
|
39 |
end;
|
40 |
|
41 |
TMyVMatrix = class(TStVMatrix)
|
42 |
protected
|
43 |
Header : array[0..1023] of char;
|
44 |
public
|
45 |
constructor Create(Rows, Cols, ElementSize : Cardinal;
|
46 |
CacheRows : Integer;
|
47 |
const DataFile : string; OpenMode : Word); override;
|
48 |
function HeaderSize : LongInt; override;
|
49 |
procedure ReadHeader; override;
|
50 |
procedure WriteHeader; override;
|
51 |
end;
|
52 |
|
53 |
TSTDlg = class(TForm)
|
54 |
ArrayLB: TListBox;
|
55 |
CreateBtn: TButton;
|
56 |
Label6: TLabel;
|
57 |
VMRow: TEdit;
|
58 |
VMCol: TEdit;
|
59 |
ClearBtn: TButton;
|
60 |
FillBtn: TButton;
|
61 |
PutBtn: TButton;
|
62 |
PutRowBtn: TButton;
|
63 |
GetBtn: TButton;
|
64 |
GetRowBtn: TButton;
|
65 |
SortBtn: TButton;
|
66 |
Label3: TLabel;
|
67 |
Label4: TLabel;
|
68 |
Edit1: TEdit;
|
69 |
Edit2: TEdit;
|
70 |
|
71 |
procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
72 |
|
73 |
procedure CreateBtnClick(Sender: TObject);
|
74 |
procedure ClearBtnClick(Sender: TObject);
|
75 |
procedure FillBtnClick(Sender: TObject);
|
76 |
procedure PutBtnClick(Sender: TObject);
|
77 |
procedure GetBtnClick(Sender: TObject);
|
78 |
procedure PutRowBtnClick(Sender: TObject);
|
79 |
procedure GetRowBtnClick(Sender: TObject);
|
80 |
procedure SortBtnClick(Sender: TObject);
|
81 |
procedure FormCreate(Sender: TObject);
|
82 |
|
83 |
private
|
84 |
{ Private declarations }
|
85 |
public
|
86 |
{ Public declarations }
|
87 |
procedure SetBusy(B : Boolean);
|
88 |
procedure FillListBox;
|
89 |
procedure FillControls;
|
90 |
function GetControls(var AR : ARecord) : Boolean;
|
91 |
function ValidateRowCol(var R, C : LongInt) : Boolean;
|
92 |
procedure UpdateButtons(AOK : Boolean);
|
93 |
end;
|
94 |
|
95 |
var
|
96 |
STDlg: TSTDlg;
|
97 |
ARec : ARecord;
|
98 |
|
99 |
implementation
|
100 |
|
101 |
{$R *.DFM}
|
102 |
|
103 |
{ File and Share modes
|
104 |
|
105 |
fmOpenRead = $0000;
|
106 |
fmOpenWrite = $0001;
|
107 |
fmOpenReadWrite = $0002;
|
108 |
|
109 |
fmShareCompat = $0000;
|
110 |
fmShareExclusive = $0010;
|
111 |
fmShareDenyWrite = $0020;
|
112 |
fmShareDenyRead = $0030;
|
113 |
fmShareDenyNone = $0040;
|
114 |
}
|
115 |
|
116 |
type
|
117 |
S10 = string[10];
|
118 |
|
119 |
const
|
120 |
MaxRows = 1000;
|
121 |
MaxCols = 10;
|
122 |
RowsCached = 10;
|
123 |
FN = 'MyCache.DAT';
|
124 |
|
125 |
var
|
126 |
MyVMatrix : TMyVMatrix;
|
127 |
RowArray : array[1..MaxCols] of ARecord;
|
128 |
|
129 |
|
130 |
function MyArraySort(const E1, E2) : Integer; far;
|
131 |
var
|
132 |
R1 : ARecord absolute E1;
|
133 |
R2 : ARecord absolute E2;
|
134 |
begin
|
135 |
Result := R1.X-R2.X;
|
136 |
if Result = 0 then
|
137 |
Result := R1.Y-R2.Y;
|
138 |
end;
|
139 |
|
140 |
|
141 |
{ ========== Descendant TMyVMatrix methods =================}
|
142 |
|
143 |
constructor TMyVMatrix.Create(Rows, Cols, ElementSize : Cardinal;
|
144 |
CacheRows : Integer;
|
145 |
const DataFile : string; OpenMode : Word);
|
146 |
begin
|
147 |
strcopy(Header,'DataFile1. Contains data stored in a 2D virtual array');
|
148 |
inherited Create(Rows, Cols, ElementSize, CacheRows, DataFile, OpenMode);
|
149 |
end;
|
150 |
|
151 |
procedure TMyVMatrix.WriteHeader;
|
152 |
begin
|
153 |
FileWrite(vmDataF,Header,SizeOf(Header));
|
154 |
end;
|
155 |
|
156 |
function TMyVMatrix.HeaderSize : LongInt;
|
157 |
begin
|
158 |
Result := SizeOf(Header);
|
159 |
end;
|
160 |
|
161 |
procedure TMyVMatrix.ReadHeader;
|
162 |
begin
|
163 |
FillChar(Header,SizeOf(Header),#0);
|
164 |
FileRead(vmDataF,Header,SizeOf(Header));
|
165 |
end;
|
166 |
|
167 |
|
168 |
{ ================= Form methods ==========================}
|
169 |
|
170 |
|
171 |
procedure TSTDlg.FormCreate(Sender: TObject);
|
172 |
begin
|
173 |
UpdateButtons(False);
|
174 |
end;
|
175 |
|
176 |
procedure TSTDlg.FormClose(Sender: TObject;
|
177 |
var Action: TCloseAction);
|
178 |
begin
|
179 |
MyVMatrix.Free;
|
180 |
end;
|
181 |
|
182 |
procedure TSTDlg.SetBusy(B : Boolean);
|
183 |
begin
|
184 |
if B then
|
185 |
Screen.Cursor := crHourGlass
|
186 |
else
|
187 |
Screen.Cursor := crDefault;
|
188 |
end;
|
189 |
|
190 |
procedure TSTDlg.UpdateButtons(AOK : Boolean);
|
191 |
begin
|
192 |
ClearBtn.Enabled := AOK;
|
193 |
FillBtn.Enabled := AOK;
|
194 |
SortBtn.Enabled := AOK;
|
195 |
PutBtn.Enabled := AOK;
|
196 |
PutRowBtn.Enabled := AOK;
|
197 |
GetBtn.Enabled := AOK;
|
198 |
GetRowBtn.Enabled := AOK;
|
199 |
end;
|
200 |
|
201 |
|
202 |
procedure TSTDlg.FillListBox;
|
203 |
var
|
204 |
row, col : LongInt;
|
205 |
|
206 |
begin
|
207 |
ArrayLB.Clear;
|
208 |
|
209 |
ArrayLB.Perform(WM_SETREDRAW,0,0);
|
210 |
SetBusy(True);
|
211 |
for row := 0 to MaxRows-1 do
|
212 |
begin
|
213 |
for col := 0 to MaxCols-1 do
|
214 |
begin
|
215 |
MyVMatrix.Get(Row,Col,ARec);
|
216 |
ArrayLB.Items.Add(IntToStr(row) + ',' +
|
217 |
IntToStr(col) + ': X = ' +
|
218 |
IntToStr(ARec.X) + ' Y = ' +
|
219 |
IntToStr(ARec.Y));
|
220 |
end;
|
221 |
end;
|
222 |
ArrayLB.Perform(WM_SETREDRAW,1,0);
|
223 |
ArrayLB.Update;
|
224 |
end;
|
225 |
|
226 |
|
227 |
procedure TSTDlg.FillControls;
|
228 |
begin
|
229 |
with ARec do
|
230 |
begin
|
231 |
Edit1.Text := IntToStr(X);
|
232 |
Edit2.Text := IntToStr(Y);
|
233 |
end;
|
234 |
end;
|
235 |
|
236 |
|
237 |
function TSTDlg.GetControls(var AR : ARecord) : Boolean;
|
238 |
var
|
239 |
Code : Integer;
|
240 |
IV : LongInt;
|
241 |
begin
|
242 |
Result := False;
|
243 |
if (Edit1.Text = '') OR (Edit2.Text = '') then
|
244 |
begin
|
245 |
ShowMessage('One or more blank fields');
|
246 |
Exit;
|
247 |
end;
|
248 |
|
249 |
FillChar(AR,SizeOf(AR),#0);
|
250 |
Val(Edit1.Text,IV,Code);
|
251 |
if (Code <> 0) then
|
252 |
begin
|
253 |
ShowMessage('Illegal entry for X');
|
254 |
Exit;
|
255 |
end else
|
256 |
AR.X := IV;
|
257 |
|
258 |
Val(Edit2.Text,IV,Code);
|
259 |
if (Code <> 0) then
|
260 |
begin
|
261 |
ShowMessage('Illegal entry for Y');
|
262 |
Exit;
|
263 |
end else
|
264 |
AR.Y := IV;
|
265 |
Result := True;
|
266 |
end;
|
267 |
|
268 |
|
269 |
function TSTDlg.ValidateRowCol(var R,C : LongInt) : Boolean;
|
270 |
var
|
271 |
Code : Integer;
|
272 |
Value : LongInt;
|
273 |
|
274 |
begin
|
275 |
Result := False;
|
276 |
|
277 |
if (VMRow.Text = '') then
|
278 |
VMRow.Text := '0';
|
279 |
if (VMCol.Text = '') then
|
280 |
VMCol.Text := '0';
|
281 |
|
282 |
Val(VMRow.Text,Value,Code);
|
283 |
if (Code <> 0) then
|
284 |
begin
|
285 |
ShowMessage('Invalid row entry');
|
286 |
Exit;
|
287 |
end else
|
288 |
begin
|
289 |
if (Value < 0) or (Value > MaxRows-1) then
|
290 |
begin
|
291 |
ShowMessage('Row value out of range');
|
292 |
Exit;
|
293 |
end else
|
294 |
R := Value;
|
295 |
end;
|
296 |
|
297 |
Val(VMCol.Text,Value,Code);
|
298 |
if (Code <> 0) then
|
299 |
begin
|
300 |
ShowMessage('Invalid Col entry');
|
301 |
Exit;
|
302 |
end else
|
303 |
begin
|
304 |
if (Value < 0) or (Value > MaxCols-1) then
|
305 |
begin
|
306 |
ShowMessage('Col value out of range');
|
307 |
Exit;
|
308 |
end else
|
309 |
C := Value;
|
310 |
end;
|
311 |
|
312 |
Result := True;
|
313 |
end;
|
314 |
|
315 |
procedure TSTDlg.CreateBtnClick(Sender: TObject);
|
316 |
var
|
317 |
row,
|
318 |
col : LongInt;
|
319 |
begin
|
320 |
ArrayLB.Clear;
|
321 |
|
322 |
if (MyVMatrix <> nil) then
|
323 |
MyVMatrix.Free;
|
324 |
|
325 |
MyVMatrix := TMyVMatrix.Create(MaxRows,MaxCols,sizeof(ARecord),RowsCached,
|
326 |
FN,fmOpenReadWrite);
|
327 |
if (NOT Assigned(MyVMatrix)) then
|
328 |
begin
|
329 |
ShowMessage('Failed to create Matrix');
|
330 |
UpdateButtons(False);
|
331 |
Exit;
|
332 |
end;
|
333 |
|
334 |
SetBusy(True);
|
335 |
Randomize;
|
336 |
for row := 0 to MaxRows-1 do
|
337 |
begin
|
338 |
for col := 0 to MaxCols-1 do
|
339 |
begin
|
340 |
with ARec do
|
341 |
begin
|
342 |
X := Random(1000);
|
343 |
Y := Random(1000);
|
344 |
MyVMatrix.Put(Row,Col,ARec);
|
345 |
end;
|
346 |
end;
|
347 |
end;
|
348 |
FillListBox;
|
349 |
|
350 |
VMRow.Text := '0';
|
351 |
VMCol.Text := '0';
|
352 |
MyVMatrix.Get(0,0,ARec);
|
353 |
|
354 |
FillControls;
|
355 |
UpdateButtons(True);
|
356 |
|
357 |
SetBusy(False);
|
358 |
end;
|
359 |
|
360 |
procedure TSTDlg.ClearBtnClick(Sender: TObject);
|
361 |
begin
|
362 |
MyVMatrix.Clear;
|
363 |
ArrayLB.Clear;
|
364 |
|
365 |
VMRow.Text := '0';
|
366 |
VMCol.Text := '0';
|
367 |
MyVMatrix.Get(0,0,ARec);
|
368 |
|
369 |
FillControls;
|
370 |
end;
|
371 |
|
372 |
procedure TSTDlg.FillBtnClick(Sender: TObject);
|
373 |
begin
|
374 |
if NOT GetControls(ARec) then
|
375 |
Exit;
|
376 |
MyVMatrix.Fill(ARec);
|
377 |
|
378 |
FillListBox;
|
379 |
|
380 |
VMRow.Text := '0';
|
381 |
VMCol.Text := '0';
|
382 |
|
383 |
MyVMatrix.Get(0, 0, ARec);
|
384 |
FillControls;
|
385 |
SetBusy(False);
|
386 |
end;
|
387 |
|
388 |
procedure TSTDlg.PutBtnClick(Sender: TObject);
|
389 |
var
|
390 |
Code,
|
391 |
Row,
|
392 |
Col : LongInt;
|
393 |
|
394 |
begin
|
395 |
if NOT GetControls(ARec) then
|
396 |
Exit;
|
397 |
if NOT ValidateRowCol(Row,Col) then
|
398 |
Exit;
|
399 |
|
400 |
MyVMatrix.Put(Row,Col,ARec);
|
401 |
|
402 |
Code := (Row * MaxRows) + Col;
|
403 |
ArrayLB.Items[Code] := IntToStr(row) + ',' +
|
404 |
IntToStr(col) + ': X = ' +
|
405 |
IntToStr(ARec.X) + ' Y = ' +
|
406 |
IntToStr(ARec.Y);
|
407 |
|
408 |
MyVMatrix.Get(Row, Col, ARec);
|
409 |
FillControls;
|
410 |
end;
|
411 |
|
412 |
procedure TSTDlg.GetBtnClick(Sender: TObject);
|
413 |
var
|
414 |
row,
|
415 |
col : LongInt;
|
416 |
begin
|
417 |
if NOT ValidateRowCol(Row,Col) then
|
418 |
Exit;
|
419 |
MyVMatrix.Get(Row,Col,ARec);
|
420 |
FillControls;
|
421 |
end;
|
422 |
|
423 |
procedure TSTDlg.PutRowBtnClick(Sender: TObject);
|
424 |
var
|
425 |
Code : Integer;
|
426 |
row,
|
427 |
step,
|
428 |
Value : LongInt;
|
429 |
|
430 |
begin
|
431 |
if NOT GetControls(ARec) then
|
432 |
Exit;
|
433 |
if (VMRow.Text = '') then
|
434 |
VMRow.Text := '0';
|
435 |
|
436 |
Val(VMRow.Text,Value,Code);
|
437 |
if (Code <> 0) then
|
438 |
begin
|
439 |
ShowMessage('Invalid Row Entry');
|
440 |
Exit;
|
441 |
end else
|
442 |
begin
|
443 |
if (Value < 0) OR (Value >= MaxRows) then
|
444 |
begin
|
445 |
ShowMessage('Row out of range');
|
446 |
Exit;
|
447 |
end else
|
448 |
Row := Value;
|
449 |
end;
|
450 |
|
451 |
FillStruct(RowArray,MaxCols,ARec,SizeOf(ARec));
|
452 |
MyVMatrix.PutRow(Row,RowArray);
|
453 |
|
454 |
ArrayLB.Clear;
|
455 |
ArrayLB.Perform(WM_SETREDRAW,0,0);
|
456 |
|
457 |
for step := 1 to MaxCols do
|
458 |
ArrayLB.Items.Add(IntToStr(row) + ',' +
|
459 |
IntToStr(step) + ': X = ' +
|
460 |
IntToStr(ARec.X) + ' Y = ' +
|
461 |
IntToStr(ARec.Y));
|
462 |
|
463 |
ArrayLB.Perform(WM_SETREDRAW,1,0);
|
464 |
ArrayLB.Update;
|
465 |
|
466 |
MyVMatrix.Get(Row, 0, ARec);
|
467 |
FillControls;
|
468 |
|
469 |
SetBusy(False);
|
470 |
end;
|
471 |
|
472 |
procedure TSTDlg.GetRowBtnClick(Sender: TObject);
|
473 |
var
|
474 |
Code : Integer;
|
475 |
Row,
|
476 |
step,
|
477 |
Value : LongInt;
|
478 |
|
479 |
begin
|
480 |
if (VMRow.Text = '') then
|
481 |
VMRow.Text := '0';
|
482 |
|
483 |
Val(VMRow.Text,Value,Code);
|
484 |
if (Code <> 0) then
|
485 |
begin
|
486 |
ShowMessage('Invalid Row Entry');
|
487 |
Exit;
|
488 |
end else
|
489 |
begin
|
490 |
if (Value < 0) OR (Value >= MaxRows) then
|
491 |
begin
|
492 |
ShowMessage('Row out of range');
|
493 |
Exit;
|
494 |
end else
|
495 |
Row := Value;
|
496 |
end;
|
497 |
FillChar(ARec,SizeOf(ARec),#0);
|
498 |
FillStruct(RowArray,MaxCols,ARec,SizeOf(ARec));
|
499 |
MyVMatrix.GetRow(Row,RowArray);
|
500 |
|
501 |
ArrayLB.Clear;
|
502 |
ArrayLB.Perform(WM_SETREDRAW,0,0);
|
503 |
|
504 |
for step := 1 to MaxCols do
|
505 |
ArrayLB.Items.Add(IntToStr(row) + ',' +
|
506 |
IntToStr(step) + ': X = ' +
|
507 |
IntToStr(ARec.X) + ' Y = ' +
|
508 |
IntToStr(ARec.Y));
|
509 |
|
510 |
MyVMatrix.Get(Row, 0, ARec);
|
511 |
FillControls;
|
512 |
|
513 |
ArrayLB.Perform(WM_SETREDRAW,1,0);
|
514 |
ArrayLB.Update;
|
515 |
end;
|
516 |
|
517 |
procedure TSTDlg.SortBtnClick(Sender: TObject);
|
518 |
var
|
519 |
row,
|
520 |
col : LongInt;
|
521 |
begin
|
522 |
SetBusy(True);
|
523 |
MyVMatrix.SortRows(0,MyArraySort);
|
524 |
|
525 |
ArrayLB.Clear;
|
526 |
col := 0;
|
527 |
ArrayLB.Perform(WM_SETREDRAW,0,0);
|
528 |
for row := 0 to MaxRows-1 do
|
529 |
begin
|
530 |
MyVMatrix.Get(row,col,ARec);
|
531 |
ArrayLB.Items.Add(IntToStr(row) + ',' +
|
532 |
IntToStr(col) + ': X = ' +
|
533 |
IntToStr(ARec.X) + ' Y = ' +
|
534 |
IntToStr(ARec.Y));
|
535 |
end;
|
536 |
ArrayLB.Perform(WM_SETREDRAW,1,0);
|
537 |
ArrayLB.Update;
|
538 |
|
539 |
SetBusy(False);
|
540 |
end;
|
541 |
|
542 |
|
543 |
end.
|