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