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
|
{
This file is part of the Free Pascal Integrated Development Environment
Copyright (c) 1998 by Berczi Gabor
Routines to create .tph 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.
**********************************************************************}
unit WTPHWriter;
interface
uses
Objects, WoaHelp, WHelp;
const
HelpStamp = 'TURBO PASCAL HelpFile.';
DefFormatVersion = $34;
type
PHelpFileWriter = ^THelpFileWriter;
THelpFileWriter = object(TOAHelpFile)
constructor Init(AFileName: string; AID: word);
function CreateTopic(HelpCtx: THelpCtx): PTopic; virtual;
procedure AddTopicToIndex(IndexTag: string; P: PTopic); virtual;
procedure AddLineToTopic(P: PTopic; Line: string); virtual;
procedure AddLinkToTopic(P: PTopic; AHelpCtx: THelpCtx);
procedure AddIndexEntry(Tag: string; P: PTopic); virtual;
function WriteFile: boolean; virtual;
destructor Done; virtual;
private
procedure CompleteContextNo;
procedure CalcTopicOfs;
procedure WriteHeader(var S: TStream);
procedure WriteCompressionRecord(var S: TStream);
procedure WriteContextTable(var S: TStream);
procedure WriteIndexTable(var S: TStream);
procedure WriteTopic(var S: TStream; T: PTopic);
procedure WriteRecord(var S: TStream; RecType: byte; var Buf; Size: word);
end;
implementation
constructor THelpFileWriter.Init(AFileName: string; AID: word);
var OK: boolean;
begin
THelpFile.Init(AID);
New(F, Init(AFileName, stCreate, HelpStreamBufSize));
OK:=F<>nil;
if OK then OK:=(F^.Status=stOK);
if OK=false then Fail;
end;
function THelpFileWriter.CreateTopic(HelpCtx: THelpCtx): PTopic;
var P: PTopic;
begin
if (HelpCtx<>0) and (SearchTopic(HelpCtx)<>nil) then
P:=nil
else
begin
P:=NewTopic(ID,HelpCtx,0,'');
Topics^.Insert(P);
end;
CreateTopic:=P;
end;
procedure THelpFileWriter.AddTopicToIndex(IndexTag: string; P: PTopic);
begin
IndexEntries^.Insert(NewIndexEntry(IndexTag,P^.FileID,P^.HelpCtx));
end;
procedure THelpFileWriter.AddLineToTopic(P: PTopic; Line: string);
var OldText: pointer;
OldSize: word;
begin
if P=nil then Exit;
OldText:=P^.Text; OldSize:=P^.TextSize;
Inc(P^.TextSize,length(Line)+1);
GetMem(P^.Text,P^.TextSize);
if OldText<>nil then Move(OldText^,P^.Text^,OldSize);
Move(Line[1],P^.Text^[OldSize],length(Line));
P^.Text^[OldSize+length(Line)]:=0;
if OldText<>nil then FreeMem(OldText,OldSize);
end;
procedure THelpFileWriter.AddLinkToTopic(P: PTopic; AHelpCtx: THelpCtx);
var OldEntries: pointer;
OldCount : word;
OldSize : word;
begin
if P=nil then Exit;
OldEntries:=P^.Links; OldCount:=P^.LinkCount; OldSize:=P^.LinkSize;
Inc(P^.LinkCount);
GetMem(P^.Links,P^.LinkSize);
if OldEntries<>nil then Move(OldEntries^,P^.Links^,OldSize);
with P^.Links^[P^.LinkCount-1] do
begin
FileID:=ID;
Context:=AHelpCtx;
end;
if OldEntries<>nil then FreeMem(OldEntries,OldSize);
end;
procedure THelpFileWriter.AddIndexEntry(Tag: string; P: PTopic);
begin
if P=nil then Exit;
IndexEntries^.Insert(NewIndexEntry(Tag,P^.FileID,P^.HelpCtx));
end;
function THelpFileWriter.WriteFile: boolean;
var I: sw_integer;
CtxStart: longint;
begin
CompleteContextNo;
CalcTopicOfs;
WriteHeader(F^);
WriteCompressionRecord(F^);
CtxStart:=F^.GetPos;
WriteContextTable(F^);
WriteIndexTable(F^);
for I:=0 to Topics^.Count-1 do
begin
WriteTopic(F^,Topics^.At(I));
end;
F^.Seek(CtxStart);
WriteContextTable(F^);
end;
procedure THelpFileWriter.WriteHeader(var S: TStream);
var St: string;
begin
Version.FormatVersion:=DefFormatVersion;
St:=HelpStamp+#0#$1a;
F^.Write(St[1],length(St));
St:=Signature;
F^.Write(St[1],length(St));
F^.Write(Version,SizeOf(Version));
WriteRecord(F^,rtFileHeader,Header,SizeOf(Header));
end;
procedure THelpFileWriter.WriteCompressionRecord(var S: TStream);
var CR: THLPCompression;
begin
FillChar(CR,SizeOf(CR),0);
WriteRecord(F^,rtCompression,CR,SizeOf(CR));
end;
procedure THelpFileWriter.WriteIndexTable(var S: TStream);
const BufSize = 65000;
var P: ^THLPIndexTable;
TableSize: word;
procedure AddByte(B: byte);
begin
PByteArray(@P^.Entries)^[TableSize]:=B;
Inc(TableSize);
end;
procedure AddEntry(Tag: string; HelpCtx: word);
var Len,I: byte;
begin
Len:=length(Tag); if Len>31 then Len:=31;
AddByte(Len);
for I:=1 to Len do
AddByte(ord(Tag[I]));
AddByte(Lo(HelpCtx)); AddByte(Hi(HelpCtx));
end;
var I: sw_integer;
begin
if IndexEntries^.Count=0 then Exit;
GetMem(P,BufSize);
TableSize:=0;
P^.IndexCount:=IndexEntries^.Count;
for I:=0 to IndexEntries^.Count-1 do
with IndexEntries^.At(I)^ do
AddEntry(Tag^,HelpCtx);
Inc(TableSize,SizeOf(P^.IndexCount));
WriteRecord(F^,rtIndex,P^,TableSize);
FreeMem(P,BufSize);
end;
procedure THelpFileWriter.WriteContextTable(var S: TStream);
var Ctxs: ^THLPContexts;
CtxSize,I: word;
T: PTopic;
MaxCtx: longint;
begin
if Topics^.Count=0 then MaxCtx:=1 else
MaxCtx:=Topics^.At(Topics^.Count-1)^.HelpCtx;
CtxSize:=SizeOf(Ctxs^.ContextCount)+SizeOf(Ctxs^.Contexts[0])*(MaxCtx+1);
GetMem(Ctxs,CtxSize); FillChar(Ctxs^,CtxSize,0);
Ctxs^.ContextCount:=MaxCtx+1;
for I:=1 to Topics^.Count do
begin
T:=Topics^.At(I-1);
with Ctxs^.Contexts[T^.HelpCtx] do
begin
LoW:=(T^.FileOfs and $ffff);
HiB:=(T^.FileOfs shr 16) and $ff;
end;
end;
WriteRecord(F^,rtContext,Ctxs^,CtxSize);
FreeMem(Ctxs,CtxSize);
end;
procedure THelpFileWriter.WriteTopic(var S: TStream; T: PTopic);
var TextBuf: PByteArray;
TextSize: word;
KWBuf: ^THLPKeywordRecord;
I,KWBufSize: word;
begin
T^.FileOfs:=S.GetPos;
TextBuf:=T^.Text; TextSize:=T^.TextSize;
WriteRecord(F^,rtText,TextBuf^,TextSize);
{ write keyword record here }
KWBufSize:=SizeOf(KWBuf^)+SizeOf(KWBuf^.Keywords[0])*T^.LinkCount;
GetMem(KWBuf,KWBufSize); FillChar(KWBuf^,KWBufSize,0);
KWBuf^.KeywordCount:=T^.LinkCount;
for I:=0 to T^.LinkCount-1 do
KWBuf^.Keywords[I].kwContext:=T^.Links^[I].Context;
WriteRecord(F^,rtKeyword,KWBuf^,KWBufSize);
FreeMem(KWBuf,KWBufSize);
end;
procedure THelpFileWriter.CompleteContextNo;
var P: PTopic;
NextTopicID: THelpCtx;
function SearchNextFreeTopicID: THelpCtx;
begin
while Topics^.SearchTopic(NextTopicID)<>nil do
Inc(NextTopicID);
SearchNextFreeTopicID:=NextTopicID;
end;
begin
NextTopicID:=1;
repeat
P:=Topics^.SearchTopic(0);
if P<>nil then
begin
Topics^.Delete(P);
P^.HelpCtx:=SearchNextFreeTopicID;
Topics^.Insert(P);
end;
until P=nil;
end;
procedure THelpFileWriter.CalcTopicOfs;
begin
end;
procedure THelpFileWriter.WriteRecord(var S: TStream; RecType: byte; var Buf; Size: word);
var RH: THLPRecordHeader;
begin
RH.RecType:=RecType; RH.RecLength:=Size;
S.Write(RH,SizeOf(RH));
S.Write(Buf,Size);
end;
destructor THelpFileWriter.Done;
begin
inherited Done;
end;
END.
|