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
|
unit raetselFileUnit;
interface
uses
dialogs, math;
type
tWort = record
w: array of byte;
bits: integer;
end;
tRaetselFile = class
private
f: file;
wacc: boolean;
inhalt,
conv: array of byte;
fPoint: integer;
wB: array of tWort;
procedure fFlush;
procedure initWB;
procedure concatWs(a,b: integer);
function bIsAX(a: tWort; b: array of byte): boolean;
procedure rConvert;
procedure wConvert;
public
kennung: string;
procedure fAssignFile(fileName: string);
function fReset: boolean;
procedure fRewrite;
procedure fCloseFile;
procedure fBlockWrite(var data; len: integer);
function fBlockRead(var data; len: integer): boolean;
end;
implementation
procedure tRaetselFile.fAssignFile(FileName: string);
begin
assignFile(f,fileName);
end;
function tRaetselFile.fReset: boolean;
var
c: cardinal;
s: string;
begin
wacc:=false;
result:=false;
c:=0;
reset(f,1);
if fileSize(f) < 4 then begin
fCloseFile;
exit;
end;
blockRead(f,c,4);
if c<>$26594131 then begin
fCloseFile;
exit;
end;
setLength(conv,fileSize(f)-4);
blockRead(f,conv[0],length(conv));
rConvert;
if length(inhalt)<length(kennung) then begin
fCloseFile;
exit;
end;
s:=kennung;
fBlockRead(s[1],length(s));
if s<>kennung then begin
fCloseFile;
exit;
end;
result:=true;
end;
procedure tRaetselFile.initWB;
var
b: byte;
begin
setLength(wB,128);
for b:=0 to 127 do begin
setLength(wB[b].w,2);
wB[b].w[0]:=b;
wB[b].w[1]:=0;
wB[b].bits:=7;
end;
end;
procedure tRaetselFile.concatWs(a,b: integer);
var
i: integer;
begin
setLength(wB,length(wB)+1); // wB erweitern
with wB[length(wB)-1] do begin
bits:=wB[a].bits+wB[b].bits;
setLength(w,(bits+7) div 8 + 1);
for i:=0 to length(w)-1 do
w[i]:=0;
for i:=0 to length(wB[a].w)-2 do
w[i]:=wB[a].w[i];
for i:=0 to length(wB[b].w)-2 do begin
w[length(wB[a].w)-2+i]:=
w[length(wB[a].w)-2+i] or
($ff and (wB[b].w[i] shl (wB[a].bits mod 8)));
if length(wB[a].w)-1+i < length(w) then
w[length(wB[a].w)-1+i]:=
w[length(wB[a].w)-1+i] or
(wB[b].w[i] shr (8 - (wB[a].bits mod 8)));
end;
end;
end;
function tRaetselFile.bIsAX(A: tWort; b: array of byte): boolean;
var
i: integer;
begin
result:=true;
for i:=0 to (A.bits div 8)-1 do
result:=result and (A.w[i] = b[i]);
result:=result and ((A.w[length(A.w)-2] and ($ff shr (8-(A.bits mod 8)))) =
(b[length(A.w)-2] and ($ff shr (8-(A.bits mod 8)))));
end;
procedure tRaetselFile.rConvert;
var
rP: longint;
wP,i: integer;
passt,lp: integer;
wBuff: byte;
begin
initWB;
setLength(inhalt,0);
rP:=0;
wP:=0;
wBuff:=0;
lp:=-1;
setLength(conv,length(conv)+1);
conv[length(conv)-1]:=0;
while rP<((length(conv)-1)*8) do begin
passt:=0;
for i:=0 to ceil(ln(length(wB))/ln(2))-1 do begin
passt:=passt or (byte(odd(conv[rP div 8] shr (rP mod 8))) shl i);
inc(rP);
end;
for i:=0 to wB[passt].bits-1 do begin
if wP=8 then begin
setLength(inhalt,length(inhalt)+1);
inhalt[length(inhalt)-1]:=wBuff;
wP:=0;
wBuff:=0;
end;
wBuff:=wBuff or (byte(odd((wB[passt].w[i div 8] shr (i mod 8)))) shl wP);
inc(wP);
end;
if lp<>-1 then
concatWs(lp,passt);
lp:=passt;
end;
setLength(conv,length(conv)+1);
conv[length(conv)-1]:=wBuff;
end;
procedure tRaetselFile.wConvert;
var
rP: longint;
wP,i,j: integer;
rBuff: array of byte;
rBBits,passt,lp: integer;
wBuff: byte;
begin
initWB;
setLength(conv,0);
rP:=0;
wP:=0;
wBuff:=0;
lp:=-1;
setLength(inhalt,length(inhalt)+1);
inhalt[length(inhalt)-1]:=0;
while rP<((length(inhalt)-1)*8) do begin
setLength(rBuff,0);
rBBits:=0;
passt:=-1;
for i:=length(wB)-1 downto 0 do
with wB[i] do begin
if bits > (8*length(inhalt) - rP) then continue;
if bits > rBBits then begin // mehr r-buffern
setLength(rBuff,(bits+7) div 8);
rBBits:=bits;
for j:=0 to length(rBuff)-1 do begin
rBuff[j]:=0;
if j + rP div 8 < length(inhalt) then
rBuff[j]:=rBuff[j] or (inhalt[j + rP div 8] shr (rP mod 8));
if j+1 + rP div 8 < length(inhalt) then
rBuff[j]:=rBuff[j] or ($ff and (inhalt[(rP div 8) + j+1] shl (8-(rP mod 8))));
end;
end;
if ((passt=-1) or (wB[passt].bits < wB[i].bits)) and
bIsAX(wB[i],rBuff) then
passt:=i;
end;
if passt=-1 then begin // geht ja gar nicht - geht ja wohl!
messageDlg('Zu wenig wörter im wörterbuch!',mterror,[mbOk],0);
exit;
end;
rP:=rP+wB[passt].bits;
for i:=0 to ceil(ln(length(wB))/ln(2))-1 do begin // wB-index speichern
if wP=8 then begin // w-buffer leeren
setLength(conv,length(conv)+1);
conv[length(conv)-1]:=wBuff;
wP:=0;
wBuff:=0;
end;
wBuff:=wBuff or byte(odd(passt shr i)) shl wP;
inc(wP);
end;
if lp<>-1 then
concatWs(lp,passt);
lp:=passt;
end;
setLength(conv,length(conv)+1);
conv[length(conv)-1]:=wBuff;
end;
procedure tRaetselFile.fFlush;
begin
wConvert;
blockWrite(f,conv[0],length(conv));
setLength(inhalt,0);
end;
procedure tRaetselFile.fRewrite;
var
c: cardinal;
begin
wacc:=true;
rewrite(f,1);
c:=$26594131;
blockWrite(f,c,4);
setLength(inhalt,length(kennung));
move(kennung[1],inhalt[0],length(kennung));
fPoint:=length(inhalt);
end;
procedure tRaetselFile.fCloseFile;
begin
if wacc then begin
fFlush;
closeFile(f);
end;
setLength(inhalt,0);
fPoint:=0;
end;
procedure tRaetselFile.fBlockWrite(var data; len: integer);
begin
setLength(inhalt,length(inhalt)+len);
move(data,inhalt[fPoint],len);
fPoint:=length(inhalt);
end;
function tRaetselFile.fBlockRead(var data; len: integer): boolean;
begin
result:=len<=(length(inhalt)-fPoint);
if not result then begin
fCloseFile;
exit;
end;
move(inhalt[fPoint],data,len);
fPoint:=fPoint+len;
end;
end.
|