xref: /trunk/main/solenv/bin/modules/installer/files.pm (revision cdf0e10c)
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::files;
29
30use installer::exiter;
31use installer::logger;
32
33############################################
34# File Operations
35############################################
36
37sub check_file
38{
39	my ($arg) = @_;
40
41	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::check_file : $arg"); }
42
43	if(!( -f $arg ))
44	{
45		installer::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
46	}
47}
48
49sub read_file
50{
51	my ($localfile) = @_;
52	my @localfile = ();
53
54	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_file : $localfile"); }
55
56	open( IN, "<$localfile" ) || installer::exiter::exit_program("ERROR: Cannot open file $localfile for reading", "read_file");
57
58#	Don't use "my @localfile = <IN>" here, because
59#	perl has a problem with the internal "large_and_huge_malloc" function
60#	when calling perl using MacOS 10.5 with a perl built with MacOS 10.4
61	while ( $line = <IN> ) {
62		push @localfile, $line;
63	}
64
65	close( IN );
66
67	return \@localfile;
68}
69
70###########################################
71# Saving files, arrays and hashes
72###########################################
73
74sub save_file
75{
76	my ($savefile, $savecontent) = @_;
77
78	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_file : $savefile : $#{$savecontent}"); }
79
80	if ( open( OUT, ">$savefile" ) )
81	{
82		print OUT @{$savecontent};
83		close( OUT);
84	}
85	else
86	{
87		# it is useless to save a log file, if there is no write access
88
89		if ( $savefile =~ /\.log/ )
90		{
91			print "\n*************************************************\n";
92			print "ERROR: Cannot write log file: $savefile";
93			print "\n*************************************************\n";
94			exit(-1);	# exiting the program to avoid endless loops
95		}
96
97		installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_file");
98	}
99}
100
101sub save_hash
102{
103	my ($savefile, $hashref) = @_;
104
105	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_hash : $savefile"); }
106
107	my @printcontent = ();
108
109	my $itemkey;
110
111	foreach $itemkey ( keys %{$hashref} )
112	{
113		my $line = "";
114		my $itemvalue = $hashref->{$itemkey};
115		$line = $itemkey . "=" . $itemvalue . "\n";
116		push(@printcontent, $line);
117	}
118
119	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_hash");
120	print OUT @printcontent;
121	close( OUT);
122}
123
124sub save_array_of_hashes
125{
126	my ($savefile, $arrayref) = @_;
127
128	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); }
129
130	my @printcontent = ();
131
132	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
133	{
134		my $line = "";
135		my $hashref = ${$arrayref}[$i];
136		my $itemkey;
137
138		foreach $itemkey ( keys %{$hashref} )
139		{
140			my $itemvalue = $hashref->{$itemkey};
141			$line = $line . $itemkey . "=" . $itemvalue . "\t";
142		}
143
144		$line = $line . "\n";
145
146		push(@printcontent, $line);
147	}
148
149	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes");
150	print OUT @printcontent;
151	close( OUT);
152}
153
154sub save_array_of_hashes_modules
155{
156	my ($savefile, $arrayref) = @_;
157
158	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); }
159
160	my @printcontent = ();
161
162	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
163	{
164		my $line = "***************************************************\n";
165		my $hashref = ${$arrayref}[$i];
166		my $itemkey;
167
168		foreach $itemkey ( keys %{$hashref} )
169		{
170			my $itemvalue = $hashref->{$itemkey};
171			$line = $line . $itemkey . "=" . $itemvalue . "\n";
172		}
173
174		$line = $line . "\n";
175
176		push(@printcontent, $line);
177	}
178
179	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes");
180	print OUT @printcontent;
181	close( OUT);
182}
183
184###########################################
185# Binary file operations
186###########################################
187
188sub read_binary_file
189{
190	my ($filename) = @_;
191
192	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_binary_file : $filename"); }
193
194	my $file;
195
196	open( IN, "<$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "read_binary_file");
197	binmode IN;
198	seek IN, 0, 2;
199	my $length = tell IN;
200	seek IN, 0, 0;
201	read IN, $file, $length;
202	close IN;
203
204	return $file;
205}
206
207sub save_binary_file
208{
209	my ($file, $filename) = @_;
210
211	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_binary_file : $filename"); }
212
213	open( OUT, ">$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for writing", "save_binary_file");
214	binmode OUT;
215	print OUT $file;
216	close OUT;
217}
218
2191;
220