merge the formfield patch from ooo-build
[ooovba.git] / solenv / bin / modules / installer / files.pm
blob9f48771ddd588e05de9bfdbc71395d63d0464cf5
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: files.pm,v $
11 # $Revision: 1.9 $
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::files;
34 use installer::exiter;
35 use installer::logger;
37 ############################################
38 # File Operations
39 ############################################
41 sub check_file
43 my ($arg) = @_;
45 if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::check_file : $arg"); }
47 if(!( -f $arg ))
49 installer::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
53 sub read_file
55 my ($localfile) = @_;
56 my @localfile = ();
58 if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_file : $localfile"); }
60 open( IN, "<$localfile" ) || installer::exiter::exit_program("ERROR: Cannot open file $localfile for reading", "read_file");
62 # Don't use "my @localfile = <IN>" here, because
63 # perl has a problem with the internal "large_and_huge_malloc" function
64 # when calling perl using MacOS 10.5 with a perl built with MacOS 10.4
65 while ( $line = <IN> ) {
66 push @localfile, $line;
69 close( IN );
71 return \@localfile;
74 ###########################################
75 # Saving files, arrays and hashes
76 ###########################################
78 sub save_file
80 my ($savefile, $savecontent) = @_;
82 if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_file : $savefile : $#{$savecontent}"); }
84 if ( open( OUT, ">$savefile" ) )
86 print OUT @{$savecontent};
87 close( OUT);
89 else
91 # it is useless to save a log file, if there is no write access
93 if ( $savefile =~ /\.log/ )
95 print "\n*************************************************\n";
96 print "ERROR: Cannot write log file: $savefile";
97 print "\n*************************************************\n";
98 exit(-1); # exiting the program to avoid endless loops
101 installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_file");
105 sub save_hash
107 my ($savefile, $hashref) = @_;
109 if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_hash : $savefile"); }
111 my @printcontent = ();
113 my $itemkey;
115 foreach $itemkey ( keys %{$hashref} )
117 my $line = "";
118 my $itemvalue = $hashref->{$itemkey};
119 $line = $itemkey . "=" . $itemvalue . "\n";
120 push(@printcontent, $line);
123 open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_hash");
124 print OUT @printcontent;
125 close( OUT);
128 sub save_array_of_hashes
130 my ($savefile, $arrayref) = @_;
132 if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); }
134 my @printcontent = ();
136 for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
138 my $line = "";
139 my $hashref = ${$arrayref}[$i];
140 my $itemkey;
142 foreach $itemkey ( keys %{$hashref} )
144 my $itemvalue = $hashref->{$itemkey};
145 $line = $line . $itemkey . "=" . $itemvalue . "\t";
148 $line = $line . "\n";
150 push(@printcontent, $line);
153 open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes");
154 print OUT @printcontent;
155 close( OUT);
158 sub save_array_of_hashes_modules
160 my ($savefile, $arrayref) = @_;
162 if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); }
164 my @printcontent = ();
166 for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
168 my $line = "***************************************************\n";
169 my $hashref = ${$arrayref}[$i];
170 my $itemkey;
172 foreach $itemkey ( keys %{$hashref} )
174 my $itemvalue = $hashref->{$itemkey};
175 $line = $line . $itemkey . "=" . $itemvalue . "\n";
178 $line = $line . "\n";
180 push(@printcontent, $line);
183 open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes");
184 print OUT @printcontent;
185 close( OUT);
188 ###########################################
189 # Binary file operations
190 ###########################################
192 sub read_binary_file
194 my ($filename) = @_;
196 if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_binary_file : $filename"); }
198 my $file;
200 open( IN, "<$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "read_binary_file");
201 binmode IN;
202 seek IN, 0, 2;
203 my $length = tell IN;
204 seek IN, 0, 0;
205 read IN, $file, $length;
206 close IN;
208 return $file;
211 sub save_binary_file
213 my ($file, $filename) = @_;
215 if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_binary_file : $filename"); }
217 open( OUT, ">$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for writing", "save_binary_file");
218 binmode OUT;
219 print OUT $file;
220 close OUT;