now it's like HP-UX-ready ;p
[public/netxms.git] / tools / mc.pl
1 #!c:/apps/perl/bin/pelr -w
2
3 # $Id: mc.pl,v 1.6 2006-09-07 22:40:59 alk Exp $
4
5 ###############################################################################
6 #
7 # VC++ message compiler alternative.
8 # read .mc files and generate corresponding source & include
9 #
10 ###############################################################################
11
12 # disabled; broke build on HP-UX (perl 5.005_02)
13 #use strict;
14 #use Data::Dumper;
15
16 # file names
17 my $inFile = shift || die "Usage : mc.pl <input_file>";
18 my $outHeader = $inFile;
19 $outHeader =~ s/\.[a-z]+$//;
20 my $outSource = $outHeader . ".c";
21 $outHeader .= ".h";
22
23 my $type = "DWORD";
24 my $messageId = 0;
25 my $comment;
26 my $symbolicName = "";
27 my %structData;
28
29 open(IN, "<$inFile") || die "input file: $!";
30 open(OUTH, ">$outHeader") || die "out header: $!";
31 open(OUTS, ">$outSource") || die "out source: $!";
32
33 while(<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 }
94 printf OUTH ("#define %-30s (($type)0x%0" . $size . "x)\n\n", $symbolicName, $messageId);
95 last;
96 }
97 }
98 }
99
100 print OUTS "/* autogenerated from $inFile */\n\n";
101 print OUTS "char *g_szMessages[] = {\n";
102
103 my @keys = sort { $a <=> $b } (keys %structData);
104
105 my $i;
106 for ($i = 0; $i < $keys[$#keys] + 1; $i++) {
107 if (defined $structData{$i}) {
108 $structData{$i} =~ s/\"/\\\"/g;
109 print OUTS "\t\"" . $structData{$i} . "\"";
110 } else {
111 print OUTS "\t\"\"";
112 }
113
114 if ($i == $keys[$#keys]) {
115 print OUTS "\n";
116 } else {
117 print OUTS ",\n";
118 }
119 }
120
121 print OUTS "};\n";
122 print OUTS "\nunsigned long g_dwNumMessages = $i;\n";
123
124 close IN;
125 close OUTH;
126 close OUTS;