now it's like HP-UX-ready ;p
[public/netxms.git] / tools / mc.pl
CommitLineData
6dde64eb
AK
1#!c:/apps/perl/bin/pelr -w
2
3da83dc7 3# $Id: mc.pl,v 1.6 2006-09-07 22:40:59 alk Exp $
6dde64eb
AK
4
5###############################################################################
6#
7# VC++ message compiler alternative.
8# read .mc files and generate corresponding source & include
9#
10###############################################################################
11
3da83dc7
AK
12# disabled; broke build on HP-UX (perl 5.005_02)
13#use strict;
14#use Data::Dumper;
6dde64eb
AK
15
16# file names
17my $inFile = shift || die "Usage : mc.pl <input_file>";
18my $outHeader = $inFile;
19$outHeader =~ s/\.[a-z]+$//;
20my $outSource = $outHeader . ".c";
21$outHeader .= ".h";
22
23my $type = "DWORD";
24my $messageId = 0;
25my $comment;
26my $symbolicName = "";
27my %structData;
28
29open(IN, "<$inFile") || die "input file: $!";
30open(OUTH, ">$outHeader") || die "out header: $!";
31open(OUTS, ">$outSource") || die "out source: $!";
32
33while(<IN>) {
34 chomp;
35 my $in = $_;
36 my $text = "";
37
38 if ($in eq "") {
39 next;
40 }
41
42 if ($in =~ /^[;#]+.*$/) {
43 print OUTH substr($in, 1) . "\n";
44 next; # comment -> skip
45 }
46
47 if ($in =~ /^MessageIdTypedef=([A-Z]+)$/i) {
48 # typedef
49 $type = $1;
50
51 next;
52 }
53
54 if ($in =~ /^MessageId=([0-9]*)$/i) {
55 if ($1 eq "") {
56 $messageId++;
57 } else {
58 $messageId = $1 + 0;
59 }
60
61 next;
62 }
63 if ($in =~ /^SymbolicName=(.+)$/i) {
64 $symbolicName = $1;
65
66 next;
67 }
68 if ($in =~ /^Language=(.*)$/i) {
69 # ignore language
70 next;
71 }
72
73 # multiline text
74 $text = $in;
75 while(<IN>) {
76 chomp;
77 if ($_ ne ".") {
78 $text .= "$_\n";
79 # end
80 } else {
81 if ($symbolicName eq "") {
82 last;
83 }
84 print OUTH "//\n// MessageId: $symbolicName\n//\n";
85 print OUTH "// MessageText:\n//\n// ";
86 $structData{$messageId} = $text;
87 $text =~ s/\n/\n\/\//gm;
88 print OUTH $text . "\n//\n";
89
90 my $size = 8;
91 if ($type =~ /^(WORD|short)$/i) {
92 $size = 4;
93 }
eeb5e39c 94 printf OUTH ("#define %-30s (($type)0x%0" . $size . "x)\n\n", $symbolicName, $messageId);
6dde64eb
AK
95 last;
96 }
97 }
98}
99
100print OUTS "/* autogenerated from $inFile */\n\n";
101print OUTS "char *g_szMessages[] = {\n";
102
2bdde1f8 103my @keys = sort { $a <=> $b } (keys %structData);
6dde64eb 104
2bdde1f8
AK
105my $i;
106for ($i = 0; $i < $keys[$#keys] + 1; $i++) {
6dde64eb
AK
107 if (defined $structData{$i}) {
108 $structData{$i} =~ s/\"/\\\"/g;
109 print OUTS "\t\"" . $structData{$i} . "\"";
110 } else {
111 print OUTS "\t\"\"";
112 }
113
2bdde1f8 114 if ($i == $keys[$#keys]) {
6dde64eb
AK
115 print OUTS "\n";
116 } else {
117 print OUTS ",\n";
118 }
119}
120
3662e9d3 121print OUTS "};\n";
2bdde1f8 122print OUTS "\nunsigned long g_dwNumMessages = $i;\n";
6dde64eb
AK
123
124close IN;
125close OUTH;
126close OUTS;