xref: /AOO41X/main/solenv/bin/modules/installer/converter.pm (revision ef1ef8e674fabf3a541d12c6e6c14cecdfc2f9e7)
1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28package installer::converter;
29
30use installer::globals;
31
32#############################
33# Converter
34#############################
35
36sub convert_array_to_hash
37{
38    my ($arrayref) = @_;
39
40    my %newhash = ();
41
42    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
43    {
44        my $line = ${$arrayref}[$i];
45
46        if ( $line =~ /^\s*([\w-]+?)\s+(.*?)\s*$/ )
47        {
48            my $key = $1;
49            my $value = $2;
50            $newhash{$key} = $value;
51        }
52    }
53
54    return \%newhash;
55}
56
57sub convert_hash_into_array
58{
59    my ($hashref) = @_;
60
61    my @array = ();
62    my $key;
63
64    foreach $key (keys %{$hashref})
65    {
66        my $value = $hashref->{$key};
67        my $input = "$key = $value\n";
68        push(@array ,$input);
69    }
70
71    return \@array
72}
73
74#############################################################################
75# Converting a string list with separator $listseparator
76# into an array
77#############################################################################
78
79sub convert_stringlist_into_array_without_linebreak_and_quotes
80{
81    my ( $includestringref, $listseparator ) = @_;
82
83    my @newarray = ();
84    my $first;
85    my $last = ${$includestringref};
86
87    while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
88    {
89        $first = $1;
90        $last = $2;
91        $first =~ s/\"//g;
92        push(@newarray, $first);
93    }
94
95    $last =~ s/\"//g;
96    push(@newarray, $last);
97
98    return \@newarray;
99}
100
101#############################################################################
102# Converting a string list with separator $listseparator
103# into an array
104#############################################################################
105
106sub convert_stringlist_into_array
107{
108    my ( $includestringref, $listseparator ) = @_;
109
110    my @newarray = ();
111    my $first;
112    my $last = ${$includestringref};
113
114    while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
115    {
116        $first = $1;
117        $last = $2;
118        # Problem with two directly following listseparators. For example a path with two ";;" directly behind each other
119        $first =~ s/^$listseparator//;
120        push(@newarray, "$first\n");
121    }
122
123    push(@newarray, "$last\n");
124
125    return \@newarray;
126}
127
128#############################################################################
129# Converting a string list with separator $listseparator
130# into an array
131#############################################################################
132
133sub convert_stringlist_into_array_without_newline
134{
135    my ( $includestringref, $listseparator ) = @_;
136
137    my @newarray = ();
138    my $first;
139    my $last = ${$includestringref};
140
141    while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
142    {
143        $first = $1;
144        $last = $2;
145        push(@newarray, "$first");
146    }
147
148    push(@newarray, "$last");
149
150    return \@newarray;
151}
152
153#############################################################################
154# Converting a string list with separator $listseparator
155# into a hash with values 1.
156#############################################################################
157
158sub convert_stringlist_into_hash
159{
160    my ( $includestringref, $listseparator ) = @_;
161
162    my %newhash = ();
163    my $first;
164    my $last = ${$includestringref};
165
166    while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
167    {
168        $first = $1;
169        $last = $2;
170        $newhash{$first} = 1;
171    }
172
173    $newhash{$last} = 1;
174
175    return \%newhash;
176}
177
178#############################################################################
179# Converting a string list with separator $listseparator
180# into an array
181#############################################################################
182
183sub convert_whitespace_stringlist_into_array
184{
185    my ( $includestringref ) = @_;
186
187    my @newarray = ();
188    my $first;
189    my $last = ${$includestringref};
190
191    while ( $last =~ /^\s*(\S+?)\s+(\S+)\s*$/)  # "$" for minimal matching
192    {
193        $first = $1;
194        $last = $2;
195        push(@newarray, "$first\n");
196    }
197
198    push(@newarray, "$last\n");
199
200    return \@newarray;
201}
202
203#############################################################################
204# Converting an array into a comma separated string
205#############################################################################
206
207sub convert_array_to_comma_separated_string
208{
209    my ( $arrayref ) = @_;
210
211    my $newstring = "";
212
213    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
214    {
215        my $arrayentry = ${$arrayref}[$i];
216        $arrayentry =~ s/\s*$//;
217        $newstring = $newstring . $arrayentry . ",";
218    }
219
220    $newstring =~ s/\,\s*$//;
221
222    return $newstring;
223}
224
225#############################################################################
226# Converting an array into a space separated string
227#############################################################################
228
229sub convert_array_to_space_separated_string
230{
231    my ( $arrayref ) = @_;
232
233    my $newstring = "";
234
235    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
236    {
237        my $arrayentry = ${$arrayref}[$i];
238        $arrayentry =~ s/\s*$//;
239        $newstring = $newstring . $arrayentry . " ";
240    }
241
242    $newstring =~ s/\s*$//;
243
244    return $newstring;
245}
246
247#############################################################################
248# The file name contains for some files "/". If this programs runs on
249# a windows platform, this has to be converted to "\".
250#############################################################################
251
252sub convert_slash_to_backslash
253{
254    my ($filesarrayref) = @_;
255
256    for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
257    {
258        my $onefile = ${$filesarrayref}[$i];
259        if ( $onefile->{'Name'} ) { $onefile->{'Name'} =~ s/\//\\/g; }
260    }
261}
262
263############################################################################
264# Creating a copy of an existing file object
265# No converter
266############################################################################
267
268sub copy_item_object
269{
270    my ($olditemhashref, $newitemhashref) = @_;
271
272    foreach $key (keys %{$olditemhashref})
273    {
274        my $value = $olditemhashref->{$key};
275        $newitemhashref->{$key} = $value;
276    }
277}
278
279#################################################################
280# Windows pathes must not contain the following structure:
281# c:\dirA\dirB\..\dirC
282# This has to be exchanged to
283# c:\dirA\dirC
284#################################################################
285
286sub make_path_conform
287{
288    my ( $path ) = @_;
289
290    my $oldpath = $path;
291
292    while ( $path =~ /(^.*)(\Q$installer::globals::separator\E.*?[^\.])(\Q$installer::globals::separator\E\.\.)(\Q$installer::globals::separator\E.*$)/ )
293    {
294        my $part1 = $1;
295        my $part2 = $4;
296
297        # $2 must not end with a "." ! Problem with "..\.."
298
299        $path = $part1 . $part2;
300    }
301
302    return $path;
303}
304
305#################################################################
306# Copying an item collector
307# A reference to an array consisting of references to hashes.
308#################################################################
309
310sub copy_collector
311{
312    my ( $oldcollector ) = @_;
313
314    my @newcollector = ();
315
316    for ( my $i = 0; $i <= $#{$oldcollector}; $i++ )
317    {
318        my %newhash = ();
319        my $key;
320
321        foreach $key (keys %{${$oldcollector}[$i]})
322        {
323            $newhash{$key} = ${$oldcollector}[$i]->{$key};
324        }
325
326        push(@newcollector, \%newhash);
327    }
328
329    return \@newcollector;
330}
331
332#################################################################
333# Copying an array
334#################################################################
335
336sub copy_array_from_references
337{
338    my ( $arrayref ) = @_;
339
340    my @newarray = ();
341
342    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
343    {
344        push(@newarray, ${$arrayref}[$i]);
345    }
346
347    return \@newarray;
348}
349
350###########################################################
351# Copying a hash
352###########################################################
353
354sub copy_hash_from_references
355{
356    my ($hashref) = @_;
357
358    my %newhash = ();
359    my $key;
360
361    foreach $key (keys %{$hashref})
362    {
363        $newhash{$key} = $hashref->{$key};
364    }
365
366    return \%newhash;
367}
368
369#################################################################
370# Combining two arrays, first wins
371#################################################################
372
373sub combine_arrays_from_references_first_win
374{
375    my ( $arrayref1, $arrayref2 ) = @_;
376
377    my $hashref1 = convert_array_to_hash($arrayref1);
378    my $hashref2 = convert_array_to_hash($arrayref2);
379    my %commonhash = ();
380    my @newarray = ();
381
382    # starting with second hash
383    foreach my $key ( keys %{$hashref2} ) { $commonhash{$key} = $hashref2->{$key}; }
384    # overwriting with first hash
385    foreach my $key ( keys %{$hashref1} ) { $commonhash{$key} = $hashref1->{$key}; }
386
387    # Creating the new array
388    foreach my $key ( keys %commonhash ) { push(@newarray, "$key $commonhash{$key}\n"); }
389
390    return \@newarray;
391}
392
393#################################################################
394# Combining two arrays
395#################################################################
396
397sub combine_arrays_from_references
398{
399    my ( $arrayref1, $arrayref2 ) = @_;
400
401    my @newarray = ();
402
403    for ( my $i = 0; $i <= $#{$arrayref1}; $i++ )
404    {
405        push(@newarray, ${$arrayref1}[$i]);
406    }
407
408    for ( my $i = 0; $i <= $#{$arrayref2}; $i++ )
409    {
410        push(@newarray, ${$arrayref2}[$i]);
411    }
412
413    return \@newarray;
414}
415
416#################################################################
417# Returning the current ending number of a directory
418#################################################################
419
420sub get_number_from_directory
421{
422    my ( $directory ) = @_;
423
424    my $number = 0;
425
426    if ( $directory =~ /\_(\d+)\s*$/ )
427    {
428        $number = $1;
429    }
430
431    return $number;
432}
433
434#################################################################
435# Replacing separators, that are included into quotes
436#################################################################
437
438sub replace_masked_separator
439{
440    my ($string, $separator, $replacementstring) = @_;
441
442    $string =~ s/\\\Q$separator\E/$replacementstring/g;
443
444    return $string;
445}
446
447#################################################################
448# Resolving separators, that were replaced
449# in function mask_separator_in_quotes
450#################################################################
451
452sub resolve_masked_separator
453{
454    my ($arrayref, $separator, $replacementstring) = @_;
455
456    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
457    {
458        ${$arrayref}[$i] =~ s/$replacementstring/$separator/g
459    }
460}
461
4621;
463