-
Notifications
You must be signed in to change notification settings - Fork 283
/
varlist.pas
406 lines (354 loc) · 10.3 KB
/
varlist.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
{*************************************************************************************
This file is part of Transmission Remote GUI.
Copyright (c) 2008-2019 by Yury Sidorov and Transmission Remote GUI working group.
Transmission Remote GUI is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Transmission Remote GUI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Transmission Remote GUI; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give permission to
link the code of portions of this program with the
OpenSSL library under certain conditions as described in each individual
source file, and distribute linked combinations including the two.
You must obey the GNU General Public License in all respects for all of the
code used other than OpenSSL. If you modify file(s) with this exception, you
may extend this exception to your version of the file(s), but you are not
obligated to do so. If you do not wish to do so, delete this exception
statement from your version. If you delete this exception statement from all
source files in the program, then also delete it here.
*************************************************************************************}
unit varlist;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, variants;
type
TVarList = class;
TCompareVarRowsEvent = function(Sender: TVarList; Row1, Row2: PVariant; DescendingSort: boolean): integer of object;
{ TVarList }
TVarList = class(TList)
private
FColCnt: integer;
FExtraColumns: integer;
FOnCompareVarRows: TCompareVarRowsEvent;
FOnDataChanged: TNotifyEvent;
FUpdateLockCnt: integer;
function GetItemPtr(ACol, ARow: integer): PVariant;
function GetItems(ACol, ARow: integer): variant;
function GetRowCnt: integer;
function GetRowOptions(ARow: integer): integer;
function GetRows(ARow: integer): PVariant;
function GetRow(ARow: integer): PVariant;
procedure SetColCnt(const AValue: integer);
procedure SetExtraColumns(const AValue: integer);
procedure SetItems(ACol, ARow: integer; const AValue: variant);
procedure SetRowCnt(const AValue: integer);
procedure SetRowOptions(ARow: integer; const AValue: integer);
function IntCols: integer;
procedure CheckColIndex(ColIndex: integer);
protected
procedure DoDataChanged; virtual;
public
constructor Create(AColCnt, ARowCnt: integer);
destructor Destroy; override;
procedure Clear; override;
procedure Delete(Index: Integer);
procedure Sort(ACol: integer; Descending: boolean = False); reintroduce;
function IndexOf(ACol: integer; const Value: variant): integer;
function SortedIndexOf(ACol: integer; const Value: variant): integer;
function Find(ACol: integer; const Value: variant; var Index: Integer): Boolean;
procedure BeginUpdate;
procedure EndUpdate;
procedure InsertRow(ARow: integer);
function IsUpdating: boolean;
function GetRowItem(ARow: PVariant; ACol: integer): variant;
property Items[ACol, ARow: integer]: variant read GetItems write SetItems; default;
property ItemPtrs[ACol, ARow: integer]: PVariant read GetItemPtr;
property Rows[ARow: integer]: PVariant read GetRows;
property RowOptions[ARow: integer]: integer read GetRowOptions write SetRowOptions;
property ColCnt: integer read FColCnt write SetColCnt;
property RowCnt: integer read GetRowCnt write SetRowCnt;
property Count: integer read GetRowCnt;
property OnDataChanged: TNotifyEvent read FOnDataChanged write FOnDataChanged;
property OnCompareVarRows: TCompareVarRowsEvent read FOnCompareVarRows write FOnCompareVarRows;
property ExtraColumns: integer read FExtraColumns write SetExtraColumns;
end;
function CompareVariants(const v1, v2: variant): integer;
implementation
uses Math;
{ TVarList }
function TVarList.GetItems(ACol, ARow: integer): variant;
begin
CheckColIndex(ACol);
Result:=GetRow(ARow)[ACol + IntCols];
end;
function TVarList.GetItemPtr(ACol, ARow: integer): PVariant;
begin
CheckColIndex(ACol);
Result:=GetRow(ARow) + (ACol + IntCols);
end;
function TVarList.GetRowCnt: integer;
begin
Result:=inherited GetCount;
end;
function TVarList.GetRowOptions(ARow: integer): integer;
begin
Result:=GetRow(ARow)[0];
end;
function TVarList.GetRows(ARow: integer): PVariant;
begin
Result:=GetRow(ARow);
end;
function TVarList.GetRow(ARow: integer): PVariant;
var
v: PVariant;
sz: integer;
begin
if ARow < 0 then ARow := 0;
if ARow >= Count then
SetRowCnt(ARow + 1);
v:=Get(ARow);
if v = nil then begin
sz:=SizeOf(variant)*(FColCnt + IntCols);
v:=GetMem(sz);
FillChar(v^, sz, 0);
v[0]:=0;
Put(ARow, v);
end;
Result:=v;
end;
procedure TVarList.SetColCnt(const AValue: integer);
var
i, j, ocnt, ncnt: integer;
p: PVariant;
begin
if FColCnt = AValue then exit;
ocnt:=FColCnt + IntCols;
FColCnt:=AValue;
ncnt:=FColCnt + IntCols;
for i:=0 to Count - 1 do begin
p:=GetRow(i);
for j:=ncnt to ocnt - 1 do
VarClear(p[j]);
ReAllocMem(p, ncnt*SizeOf(variant));
if ncnt > ocnt then
FillChar(p[ocnt], (ncnt - ocnt)*SizeOf(variant), 0);
end;
end;
procedure TVarList.SetExtraColumns(const AValue: integer);
begin
if FExtraColumns=AValue then exit;
if RowCnt <> 0 then
raise Exception.Create('Unable to set extra columns.');
FExtraColumns:=AValue;
end;
procedure TVarList.SetItems(ACol, ARow: integer; const AValue: variant);
begin
GetRow(ARow)[ACol + IntCols]:=AValue;
DoDataChanged;
end;
procedure TVarList.SetRowCnt(const AValue: integer);
begin
BeginUpdate;
try
while Count > AValue do
Delete(Count - 1);
SetCount(AValue);
finally
EndUpdate;
end;
end;
procedure TVarList.SetRowOptions(ARow: integer; const AValue: integer);
begin
GetRow(ARow)[0]:=AValue;
end;
function TVarList.IntCols: integer;
begin
Result:=FExtraColumns + 1;
end;
procedure TVarList.CheckColIndex(ColIndex: integer);
begin
if (ColIndex + IntCols < 0) or (ColIndex >= ColCnt) then
raise Exception.CreateFmt('Invalid column index (%d).', [ColIndex]);
end;
procedure TVarList.DoDataChanged;
begin
if Assigned(FOnDataChanged) and (FUpdateLockCnt = 0) then
FOnDataChanged(Self);
end;
constructor TVarList.Create(AColCnt, ARowCnt: integer);
begin
inherited Create;
FColCnt:=AColCnt;
RowCnt:=ARowCnt;
end;
destructor TVarList.Destroy;
begin
FOnDataChanged:=nil;
inherited Destroy;
end;
procedure TVarList.Clear;
var
i: integer;
v: PVariant;
begin
for i:=0 to Count - 1 do begin
v:=inherited Get(i);
if v <> nil then begin
VarClear(v^);
FreeMem(v);
end;
end;
inherited Clear;
DoDataChanged;
end;
procedure TVarList.Delete(Index: Integer);
var
v: PVariant;
i: integer;
begin
v:=inherited Get(Index);
if v <> nil then begin
for i:=0 to ColCnt + IntCols - 1 do
VarClear(v[i]);
FreeMem(v);
end;
inherited Delete(Index);
DoDataChanged;
end;
function CompareVariants(const v1, v2: variant): integer;
var
v1e, v2e: boolean;
begin
v1e:=VarIsNull(v1) or VarIsEmpty(v1);
v2e:=VarIsNull(v2) or VarIsEmpty(v2);
if v1e and v2e then
Result:=0
else
if v1e and not v2e then
Result:=-1
else
if not v1e and v2e then
Result:=1
else
case VarType(v1) of
varInteger,varsmallint,varshortint,varbyte,varword,varlongword,varint64,varqword:
Result:=Int64(v1) - Int64(v2);
varDouble,varSingle,varDate:
Result:=Sign(double(v1) - double(v2));
else
Result:=AnsiCompareText(v1, v2);
end;
end;
var
_SortColumn: integer;
_SortDesc: boolean;
_IntCols: integer;
_List: TVarList;
function CompareItems(Item1, Item2: Pointer): Integer;
var
v1, v2: PVariant;
i: integer;
begin
if Item1 = Item2 then begin
Result:=0;
exit;
end;
v1:=Item1;
v2:=Item2;
if Assigned(_List.OnCompareVarRows) then
Result:=_List.OnCompareVarRows(_List, v1, v2, _SortDesc)
else
Result:=0;
if Result = 0 then begin
Result:=CompareVariants(v1[_SortColumn], v2[_SortColumn]);
i:=_IntCols;
while (Result = 0) and (i < _List.ColCnt + _IntCols) do begin
if i <> _SortColumn then
Result:=CompareVariants(v1[i], v2[i]);
Inc(i);
end;
if _SortDesc then
Result:=-Result;
end;
end;
procedure TVarList.Sort(ACol: integer; Descending: boolean);
begin
_SortColumn:=ACol + IntCols;
_SortDesc:=Descending;
_IntCols:=IntCols;
_List:=Self;
inherited Sort(@CompareItems);
DoDataChanged;
end;
function TVarList.IndexOf(ACol: integer; const Value: variant): integer;
var
i: integer;
begin
for i:=0 to RowCnt - 1 do
if CompareVariants(Items[ACol, i], Value) = 0 then begin
Result:=i;
exit;
end;
Result:=-1;
end;
function TVarList.SortedIndexOf(ACol: integer; const Value: variant): integer;
begin
Result:=-1;
if not Find(ACol, Value, Result) then
Result:=-1;
end;
function TVarList.Find(ACol: integer; const Value: variant; var Index: Integer): Boolean;
var
L, R, I: Integer;
CompareRes: PtrInt;
begin
Result := false;
L := 0;
R := Count - 1;
while (L<=R) do
begin
I := L + (R - L) div 2;
CompareRes := CompareVariants(Value, Items[ACol, I]);
if (CompareRes>0) then
L := I+1
else begin
R := I-1;
if (CompareRes=0) then begin
Result := true;
L := I; // forces end of while loop
end;
end;
end;
Index := L;
end;
procedure TVarList.BeginUpdate;
begin
Inc(FUpdateLockCnt);
end;
procedure TVarList.EndUpdate;
begin
Dec(FUpdateLockCnt);
if FUpdateLockCnt = 0 then
DoDataChanged;
end;
procedure TVarList.InsertRow(ARow: integer);
begin
inherited Insert(ARow, nil);
end;
function TVarList.IsUpdating: boolean;
begin
Result:=FUpdateLockCnt > 0;
end;
function TVarList.GetRowItem(ARow: PVariant; ACol: integer): variant;
begin
CheckColIndex(ACol);
Result:=ARow[ACol + IntCols];
end;
end.