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
|
{
$Id: msg2inc.pp,v 1.15 2005/02/14 17:13:10 peter Exp $
This program is part of the Free Pascal run time library.
Copyright (c) 1998-2002 by Peter Vreman
Convert a .msg file to an .inc file with a const array of char
And for the lazy docwriters it can also generate some TeX output
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.
**********************************************************************}
program msg2inc;
uses
strings;
{$ifdef unix}
{$define EOL_ONE_CHAR}
{$endif unix}
{$ifdef amiga}
{$define EOL_ONE_CHAR}
{$endif amiga}
{$ifdef morphos}
{$define EOL_ONE_CHAR}
{$endif}
{$ifdef macos}
{$define EOL_ONE_CHAR}
{$endif}
const
version='1.00';
{$ifdef EOL_ONE_CHAR}
eollen=1;
{$else}
eollen=2;
{$endif}
msgparts = 20;
type
TMode=(M_Char,M_Tex,M_Intel,M_String,M_Renumber);
var
InFile,
OutFile,
OutName : string;
Mode : TMode;
TexHeader : boolean;
MsgTxt : pchar;
EnumTxt : pchar;
enumsize,
msgsize : longint;
msgidxmax : array[1..msgparts] of longint;
msgs : array[0..msgparts,0..999] of boolean;
procedure LoadMsgFile(const fn:string);
var
f : text;
error,
multiline : boolean;
code : word;
numpart,numidx,
line,i,j,num : longint;
ptxt,
penum : pchar;
number,
s,s1 : string;
procedure err(const msgstr:string);
begin
writeln('error in line ',line,': ',msgstr);
error:=true;
end;
begin
Writeln('Loading messagefile ',fn);
{Read the message file}
assign(f,fn);
{$I-}
reset(f);
{$I+}
if ioresult<>0 then
begin
WriteLn('fatal error: '+fn+' not found');
halt(1);
end;
{ First parse the file and count bytes needed }
fillchar(msgidxmax,sizeof(msgidxmax),0);
fillchar(msgs,sizeof(msgs),0);
error:=false;
line:=0;
multiline:=false;
msgsize:=0;
while not eof(f) do
begin
readln(f,s);
inc(line);
if multiline then
begin
if s=']' then
multiline:=false
else
inc(msgsize,length(s)+1); { +1 for linebreak }
end
else
begin
if (s<>'') and not(s[1] in ['#',';','%']) then
begin
i:=pos('=',s);
if i>0 then
begin
j:=i+1;
if not(s[j] in ['0'..'9']) then
err('no number found')
else
begin
while (s[j] in ['0'..'9']) do
inc(j);
end;
if j-i-1<>5 then
err('number length is not 5');
number:=Copy(s,i+1,j-i-1);
{ update the max index }
val(number,num,code);
numpart:=num div 1000;
if numpart=0 then
err('number should be > 1000');
numidx:=num mod 1000;
{ duplicate ? }
if msgs[numpart,numidx] then
err('duplicate number found');
msgs[numpart,numidx]:=true;
{ check range }
if numpart > msgparts then
err('number is to large')
else
if numidx > msgidxmax[numpart] then
msgidxmax[numpart]:=numidx;
if s[j+1]='[' then
begin
inc(msgsize,j-i);
multiline:=true
end
else
inc(msgsize,length(s)-i+1);
inc(enumsize,j);
end
else
err('no = found');
end;
end;
end;
if multiline then
err('still in multiline mode');
if error then
begin
close(f);
writeln('aborting');
halt(1);
end;
{ alloc memory }
getmem(msgtxt,msgsize);
ptxt:=msgtxt;
getmem(enumtxt,enumsize);
penum:=enumtxt;
{ now read the buffer in mem }
reset(f);
while not eof(f) do
begin
readln(f,s);
if multiline then
begin
if s=']' then
begin
multiline:=false;
{ overwrite last eol }
dec(ptxt);
ptxt^:=#0;
inc(ptxt);
end
else
begin
move(s[1],ptxt^,length(s));
inc(ptxt,length(s));
ptxt^:=#10;
inc(ptxt);
end;
end
else
begin
if (s<>'') and not(s[1] in ['#',';','%']) then
begin
i:=pos('=',s);
if i>0 then
begin
j:=i+1;
while (s[j] in ['0'..'9']) do
inc(j);
{enum}
move(s[1],penum^,i-1);
inc(penum,i-1);
penum^:='=';
inc(penum);
number:=Copy(s,i+1,j-i-1);
move(number[1],penum^,length(number));
inc(penum,length(number));
penum^:=#0;
inc(penum);
{ multiline start then no txt }
if s[j+1]='[' then
begin
s1:=Copy(s,i+1,j-i);
move(s1[1],ptxt^,length(s1));
inc(ptxt,length(s1));
multiline:=true;
end
else
begin
{ txt including number }
s1:=Copy(s,i+1,255);
move(s1[1],ptxt^,length(s1));
inc(ptxt,length(s1));
ptxt^:=#0;
inc(ptxt);
end;
end;
end;
end;
end;
close(f);
end;
{*****************************************************************************
WriteEnumFile
*****************************************************************************}
procedure WriteEnumFile(const fn,typename:string);
var
t : text;
i : longint;
p : pchar;
start : boolean;
begin
writeln('Writing enumfile '+fn);
{Open textfile}
assign(t,fn);
rewrite(t);
writeln(t,'const');
{Parse buffer in msgbuf and create indexs}
p:=enumtxt;
start:=true;
for i:=1 to enumsize do
begin
if start then
begin
write(t,' ');
start:=false;
end;
if p^=#0 then
begin
writeln(t,';');
start:=true;
end
else
begin
write(t,p^);
end;
inc(p);
end;
writeln(t,'');
{ msgtxt size }
writeln(t,' MsgTxtSize = ',msgsize,';');
writeln(t,'');
{ max msg idx table }
writeln(t,' MsgIdxMax : array[1..20] of longint=(');
write(t,' ');
for i:=1 to 20 do
begin
write(t,msgidxmax[i]+1);
if i<20 then
write(t,',');
if i=10 then
begin
writeln(t,'');
write(t,' ');
end;
end;
writeln(t,'');
writeln(t,' );');
close(t);
end;
{*****************************************************************************
WriteStringFile
*****************************************************************************}
procedure WriteStringFile(const fn,constname:string);
const
maxslen=240; { to overcome aligning problems }
function l0(l:longint):string;
var
s : string[16];
begin
str(l,s);
while (length(s)<5) do
s:='0'+s;
l0:=s;
end;
var
t : text;
f : file;
slen,
len,i : longint;
p : pchar;
s : string;
start,
quote : boolean;
begin
writeln('Writing stringfile ',fn);
{Open textfile}
assign(t,fn);
rewrite(t);
writeln(t,'{$ifdef Delphi}');
writeln(t,'const '+constname+' : array[0..000000] of string[',maxslen,']=(');
writeln(t,'{$else Delphi}');
writeln(t,'const '+constname+' : array[0..000000,1..',maxslen,'] of char=(');
write(t,'{$endif Delphi}');
{Parse buffer in msgbuf and create indexs}
p:=msgtxt;
slen:=0;
len:=0;
quote:=false;
start:=true;
for i:=1 to msgsize do
begin
if slen>=maxslen then
begin
if quote then
begin
write(t,'''');
quote:=false;
end;
write(t,',');
slen:=0;
inc(len);
end;
if (len>70) or (start) then
begin
if quote then
begin
write(t,'''');
quote:=false;
end;
if slen>0 then
writeln(t,'+')
else
writeln(t);
len:=0;
start:=false;
end;
if (len=0) then
write(t,' ');
if (ord(p^)>=32) and (p^<>#39) then
begin
if not quote then
begin
write(t,'''');
quote:=true;
inc(len);
end;
write(t,p^);
inc(len);
end
else
begin
if quote then
begin
write(t,'''');
inc(len);
quote:=false;
end;
write(t,'#'+chr(ord(p^) div 100+48)+chr((ord(p^) mod 100) div 10+48)+chr(ord(p^) mod 10+48));
inc(len,3);
end;
if p^ in [#0,#10] then
start:=true;
inc(slen);
inc(p);
end;
if quote then
write(t,'''');
writeln(t,'');
writeln(t,');');
close(t);
{update arraysize}
s:=l0(msgsize div maxslen); { we start with 0 }
assign(f,fn);
reset(f,1);
seek(f,34+eollen+length(constname));
blockwrite(f,s[1],5);
seek(f,90+3*eollen+2*length(constname));
blockwrite(f,s[1],5);
close(f);
end;
{*****************************************************************************
WriteCharFile
*****************************************************************************}
procedure WriteCharFile(const fn,constname:string);
function l0(l:longint):string;
var
s : string[16];
begin
str(l,s);
while (length(s)<5) do
s:='0'+s;
l0:=s;
end;
function createconst(b:byte):string;
begin
if (b in [32..127]) and (b<>39) then
createconst:=''''+chr(b)+''''
else
createconst:='#'+chr(b div 100+48)+chr((b mod 100) div 10+48)+chr(b mod 10+48)
end;
var
t : text;
f : file;
cidx,i : longint;
p : pchar;
s : string;
begin
writeln('Writing charfile '+fn);
{Open textfile}
assign(t,fn);
rewrite(t);
writeln(t,'const ',constname,' : array[1..00000] of char=(');
{Parse buffer in msgbuf and create indexs}
p:=msgtxt;
cidx:=0;
for i:=1to msgsize do
begin
if cidx=15 then
begin
if cidx>0 then
writeln(t,',')
else
writeln(t,'');
write(t,' ');
cidx:=0;
end
else
if cidx>0 then
write(t,',')
else
write(t,' ');
write(t,createconst(ord(p^)));
inc(cidx);
inc(p);
end;
writeln(t,');');
close(t);
{update arraysize}
s:=l0(msgsize);
assign(f,fn);
reset(f,1);
seek(f,18+length(constname));
blockwrite(f,s[1],5);
close(f);
end;
{*****************************************************************************
WriteIntelFile
*****************************************************************************}
procedure WriteIntelFile(const fn,constname:string);
var
t : text;
len,i : longint;
p : pchar;
start,
quote : boolean;
begin
writeln('Writing Intelfile ',fn);
{Open textfile}
assign(t,fn);
rewrite(t);
writeln(t,'procedure '+constname+';assembler;');
writeln(t,'asm');
{Parse buffer in msgbuf and create indexs}
p:=msgtxt;
len:=0;
start:=true;
quote:=false;
for i:=1to msgsize do
begin
if len>70 then
begin
if quote then
begin
write(t,'''');
quote:=false;
end;
writeln(t,'');
start:=true;
end;
if start then
begin
write(t,' db ''');
len:=0;
quote:=true;
end;
if (ord(p^)>=32) and (p^<>#39) then
begin
if not quote then
begin
write(t,',''');
quote:=true;
inc(len);
end;
write(t,p^);
inc(len);
end
else
begin
if quote then
begin
write(t,'''');
inc(len);
quote:=false;
end;
write(t,','+chr(ord(p^) div 100+48)+chr((ord(p^) mod 100) div 10+48)+chr(ord(p^) mod 10+48));
inc(len,4);
end;
inc(p);
end;
if quote then
write(t,'''');
writeln(t,'');
writeln(t,'end;');
close(t);
end;
{*****************************************************************************
RenumberFile
*****************************************************************************}
procedure RenumberFile(const fn,name:string);
var
f,t : text;
i : longint;
s,s1 : string;
begin
Writeln('Renumbering ',fn);
{Read the message file}
assign(f,fn);
{$I-}
reset(f);
{$I+}
if ioresult<>0 then
begin
WriteLn('*** message file '+fn+' not found ***');
exit;
end;
assign(t,'msg2inc.$$$');
rewrite(t);
i:=0;
while not eof(f) do
begin
readln(f,s);
if (copy(s,1,length(Name))=Name) and (s[3] in ['0'..'9']) then
begin
inc(i);
str(i,s1);
while length(s1)<3 do
s1:='0'+s1;
writeln(t,Name+s1+Copy(s,6,255));
end
else
writeln(t,s);
end;
close(t);
close(f);
{ rename new file }
erase(f);
rename(t,fn);
end;
{*****************************************************************************
WriteTexFile
*****************************************************************************}
Function EscapeString (Const S : String) : String;
Var
I : longint;
hs : string;
begin
hs:='';
for i:=1 to length(s) do
if (S[i]='$') then
begin
if (s[i+1] in ['0'..'9']) then
hs:=hs+'arg'
else
hs:=hs+'\$';
end
else
hs:=hs+s[i];
EscapeString:=hs;
end;
procedure WriteTexFile(const infn,outfn:string);
var
t,f : text;
line,
i,k : longint;
s,s1 : string;
texoutput : boolean;
begin
Writeln('Loading messagefile ',infn);
writeln('Writing TeXfile ',outfn);
{ Open infile }
assign(f,infn);
{$I-}
reset(f);
{$I+}
if ioresult<>0 then
begin
WriteLn('*** message file '+infn+' not found ***');
exit;
end;
{ Open outfile }
assign(t,outfn);
rewrite(t);
If texheader then
begin
writeln (t,'\documentclass{article}');
writeln (t,'\usepackage{html}');
writeln (t,'\usepackage{fpc}');
writeln (t,'\begin{document}');
end;
{ Parse }
line:=0;
TexOutput:=False;
while not eof(f) do
begin
readln(f,s);
inc(line);
If Pos ('# BeginOfTeX',S)=1 then
TexOutPut:=True
else if pos ('# EndOfTeX',S)=1 then
TexOutPut:=False;
if (s<>'') and not(s[1] in ['#',';']) and TeXOutPut then
begin
if s[1]='%' then
begin
Delete(s,1,1);
writeln(t,s);
end
else
begin
i:=pos('=',s);
if i>0 then
begin
inc(i);
while s[i] in ['0'..'9'] do
inc(i);
inc(i);
s1:='';
k:=0;
while (k<5) and (s[i+k]<>'_') do
begin
case s[i+k] of
'W' : s1:='Warning: ';
'E' : s1:='Error: ';
'F' : s1:='Fatal: ';
'N' : s1:='Note: ';
'I' : s1:='Info: ';
'H' : s1:='Hint: ';
end;
inc(k);
end;
if s[i+k]='_' then
inc(i,k+1);
writeln(t,'\item ['+s1+escapestring(Copy(s,i,255))+']');
end
else
writeln('error in line: ',line,' skipping');
end;
end;
end;
If TexHeader then
writeln (t,'\end{document}');
close(t);
close(f);
end;
{*****************************************************************************
Main Program
*****************************************************************************}
procedure getpara;
var
ch : char;
para : string;
files,i : word;
procedure helpscreen;
begin
writeln('usage : msg2inc [Options] <msgfile> <incfile> <constname>');
writeln('<Options> can be : -T Create .doc TeX file');
writeln(' -TS Create .doc TeX file (stand-alone)');
writeln(' -I Intel style asm output');
writeln(' -S array of string');
writeln(' -C array of char');
writeln(' -R renumber section <incfile>');
writeln(' -V Show version');
writeln(' -? or -H This HelpScreen');
halt(1);
end;
begin
Mode:=M_String;
FIles:=0;
for i:=1to paramcount do
begin
para:=paramstr(i);
if (para[1]='-') then
begin
ch:=upcase(para[2]);
delete(para,1,2);
case ch of
'T' : begin
case upcase(para[1]) of
'S' : TexHeader:=True;
end;
Mode:=M_Tex;
end;
'I' : Mode:=M_Intel;
'S' : Mode:=M_String;
'C' : Mode:=M_Char;
'R' : Mode:=M_Renumber;
'V' : begin
Writeln('Msg2Inc ',version,' for Free Pascal (C) 1998-2002 Peter Vreman');
Writeln;
Halt;
end;
'?','H' : helpscreen;
end;
end
else
begin
inc(Files);
if Files>3 then
HelpScreen;
case Files of
1 : InFile:=Para;
2 : OutFile:=Para;
3 : OutName:=Para;
end;
end;
end;
case Mode of
M_Renumber,
M_Tex : if Files<2 then
Helpscreen;
else
if FIles<3 then
HelpScreen;
end;
end;
begin
GetPara;
case Mode of
M_Renumber : begin
Renumberfile(Infile,OutFile);
end;
M_Tex : begin
WriteTexFile(InFile,Outfile);
end;
M_Intel : begin
Loadmsgfile(InFile);
WriteEnumFile(OutFile+'idx.inc',OutName+'const');
WriteIntelFile(OutFile+'txt.inc',OutName+'txt');
end;
M_String : begin
Loadmsgfile(InFile);
WriteEnumFile(OutFile+'idx.inc',OutName+'const');
WriteStringFile(OutFile+'txt.inc',OutName+'txt');
end;
M_Char : begin
Loadmsgfile(InFile);
WriteEnumFile(OutFile+'idx.inc',OutName+'const');
WriteCharFile(OutFile+'txt.inc',OutName+'txt');
end;
end;
end.
{
$Log: msg2inc.pp,v $
Revision 1.15 2005/02/14 17:13:10 peter
* truncate log
Revision 1.14 2005/01/24 17:41:57 olle
+ added support for macos
Revision 1.13 2005/01/09 16:13:09 peter
* don't force to add .tex
}
|