xref: /trunk/main/sc/addin/util/cl2c.pl (revision cdf0e10c)
1*cdf0e10cSrcweir#!/usr/solar/bin/perl
2*cdf0e10cSrcweir
3*cdf0e10cSrcweir##########################################################################
4*cdf0e10cSrcweir#
5*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6*cdf0e10cSrcweir#
7*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
8*cdf0e10cSrcweir#
9*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
10*cdf0e10cSrcweir#
11*cdf0e10cSrcweir# This file is part of OpenOffice.org.
12*cdf0e10cSrcweir#
13*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
14*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
15*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
16*cdf0e10cSrcweir#
17*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
18*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
19*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
21*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
22*cdf0e10cSrcweir#
23*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
24*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
25*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
26*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
27*cdf0e10cSrcweir#
28*cdf0e10cSrcweir##########################################################################
29*cdf0e10cSrcweir
30*cdf0e10cSrcweirif ( $#ARGV != 3 ) {
31*cdf0e10cSrcweir	print STDERR "usage: cl2c.pl <file.cl> <file.c> <file.src> <resname>\n";
32*cdf0e10cSrcweir	exit -1;
33*cdf0e10cSrcweir}
34*cdf0e10cSrcweir
35*cdf0e10cSrcweir$CL=$ARGV[0];
36*cdf0e10cSrcweir$C=$ARGV[1];
37*cdf0e10cSrcweir$SRC=$ARGV[2];
38*cdf0e10cSrcweir$RNAME=$ARGV[3];
39*cdf0e10cSrcweir
40*cdf0e10cSrcweirsub sconv
41*cdf0e10cSrcweir{
42*cdf0e10cSrcweir	local($s)=@_[0];
43*cdf0e10cSrcweir	local($o,$c);
44*cdf0e10cSrcweir	$_="";
45*cdf0e10cSrcweir	foreach $o ( unpack("C*",$s) ) {
46*cdf0e10cSrcweir		$c=chr($o);
47*cdf0e10cSrcweir		if ( $o >= 32 && $o < 127 ) {
48*cdf0e10cSrcweir			$_ .= $c;
49*cdf0e10cSrcweir		} else {
50*cdf0e10cSrcweir			$_ .= sprintf("\\%o", $o);
51*cdf0e10cSrcweir		}
52*cdf0e10cSrcweir	}
53*cdf0e10cSrcweir	return $_;
54*cdf0e10cSrcweir}
55*cdf0e10cSrcweir
56*cdf0e10cSrcweir
57*cdf0e10cSrcweirsub makeneutral {
58*cdf0e10cSrcweir
59*cdf0e10cSrcweir	print COUT "\n\n/**\n";
60*cdf0e10cSrcweir	print COUT " * Get neutral language for specific language.\n";
61*cdf0e10cSrcweir	print COUT " * This simplifies the getText switch cases and allows to handle\n";
62*cdf0e10cSrcweir	print COUT " * previously unknown language derivates due to foreign installations.\n";
63*cdf0e10cSrcweir	print COUT " * If you want to distinguish between some dialects change this function\n";
64*cdf0e10cSrcweir	print COUT " * to return the desired nLang before doing the bit masking stuff.\n";
65*cdf0e10cSrcweir	print COUT " * See xlang.h for defined LANGUAGE_*\n";
66*cdf0e10cSrcweir	print COUT " */\n";
67*cdf0e10cSrcweir
68*cdf0e10cSrcweir	# taken from tools/source/intntl/intn.cxx International::GetNeutralLanguage
69*cdf0e10cSrcweir	print COUT "static USHORT GetNeutralLanguage( USHORT nLang )\n";
70*cdf0e10cSrcweir	print COUT "{\n";
71*cdf0e10cSrcweir	print COUT "\tUSHORT nPrimLang;\n";
72*cdf0e10cSrcweir	print COUT "\n";
73*cdf0e10cSrcweir	print COUT "\t/* ignore LANGUAGE_USER* */\n";
74*cdf0e10cSrcweir	print COUT "\tif ( (nLang & 0x03FF) >= 0x0200 )\n";
75*cdf0e10cSrcweir	print COUT "\t	return nLang;\n";
76*cdf0e10cSrcweir	print COUT "\n";
77*cdf0e10cSrcweir	print COUT "\tnLang &= 0x03FF;\n";
78*cdf0e10cSrcweir	print COUT "\n";
79*cdf0e10cSrcweir	print COUT "\tnPrimLang = nLang | 0x0400;\n";
80*cdf0e10cSrcweir	print COUT "\n";
81*cdf0e10cSrcweir	print COUT "\tswitch ( nPrimLang )\n";
82*cdf0e10cSrcweir	print COUT "\t{\n";
83*cdf0e10cSrcweir	print COUT "\t\tcase LANGUAGE_CHINESE_TRADITIONAL:\n";
84*cdf0e10cSrcweir	print COUT "\t\t\tnLang = LANGUAGE_CHINESE;\n";
85*cdf0e10cSrcweir	print COUT "\t\t\tbreak;\n";
86*cdf0e10cSrcweir	print COUT "\t\tcase LANGUAGE_ENGLISH_US:\n";
87*cdf0e10cSrcweir	print COUT "\t\t\tnLang = LANGUAGE_ENGLISH;\n";
88*cdf0e10cSrcweir	print COUT "\t\t\tbreak;\n";
89*cdf0e10cSrcweir	print COUT "\t\tcase LANGUAGE_NORWEGIAN_BOKMAL:\n";
90*cdf0e10cSrcweir	print COUT "\t\t\tnLang = LANGUAGE_NORWEGIAN;\n";
91*cdf0e10cSrcweir	print COUT "\t\t\tbreak;\n";
92*cdf0e10cSrcweir	print COUT "\t\tcase LANGUAGE_PORTUGUESE_BRAZILIAN:\n";
93*cdf0e10cSrcweir	print COUT "\t\t\tnLang = LANGUAGE_PORTUGUESE;\n";
94*cdf0e10cSrcweir	print COUT "\t\t\tbreak;\n";
95*cdf0e10cSrcweir	print COUT "\n";
96*cdf0e10cSrcweir	print COUT "\t\tdefault:\n";
97*cdf0e10cSrcweir	print COUT "\t\t\tnLang = nPrimLang;\n";
98*cdf0e10cSrcweir	print COUT "\t\t\tbreak;\n";
99*cdf0e10cSrcweir	print COUT "\t}\n";
100*cdf0e10cSrcweir	print COUT "\n";
101*cdf0e10cSrcweir	print COUT "\treturn nLang;\n";
102*cdf0e10cSrcweir	print COUT "}\n";
103*cdf0e10cSrcweir	print COUT "\n";
104*cdf0e10cSrcweir
105*cdf0e10cSrcweir}
106*cdf0e10cSrcweir
107*cdf0e10cSrcweir
108*cdf0e10cSrcweirsub maketext {
109*cdf0e10cSrcweir
110*cdf0e10cSrcweir	print COUT "\n\n/**\n";
111*cdf0e10cSrcweir	print COUT " * Get text resource for current language.\n";
112*cdf0e10cSrcweir	print COUT " * Remember that 8-bit characters are shown in\n";
113*cdf0e10cSrcweir	print COUT " * system dependend code pages!\n";
114*cdf0e10cSrcweir	print COUT " * To get correct results you will have to distuinguish\n";
115*cdf0e10cSrcweir	print COUT " * for example between UNIX and WIN and OS2 target systems.\n";
116*cdf0e10cSrcweir	print COUT " */\n";
117*cdf0e10cSrcweir
118*cdf0e10cSrcweir	print COUT "static char* getText( int nResource )\n{\n";
119*cdf0e10cSrcweir	print COUT "\tswitch( nResource ) {\n";
120*cdf0e10cSrcweir
121*cdf0e10cSrcweir	$resflag=0;
122*cdf0e10cSrcweir	$strname="";
123*cdf0e10cSrcweir	$cnt=0;
124*cdf0e10cSrcweir	$text_english="";
125*cdf0e10cSrcweir
126*cdf0e10cSrcweir	while (<SRCIN>) {
127*cdf0e10cSrcweir		$resflag=1 if ( /Resource\s$RNAME/ );
128*cdf0e10cSrcweir
129*cdf0e10cSrcweir		if ( /\{/ ) {
130*cdf0e10cSrcweir			if ( ++$cnt == 2 ) {
131*cdf0e10cSrcweir				# start language
132*cdf0e10cSrcweir				$text_english="";
133*cdf0e10cSrcweir				print COUT "\t\t\tswitch( _nLanguage ) {\n";
134*cdf0e10cSrcweir				next;
135*cdf0e10cSrcweir			}
136*cdf0e10cSrcweir		}
137*cdf0e10cSrcweir
138*cdf0e10cSrcweir		if ( /\}/ ) {
139*cdf0e10cSrcweir			if ( --$cnt == 1 ) {
140*cdf0e10cSrcweir				# end language
141*cdf0e10cSrcweir
142*cdf0e10cSrcweir				if ( $text_english ne "" ) {
143*cdf0e10cSrcweir					print COUT "\t\t\t\tcase LANGUAGE_ENGLISH:\n\t\t\t\tdefault:\n";
144*cdf0e10cSrcweir					print COUT "\t\t\t\treturn(" . $text_english . ")\;\n";
145*cdf0e10cSrcweir				}
146*cdf0e10cSrcweir
147*cdf0e10cSrcweir				print COUT "\t\t\t}\n\t\t\tbreak;\n";
148*cdf0e10cSrcweir				next;
149*cdf0e10cSrcweir			} elsif ( $cnt == 0 ) {
150*cdf0e10cSrcweir				# end of resource
151*cdf0e10cSrcweir				$resflag=0;
152*cdf0e10cSrcweir				print COUT "\t\tdefault:\n\t\t\tbreak;\n";
153*cdf0e10cSrcweir				print COUT "\t}\n\treturn(\"\");\n}\n";
154*cdf0e10cSrcweir				next;
155*cdf0e10cSrcweir			}
156*cdf0e10cSrcweir
157*cdf0e10cSrcweir		}
158*cdf0e10cSrcweir
159*cdf0e10cSrcweir		if ( $resflag && $cnt == 1) {
160*cdf0e10cSrcweir			if ( /\sString\s(([A-Z]|\_|[0-9]|[a-z])*)/ ) {
161*cdf0e10cSrcweir				$strname=$1;
162*cdf0e10cSrcweir				print COUT "\t\tcase " . $strname . ":\n";
163*cdf0e10cSrcweir			}
164*cdf0e10cSrcweir		}
165*cdf0e10cSrcweir
166*cdf0e10cSrcweir		if ( $cnt == 2 && /^\s*Text/ ) {
167*cdf0e10cSrcweir			$langname="german";
168*cdf0e10cSrcweir			($textdef,@textx)=split(/=/);
169*cdf0e10cSrcweir			$text=join("=",@textx);
170*cdf0e10cSrcweir			if ( $textdef =~ /\[\s+(.*)\s+\]/ ) {
171*cdf0e10cSrcweir				$langname=$1;
172*cdf0e10cSrcweir			}
173*cdf0e10cSrcweir            else {
174*cdf0e10cSrcweir                $langname="ENGLISH_US";     # no [...] => not to be translated
175*cdf0e10cSrcweir            }
176*cdf0e10cSrcweir
177*cdf0e10cSrcweir			$langname="LANGUAGE_" . uc($langname);
178*cdf0e10cSrcweir
179*cdf0e10cSrcweir			chop($text) while ( $text=~/(\r|\n|\;)$/ );
180*cdf0e10cSrcweir			$text=sconv($text);
181*cdf0e10cSrcweir			# english_us, not english because it's developer's pigeon
182*cdf0e10cSrcweir			if ( $langname eq "LANGUAGE_ENGLISH_US" ) {
183*cdf0e10cSrcweir				$text_english=$text;
184*cdf0e10cSrcweir			}
185*cdf0e10cSrcweir            # ISO coded, obtain at least the default
186*cdf0e10cSrcweir            elsif ( $langname =~ /^LANGUAGE_EN-US$/ ) {
187*cdf0e10cSrcweir				$text_english=$text;
188*cdf0e10cSrcweir            }
189*cdf0e10cSrcweir			# we don't know about USER languages, ENGLISH will be appended later
190*cdf0e10cSrcweir			elsif ( ! ( $langname =~ /LANGUAGE_USER/ || $langname =~ /^LANGUAGE_ENGLISH$/ ) ) {
191*cdf0e10cSrcweir				# ER 28.04.99: for the moment only German and English are
192*cdf0e10cSrcweir				# exported, because we have a problem with non-existing
193*cdf0e10cSrcweir				# character code tables for systems other than Windoze.
194*cdf0e10cSrcweir				# => Chinese would be definitely mixed up and we don't
195*cdf0e10cSrcweir				# want to insult anybody.. others like Spanish would look
196*cdf0e10cSrcweir				# very ugly, but we'll have to live with bad German Umlauts.
197*cdf0e10cSrcweir				if ( $langname =~ /LANGUAGE_(GERMAN|ENGLISH)/ ) {
198*cdf0e10cSrcweir					print COUT "\t\t\t\tcase " . $langname . ":\n";
199*cdf0e10cSrcweir					print COUT "\t\t\t\treturn(" . $text . ")\;\n";
200*cdf0e10cSrcweir				}
201*cdf0e10cSrcweir			}
202*cdf0e10cSrcweir
203*cdf0e10cSrcweir		}
204*cdf0e10cSrcweir	}
205*cdf0e10cSrcweir
206*cdf0e10cSrcweir	makeneutral();
207*cdf0e10cSrcweir
208*cdf0e10cSrcweir}
209*cdf0e10cSrcweir
210*cdf0e10cSrcweiropen(CLIN,"<$CL") || die "can not open $CL\n";
211*cdf0e10cSrcweiropen(SRCIN,"<$SRC") || die "can not open $CL\n";
212*cdf0e10cSrcweiropen(COUT,">$C") || die "can not open $CL\n";
213*cdf0e10cSrcweir
214*cdf0e10cSrcweir$ccnt=0;
215*cdf0e10cSrcweir$incomment=0;
216*cdf0e10cSrcweirwhile(<CLIN>) {
217*cdf0e10cSrcweir	if ( /^\/\*--(-*)/ ) {
218*cdf0e10cSrcweir		$incomment=1;
219*cdf0e10cSrcweir		$ccnt++;
220*cdf0e10cSrcweir	}
221*cdf0e10cSrcweir
222*cdf0e10cSrcweir	print COUT $_ if ( $incomment==0 || $ccnt==1 );
223*cdf0e10cSrcweir
224*cdf0e10cSrcweir	&maketext() if ( /^static USHORT _nLanguage=/ );
225*cdf0e10cSrcweir
226*cdf0e10cSrcweir	if ( /(-*)--\*\/$/ ) {
227*cdf0e10cSrcweir		$incomment=0;
228*cdf0e10cSrcweir	}
229*cdf0e10cSrcweir
230*cdf0e10cSrcweir}
231*cdf0e10cSrcweir
232*cdf0e10cSrcweirclose(CLIN);
233*cdf0e10cSrcweirclose(SRCIN);
234*cdf0e10cSrcweirclose(COUT);
235*cdf0e10cSrcweir
236*cdf0e10cSrcweirexit 0;
237*cdf0e10cSrcweir
238*cdf0e10cSrcweir
239