update dev300-m58
[ooovba.git] / solenv / bin / modules / installer / converter.pm
blobf9db1c902c0ba6439072f7615c3128a990c27d12
1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 #
5 # Copyright 2008 by Sun Microsystems, Inc.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # $RCSfile: converter.pm,v $
11 # $Revision: 1.18 $
13 # This file is part of OpenOffice.org.
15 # OpenOffice.org is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License version 3
17 # only, as published by the Free Software Foundation.
19 # OpenOffice.org is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU Lesser General Public License version 3 for more details
23 # (a copy is included in the LICENSE file that accompanied this code).
25 # You should have received a copy of the GNU Lesser General Public License
26 # version 3 along with OpenOffice.org. If not, see
27 # <http://www.openoffice.org/license.html>
28 # for a copy of the LGPLv3 License.
30 #*************************************************************************
32 package installer::converter;
34 use installer::globals;
36 #############################
37 # Converter
38 #############################
40 sub convert_array_to_hash
42 my ($arrayref) = @_;
44 my %newhash = ();
46 for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
48 my $line = ${$arrayref}[$i];
50 if ( $line =~ /^\s*([\w-]+?)\s+(.*?)\s*$/ )
52 my $key = $1;
53 my $value = $2;
54 $newhash{$key} = $value;
58 return \%newhash;
61 sub convert_hash_into_array
63 my ($hashref) = @_;
65 my @array = ();
66 my $key;
68 foreach $key (keys %{$hashref})
70 my $value = $hashref->{$key};
71 my $input = "$key = $value\n";
72 push(@array ,$input);
75 return \@array
78 #############################################################################
79 # Converting a string list with separator $listseparator
80 # into an array
81 #############################################################################
83 sub convert_stringlist_into_array_without_linebreak_and_quotes
85 my ( $includestringref, $listseparator ) = @_;
87 my @newarray = ();
88 my $first;
89 my $last = ${$includestringref};
91 while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
93 $first = $1;
94 $last = $2;
95 $first =~ s/\"//g;
96 push(@newarray, $first);
99 $last =~ s/\"//g;
100 push(@newarray, $last);
102 return \@newarray;
105 #############################################################################
106 # Converting a string list with separator $listseparator
107 # into an array
108 #############################################################################
110 sub convert_stringlist_into_array
112 my ( $includestringref, $listseparator ) = @_;
114 my @newarray = ();
115 my $first;
116 my $last = ${$includestringref};
118 while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
120 $first = $1;
121 $last = $2;
122 # Problem with two directly following listseparators. For example a path with two ";;" directly behind each other
123 $first =~ s/^$listseparator//;
124 push(@newarray, "$first\n");
127 push(@newarray, "$last\n");
129 return \@newarray;
132 #############################################################################
133 # Converting a string list with separator $listseparator
134 # into an array
135 #############################################################################
137 sub convert_stringlist_into_array_without_newline
139 my ( $includestringref, $listseparator ) = @_;
141 my @newarray = ();
142 my $first;
143 my $last = ${$includestringref};
145 while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
147 $first = $1;
148 $last = $2;
149 push(@newarray, "$first");
152 push(@newarray, "$last");
154 return \@newarray;
157 #############################################################################
158 # Converting a string list with separator $listseparator
159 # into a hash with values 1.
160 #############################################################################
162 sub convert_stringlist_into_hash
164 my ( $includestringref, $listseparator ) = @_;
166 my %newhash = ();
167 my $first;
168 my $last = ${$includestringref};
170 while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
172 $first = $1;
173 $last = $2;
174 $newhash{$first} = 1;
177 $newhash{$last} = 1;
179 return \%newhash;
182 #############################################################################
183 # Converting a string list with separator $listseparator
184 # into an array
185 #############################################################################
187 sub convert_whitespace_stringlist_into_array
189 my ( $includestringref ) = @_;
191 my @newarray = ();
192 my $first;
193 my $last = ${$includestringref};
195 while ( $last =~ /^\s*(\S+?)\s+(\S+)\s*$/) # "$" for minimal matching
197 $first = $1;
198 $last = $2;
199 push(@newarray, "$first\n");
202 push(@newarray, "$last\n");
204 return \@newarray;
207 #############################################################################
208 # Converting an array into a comma separated string
209 #############################################################################
211 sub convert_array_to_comma_separated_string
213 my ( $arrayref ) = @_;
215 my $newstring = "";
217 for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
219 my $arrayentry = ${$arrayref}[$i];
220 $arrayentry =~ s/\s*$//;
221 $newstring = $newstring . $arrayentry . ",";
224 $newstring =~ s/\,\s*$//;
226 return $newstring;
229 #############################################################################
230 # Converting an array into a space separated string
231 #############################################################################
233 sub convert_array_to_space_separated_string
235 my ( $arrayref ) = @_;
237 my $newstring = "";
239 for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
241 my $arrayentry = ${$arrayref}[$i];
242 $arrayentry =~ s/\s*$//;
243 $newstring = $newstring . $arrayentry . " ";
246 $newstring =~ s/\s*$//;
248 return $newstring;
251 #############################################################################
252 # The file name contains for some files "/". If this programs runs on
253 # a windows platform, this has to be converted to "\".
254 #############################################################################
256 sub convert_slash_to_backslash
258 my ($filesarrayref) = @_;
260 for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
262 my $onefile = ${$filesarrayref}[$i];
263 if ( $onefile->{'Name'} ) { $onefile->{'Name'} =~ s/\//\\/g; }
267 ############################################################################
268 # Creating a copy of an existing file object
269 # No converter
270 ############################################################################
272 sub copy_item_object
274 my ($olditemhashref, $newitemhashref) = @_;
276 foreach $key (keys %{$olditemhashref})
278 my $value = $olditemhashref->{$key};
279 $newitemhashref->{$key} = $value;
283 #################################################################
284 # Windows pathes must not contain the following structure:
285 # c:\dirA\dirB\..\dirC
286 # This has to be exchanged to
287 # c:\dirA\dirC
288 #################################################################
290 sub make_path_conform
292 my ( $path ) = @_;
294 my $oldpath = $path;
296 while ( $path =~ /(^.*)(\Q$installer::globals::separator\E.*?[^\.])(\Q$installer::globals::separator\E\.\.)(\Q$installer::globals::separator\E.*$)/ )
298 my $part1 = $1;
299 my $part2 = $4;
301 # $2 must not end with a "." ! Problem with "..\.."
303 $path = $part1 . $part2;
306 return $path;
309 #################################################################
310 # Copying an item collector
311 # A reference to an array consisting of references to hashes.
312 #################################################################
314 sub copy_collector
316 my ( $oldcollector ) = @_;
318 my @newcollector = ();
320 for ( my $i = 0; $i <= $#{$oldcollector}; $i++ )
322 my %newhash = ();
323 my $key;
325 foreach $key (keys %{${$oldcollector}[$i]})
327 $newhash{$key} = ${$oldcollector}[$i]->{$key};
330 push(@newcollector, \%newhash);
333 return \@newcollector;
336 #################################################################
337 # Copying an array
338 #################################################################
340 sub copy_array_from_references
342 my ( $arrayref ) = @_;
344 my @newarray = ();
346 for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
348 push(@newarray, ${$arrayref}[$i]);
351 return \@newarray;
354 ###########################################################
355 # Copying a hash
356 ###########################################################
358 sub copy_hash_from_references
360 my ($hashref) = @_;
362 my %newhash = ();
363 my $key;
365 foreach $key (keys %{$hashref})
367 $newhash{$key} = $hashref->{$key};
370 return \%newhash;
373 #################################################################
374 # Combining two arrays, first wins
375 #################################################################
377 sub combine_arrays_from_references_first_win
379 my ( $arrayref1, $arrayref2 ) = @_;
381 my $hashref1 = convert_array_to_hash($arrayref1);
382 my $hashref2 = convert_array_to_hash($arrayref2);
383 my %commonhash = ();
384 my @newarray = ();
386 # starting with second hash
387 foreach my $key ( keys %{$hashref2} ) { $commonhash{$key} = $hashref2->{$key}; }
388 # overwriting with first hash
389 foreach my $key ( keys %{$hashref1} ) { $commonhash{$key} = $hashref1->{$key}; }
391 # Creating the new array
392 foreach my $key ( keys %commonhash ) { push(@newarray, "$key $commonhash{$key}\n"); }
394 return \@newarray;
397 #################################################################
398 # Combining two arrays
399 #################################################################
401 sub combine_arrays_from_references
403 my ( $arrayref1, $arrayref2 ) = @_;
405 my @newarray = ();
407 for ( my $i = 0; $i <= $#{$arrayref1}; $i++ )
409 push(@newarray, ${$arrayref1}[$i]);
412 for ( my $i = 0; $i <= $#{$arrayref2}; $i++ )
414 push(@newarray, ${$arrayref2}[$i]);
417 return \@newarray;
420 #################################################################
421 # Returning the current ending number of a directory
422 #################################################################
424 sub get_number_from_directory
426 my ( $directory ) = @_;
428 my $number = 0;
430 if ( $directory =~ /\_(\d+)\s*$/ )
432 $number = $1;
435 return $number;
438 #################################################################
439 # Replacing separators, that are included into quotes
440 #################################################################
442 sub replace_masked_separator
444 my ($string, $separator, $replacementstring) = @_;
446 $string =~ s/\\\Q$separator\E/$replacementstring/g;
448 return $string;
451 #################################################################
452 # Resolving separators, that were replaced
453 # in function mask_separator_in_quotes
454 #################################################################
456 sub resolve_masked_separator
458 my ($arrayref, $separator, $replacementstring) = @_;
460 for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
462 ${$arrayref}[$i] =~ s/$replacementstring/$separator/g