wvphelp.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. {
  2. This file is part of the Free Pascal Integrated Development Environment
  3. Copyright (c) 2000 by Berczi Gabor
  4. Help support for (.VPH) help files
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$R-}
  12. unit WVPHelp;
  13. interface
  14. uses Objects,
  15. WUtils,WHelp;
  16. const
  17. VPHFileSignature = 'HS';
  18. type
  19. TVPHFileHeader = packed record
  20. SectionCount: byte; { #1 }
  21. TotalTopics : word;
  22. end;
  23. TVPHTopicEntry = packed record
  24. TopicOfs : word;
  25. Dunno : byte;
  26. end;
  27. PVPHTopicTable = ^TVPHTopicTable;
  28. TVPHTopicTable = packed array[0..(MaxBytes div sizeof(TVPHTopicEntry))-1] of TVPHTopicEntry;
  29. PVPHSectionTable = ^TVPHSectionTable;
  30. TVPHSectionTable = packed array[0..4095] of longint;
  31. PVPHHelpFile = ^TVPHHelpFile;
  32. TVPHHelpFile = object(THelpFile)
  33. constructor Init(AFileName: string; AID: word);
  34. destructor Done; virtual;
  35. public
  36. function LoadIndex: boolean; virtual;
  37. function ReadTopic(T: PTopic): boolean; virtual;
  38. private
  39. F: PStream;
  40. Header: TVPHFileHeader;
  41. TopicTable: PVPHTopicTable;
  42. TopicTableSize: longint;
  43. SectionTable: PVPHSectionTable;
  44. SectionTableSize: longint;
  45. TopicBaseOfs: longint;
  46. function ReadHeader: boolean;
  47. function ReadTopicTable: boolean;
  48. function ReadBlock(Data: pointer; DataSize: longint): boolean;
  49. end;
  50. TVPHGetAttrColorProc = function(TextStyle, TextColor: byte; var Color: byte): boolean;
  51. function DefVPHGetAttrColor(TextStyle, TextColor: byte; var Color: byte): boolean;
  52. const VPHGetAttrColor : TVPHGetAttrColorProc = {$ifdef fpc}@{$endif}DefVPHGetAttrColor;
  53. procedure RegisterHelpType;
  54. implementation
  55. function DefVPHGetAttrColor(TextStyle, TextColor: byte; var Color: byte): boolean;
  56. begin
  57. DefVPHGetAttrColor:=false;
  58. end;
  59. constructor TVPHHelpFile.Init(AFileName: string; AID: word);
  60. var OK: boolean;
  61. begin
  62. if inherited Init(AID)=false then Fail;
  63. F:=New(PFastBufStream, Init(AFileName, stOpenRead, HelpStreamBufSize));
  64. OK:=F<>nil;
  65. if OK then OK:=(F^.Status=stOK);
  66. if OK then
  67. begin
  68. OK:=ReadHeader;
  69. if OK then
  70. begin
  71. SectionTableSize:=sizeof(SectionTable^[0])*Header.SectionCount;
  72. GetMem(SectionTable,SectionTableSize);
  73. F^.Read(SectionTable^,SectionTableSize);
  74. OK:=(F^.Status=stOK);
  75. end;
  76. if OK then
  77. OK:=ReadBlock(nil,2);
  78. if OK then
  79. begin
  80. TopicTableSize:=sizeof(TopicTable^[0])*Header.TotalTopics;
  81. GetMem(TopicTable,TopicTableSize);
  82. OK:=ReadTopicTable;
  83. end;
  84. end;
  85. if OK=false then
  86. begin
  87. Done;
  88. Fail;
  89. end;
  90. end;
  91. function TVPHHelpFile.ReadHeader: boolean;
  92. var OK: boolean;
  93. begin
  94. F^.Read(Header,sizeof(Header));
  95. OK:=(F^.Status=stOK);
  96. ReadHeader:=OK;
  97. end;
  98. function TVPHHelpFile.LoadIndex: boolean;
  99. var OK: boolean;
  100. begin
  101. OK:=false;
  102. LoadIndex:=OK;
  103. end;
  104. function TVPHHelpFile.ReadBlock(Data: pointer; DataSize: longint): boolean;
  105. var OK: boolean;
  106. C: char;
  107. begin
  108. F^.Read(C,sizeof(C));
  109. OK:=(F^.Status=stOK) and (C='þ');
  110. if OK then
  111. begin
  112. if Assigned(Data) then
  113. F^.Read(Data^,DataSize)
  114. else
  115. F^.Seek(F^.GetPos+DataSize);
  116. OK:=(F^.Status=stOK);
  117. end;
  118. ReadBlock:=OK;
  119. end;
  120. function TVPHHelpFile.ReadTopicTable: boolean;
  121. var OK: boolean;
  122. begin
  123. OK:=ReadBlock(TopicTable,TopicTableSize);
  124. TopicBaseOfs:=F^.GetPos;
  125. ReadTopicTable:=OK;
  126. end;
  127. function TVPHHelpFile.ReadTopic(T: PTopic): boolean;
  128. var OK: boolean;
  129. begin
  130. OK:=false;
  131. ReadTopic:=OK;
  132. end;
  133. destructor TVPHHelpFile.Done;
  134. begin
  135. if Assigned(TopicTable) and (TopicTableSize>0) then
  136. FreeMem(TopicTable{$ifndef FP},TopicTableSize{$endif});
  137. TopicTable:=nil;
  138. if Assigned(SectionTable) and (SectionTableSize>0) then
  139. FreeMem(SectionTable{$ifndef FP},SectionTableSize{$endif});
  140. SectionTable:=nil;
  141. if Assigned(F) then Dispose(F, Done); F:=nil;
  142. inherited Done;
  143. end;
  144. function CreateProc(const FileName,Param: string;Index : longint): PHelpFile;
  145. begin
  146. CreateProc:=New(PVPHHelpFile, Init(FileName,Index));
  147. end;
  148. procedure RegisterHelpType;
  149. begin
  150. RegisterHelpFileType({$ifdef FPC}@{$endif}CreateProc);
  151. end;
  152. END.