xref: /aoo42x/main/solenv/bin/modules/pre2par/work.pm (revision cdf0e10c)
1*cdf0e10cSrcweir#*************************************************************************
2*cdf0e10cSrcweir#
3*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir#
5*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir#
7*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir#
9*cdf0e10cSrcweir# This file is part of OpenOffice.org.
10*cdf0e10cSrcweir#
11*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir#
15*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir#
21*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir#
26*cdf0e10cSrcweir#*************************************************************************
27*cdf0e10cSrcweir
28*cdf0e10cSrcweir
29*cdf0e10cSrcweirpackage pre2par::work;
30*cdf0e10cSrcweir
31*cdf0e10cSrcweiruse pre2par::exiter;
32*cdf0e10cSrcweiruse pre2par::remover;
33*cdf0e10cSrcweiruse pre2par::pathanalyzer;
34*cdf0e10cSrcweir
35*cdf0e10cSrcweir############################################
36*cdf0e10cSrcweir# pre2par working module
37*cdf0e10cSrcweir############################################
38*cdf0e10cSrcweir
39*cdf0e10cSrcweir############################################
40*cdf0e10cSrcweir# procedure to split a line, that contains
41*cdf0e10cSrcweir# more than one par file lines
42*cdf0e10cSrcweir############################################
43*cdf0e10cSrcweir
44*cdf0e10cSrcweirsub split_line
45*cdf0e10cSrcweir{
46*cdf0e10cSrcweir	my ($line, $parfile) = @_;
47*cdf0e10cSrcweir
48*cdf0e10cSrcweir	while ( $line =~ /^((?:[^"]|\"(?:[^"\\]|\\.)*\")*?\;\s+)\s*(.*)$/ )
49*cdf0e10cSrcweir	{
50*cdf0e10cSrcweir		my $oneline = $1;
51*cdf0e10cSrcweir		$line = $2;
52*cdf0e10cSrcweir		pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
53*cdf0e10cSrcweir		$oneline = $oneline . "\n";
54*cdf0e10cSrcweir		push(@{$parfile}, $oneline);
55*cdf0e10cSrcweir
56*cdf0e10cSrcweir		if ( $line =~ /^\s*End\s+(\w+.*$)/i )
57*cdf0e10cSrcweir		{
58*cdf0e10cSrcweir			$line = $1;
59*cdf0e10cSrcweir			push(@{$parfile}, "End\n\n");
60*cdf0e10cSrcweir		}
61*cdf0e10cSrcweir	}
62*cdf0e10cSrcweir
63*cdf0e10cSrcweir	# the last line
64*cdf0e10cSrcweir
65*cdf0e10cSrcweir	pre2par::remover::remove_leading_and_ending_whitespaces(\$line);
66*cdf0e10cSrcweir	$line = $line . "\n";
67*cdf0e10cSrcweir	push(@{$parfile}, $line);
68*cdf0e10cSrcweir
69*cdf0e10cSrcweir	if ( $line =~ /^\s*End\s*$/i ) { push(@{$parfile}, "\n"); }
70*cdf0e10cSrcweir}
71*cdf0e10cSrcweir
72*cdf0e10cSrcweir###################################################################
73*cdf0e10cSrcweir# Preprocessing the pre file to split all lines with semicolon
74*cdf0e10cSrcweir###################################################################
75*cdf0e10cSrcweir
76*cdf0e10cSrcweirsub preprocess_macros
77*cdf0e10cSrcweir{
78*cdf0e10cSrcweir	my ($prefile) = @_;
79*cdf0e10cSrcweir
80*cdf0e10cSrcweir	my @newprefile = ();
81*cdf0e10cSrcweir
82*cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$prefile}; $i++ )
83*cdf0e10cSrcweir	{
84*cdf0e10cSrcweir		my $oneline = ${$prefile}[$i];
85*cdf0e10cSrcweir		if ( $oneline =~ /\;\s*\w+/ )
86*cdf0e10cSrcweir		{
87*cdf0e10cSrcweir			split_line($oneline, \@newprefile);
88*cdf0e10cSrcweir		}
89*cdf0e10cSrcweir		else
90*cdf0e10cSrcweir		{
91*cdf0e10cSrcweir			push(@newprefile, $oneline);
92*cdf0e10cSrcweir		}
93*cdf0e10cSrcweir	}
94*cdf0e10cSrcweir
95*cdf0e10cSrcweir	return \@newprefile;
96*cdf0e10cSrcweir}
97*cdf0e10cSrcweir
98*cdf0e10cSrcweir############################################
99*cdf0e10cSrcweir# main working procedure
100*cdf0e10cSrcweir############################################
101*cdf0e10cSrcweir
102*cdf0e10cSrcweirsub convert
103*cdf0e10cSrcweir{
104*cdf0e10cSrcweir	my ($prefile) = @_;
105*cdf0e10cSrcweir
106*cdf0e10cSrcweir	my @parfile = ();
107*cdf0e10cSrcweir
108*cdf0e10cSrcweir	my $iscodesection = 0;
109*cdf0e10cSrcweir	my $ismultiliner = 0;
110*cdf0e10cSrcweir	my $globalline = "";
111*cdf0e10cSrcweir
112*cdf0e10cSrcweir	# Preprocessing the pre file to split all lines with semicolon
113*cdf0e10cSrcweir	$prefile = preprocess_macros($prefile);
114*cdf0e10cSrcweir
115*cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$prefile}; $i++ )
116*cdf0e10cSrcweir	{
117*cdf0e10cSrcweir		my $oneline = ${$prefile}[$i];
118*cdf0e10cSrcweir
119*cdf0e10cSrcweir		if ($iscodesection)
120*cdf0e10cSrcweir		{
121*cdf0e10cSrcweir			if ( $oneline =~ /^\s*\}\;\s*$/ )
122*cdf0e10cSrcweir			{
123*cdf0e10cSrcweir				$iscodesection = 0;
124*cdf0e10cSrcweir			}
125*cdf0e10cSrcweir			else	# nothing to do for code inside a code section
126*cdf0e10cSrcweir			{
127*cdf0e10cSrcweir				push(@parfile, $oneline);
128*cdf0e10cSrcweir				next;
129*cdf0e10cSrcweir			 }
130*cdf0e10cSrcweir		}
131*cdf0e10cSrcweir
132*cdf0e10cSrcweir		if ( $oneline =~ /^\s*$/ ) { next; }
133*cdf0e10cSrcweir
134*cdf0e10cSrcweir		if ( $oneline =~ /^\s*Code\s+\=\s+\{/ )
135*cdf0e10cSrcweir		{
136*cdf0e10cSrcweir			$iscodesection = 1;
137*cdf0e10cSrcweir		}
138*cdf0e10cSrcweir
139*cdf0e10cSrcweir		pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
140*cdf0e10cSrcweir
141*cdf0e10cSrcweir		my $insertemptyline = 0;
142*cdf0e10cSrcweir
143*cdf0e10cSrcweir		if ( $oneline =~ /^\s*End\s*$/i ) { $insertemptyline = 1; }
144*cdf0e10cSrcweir
145*cdf0e10cSrcweir		# Sometimes the complete file is in one line, then the gid line has to be separated
146*cdf0e10cSrcweir
147*cdf0e10cSrcweir		if ( $oneline =~ /^\s*(\w+\s+\w+)\s+(\w+\s+\=.*$)/ )	# three words before the equal sign
148*cdf0e10cSrcweir		{
149*cdf0e10cSrcweir			my $gidline = $1;
150*cdf0e10cSrcweir			$oneline = $2;
151*cdf0e10cSrcweir			$gidline = $gidline . "\n";
152*cdf0e10cSrcweir
153*cdf0e10cSrcweir			push(@parfile, $gidline);
154*cdf0e10cSrcweir		}
155*cdf0e10cSrcweir
156*cdf0e10cSrcweir		if ( $oneline =~ /\;\s*\w+/ )
157*cdf0e10cSrcweir		{
158*cdf0e10cSrcweir			split_line($oneline, \@parfile);
159*cdf0e10cSrcweir			next;
160*cdf0e10cSrcweir		}
161*cdf0e10cSrcweir
162*cdf0e10cSrcweir		# searching for lines with brackets, like Customs = { ..., which can be parted above several lines
163*cdf0e10cSrcweir
164*cdf0e10cSrcweir		if ( $oneline =~ /^\s*\w+\s+\=\s*\(.*\)\s*\;\s*$/ )		# only one line
165*cdf0e10cSrcweir		{
166*cdf0e10cSrcweir			if (( ! ( $oneline =~ /^\s*Assignment\d+\s*\=/ )) && ( ! ( $oneline =~ /^\s*PatchAssignment\d+\s*\=/ )))
167*cdf0e10cSrcweir			{
168*cdf0e10cSrcweir				$oneline =~ s/\s//g;		# removing whitespaces in lists
169*cdf0e10cSrcweir				$oneline =~ s/\=/\ \=\ /;	# adding whitespace around equals sign
170*cdf0e10cSrcweir			}
171*cdf0e10cSrcweir		}
172*cdf0e10cSrcweir
173*cdf0e10cSrcweir		if ( $oneline =~ /^\s*\w+\s+\=\s*$/ )
174*cdf0e10cSrcweir		{
175*cdf0e10cSrcweir			$oneline =~ s/\s*$//;
176*cdf0e10cSrcweir			pre2par::exiter::exit_program("Error: Illegal syntax, no line break after eqals sign allowed. Line: \"$oneline\"", "convert");
177*cdf0e10cSrcweir		}
178*cdf0e10cSrcweir
179*cdf0e10cSrcweir		if (( $oneline =~ /^\s*\w+\s+\=\s*\(/ ) && (!( $oneline =~ /\)\s*\;\s*$/ )))	 # several lines
180*cdf0e10cSrcweir		{
181*cdf0e10cSrcweir			$ismultiliner = 1;
182*cdf0e10cSrcweir			$oneline =~ s/\s//g;
183*cdf0e10cSrcweir			$globalline .= $oneline;
184*cdf0e10cSrcweir			next;						# not including yet
185*cdf0e10cSrcweir		}
186*cdf0e10cSrcweir
187*cdf0e10cSrcweir		if ( $ismultiliner )
188*cdf0e10cSrcweir		{
189*cdf0e10cSrcweir			$oneline =~ s/\s//g;
190*cdf0e10cSrcweir			$globalline .= $oneline;
191*cdf0e10cSrcweir
192*cdf0e10cSrcweir			if ( $oneline =~ /\)\s*\;\s*$/ ) {	$ismultiliner = 0; }
193*cdf0e10cSrcweir
194*cdf0e10cSrcweir			if (! ( $ismultiliner ))
195*cdf0e10cSrcweir			{
196*cdf0e10cSrcweir				$globalline =~ s/\=/\ \=\ /;	# adding whitespace around equals sign
197*cdf0e10cSrcweir				$globalline .= "\n";
198*cdf0e10cSrcweir				push(@parfile, $globalline);
199*cdf0e10cSrcweir				$globalline = "";
200*cdf0e10cSrcweir			}
201*cdf0e10cSrcweir
202*cdf0e10cSrcweir			next;
203*cdf0e10cSrcweir		}
204*cdf0e10cSrcweir
205*cdf0e10cSrcweir		$oneline = $oneline . "\n";
206*cdf0e10cSrcweir
207*cdf0e10cSrcweir		$oneline =~ s/\s*\=\s*/ \= /;	# nice, to have only one whitespace around equal signs
208*cdf0e10cSrcweir
209*cdf0e10cSrcweir        # Concatenate adjacent string literals:
210*cdf0e10cSrcweir        while ($oneline =~
211*cdf0e10cSrcweir               s/^((?:[^"]*
212*cdf0e10cSrcweir                      \"(?:[^\\"]|\\.)*\"
213*cdf0e10cSrcweir                      (?:[^"]*[^[:blank:]"][^"]*\"(?:[^\\"]|\\.)*\")*)*
214*cdf0e10cSrcweir                   [^"]*
215*cdf0e10cSrcweir                   \"(?:[^\\"]|\\.)*)
216*cdf0e10cSrcweir                 \"[[:blank:]]*\"
217*cdf0e10cSrcweir                 ((?:[^\\"]|\\.)*\")
218*cdf0e10cSrcweir                /\1\2/x)
219*cdf0e10cSrcweir        {}
220*cdf0e10cSrcweir
221*cdf0e10cSrcweir		push(@parfile, $oneline);
222*cdf0e10cSrcweir
223*cdf0e10cSrcweir		if ($insertemptyline) { push(@parfile, "\n"); }
224*cdf0e10cSrcweir
225*cdf0e10cSrcweir	}
226*cdf0e10cSrcweir
227*cdf0e10cSrcweir	return \@parfile;
228*cdf0e10cSrcweir}
229*cdf0e10cSrcweir
230*cdf0e10cSrcweir############################################
231*cdf0e10cSrcweir# formatting the par file
232*cdf0e10cSrcweir############################################
233*cdf0e10cSrcweir
234*cdf0e10cSrcweirsub formatter
235*cdf0e10cSrcweir{
236*cdf0e10cSrcweir	my ($parfile) = @_;
237*cdf0e10cSrcweir
238*cdf0e10cSrcweir	my $iscodesection = 0;
239*cdf0e10cSrcweir
240*cdf0e10cSrcweir	my $tabcounter = 0;
241*cdf0e10cSrcweir	my $isinsideitem = 0;
242*cdf0e10cSrcweir	my $currentitem;
243*cdf0e10cSrcweir
244*cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$parfile}; $i++ )
245*cdf0e10cSrcweir	{
246*cdf0e10cSrcweir		my $oneline = ${$parfile}[$i];
247*cdf0e10cSrcweir		my $isitemline = 0;
248*cdf0e10cSrcweir
249*cdf0e10cSrcweir		if (! $isinsideitem )
250*cdf0e10cSrcweir		{
251*cdf0e10cSrcweir			for ( my $j = 0; $j <= $#pre2par::globals::allitems; $j++ )
252*cdf0e10cSrcweir			{
253*cdf0e10cSrcweir				if ( $oneline =~ /^\s*$pre2par::globals::allitems[$j]\s+\w+\s*$/ )
254*cdf0e10cSrcweir				{
255*cdf0e10cSrcweir					$currentitem = $pre2par::globals::allitems[$j];
256*cdf0e10cSrcweir					$isitemline = 1;
257*cdf0e10cSrcweir					$isinsideitem = 1;
258*cdf0e10cSrcweir					$tabcounter = 0;
259*cdf0e10cSrcweir					last;
260*cdf0e10cSrcweir				}
261*cdf0e10cSrcweir			}
262*cdf0e10cSrcweir		}
263*cdf0e10cSrcweir
264*cdf0e10cSrcweir		if ( $isitemline )
265*cdf0e10cSrcweir		{
266*cdf0e10cSrcweir			next;	# nothing to do
267*cdf0e10cSrcweir		}
268*cdf0e10cSrcweir
269*cdf0e10cSrcweir		if ( $oneline =~ /^\s*end\s*$/i )
270*cdf0e10cSrcweir		{
271*cdf0e10cSrcweir			$isinsideitem = 0;
272*cdf0e10cSrcweir			$tabcounter--;
273*cdf0e10cSrcweir		}
274*cdf0e10cSrcweir
275*cdf0e10cSrcweir		if ( $isinsideitem )
276*cdf0e10cSrcweir		{
277*cdf0e10cSrcweir			$oneline = "\t" . $oneline;
278*cdf0e10cSrcweir			${$parfile}[$i] = $oneline;
279*cdf0e10cSrcweir		}
280*cdf0e10cSrcweir	}
281*cdf0e10cSrcweir}
282*cdf0e10cSrcweir
283*cdf0e10cSrcweir###################################################
284*cdf0e10cSrcweir# Returning the language file name
285*cdf0e10cSrcweir###################################################
286*cdf0e10cSrcweir
287*cdf0e10cSrcweirsub getlangfilename
288*cdf0e10cSrcweir{
289*cdf0e10cSrcweir	return $pre2par::globals::langfilename;
290*cdf0e10cSrcweir}
291*cdf0e10cSrcweir
292*cdf0e10cSrcweir###################################################
293*cdf0e10cSrcweir# Creating the ulf file name from the
294*cdf0e10cSrcweir# corresponding pre file name
295*cdf0e10cSrcweir###################################################
296*cdf0e10cSrcweir
297*cdf0e10cSrcweirsub getulffilename
298*cdf0e10cSrcweir{
299*cdf0e10cSrcweir	my ($prefilename) = @_;
300*cdf0e10cSrcweir
301*cdf0e10cSrcweir	my $ulffilename = $prefilename;
302*cdf0e10cSrcweir	$ulffilename =~ s/\.pre\s*$/\.ulf/;
303*cdf0e10cSrcweir	pre2par::pathanalyzer::make_absolute_filename_to_relative_filename(\$ulffilename);
304*cdf0e10cSrcweir
305*cdf0e10cSrcweir	return $ulffilename;
306*cdf0e10cSrcweir}
307*cdf0e10cSrcweir
308*cdf0e10cSrcweir############################################
309*cdf0e10cSrcweir# Checking if a file exists
310*cdf0e10cSrcweir############################################
311*cdf0e10cSrcweir
312*cdf0e10cSrcweirsub fileexists
313*cdf0e10cSrcweir{
314*cdf0e10cSrcweir	my ($langfilename) = @_;
315*cdf0e10cSrcweir
316*cdf0e10cSrcweir	my $fileexists = 0;
317*cdf0e10cSrcweir
318*cdf0e10cSrcweir	if( -f $langfilename ) { $fileexists = 1; }
319*cdf0e10cSrcweir
320*cdf0e10cSrcweir	return $fileexists;
321*cdf0e10cSrcweir}
322*cdf0e10cSrcweir
323*cdf0e10cSrcweir############################################
324*cdf0e10cSrcweir# Checking the existence of ulf and
325*cdf0e10cSrcweir# jlf/mlf files
326*cdf0e10cSrcweir############################################
327*cdf0e10cSrcweir
328*cdf0e10cSrcweirsub check_existence_of_langfiles
329*cdf0e10cSrcweir{
330*cdf0e10cSrcweir	my ($langfilename, $ulffilename) = @_;
331*cdf0e10cSrcweir
332*cdf0e10cSrcweir	my $do_localize = 0;
333*cdf0e10cSrcweir
334*cdf0e10cSrcweir	if (( fileexists($ulffilename) ) && ( ! fileexists($langfilename) )) { pre2par::exiter::exit_program("Error: Did not find language file $langfilename", "check_existence_of_langfiles"); }
335*cdf0e10cSrcweir	if (( fileexists($ulffilename) ) && ( fileexists($langfilename) )) { $do_localize = 1; }
336*cdf0e10cSrcweir
337*cdf0e10cSrcweir	return $do_localize;
338*cdf0e10cSrcweir}
339*cdf0e10cSrcweir
340*cdf0e10cSrcweir############################################
341*cdf0e10cSrcweir# Checking that the pre file has content
342*cdf0e10cSrcweir############################################
343*cdf0e10cSrcweir
344*cdf0e10cSrcweirsub check_content
345*cdf0e10cSrcweir{
346*cdf0e10cSrcweir	my ($filecontent, $filename) = @_;
347*cdf0e10cSrcweir
348*cdf0e10cSrcweir	if ( $#{$filecontent} < 0 ) { pre2par::exiter::exit_program("Error: $filename has no content!", "check_content"); }
349*cdf0e10cSrcweir}
350*cdf0e10cSrcweir
351*cdf0e10cSrcweir############################################
352*cdf0e10cSrcweir# Checking content of par files.
353*cdf0e10cSrcweir# Currently only size.
354*cdf0e10cSrcweir############################################
355*cdf0e10cSrcweir
356*cdf0e10cSrcweirsub diff_content
357*cdf0e10cSrcweir{
358*cdf0e10cSrcweir	my ($content1, $content2, $filename) = @_;
359*cdf0e10cSrcweir
360*cdf0e10cSrcweir	if ( $#{$content1} != $#{$content2} ) { pre2par::exiter::exit_program("Error: $filename was not saved correctly!", "diff_content"); }
361*cdf0e10cSrcweir}
362*cdf0e10cSrcweir
363*cdf0e10cSrcweir1;
364