-
Notifications
You must be signed in to change notification settings - Fork 58
/
IBBlobFilter.pas
229 lines (199 loc) · 6.02 KB
/
IBBlobFilter.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
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
{********************************************************************}
{ TIBBlobFilter }
{ Copyright (c) 2002 by }
{ Ivan Ravin email: <[email protected]> }
{ }
{ Adapted for FIBPlus by Serge Buzadzhy }
{ }
{********************************************************************}
unit IBBlobFilter;
interface
{$I FIBPlus.inc}
uses
Classes;
type
PIBBlobFilterProc = ^TIBBlobFilterProc;
TIBBlobFilterProc = procedure (var BlobBuffer; var BlobSize:longint);
TIBBlobFilters = class(TObject)
private
FIBBlobFilterList : TList;
FSorted : boolean;
function GetFilterProc(BlobSubType:integer;ForEncode:boolean):PIBBlobFilterProc;
procedure Sort;
public
destructor Destroy; override;
function Find(BlobSubType:integer;var anIndex:integer):boolean;
procedure RegisterBlobFilter(BlobSubType:integer;
EncodeProc,DecodeProc:PIBBlobFilterProc);
procedure RemoveBlobFilter(BlobSubType:integer);
procedure IBFilterBuffer(var BlobBuffer:PAnsiChar;var BlobSize:longint;
BlobSubType:integer;ForEncode: boolean);
end;
implementation
type
PIBBlobFilter = ^TIBBlobFilter;
TIBBlobFilter = record
SubType : integer;
EncodeProc : PIBBlobFilterProc;
DecodeProc : PIBBlobFilterProc;
end;
var IBBlobFilters : TIBBlobFilters;
procedure UnLoadFilterList;
begin
if not assigned(IBBlobFilters) then
Exit;
IBBlobFilters.Free;
IBBlobFilters:=nil;
end;
{ TIBBlobFilters }
destructor TIBBlobFilters.Destroy;
var i:integer;
IBBlobFilter:PIBBlobFilter;
begin
if assigned(FIBBlobFilterList) then
begin
for i:=0 to FIBBlobFilterList.Count-1 do
begin
IBBlobFilter:=FIBBlobFilterList.Items[i];
Dispose(IBBlobFilter);
end;
FIBBlobFilterList.Free;
FIBBlobFilterList:=nil;
end;
inherited;
end;
function TIBBlobFilters.Find(BlobSubType: integer;
var anIndex: integer): boolean;
var
L, H, I, C: Integer;
begin
if not Assigned(FIBBlobFilterList) then
begin
Result:=False;
anIndex:=0;
Exit;
end;
if not FSorted then
Sort;
Result := False;
L := 0;
H := FIBBlobFilterList.Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := PIBBlobFilter(FIBBlobFilterList.Items[i])^.SubType - BlobSubType;
if C < 0 then
L := I + 1
else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
L := I;
end;
end;
end;
anIndex := L;
end;
function TIBBlobFilters.GetFilterProc(BlobSubType: integer;
ForEncode: boolean): PIBBlobFilterProc;
var i:integer;
IBBlobFilter:PIBBlobFilter;
begin
result:=nil;
if not assigned(FIBBlobFilterList) then
Exit;
if Find(BlobSubType,i) then
begin
IBBlobFilter:=FIBBlobFilterList.Items[i];
if ForEncode then
result:=IBBlobFilter^.EncodeProc
else
result:=IBBlobFilter^.DecodeProc;
end;
end;
procedure TIBBlobFilters.IBFilterBuffer(var BlobBuffer: PAnsiChar;
var BlobSize: Integer; BlobSubType: integer; ForEncode: boolean);
var
pProc:PIBBlobFilterProc;
IBBlobFilterProc:TIBBlobFilterProc;
begin
pProc := nil;
if (BlobSubType<0) and (BlobSize>0) and (BlobBuffer<>nil) then
pProc:=GetFilterProc(BlobSubType,ForEncode);
if assigned(pProc) then
begin
IBBlobFilterProc:=TIBBlobFilterProc(pProc);
IBBlobFilterProc(BlobBuffer,BlobSize)
end;
end;
procedure TIBBlobFilters.RegisterBlobFilter(BlobSubType: integer;
EncodeProc, DecodeProc: PIBBlobFilterProc);
var
IBBlobFilter:PIBBlobFilter;
i:integer;
begin
i:=0;
if not Assigned(FIBBlobFilterList) then
FIBBlobFilterList := TList.Create;
if Find(BlobSubType,i) then
IBBlobFilter:=FIBBlobFilterList.Items[i]
else
begin
new(IBBlobFilter);
IBBlobFilter^.SubType:=BlobSubType;
FIBBlobFilterList.Add(IBBlobFilter);
FSorted:=false;
end;
IBBlobFilter^.EncodeProc:=EncodeProc;
IBBlobFilter^.DecodeProc:=DecodeProc;
end;
procedure TIBBlobFilters.RemoveBlobFilter(BlobSubType: integer);
var i:integer;
IBBlobFilter:PIBBlobFilter;
begin
if not assigned(FIBBlobFilterList) then
Exit;
if Find(BlobSubType,i) then
begin
IBBlobFilter:=FIBBlobFilterList.Items[i];
Dispose(IBBlobFilter);
FIBBlobFilterList.Delete(I);
end;
end;
function CompareFilters(p1,p2:Pointer):integer;
begin
result:=PIBBlobFilter(p1)^.SubType-PIBBlobFilter(p2)^.SubType;
end;
procedure TIBBlobFilters.Sort;
begin
if Assigned(FIBBlobFilterList) then
begin
FIBBlobFilterList.Sort(CompareFilters);
FSorted:=true;
end;
end;
initialization
finalization
UnLoadFilterList;
end.