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
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2003 by the Free Pascal development team
Windows message compiler unit.
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.
**********************************************************************}
{$ifdef fpc}
{$mode objfpc}
{$h+}
{$endif}
unit msgcomp;
interface
Uses Classes;
Type
TMessagehandler = Procedure(Sender : Tobject; Msg : String) Of Object;
TMessageCompiler = Class
Private
FErrors : Integer;
FEscapeNeeded : Boolean;
FOnVerbose,
FOnError : TMessageHandler;
FCompiling : Boolean;
FMC: TStream;
FPas: TStream;
FRC: TStream;
FMsg: TStream;
FLocaleID: Integer;
FSubLocaleID: Integer;
FMsgFileName: String;
FUnitName: String;
Procedure SWriteLn(S : String; Stream : TStream);
Procedure CompileError(EMsg : String;Line : Integer; Value : String);
Procedure ProcessMessages(Lines : TStrings; MsgList : Tlist);
Procedure WriteMsgFile(MsgList : TList; Stream : TStream);
Procedure WriteRCFile(MsgList : TList; Stream : TStream);
Procedure WritePasFile(MsgList : TList; Stream : TStream);
Procedure ClearList(MsgList : Tlist);
procedure SetStream(const Index: Integer; const Value: TStream);
Function GetStream(const Index: Integer) : TStream;
Procedure Verbose(Fmt : String;Args : Array of const);
Public
Constructor Create;
Function Compile : Boolean;
Property MC : TStream index 1 Read GetStream Write SetStream;
Property Msg : TStream index 2 Read GetStream Write SetStream;
Property Pas : TStream index 3 Read GetStream Write SetStream;
Property RC : TStream Index 4 Read GetStream Write SetStream;
Property LocaleID : Integer Read FLocaleID Write FlocaleID;
Property SubLocaleID : Integer Read FSubLocaleID Write FSublocaleID;
Property MessageFileName : String Read FMsgFileName Write FMsgFileName;
Property UnitName : String Read FUnitName Write FUnitName;
Property EscapeNeeded : Boolean Read FEscapeNeeded Write FEscapeNeeded;
Property OnVerbose : TMessageHandler Read FOnVerbose Write FOnVerbose;
Property OnError : TMessageHandler Read FOnError Write FOnError;
Property Errors : Integer Read FErrors;
End;
TMessageEntry = Class(TObject)
MessageID : Cardinal;
TotalMessageOfs : Cardinal;
MessageAlias,
Language,
MessageText : String;
Function OffsetToNext : Cardinal;
End;
implementation
Uses SysUtils ;
Const
SC = SizeOF(Cardinal);
Resourcestring
SErrSetStreamNotAllowed = 'Setting stream during compile is not allowed.';
SUnknownLine = 'Unknown error: "%s"';
SNoMessage = 'Message starts without MessageID : "%s"';
SErrUnknownDirective = 'Unknown directive "%s"';
SErrInLine = 'Error: line %d: %s';
SFoundMessageID = 'Found messageID : %s';
SStartCompiling = 'Start compiling: %d lines.';
SErrUnexpected = 'Unexpected error : %s';
SWritingMessageFile = 'Writing %d entries to message file.';
SWritingPasFile = 'Writing to unit "%s"';
SWrotePasFile = 'Wrote %d constants to unit "%s"';
SWritingRCFile = 'Writing rc file.';
SErrNoNumber = 'Not a valid integer: %s';
procedure TMessageCompiler.ClearList(MsgList: Tlist);
Var
I : Integer;
begin
For I:=0 to MsgList.Count-1 do
TMessageEntry(MsgList[I]).Free;
MsgList.Clear;
end;
Function TMessageCompiler.Compile : Boolean;
Var
Lines : TStrings;
MsgList : TList;
Begin
FErrors:=0;
MsgList:=TList.Create;
try
Lines:=TStringList.Create;
Try
Lines.LoadFromStream(MC);
ProcessMessages(Lines,MSgList);
If (FErrors=0) then
begin
If Assigned(Msg) then
WriteMsgFile(MsgList,Msg);
if Assigned(Pas) then
WritePasFile(MsgList,Pas);
if Assigned(RC) then
WriteRCFile(MsgList,RC);
end;
Finally
Lines.Free;
end;
Finally
ClearList(MsgList);
end;
Result:=(FErrors=0);
End;
Procedure TMessageCompiler.Verbose(Fmt : String;Args : Array of const);
begin
if Assigned(FOnverbose) then
FOnVerBose(Self,Format(Fmt,Args));
end;
Function HexToInt(Hex : String) : Integer;
Const HexSymbols : String = '0123456789ABCDEF';
Var I,J : Integer;
Begin
Hex := UpperCase(Hex);
Result := 0;
J := Length(Hex);
For I := 1 to J do
Result := Result+((Pos(Hex[J-I+1],HexSymbols)-1) shl ((I-1)*4));
End;
Constructor TMessageCompiler.Create;
begin
// English
LocaleID:=9;
SubLocaleID:=1;
end;
Procedure TMessageCompiler.CompileError(EMsg : String;Line : Integer; Value : String);
begin
Inc(FErrors);
EMsg:=Format(EMsg,[Value]);
If Assigned(FOnError) then
FOnError(Self,Format(SErrInLine,[Line,EMsg]));
end;
Procedure TMessageCompiler.ProcessMessages(Lines : TStrings; MsgList : TList);
Var
Line : Integer;
Me : TMessageEntry;
Function Pad(S : String; Len : Integer) : String;
Var I : Integer;
Begin
For I := Length(S) to Len-1 do S := S+' ';
Result := S;
End;
Function SkipLine(Var S : String) : Boolean;
Var
I : Integer;
begin
I:=Pos(';',S);
If (I<>0) then
S:=Copy(S,1,I-1);
Result:=Length(S)=0;
end;
Procedure DoDirective(S : String; I : Integer);
Var
MsgID : Integer;
T : String;
begin
T := UpperCase(Copy(S,1,I-1));
Delete(S,1,I);
S:=Trim(S);
If (T='MESSAGEID') Then
begin
Verbose(SFoundMessageID,[S]);
S:=Uppercase(S);
If Pos('0X',S)<>0 then
begin
Delete(S,1,2);
MsgId:=HexToInt(S);
end
else
MsgID:=StrToIntDef(S,-1);
If (MsgID=-1) then
CompileError(Format(SErrNoNumber,[S]),Line,T)
else
begin
Me:=TMessageENtry.Create;
Me.MessageID:=MsgID;
end;
End
Else If (T = 'SYMBOLICNAME') Then
Begin
If Assigned(me) then
Me.MessageAlias:=S;
End
Else If (T = 'LANGUAGE') Then
begin
If assigned(ME) then
Me.Language:=S;
end
else
CompileError(SErrUnknownDirective,Line,T);
End;
Var
I,Count : Integer;
S : String;
Begin
Count := Lines.Count-1;
Verbose(SStartCOmpiling,[Count]);
Line:=0;
Me:=Nil;
While Line<=Count do
Begin
Try
S:=Lines[Line];
If Not SkipLine(S) then
begin
I:=Pos('=',S);
If (I<>0) then
DoDirective(S,I)
else
If (Me=Nil) Then
CompileError(SNoMessage,Line,S)
else
begin
// Message starts.
While (S<>'.') do
begin
If Length(Me.MessageText)>0 then
Me.MessageText:=Me.MessageText+#13#10+S
else
Me.MessageText:=S;
Inc(Line);
If Line<=Count then
S:=Lines[Line]
end;
MsgList.Add(Me);
Me:=Nil;
end;
End;
Except
On E : Exception do
CompileError(SErrUnexpected,Line,E.Message);
End;
Inc(Line);
end;
End;
procedure TMessageCompiler.SetStream(const Index: Integer;
const Value: TStream);
begin
If FCompiling then
Raise Exception.Create(SErrSetStreamNotAllowed);
Case index of
1 : FMC := Value;
2 : FMsg := Value;
3 : FPas := Value;
4 : FRC := Value;
end;
end;
Function TMessageCompiler.GetStream(const Index: Integer) : TStream;
begin
Case index of
1 : Result:=FMC;
2 : Result:=FMsg;
3 : Result:=FPas;
4 : Result:=FRC;
end;
end;
procedure TMessageCompiler.SWriteLn(S: String; Stream: TStream);
begin
S:=S+#13#10;
Stream.Write(S[1],Length(S));
end;
Procedure TMessageCompiler.WriteMSGFile(MsgList : Tlist; Stream : TStream);
Var
I,Count : Integer;
HeaderEntry,NullEntry: Array[1..3] of cardinal;
O,BO : Cardinal;
M : TMessageEntry;
S : String;
Begin
Verbose(SWritingMessageFile,[MsgList.Count]);
NullEntry[1]:=0;
NullEntry[2]:=0;
NullEntry[3]:=0;
Count:=MsgList.Count;
Stream.Write(Count,SC);
BO:=((SC*3)*Count)+SC; // Header size...
// Loop 1: Header entries.
For I:=0 to Count-1 do
begin
M:=TMessageEntry(MsgList[I]);
HeaderEntry[1]:=M.MessageID;
HeaderEntry[2]:=M.MessageID;
HeaderEntry[3]:=BO;
BO:=BO+M.OffsetToNext;
Stream.Write(HeaderEntry,SizeOf(HeaderEntry));
end;
For I:=0 to Count-1 do
begin
M:=TMessageEntry(MsgList[I]);
O:=M.OffsetToNext;
Stream.Write(O,SizeOf(O));
Dec(O,SC);
S:=M.MessageText;
Stream.Write(S[1],Length(S));
If (Length(S)<O) then
Stream.Write(NullEntry,O-Length(S));
end;
End;
procedure TMessageCompiler.WritePasFile(MsgList: TList; Stream: TStream);
Var
I,Count : Integer;
ME : TMessageEntry;
begin
Verbose(SWritingPasFile,[UnitName]);
SWriteln(Format('Unit %s;',[UnitName]),Stream);
SWriteln('',Stream);
SWriteln('Interface',Stream);
SWriteln('',Stream);
SWriteln('Const',Stream);
Count:=0;
For I:=0 to MsgList.Count-1 do
begin
Me:=TMessageEntry(MsgList[I]);
With Me do
If (MessageAlias<>'') then
begin
Swriteln(Format(' %s = %d; ',[MessageAlias,MessageID]),Stream);
Inc(Count);
end;
end;
SWriteln('',Stream);
SWriteln('Implementation',Stream);
SWriteln('',Stream);
SWriteln('end.',Stream);
Verbose(SWrotePasFile,[Count,UnitName]);
end;
procedure TMessageCompiler.WriteRCFile(MsgList: TList; Stream: TStream);
Const
LangLine = 'LANGUAGE 0x%s,0x%s';
FileLine = '1 11 "%s"';
Var
S : String;
begin
Verbose(SWritingRCFile,[]);
S:=Format(LangLine,[IntToHex(LocaleID,1),IntToHex(SubLocaleID,1)]);
SWriteLn(S,Stream);
S:=MessageFileName;
If EscapeNeeded Then
S:=StringReplace(S,'\','\\',[rfReplaceAll]);
SWriteLn(Format(FileLine,[S]),Stream);
end;
{ TMessageEntry }
function TMessageEntry.OffsetToNext: Cardinal;
begin
Result:=((Length(MessageText) div SC) +2) * SC;
end;
end.
|