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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
{
This file is part of the Free Pascal Integrated Development Environment
Copyright (c) 2000 by Berczi Gabor
Help support for OS/2 (.INF) help files
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program 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.
**********************************************************************}
{$R-}
unit WOS2Help;
interface
uses Objects,
WUtils,WHelp;
const
INFFileSignature = 'HS';
{ TOCEntry flags }
inf_tef_HasChildren = $80; { following nodes are a higher level }
inf_tef_Hidden = $40; { this entry doesn't appear in VIEW.EXE's presentation of the toc }
inf_tef_Extended = $20; { extended entry format }
inf_tef_LevelMask = $0f;
type
TINFFileHeader = packed record
Signature : array[1..2] of char;{ ID magic word (5348h = "HS") }
Unknown1 : byte; { unknown purpose, could be third letter of ID }
Flags : byte; { probably a flag word... }
{ bit 0: set if INF style file }
{ bit 4: set if HLP style file }
{ patching this byte allows reading HLP files }
{ using the VIEW command, while help files }
{ seem to work with INF settings here as well. }
HeaderSize : word; { total size of header }
Unknown2 : word; { unknown purpose }
NumTOC : word; { 16 bit number of entries in the tocarray }
TOCStrTabOfs: longint; { 32 bit file offset of the start of the }
{ strings for the table-of-contents }
TOCStrTabSize: longint; { number of bytes in file occupied by the }
{ table-of-contents strings }
TOCArrayOfs : longint; { 32 bit file offset of the start of tocarray }
NumResPanels: word; { number of panels with ressource numbers }
ResTabOfs : longint; { 32 bit file offset of ressource number table }
NumNames : word; { number of panels with textual name }
NameTabOfs : longint; { 32 bit file offset to panel name table }
NumIndexes : word; { number of index entries }
IndexTabOfs : longint; { 32 bit file offset to index table }
IndexTabSize: longint; { size of index table }
Unknown3: array[1..10] of byte; { unknown purpose }
SearchTabOfs: longint; { 32 bit file offset of full text search table }
SearchTabSize:longint; { size of full text search table }
NumSlots : word; { number of "slots" }
SlotTabOfs : longint; { file offset of the slots array }
DictSize : longint; { number of bytes occupied by the "dictionary" }
NumDictEntries: word; { number of entries in the dictionary }
DictOfs : longint; { file offset of the start of the dictionary }
ImageOfs : longint; { file offset of image data }
Unknown4 : byte; { unknown purpose }
NLSTabOfs : longint; { 32 bit file offset of NLS table }
NLSTabSize : longint; { size of NLS table }
ExtBlockOfs : longint; { 32 bit file offset of extended data block }
Unknown5: array[1..12] of byte;{ unknown purpose }
Title : array[1..48] of char;{ ASCII title of database }
end;
PINFTOCArray = ^TINFTOCArray;
TINFTOCArray = packed array[0..16382] of longint;
PINFSlotArray = ^TINFSlotArray;
TINFSlotArray = packed array[0..32766] of word;
PSlotArray = ^TSlotArray;
TSlotArray = packed array[0..16382] of longint;
TINFTOCEntry = packed record
Size : byte; { length of the entry including this byte }
Flags : byte; { flag byte, description folows (MSB first) }
NumSlots : byte; { number of "slots" occupied by the text for }
{ this toc entry }
Slots : record end;
end;
TINFTopicHeader = packed record
Stuff : byte; { ?? }
LocalDictPos : longint; { file offset of the local dictionary }
NumLocalDict : byte; { number of entries in the local dict }
TextSize : word; { number of bytes in the text }
Text : record end;{ encoded text of the article }
end;
TINFEscHeader = packed record
EscLen : byte; { length of the sequence (including esclen but excluding FF) }
EscCode: byte; { which escape function }
end;
POS2HelpFile = ^TOS2HelpFile;
TOS2HelpFile = object(THelpFile)
constructor Init(AFileName: string; AID: word);
destructor Done; virtual;
public
function LoadIndex: boolean; virtual;
function ReadTopic(T: PTopic): boolean; virtual;
private
F: PStream;
Header: TINFFileHeader;
Dictionary: PUnsortedStringCollection;
Slots: PSlotArray;
SlotsSize: longint;
function ReadHeader: boolean;
function ReadSlots: boolean;
function ReadDictionary: boolean;
function ReadTOC: boolean;
function ReadTopicRec(FileOfs: longint; Topic: PTopic; Lines: PUnsortedStringCollection): boolean;
end;
TINFGetAttrColorProc = function(TextStyle, TextColor: byte; var Color: byte): boolean;
function DefINFGetAttrColor(TextStyle, TextColor: byte; var Color: byte): boolean;
const INFGetAttrColor : TINFGetAttrColorProc = {$ifdef fpc}@{$endif}DefINFGetAttrColor;
procedure RegisterHelpType;
implementation
function DefINFGetAttrColor(TextStyle, TextColor: byte; var Color: byte): boolean;
{
style; // 1,2,3: same as :hp#.
// 4,5,6: same as :hp5,6,7.
// 0 returns to plain text
color; // 1,2,3: same as :hp4,8,9.
// 0: default color
:hp4 text is light blue
:hp8 text is red
:hp9 text is magenta
:hp1 is italic font
:hp2 is bold font
:hp3 is bold italic font
:hp5 is normal underlined font
:hp6 is italic underlined font
:hp7 is bold underlined font
}
begin
DefINFGetAttrColor:=false;
end;
function KillNonASCII(S: string): string;
var I: sw_integer;
begin
for I:=1 to length(S) do
if S[I]<#32 then
S[I]:='.';
KillNonASCII:=S;
end;
function ContainsNonASCIIChar(const S: string): boolean;
var I: sw_integer;
begin
ContainsNonASCIIChar:=false;
for I:=1 to length(S) do
if S[I]<#32 then
begin
ContainsNonASCIIChar:=true;
Break;
end;
end;
constructor TOS2HelpFile.Init(AFileName: string; AID: word);
var OK: boolean;
begin
if inherited Init(AID)=false then Fail;
New(Dictionary, Init(100,1000));
F:=New(PFastBufStream, Init(AFileName, stOpenRead, HelpStreamBufSize));
OK:=F<>nil;
if OK then OK:=(F^.Status=stOK);
if OK then OK:=ReadHeader;
if OK=false then
begin
Done;
Fail;
end;
end;
function TOS2HelpFile.ReadHeader: boolean;
var OK: boolean;
begin
F^.Read(Header,sizeof(Header));
OK:=(F^.Status=stOK);
OK:=OK and (Header.Signature=INFFileSignature);
ReadHeader:=OK;
end;
function TOS2HelpFile.LoadIndex: boolean;
var OK: boolean;
begin
OK:=ReadDictionary;
if OK then OK:=ReadSlots;
if OK then OK:=ReadTOC;
LoadIndex:=OK;
end;
function TOS2HelpFile.ReadDictionary: boolean;
var OK: boolean;
I: longint;
C: array[0..255] of char;
B: byte;
begin
F^.Seek(Header.DictOfs);
OK:=(F^.Status=stOK);
I:=0;
while OK and (I<Header.NumDictEntries) do
begin
FillChar(C,sizeof(C),0);
F^.Read(B,sizeof(B)); F^.Read(C,B-1);
OK:=(F^.Status=stOK);
if OK then
Dictionary^.InsertStr(StrPas(@C));
Inc(I);
end;
ReadDictionary:=OK;
end;
function TOS2HelpFile.ReadSlots: boolean;
var OK: boolean;
begin
SlotsSize:=Header.NumSlots*sizeof(Slots^[0]);
GetMem(Slots,SlotsSize);
F^.Seek(Header.SlotTabOfs);
OK:=(F^.Status=stOK);
if OK then
begin
F^.Read(Slots^,SlotsSize);
OK:=(F^.Status=stOK);
end;
ReadSlots:=OK;
end;
function TOS2HelpFile.ReadTOC: boolean;
var OK: boolean;
I,Count: longint;
TE: TINFTOCEntry;
W: word;
C: array[0..255] of char;
StartOfs: longint;
S: string;
SubSlots: PWordArray;
SubSlotsSize: longint;
TOC: PINFTOCArray;
TOCSize: longint;
begin
F^.Seek(Header.TOCArrayOfs); TOCSize:=Header.TOCStrTabSize;
OK:=(F^.Status=stOK);
if OK then
begin
GetMem(TOC,TOCSize);
F^.Read(TOC^,TOCSize);
OK:=(F^.Status=stOK);
I:=0;
while OK and (I<Header.NumTOC) do
begin
F^.Seek(TOC^[I]);
OK:=(F^.Status=stOK);
if OK then
begin
StartOfs:=F^.GetPos;
F^.Read(TE,sizeof(TE));
OK:=(F^.Status=stOK);
end;
if OK and ((TE.Flags and inf_tef_Extended)<>0) then
begin
F^.Read(W,sizeof(W));
Count:=0;
if (W and 1)<>0 then Inc(Count,5);
if (W and 2)<>0 then Inc(Count,5);
if (W and 4)<>0 then Inc(Count,2);
if (W and 8)<>0 then Inc(Count,2);
F^.Seek(F^.GetPos+Count);
OK:=(F^.Status=stOK);
end;
if OK then
begin
SubSlotsSize:=sizeof(Word)*TE.NumSlots;
GetMem(SubSlots,SubSlotsSize);
F^.Read(SubSlots^,SubSlotsSize);
FillChar(C,sizeof(C),0);
F^.Read(C,Max(0,TE.Size-(F^.GetPos-StartOfs)));
OK:=(F^.Status=stOK);
if OK then
begin
S:=StrPas(@C);
AddTopic(I,StartOfs,S,SubSlots,SubSlotsSize);
if (S<>'') and (not ContainsNonASCIIChar(S)) then
AddIndexEntry(S,I);
{ !}
end;
FreeMem(SubSlots,SubSlotsSize);
end;
Inc(I);
end;
FreeMem(TOC,TOCSize); TOC:=nil;
end;
ReadTOC:=OK;
end;
function TOS2HelpFile.ReadTopicRec(FileOfs: longint; Topic: PTopic; Lines: PUnsortedStringCollection): boolean;
var Line: string;
CharsInLine: sw_integer;
LeftMargin: byte;
TextStyle,TextColor: byte;
InMonospace: boolean;
Align: (alLeft,alRight,alCenter);
LineNo: longint;
procedure FlushLine;
begin
if Line<>'' then Lines^.InsertStr(Line);
Line:='';
end;
procedure AddChar(C: char);
begin
if length(Line)>=255 then FlushLine;
Line:=Line+C;
end;
procedure AddString(const S: string);
var I: sw_integer;
begin
for I:=1 to length(S) do
AddChar(S[I]);
end;
procedure AddTextChar(C: char);
begin
if (C=hscLineBreak) then
begin
case Align of
alRight : AddChar(hscRight);
alCenter: AddChar(hscCenter);
end;
end;
if (CharsInLine=0) and (C<>hscLineBreak) then
begin
if (LeftMargin>0) then
begin
AddString(CharStr(#255,LeftMargin)+hscLineStart);
Inc(CharsInLine,LeftMargin);
end else
if InMonospace then
begin
AddChar(' ');
Inc(CharsInLine);
end;
end;
AddChar(C);
if C=hscLineBreak then
begin
CharsInLine:=0;
Inc(LineNo);
end
else
Inc(CharsInLine);
end;
procedure AddText(const S: string);
var I: sw_integer;
begin
for I:=1 to length(S) do
AddTextChar(S[I]);
end;
var H: TINFTopicHeader;
Text: PByteArray;
TextOfs: longint;
Dict: PWordArray;
Spacing: boolean;
function NextByte: byte;
begin
NextByte:=Text^[TextOfs];
Inc(TextOfs);
end;
procedure ReadBytes(DataPtr: pointer; NumBytes: sw_integer);
var I: sw_integer;
begin
for I:=1 to NumBytes do
if Assigned(DataPtr) then
PByteArray(DataPtr)^[I-1]:=NextByte
else
NextByte;
end;
procedure AddWord(LocalIndex: word);
begin
AddText(GetStr(Dictionary^.At(Dict^[LocalIndex])));
if Spacing and not InMonospace then AddTextChar(' ');
end;
var
DictSize,EscStartOfs: longint;
OK: boolean;
EH: TINFEscHeader;
B,Color: byte;
W: word;
CurLinkCtx: longint;
InTempMargin: boolean;
begin
F^.Reset;
F^.Seek(FileOfs);
OK:=(F^.Status=stOK);
if OK then
begin
F^.Read(H,sizeof(H));
OK:=(F^.Status=stOK);
end;
if OK then
begin
LineNo:=0;
Line:=''; LeftMargin:=0;
InTempMargin:=false;
CharsInLine:=0; TextStyle:=0; TextColor:=0; Align:=alLeft;
CurLinkCtx:=-1; InMonospace:=false;
DictSize:=H.NumLocalDict*sizeof(Dict^[0]);
GetMem(Text,H.TextSize);
GetMem(Dict,DictSize);
F^.Read(Text^,H.TextSize);
F^.Seek(H.LocalDictPos);
F^.Read(Dict^,DictSize);
OK:=(F^.Status=stOK);
TextOfs:=0; Spacing:=true;
while OK and (TextOfs<H.TextSize) do
begin
B:=NextByte;
if (B<H.NumLocalDict) then
begin
AddWord(B);
end else
case B of
$fa : begin
if (LineNo>0) then
AddTextChar(hscLineBreak);
if InTempMargin then
LeftMargin:=0;
AddTextChar(hscLineBreak);
Spacing:=true;
end;
$fb : { ??? };
$fc : Spacing:=not Spacing;
$fd : begin
AddTextChar(hscLineBreak);
Spacing:=true;
end;
$fe : AddChar(' ');
$ff : begin
EscStartOfs:=TextOfs;
ReadBytes(@EH,sizeof(EH));
case EH.EscCode of
$02,$11,$12 :
begin
{ set left margin }
if EH.EscCode=$11 then
AddTextChar(hscLineBreak);
LeftMargin:=NextByte;
end;
$03 :
{ right margin, not used }
NextByte;
$04 :
begin
TextStyle:=NextByte;
if (TextStyle=0) or (not INFGetAttrColor(TextStyle,TextColor,Color)) then
AddChar(hscNormText)
else
AddText(hscTextColor+chr(Color));
end;
$05 :
begin
W:=word(NextByte)*256+NextByte;
AddChar(hscLink);
CurLinkCtx:=W;
end;
$08 :
if CurLinkCtx<>-1 then
begin
AddChar(hscLink);
AddLinkToTopic(Topic,ID,CurLinkCtx);
end;
$0b :
begin
if CharsInLine>0 then
AddTextChar(hscLineBreak);
AddTextChar(hscLineBreak);
AddChar(hscCode);
InMonospace:=true;
end;
$0c :
begin
AddChar(hscCode);
InMonospace:=false;
end;
$0d :
begin
TextColor:=NextByte;
if (TextColor=0) or (not INFGetAttrColor(TextStyle,TextColor,Color)) then
AddChar(hscNormText)
else
AddText(hscTextColor+chr(Color));
end;
$0e :
begin
AddText(hscLineBreak+'[img]'+hscLineBreak);
end;
$1a :
begin
if CharsInLine>0 then AddText(hscLineBreak);
case NextByte of
1 : Align:=alLeft;
2 : Align:=alRight;
4 : Align:=alCenter;
end;
Spacing:=false;
end;
$1b :
begin
Spacing:=true;
end;
$1c :
begin
LeftMargin:=CharsInLine;
InTempMargin:=true;
end;
end;
TextOfs:=EscStartOfs+EH.EscLen;
end;
end;
end;
if CharsInLine>0 then
AddTextChar(hscLineBreak);
AddTextChar(hscLineBreak);
FlushLine;
FreeMem(Dict,DictSize);
FreeMem(Text,H.TextSize);
end;
F^.Reset;
ReadTopicRec:=OK;
end;
function TOS2HelpFile.ReadTopic(T: PTopic): boolean;
var OK: boolean;
I,NumSlots,Idx: sw_integer;
TopicOfs: longint;
L: PUnsortedStringCollection;
Title: string;
begin
OK:=false;
NumSlots:=T^.ExtDataSize div 2; { extdata is array of word }
New(L, Init(100,100));
Title:=GetStr(T^.Param);
if Title<>'' then
begin
L^.InsertStr(' '+Title+' Ü'+hscLineBreak);
L^.InsertStr(' '+CharStr('ß',length(Title)+3)+hscLineBreak);
end;
if 0<T^.HelpCtx then
begin
L^.InsertStr(hscLink+'[previous topic]'+hscLink+' ');
AddLinkToTopic(T,ID,T^.HelpCtx-1);
end;
if T^.HelpCtx<Header.NumTOC then
begin
L^.InsertStr(hscLink+'[next topic]'+hscLink);
AddLinkToTopic(T,ID,T^.HelpCtx+1);
end;
L^.InsertStr(hscLineBreak);
for I:=0 to NumSlots-1 do
begin
Idx:=PWordArray(T^.ExtData)^[I];
TopicOfs:=Slots^[Idx];
OK:=ReadTopicRec(TopicOfs,T,L);
if not OK then
Break;
end;
if OK then BuildTopic(L,T);
Dispose(L, Done);
ReadTopic:=OK;
end;
destructor TOS2HelpFile.Done;
begin
if Assigned(Slots) then FreeMem(Slots, SlotsSize); Slots:=nil;
if Assigned(Dictionary) then Dispose(Dictionary, Done); Dictionary:=nil;
if Assigned(F) then Dispose(F, Done); F:=nil;
inherited Done;
end;
function CreateProc(const FileName,Param: string;Index : longint): PHelpFile;
begin
CreateProc:=New(POS2HelpFile, Init(FileName,Index));
end;
procedure RegisterHelpType;
begin
RegisterHelpFileType({$ifdef FPC}@{$endif}CreateProc);
end;
END.
|