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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
|
unit HelpFile;
{$mode objfpc}{$H+}
interface
// Encapsulates the basic reading of a help file's structure.
uses
Classes
,SysUtils
,fpg_imagelist
,IPFFileFormatUnit
,HelpTopic
,HelpBitmap
,SearchTable
;
type
TIndexEntry = class(TObject)
private
name: String;
topic: TTopic;
flags: uint8;
public
constructor Create(aName: String; aTopic: TTopic; aFlags: uint8);
destructor Destroy; override;
property getTopic: TTopic read topic;
function getLabel: String;
function isGlobal: boolean;
function getLevel: integer;
end;
TIndex = class(TObject)
private
entries: TStringList;
public
constructor Create;
destructor Destroy; override;
function Count: longint;
function GetLabels: TStringList;
function GetTopic(aPos: longint): TTopic;
procedure Add(anIndexEntry: TIndexEntry);
end;
THelpFile = class(TObject)
private
function GetStringResourceIDCount: integer;
function GetNumericResourceIDCount: integer;
protected
_Filename : string;
_FileSize : longint;
_Handle: TFileStream;
_pSlotData: pUInt16;
_SlotDataSize: longint;
_Title: string;
_Topics: TList; // of TTopics
_Dictionary: TStringList; // pointers to strings.
_Index: TIndex;
_SearchTable: TSearchTable;
_ReferencedFiles: TStringList;
_FontTable: TList;
_pHeader: TPHelpFileHeader;
_pExtendedHeader: TPExtendedHelpFileHeader;
_pContentsData: pointer;
_pResourceData: pointer;
_pSearchData: pointer;
_pHighlightWords: UInt32ArrayPointer;
_pSlotOffsets: Uint32ArrayPointer;
_pDictionaryData: pointer;
_pFontTableData: pointer;
_pTopicNameData: pointer;
_pTopicGlobalNamesData: pointer;
procedure InitMembers;
procedure Open;
procedure Close;
procedure ReadFileBlock( Var Dest: pointer;
const StartPosition: LongWord;
const Length: LongWord);
procedure ReadHeader;
procedure ReadContents;
procedure ReadDictionary;
procedure ReadSearchTable;
procedure ReadIndex;
procedure ReadReferencedFilesTable;
procedure ReadFontTableData;
procedure ParseFontTable;
function GetTopic( Index: longint ): TTopic;
function GetTopicCount: longint;
function GetDictionaryCount: longint;
function GetDictionaryWord( Index: longint ): string;
function GetHighlightWords: UInt32ArrayPointer;
function GetSearchTable: TSearchTable;
// Lookup global or local panel name list
function FindTopicByName( const Name: string;
Var pData: pointer;
Count: longint;
Offset: longint ): TTopic;
public
constructor Create( const aFileName: string );
destructor Destroy; override;
function GetIndex: TIndex;
property Title: string read _Title;
property Topics[ Index: longint ]: TTopic read GetTopic;
property TopicList: TList read _Topics;
property TopicCount: longint read GetTopicCount;
property StringResourceIDCount: integer read GetStringResourceIDCount;
property NumericResourceIDCount: integer read GetNumericResourceIDCount;
property Index: TIndex read GetIndex;
property Filename: string read _FileName;
property ReferencedFiles: TStringList read _ReferencedFiles;
procedure GetImages( ImageOffsets: TList; Images: TfpgImageList );
function GetImage( ImageOffset: longint ): THelpBitmap;
property DictionaryCount: longint read GetDictionaryCount;
property DictionaryWords[ Index: longint ]: string read GetDictionaryWord;
function IndexOfTopic( Topic: TTopic ): longint;
property SearchTable: TSearchTable read GetSearchTable;
function FindTopicByResourceID( ID: uint16 ): TTopic;
function FindTopicByLocalName( const Name: string ): TTopic;
function FindTopicByGlobalName( const Name: string ): TTopic;
function FindTopicByTitleStartsWith( const SearchText: string ): TTopic;
function FindTopicByTitleContains( const SearchText: string ): TTopic;
function FindTopicByIndexStartsWith( const SearchText: string ): TTopic;
function FindTopicByIndexContains( const SearchText: string ): TTopic;
procedure FindResourceIDsForTopic( Topic: TTopic;
ResourceIDs: TList );
property HighlightWords: UInt32ArrayPointer read GetHighlightWords;
property FileSize: longint read _FileSize;
procedure SetupFontSubstitutes( Substitutions: string );
public
NotesLoaded: boolean; // used externally
end;
// Returns helpfile that the given topic is within
Function TopicFile( Topic: TTopic ): THelpFile;
function GetHelpFileTitle( const Filename: string ): string;
Implementation
uses
// BseErr,
// StringUtilsUnit,
// CharUtilsUnit,
// DebugUnit,
// ACLFileIOUtility,
// ACLLanguageUnit;
fpg_main
,fpg_utils
,nvUtilities
,ACLStringUtility
;
// Load "missing" bitmap
{ TODO -oGraeme -cbitmap : Create and load a "missing image" image }
{.$R Images}
const
FileErrorNotFound = 'File not found ';
FileErrorAccessDenied = 'File access denied';
FileErrorInUse = 'File in use';
FileErrorInvalidHeader = 'Invalid file header';
// -----------
// TIndexEntry
// -----------
CONSTRUCTOR TIndexEntry.Create(aName: String; aTopic: TTopic; aFlags: uint8);
begin
LogEvent(LogObjConstDest, 'TIndexEntry.Create');
name := aName;
topic := aTopic;
flags := aFlags;
end;
DESTRUCTOR TIndexEntry.Destroy;
begin
LogEvent(LogObjConstDest, 'TIndexEntry.Destroy');
topic := nil;
inherited Destroy;
end;
FUNCTION TIndexEntry.getLabel: String;
begin
result := name;
// index level check (level 1 or 2)
if (getLevel) > 1 then
begin
result := '- ' + result;
end;
if isGlobal then
begin
result := result + ' (g)';
end;
end;
FUNCTION TIndexEntry.isGlobal: boolean;
begin
result := (flags and 64) > 0
end;
FUNCTION TIndexEntry.getLevel: integer;
begin
result := 1;
// index level check (level 1 or 2)
if (flags and 2 ) > 0 then
begin
result := 2;
end;
end;
// -----------
// TIndex
// -----------
CONSTRUCTOR TIndex.Create;
begin
inherited Create;
entries := TStringList.Create;
// labels := nil; // lazy
end;
DESTRUCTOR TIndex.Destroy;
var
i : longint;
tmpEntry : TIndexEntry;
begin
LogEvent(LogObjConstDest, 'TIndex.Destroy (size:' + IntToStr(entries.Count) + ')');
for i := 0 to entries.Count - 1 do
begin
tmpEntry := TIndexEntry(entries.Objects[i]);
if tmpEntry <> nil then
begin
tmpEntry.Free;
entries.Objects[i] := nil;
end;
end;
entries.Free;
inherited Destroy;
end;
FUNCTION TIndex.Count: longint;
begin
result := entries.Count;
end;
FUNCTION TIndex.GetLabels: TStringList;
begin
result := entries;
end;
FUNCTION TIndex.GetTopic(aPos: longint): TTopic;
begin
result := TIndexEntry(entries.Objects[aPos]).getTopic;
end;
PROCEDURE TIndex.add(anIndexEntry: TIndexEntry);
begin
// LogEvent(LogDebug, 'TIndex.add(' + anIndexEntry.getLabel + ', ' + anIndexEntry.ClassName + ')');
entries.AddObject(anIndexEntry.getLabel, anIndexEntry);
end;
//Procedure OnLanguageEvent( Language: TLanguageFile;
// const Apply: boolean );
//var
// tmpPrefix : String;
//begin
// tmpPrefix := 'HelpFile' + LANGUAGE_LABEL_DELIMITER;
//
// Language.LL( Apply, FileErrorNotFound, tmpPrefix + 'FileErrorNotFound', 'File not found' );
// Language.LL( Apply, FileErrorAccessDenied, tmpPrefix + 'FileErrorAccessDenied', 'Access denied' );
// Language.LL( Apply, FileErrorInUse, tmpPrefix + 'FileErrorInUse', 'File in use by another program' );
// Language.LL( Apply,
// FileErrorInvalidHeader,
// tmpPrefix + 'FileErrorInvalidHeader',
// 'File doesn''t appear to be an OS/2 Help document (header ID not correct)' );
// Language.LL( Apply,
// ErrorCorruptHelpFile,
// tmpPrefix + 'ErrorCorruptHelpFile',
// 'File is corrupt' );
//end;
Function TopicFile( Topic: TTopic ): THelpFile;
Begin
Result := Topic.HelpFile as THelpFile;
end;
function THelpFile.GetStringResourceIDCount: integer;
begin
Result := _pHeader^.nname;
end;
function THelpFile.GetNumericResourceIDCount: integer;
begin
Result := _pHeader^.nres;
end;
procedure THelpFile.InitMembers;
begin
_SlotDataSize := 0;
_pHeader := nil;
_pExtendedHeader := nil;
_pContentsData := nil;
_pSlotOffsets := nil;
_pResourceData := nil;
_pSearchData := nil;
_pDictionaryData := nil;
// _pIndexData := nil;
_pFontTableData := nil;
_pHighlightWords := nil;
_Dictionary:= TStringList.Create;
_Topics := TList.Create;
// _Index := TStringList.Create;
_ReferencedFiles := TStringList.Create;
_FontTable := TList.Create;
NotesLoaded := false;
end;
constructor THelpFile.Create(const aFileName: string);
begin
LogEvent(LogObjConstDest, 'THelpFile.Create (file:' + aFileName + ')');
LogEvent(LogParse, 'Helpfile Load: ' + aFileName);
_FileName := aFileName;
InitMembers;
Open;
// we always need these basics:
try
ReadHeader;
ReadContents;
ReadDictionary;
ReadFontTableData;
ParseFontTable;
ReadReferencedFilesTable;
except
Close;
raise;
end;
// the rest is loaded on demand
end;
destructor THelpFile.Destroy;
begin
LogEvent(LogObjConstDest, 'THelpFile.Destroy');
Dispose( _pHeader );
Dispose( _pExtendedHeader );
FreeMem( _pContentsData );
FreeMem( _pSlotOffsets );
FreeMem( _pResourceData );
FreeMem( _pSearchData );
FreeMem( _pDictionaryData );
// DeallocateMemory( _pIndexData );
FreeMem( _pFontTableData );
FreeMem( _pHighlightWords );
// index entries are pointing to topics
// so let us clean them first
if Assigned( _Index ) then
_Index.Free;
if Assigned( _Topics ) then
DestroyListAndObjects( _Topics );
_Dictionary.Free;
_SearchTable.Free;
_ReferencedFiles.Free;
_FontTable.Free;
_Handle.Free;
end;
procedure THelpFile.Open;
begin
LogEvent(LogDebug, 'Open File >>');
if not FileExists( _Filename ) then
raise EHelpFileException.Create( FileErrorNotFound );
try
_Handle := TFileStream.Create(_FileName, fmOpenRead or fmShareDenyWrite);
except
on E: Exception do
raise EHelpFileException.Create(E.Message);
end;
//case rc of
// ERROR_FILE_NOT_FOUND: // crap, this doesn't actually occur!
// raise EHelpFileException.Create( FileErrorNotFound );
//
// ERROR_ACCESS_DENIED:
// raise EHelpFileException.Create( FileErrorAccessDenied );
//
// ERROR_SHARING_VIOLATION:
// raise EHelpFileException.Create( FileErrorInUse );
//
// else
// raise EHelpFileException.Create( SysErrorMessage( rc ) );
//end;
_FileSize := GetFileSize(_Filename);
LogEvent(LogDebug, 'Open File <<');
end;
procedure THelpFile.Close;
begin
_Handle.Free;
_Handle := nil;
end;
procedure THelpFile.ReadFileBlock(var Dest: pointer;
const StartPosition: LongWord; const Length: LongWord);
var
bytes: LongWord;
begin
if Length = 0 then
exit; // nothing to read - go home!
_Handle.Seek(StartPosition, soBeginning);
// we allocate early so this should never happen
if Dest = nil then
Dest := GetMem(Length);
bytes := _Handle.Read(Dest^, Length);
if bytes <> Length then
raise EHelpFileException.Create(ErrorCorruptHelpFile);
end;
procedure THelpFile.ReadHeader;
begin
LogEvent(LogParse, 'Read header');
New(_pHeader);
ReadFileBlock( _pHeader,
0,
sizeof( THelpFileHeader ) );
if _pHeader^.ID <> INF_HEADER_ID then
begin
// not an OS/2 help file.
if (Byte(_pHeader^.ID[0]) = $5f) and (Byte(_pHeader^.ID[1]) = $3f) then
raise EWindowsHelpFormatException.Create( 'It seems we have a Win16 help file!' );
raise EHelpFileException.Create( FileErrorInvalidHeader );
end;
_Title := _pHeader^.Title;
if _pHeader^.extstart > 0 then
begin
New(_pExtendedHeader);
// read extended header
ReadFileBlock( _pExtendedHeader,
_pHeader^.extstart,
sizeof( _pExtendedHeader^ ) );
end;
end;
procedure THelpFile.ReadContents;
var
Topic: TTopic;
EntryIndex: longint;
pEntry: pTTOCEntryStart;
pEnd: pbyte;
tocarray: UInt32ArrayPointer;
pData: Pointer;
p: PByte;
begin
LogEvent(LogParse, 'Read contents');
if _pHeader^.ntoc = 0 then
exit; // explicit check required since ntoc is unsigned
// Presize the topics list to save reallocation time
_Topics.Capacity := _pHeader^.ntoc;
// read toc offsets array
//ReadFileBlock( tocarray,
// _pHeader^.tocoffsetsstart,
// _pHeader^.ntoc * SizeOf(uint32) );
// read slots first so that Topics can refer to it.
ReadFileBlock( _pSlotOffsets,
_pHeader^.slotsstart,
_pHeader^.nslots * sizeof( uint32 ) );
ReadFileBlock( _pContentsData,
_pHeader^.tocstart,
_pHeader^.toclen );
pEntry := _pContentsData;
pEnd := _pContentsData + _pHeader^.toclen;
p := PByte(pEntry);
for EntryIndex := 0 to _pHeader^.ntoc - 1 do
begin
// pEntry := _Handle.Seek(tocarray[EntryIndex], soBeginning);
// pEntry := tocarray[EntryIndex];
if p >= pEnd then
// runs off end of data!
raise EHelpFileException.Create( ErrorCorruptHelpFile );
Topic := TTopic.Create( _Handle,
_pSlotOffsets,
_Dictionary,
pEntry,
_FontTable,
_ReferencedFiles );
Topic.HelpFile := Self;
Topic.Index := EntryIndex;
_Topics.Add( Topic );
p := PByte(pEntry);
inc(p, pEntry^.Length);
pEntry := pTTOCentryStart(p);
end;
end;
procedure THelpFile.ReadDictionary;
var
i: longint;
Len: uint8;
p: pbyte;
pEnd: pbyte;
s: string;
c: array[0..255] of char;
begin
LogEvent(LogParse, 'Read dictionary');
if _pHeader^.ndict = 0 then
exit; // explicit check required since ndict is unsigned
ReadFileBlock( _pDictionaryData,
_pHeader^.dictstart,
_pHeader^.dictlen );
P := _pDictionaryData;
pEnd := _pDictionaryData + _pHeader^.dictlen;
// Presize the dictionary to save reallocation
_Dictionary.Capacity := _pHeader^.ndict;
for i := 0 to _pHeader^.ndict - 1 do
begin
// adjust length so we can use as a Pascal string
// (file uses length including length byte,
// Pascal string have length excluding length byte)
if p >= pEnd then
// ran off end of data
raise EHelpFileException.Create( 'Error reading help file dictionary' );
FillChar(c, sizeof(c), 0); // fill string with NUL chars
Len := p^ - 1; // read string length value (corrected length)
Inc(p, sizeof(byte)); // move pointer
Move(p^, c, Len); // read string of dictionary
s := c; // convert PChar to String type
_Dictionary.Add( s );
Inc(p, Len); // move pointer to next item
end;
end;
function THelpFile.GetIndex: TIndex;
begin
if _Index = nil then
begin
ReadIndex;
end;
Result := _Index;
end;
type
TIndexEntryHeader = packed record
TextLength: uint8;
Flags: uint8;
NumberOfRoots: uint8;
TOCIndex: uint16;
end;
pTIndexEntryHeader = ^TIndexEntryHeader;
procedure THelpFile.ReadIndex;
var
IndexIndex: longint;
pEntryHeader: pTIndexEntryHeader;
EntryText: string;
IndexTitleLen: longint;
p: pByte;
pEnd: pByte;
pIndexData: pointer;
tmpIndexEntry: TIndexEntry;
begin
LogEvent(LogParse, 'Read index');
_Index := TIndex.Create;
if _pHeader^.nindex = 0 then
exit; // explicit check required since ndict is unsigned
pIndexData := nil;
ReadFileBlock( pIndexData,
_pHeader^.indexstart,
_pHeader^.indexlen );
P := pIndexData;
pEnd := pIndexData + _pHeader^.indexlen;
for IndexIndex := 0 to _pHeader^.nindex - 1 do
begin
if p >= pEnd then
// ran off end of data
raise EHelpFileException.Create( 'Error reading help file index' );
pEntryHeader := pTIndexEntryHeader(p);
IndexTitleLen := pEntryHeader^.TextLength;
inc( p, sizeof( TIndexEntryHeader ) );
EntryText := '';
SetString(EntryText, PChar(p), IndexTitleLen);
if pEntryHeader^.TOCIndex < _Topics.Count then
begin
tmpIndexEntry := TIndexEntry.Create(EntryText, TTopic(_Topics[pEntryHeader^.TOCIndex]), pEntryHeader^.flags);
_Index.Add(tmpIndexEntry);
end
else
// raise EHelpFileException.Create( 'Error reading help file index - out of range topic reference' );
; // pass! something special
inc( p, IndexTitleLen
+ pEntryHeader^.NumberOfRoots
* sizeof( uint32 ) ); // skip 'roots' for index search
end;
FreeMem( pIndexData );
end;
function THelpFile.GetSearchTable: TSearchTable;
begin
if _SearchTable = nil then
ReadSearchTable;
Result := _SearchTable;
end;
procedure THelpFile.ReadSearchTable;
var
SearchTableOffset: longint;
SearchTableRecordLengthIs16Bit: boolean;
begin
LogEvent(LogParse, 'Read search table');
if _pHeader^.SearchLen = 0 then
begin
LogEvent(LogParse, 'Read search table (len = 0');
exit;
end;
SearchTableOffset := _pHeader^.SearchStart and $7fffffff;
SearchTableRecordLengthIs16Bit := _pHeader^.SearchStart and $80000000 > 0;
ReadFileBlock( _pSearchData,
SearchTableOffset,
_pHeader^.SearchLen );
_SearchTable := TSearchTable.Create( _pSearchData,
SearchTableRecordLengthIs16Bit,
_Dictionary.Count,
_Topics.Count );
end;
function THelpFile.GetHighlightWords: UInt32ArrayPointer;
begin
if _pHighlightWords = nil then
_pHighlightWords := GetMem( _Dictionary.Count * sizeof( UInt32 ) );
Result := _pHighlightWords;
end;
function THelpFile.FindTopicByResourceID( ID: uint16 ): TTopic;
var
i: longint;
pResourceIDs: UInt16ArrayPointer;
pTopicIndices: UInt16ArrayPointer;
FileResourceID: uint16;
TopicIndex: uint16;
begin
Result := nil;
if _pHeader^.nres = 0 then
// since nres is unsigned
exit;
if _pResourceData = nil then
begin
ReadFileBlock( _pResourceData,
_pHeader^.resstart,
(_pHeader^.nres * sizeof( uint16 )) * 2 ); // list of IDs, list of topics
end;
pResourceIDs := _pResourceData;
pTopicIndices := _pResourceData
+ _pHeader^.nres * sizeof( uint16 );
for i := 0 to _pHeader^.nres - 1 do
begin
FileResourceID := pResourceIDs^[ i ];
if FileResourceID = ID then
begin
// found
TopicIndex := pTopicIndices^[ i ];
Result := TTopic(_Topics[ TopicIndex ]);
exit;
end;
end;
end;
// Look up a local "panel name" and return associated topic, if any.
function THelpFile.FindTopicByLocalName( const Name: string ): TTopic;
begin
Result := FindTopicByName( Name,
_pTopicNameData,
_pHeader^.nname,
_pHeader^.namestart );
end;
function THelpFile.FindTopicByGlobalName( const Name: string ): TTopic;
begin
Result := nil;
if _pExtendedHeader = nil then
// no extended header - no global list to lookup
exit;
Result := FindTopicByName( Name,
_pTopicGlobalNamesData,
_pExtendedHeader ^. EntryInGNameTable,
_pExtendedHeader ^. HelpPanelGNameTblOffset );
end;
// The text of the names are stored in the (global) dictionary
// with a table referencing them.
// We could use a binary search here... but whatever...
function THelpFile.FindTopicByName( const Name: string;
Var pData: pointer;
Count: longint;
Offset: longint ): TTopic;
var
i: longint;
pNameTable: UInt16ArrayPointer;
pTopicIndices: UInt16ArrayPointer;
TopicIndex: uint16;
TopicNameWordIndex: uint16;
TopicName: string;
begin
Result := nil;
if Count = 0 then
// since it's unsigned
exit;
if pData = nil then
ReadFileBlock( pData,
Offset,
Count * sizeof( uint16 ) * 2 ); // list of name words, list of topics
// get pointers to the two parts of the table
pNameTable := pData;
pTopicIndices := pData
+ Count * sizeof( uint16 );
for i := 0 to Count - 1 do
begin
TopicNameWordIndex := pNameTable^[ i ];
TopicName := DictionaryWords[ TopicNameWordIndex ];
if CompareText( TopicName, Name ) = 0 then
begin
// found
TopicIndex := pTopicIndices^[ i ];
Result := TTopic(_Topics[ TopicIndex ]);
exit;
end;
end;
end;
// TODO move to index class
function THelpFile.FindTopicByIndexStartsWith( const SearchText: string ): TTopic;
var
i: longint;
tmpLabel: String;
begin
result := nil;
GetIndex; // make sure it's read
for i := 0 to _Index.Count - 1 do
begin
tmpLabel := _Index.GetLabels[i];
if SameText(tmpLabel, SearchText) then
begin
// found
result := Index.getTopic(i);
exit;
end;
end;
end;
function THelpFile.FindTopicByIndexContains(const SearchText: string): TTopic;
var
i: longint;
tmpLabel: String;
begin
result := nil;
GetIndex; // make sure it's read
for i := 0 to _Index.Count - 1 do
begin
tmpLabel := _Index.GetLabels[i];
if Pos(UpperCase(SearchText), UpperCase(tmpLabel)) > 0 then
begin
// found
result := Index.getTopic(i);
exit;
end;
end;
end;
function THelpFile.FindTopicByTitleStartsWith( const SearchText: string ): TTopic;
var
i: longint;
tmpTopic: TTopic;
tmpLevel : integer;
tmpMore : boolean;
begin
result := nil;
tmpLevel := 0;
repeat
tmpMore := false;
inc(tmpLevel);
for i := 0 to _Topics.Count - 1 do
begin
tmpTopic := TTopic(_Topics[i]);
if tmpLevel = tmpTopic.ContentsLevel then
begin
if StrStartsWithIgnoringCase(tmpTopic.Title, SearchText) then
begin
result := tmpTopic;
exit;
end;
end;
if tmpLevel < tmpTopic.ContentsLevel then
begin
tmpMore := True;
end;
end;
until NOT tmpMore;
end;
function THelpFile.FindTopicByTitleContains( const SearchText: string ): TTopic;
var
i: longint;
tmpTopic: TTopic;
tmpLevel : integer;
tmpMore : boolean;
begin
result := nil;
tmpLevel := 0;
repeat
tmpMore := false;
inc(tmpLevel);
for i := 0 to _Topics.Count - 1 do
begin
tmpTopic := TTopic(_Topics[i]);
if tmpLevel = tmpTopic.ContentsLevel then
begin
if CaseInsensitivePos( SearchText, tmpTopic.Title) > 0 then
begin
result := tmpTopic;
exit;
end;
end;
if tmpLevel < tmpTopic.ContentsLevel then
begin
tmpMore := True;
end;
end;
until NOT tmpMore;
end;
procedure THelpFile.FindResourceIDsForTopic( Topic: TTopic;
ResourceIDs: TList );
var
i: longint;
pResourceIDs: UInt16ArrayPointer;
pTopicIndices: UInt16ArrayPointer;
begin
ResourceIDs.Clear;
if _pHeader^.nres = 0 then
// since nres is unsigned
exit;
if _pResourceData = nil then
ReadFileBlock( _pResourceData,
_pHeader^.resstart,
_pHeader^.nres * sizeof( uint16 ) * 2 ); // list of IDs, list of topics
pResourceIDs := _pResourceData;
pTopicIndices := _pResourceData
+ _pHeader^.nres * sizeof( uint16 );
for i := 0 to _pHeader^.nres - 1 do
begin
if pTopicIndices^[ i ] = Topic.Index then
begin
// found
ResourceIDs.Add( pointer( pResourceIDs^[ i ] ) );
end;
end;
end;
procedure THelpFile.ReadReferencedFilesTable;
var
i: longint;
p: pointer;
pData: pointer;
DatabaseName: string;
pLength: pByte;
begin
if _pExtendedHeader = nil then
// no extended header -> no referenced files table
exit;
if _pExtendedHeader^.Numdatabase = 0 then
exit;
pData := nil; // please allocate...
ReadFileBlock( pData,
_pExtendedHeader^.DatabaseOffset,
_pExtendedHeader^.DatabaseSize );
p := pData;
for i := 0 to _pExtendedHeader^.Numdatabase - 1 do
begin
pLength := p; // length byte, including itself
SetString(DatabaseName, p+1, pLength^-1); // use length value minus the length byte to get the string length
_ReferencedFiles.Add( DatabaseName );
inc( p, pLength^ ); // skip to next entry using full length (including length byte)
end;
FreeMem( pData );
end;
procedure THelpFile.ReadFontTableData;
begin
if _pExtendedHeader = nil then
// no extended header -> no font table
exit;
if _pExtendedHeader^.NumFontEntry = 0 then
exit;
ReadFileBlock( _pFontTableData,
_pExtendedHeader^.FontTableOffset,
_pExtendedHeader^.NumFontEntry * sizeof( THelpFontSpec ) );
end;
procedure THelpFile.ParseFontTable;
var
i: longint;
p: pointer;
pFontSpec: pTHelpFontSpec;
begin
_FontTable.Clear;
p := _pFontTableData;
if p = nil then
exit; // no data
for i := 0 to _pExtendedHeader^.NumFontEntry - 1 do
begin
pFontSpec := p + i * sizeof( THelpFontSpec );
_FontTable.Add( pFontSpec );
end;
end;
procedure THelpFile.GetImages( ImageOffsets: TList; Images: TfpgImageList );
var
ListIndex: longint;
ImageOffset: longint;
Bitmap: THelpBitmap;
begin
Images.Clear;
for ListIndex := 0 to ImageOffsets.Count - 1 do
begin
ImageOffset := longint( ImageOffsets[ ListIndex ] );
try
Bitmap := THelpBitmap.CreateFromHelpFile( _Handle,
_pHeader^.imgstart
+ ImageOffset );
except
on e: EHelpBitmapException do
{ raise EHelpFileException.Create( 'Error loading help bitmap at'
+ IntToStr( ImageOffset )
+ ': '
+ e.Message );}
begin
// Bitmap := THelpBitmap.Create;
Bitmap := THelpBitmap(fpgImages.GetImage('stdimg.dlg.critical'));
// Bitmap.LoadFromResourceName( 'MissingBitmap' ); // TODO: Add image resource to DocView
end;
end;
Images.AddImage(Bitmap);
end;
end;
function THelpFile.GetImage( ImageOffset: longint ): THelpBitmap;
begin
try
Result := THelpBitmap.CreateFromHelpFile( _Handle,
_pHeader^.imgstart
+ ImageOffset );
except
on e: EHelpBitmapException do
begin
result := nil;
raise EHelpFileException.Create( 'Error loading help bitmap at'
+ IntToStr( ImageOffset )
+ ': '
+ e.Message );
end;
end;
end;
function THelpFile.GetTopic( Index: longint ): TTopic;
begin
if ( Index < 0 )
or ( Index > _Topics.Count - 1 ) then
Result := nil
else
Result := TTopic(_Topics[ Index ]);
end;
function THelpFile.GetTopicCount: longint;
begin
Result := _Topics.Count;
end;
function THelpFile.IndexOfTopic( Topic: TTopic ): longint;
begin
Result := _Topics.IndexOf( Topic );
end;
function THelpFile.GetDictionaryCount: longint;
begin
Result := _Dictionary.Count;
end;
function THelpFile.GetDictionaryWord( Index: longint ): string;
begin
Result := _Dictionary[ Index ];
end;
// Looks for fonts that should be substitued to the
// users selected fixed font
// doesn't make a lot of sense for this to be here...
procedure THelpFile.SetupFontSubstitutes( Substitutions: string );
var
Item: string;
FontName: string;
SpacePos: longint;
W: longint;
H: longint;
i: longint;
pFontSpec: pTHelpFontSpec;
tmpSubstitutionItems : TStrings;
tmpCounter : integer;
tmpDimensionParts : TStrings;
s: string;
PointSize: word;
cp: integer;
begin
ParseFontTable; // (re)load table from raw data
tmpSubstitutionItems := TStringList.Create;
StrExtractStrings(tmpSubstitutionItems, Substitutions, [';'], #0);
for tmpCounter := 0 to tmpSubstitutionItems.Count - 1 do
begin
Item := tmpSubstitutionItems[tmpCounter];
try
if Item <> '' then
begin
// Look for space in xxxx WxH
SpacePos := LastDelimiter(' ', Item);
if SpacePos > 0 then
begin
// fontname comes before
FontName := StrLeft( Item, SpacePos - 1 );
Delete( Item, 1, SpacePos );
// width and height after, with an X between
tmpDimensionParts := TStringList.Create;
StrExtractStrings(tmpDimensionParts, Item, ['x'], #0);
W := StrToInt(tmpDimensionParts[0]);
H := StrToInt(tmpDimensionParts[1]);
tmpDimensionParts.Destroy;
if ( W > 0 ) and ( H > 0 ) then
begin
// Now look through the font table for matches
for i := 0 to _FontTable.Count - 1 do
begin
pFontSpec := _FontTable[ i ];
cp := pFontSpec^.Codepage;
s := StrNPas( pFontSpec^.FaceName, sizeof( pFontSpec^.FaceName ) );
if s = FontName then
begin
// same face name...
// this formula seems to give a simulated pointsize compared to
// what the original VIEW program intended.
PointSize := (pFontSpec^.Height * 2) div 3;
if ( H = PointSize ) then
begin
// match
pFontSpec^.Codepage := High(word); // font substitute marker added
// _FontTable[ i ] := SubstituteFixedFont;
end;
end;
end;
end;
end;
end;
except
end;
end;
tmpSubstitutionItems.Free;
end;
// -------------------------------------------------------------
// Get the title only from specific help file (if possible)
function GetHelpFileTitle( const Filename: string ): string;
var
Header: THelpFileHeader;
fstream: TFileStream;
Ext: string;
begin
Ext := fpgExtractFileExt( Filename );
Result := '';
if SameText( Ext, '.inf' )
or SameText( Ext, '.hlp' ) then
begin
try
try
fstream := TFileStream.Create(Filename, fmOpenRead);
fstream.Position := 0;
FillChar( Header, sizeof( Header ), 0 );
fstream.Read(Header, SizeOf(Header));
if Header.ID = INF_HEADER_ID then
Result := StrPas(Header.title);
except
// silently ignore errors - it's not to critical at this point.
end;
finally
fstream.Free;
end;
end;
end;
end.
|