Branch libreoffice-5-0-4
[LibreOffice.git] / solenv / bin / modules / installer / converter.pm
blobf825e0439bc2ec2a5d76df0e3a70d87d9fac6d25
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package installer::converter;
21 use strict;
22 use warnings;
24 use installer::globals;
26 #############################
27 # Converter
28 #############################
30 sub convert_array_to_hash
32 my ($arrayref) = @_;
34 my %newhash = ();
36 for (@{$arrayref})
38 next unless /^\s*([\w-]+?)\s+(.*?)\s*$/;
39 $newhash{$1} = $2;
42 return \%newhash;
45 #############################################################################
46 # Converting a string list with separator $listseparator
47 # into an array
48 #############################################################################
50 sub convert_stringlist_into_array
52 my ( $includestringref, $listseparator ) = @_;
54 return [map "$_\n", split /\Q$listseparator\E\s*/, ${$includestringref}];
57 #############################################################################
58 # Converting a string list with separator $listseparator
59 # into a hash with values 1.
60 #############################################################################
62 sub convert_stringlist_into_hash
64 my ( $includestringref, $listseparator ) = @_;
66 return {map {$_, 1} split /\Q$listseparator\E\s*/, ${$includestringref}};
69 #############################################################################
70 # Converting an array into a space separated string
71 #############################################################################
73 sub convert_array_to_space_separated_string
75 my ( $arrayref ) = @_;
77 my $newstring;
78 for (@{$arrayref}) {
79 my $tmp = $_;
80 $tmp =~ s/\s+$//;
81 $newstring .= "$tmp ";
83 $newstring =~ s/ $//;
85 return $newstring;
88 #############################################################################
89 # The file name contains for some files "/". If this programs runs on
90 # a windows platform, this has to be converted to "\".
91 #############################################################################
93 sub convert_slash_to_backslash
95 my ($filesarrayref) = @_;
97 for my $onefile (@{$filesarrayref})
99 if ( $onefile->{'Name'} ) { $onefile->{'Name'} =~ s/\//\\/g; }
103 ############################################################################
104 # Creating a copy of an existing file object
105 # No converter
106 ############################################################################
108 sub copy_item_object
110 my ($olditemhashref, $newitemhashref) = @_;
112 $newitemhashref = {%{$olditemhashref}};
115 #################################################################
116 # Windows paths must not contain the following structure:
117 # c:\dirA\dirB\..\dirC
118 # This has to be exchanged to
119 # c:\dirA\dirC
120 #################################################################
122 sub make_path_conform
124 my ( $path ) = @_;
125 my $s = $installer::globals::separator;
127 while ($path =~ s/[^\.\Q$s\E]+?\Q$s\E\.\.(?:\Q$s\E|$)//g) {}
129 return $path;
132 #################################################################
133 # Copying an item collector
134 # A reference to an array consisting of references to hashes.
135 #################################################################
137 sub copy_collector
139 return [map { {%{$_}} } @{$_[0]}];
142 #################################################################
143 # Combining two arrays, first wins
144 #################################################################
146 sub combine_arrays_from_references_first_win
148 my ( $arrayref1, $arrayref2 ) = @_;
150 my $hashref1 = convert_array_to_hash($arrayref1);
151 my $hashref2 = convert_array_to_hash($arrayref2);
153 # add key-value pairs from hash1 to hash2 (overwrites existing keys)
154 @{$hashref2}{keys %{$hashref1}} = values %{$hashref1};
156 return [map { "$_ $hashref2->{$_}\n" } keys %{$hashref2}];
159 #################################################################
160 # Replacing separators, that are included into quotes
161 #################################################################
163 sub replace_masked_separator
165 my ($string, $separator, $replacementstring) = @_;
167 $string =~ s/\\\Q$separator\E/$replacementstring/g;
169 return $string;
172 #################################################################
173 # Resolving separators, that were replaced
174 # in function mask_separator_in_quotes
175 #################################################################
177 sub resolve_masked_separator
179 my ($arrayref, $separator, $replacementstring) = @_;
181 for (@{$arrayref})
183 s/$replacementstring/$separator/g;