| 1 |
# |
| 2 |
# This file is part of the LibreOffice project. |
| 3 |
# |
| 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/. |
| 7 |
# |
| 8 |
# This file incorporates work covered by the following license notice: |
| 9 |
# |
| 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 . |
| 17 |
# |
| 18 |
|
| 19 |
package installer::converter; |
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use installer::globals; |
| 25 |
|
| 26 |
############################# |
| 27 |
# Converter |
| 28 |
############################# |
| 29 |
|
| 30 |
sub convert_array_to_hash |
| 31 |
{ |
| 32 |
my ($arrayref) = @_; |
| 33 |
|
| 34 |
my %newhash = (); |
| 35 |
|
| 36 |
for (@{$arrayref}) |
| 37 |
{ |
| 38 |
next unless /^\s*([\w-]+?)\s+(.*?)\s*$/; |
| 39 |
$newhash{$1} = $2; |
| 40 |
} |
| 41 |
|
| 42 |
return \%newhash; |
| 43 |
} |
| 44 |
|
| 45 |
############################################################################# |
| 46 |
# Converting a string list with separator $listseparator |
| 47 |
# into an array |
| 48 |
############################################################################# |
| 49 |
|
| 50 |
sub convert_stringlist_into_array |
| 51 |
{ |
| 52 |
my ( $includestringref, $listseparator ) = @_; |
| 53 |
|
| 54 |
return [map "$_\n", split /\Q$listseparator\E\s*/, ${$includestringref}]; |
| 55 |
} |
| 56 |
|
| 57 |
############################################################################# |
| 58 |
# Converting a string list with separator $listseparator |
| 59 |
# into a hash with values 1. |
| 60 |
############################################################################# |
| 61 |
|
| 62 |
sub convert_stringlist_into_hash |
| 63 |
{ |
| 64 |
my ( $includestringref, $listseparator ) = @_; |
| 65 |
|
| 66 |
return {map {$_, 1} split /\Q$listseparator\E\s*/, ${$includestringref}}; |
| 67 |
} |
| 68 |
|
| 69 |
############################################################################# |
| 70 |
# Converting an array into a space separated string |
| 71 |
############################################################################# |
| 72 |
|
| 73 |
sub convert_array_to_space_separated_string |
| 74 |
{ |
| 75 |
my ( $arrayref ) = @_; |
| 76 |
|
| 77 |
my $newstring; |
| 78 |
for (@{$arrayref}) { |
| 79 |
my $tmp = $_; |
| 80 |
$tmp =~ s/\s+$//; |
| 81 |
$newstring .= "$tmp "; |
| 82 |
} |
| 83 |
$newstring =~ s/ $//; |
| 84 |
|
| 85 |
return $newstring; |
| 86 |
} |
| 87 |
|
| 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 |
############################################################################# |
| 92 |
|
| 93 |
sub convert_slash_to_backslash |
| 94 |
{ |
| 95 |
my ($filesarrayref) = @_; |
| 96 |
|
| 97 |
for my $onefile (@{$filesarrayref}) |
| 98 |
{ |
| 99 |
if ( $onefile->{'Name'} ) { $onefile->{'Name'} =~ s/\//\\/g; } |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
############################################################################ |
| 104 |
# Creating a copy of an existing file object |
| 105 |
# No converter |
| 106 |
############################################################################ |
| 107 |
|
| 108 |
sub copy_item_object |
| 109 |
{ |
| 110 |
my ($olditemhashref, $newitemhashref) = @_; |
| 111 |
|
| 112 |
$newitemhashref = {%{$olditemhashref}}; |
| 113 |
} |
| 114 |
|
| 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 |
################################################################# |
| 121 |
|
| 122 |
sub make_path_conform |
| 123 |
{ |
| 124 |
my ( $path ) = @_; |
| 125 |
my $s = $installer::globals::separator; |
| 126 |
|
| 127 |
while ($path =~ s/[^\.\Q$s\E]+?\Q$s\E\.\.(?:\Q$s\E|$)//g) {} |
| 128 |
|
| 129 |
return $path; |
| 130 |
} |
| 131 |
|
| 132 |
################################################################# |
| 133 |
# Copying an item collector |
| 134 |
# A reference to an array consisting of references to hashes. |
| 135 |
################################################################# |
| 136 |
|
| 137 |
sub copy_collector |
| 138 |
{ |
| 139 |
return [map { {%{$_}} } @{$_[0]}]; |
| 140 |
} |
| 141 |
|
| 142 |
################################################################# |
| 143 |
# Combining two arrays, first wins |
| 144 |
################################################################# |
| 145 |
|
| 146 |
sub combine_arrays_from_references_first_win |
| 147 |
{ |
| 148 |
my ( $arrayref1, $arrayref2 ) = @_; |
| 149 |
|
| 150 |
my $hashref1 = convert_array_to_hash($arrayref1); |
| 151 |
my $hashref2 = convert_array_to_hash($arrayref2); |
| 152 |
|
| 153 |
# add key-value pairs from hash1 to hash2 (overwrites existing keys) |
| 154 |
@{$hashref2}{keys %{$hashref1}} = values %{$hashref1}; |
| 155 |
|
| 156 |
return [map { "$_ $hashref2->{$_}\n" } keys %{$hashref2}]; |
| 157 |
} |
| 158 |
|
| 159 |
################################################################# |
| 160 |
# Replacing separators, that are included into quotes |
| 161 |
################################################################# |
| 162 |
|
| 163 |
sub replace_masked_separator |
| 164 |
{ |
| 165 |
my ($string, $separator, $replacementstring) = @_; |
| 166 |
|
| 167 |
$string =~ s/\\\Q$separator\E/$replacementstring/g; |
| 168 |
|
| 169 |
return $string; |
| 170 |
} |
| 171 |
|
| 172 |
################################################################# |
| 173 |
# Resolving separators, that were replaced |
| 174 |
# in function mask_separator_in_quotes |
| 175 |
################################################################# |
| 176 |
|
| 177 |
sub resolve_masked_separator |
| 178 |
{ |
| 179 |
my ($arrayref, $separator, $replacementstring) = @_; |
| 180 |
|
| 181 |
for (@{$arrayref}) |
| 182 |
{ |
| 183 |
s/$replacementstring/$separator/g; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
1; |