xref: /trunk/main/solenv/bin/modules/pre2par/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 pre2par::files;
29
30use pre2par::exiter;
31
32############################################
33# File Operations
34############################################
35
36sub check_file
37{
38	my ($arg) = @_;
39
40	if(!( -f $arg ))
41	{
42		pre2par::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
43	}
44}
45
46sub read_file
47{
48	my ($localfile) = @_;
49
50	my @localfile = ();
51
52	open( IN, "<$localfile" ) || pre2par::exiter::exit_program("ERROR: Cannot open file: $localfile", "read_file");
53	while ( <IN> ) { push(@localfile, $_); }
54	close( IN );
55
56	return \@localfile;
57}
58
59###########################################
60# Saving files, arrays and hashes
61###########################################
62
63sub save_file
64{
65	my ($savefile, $savecontent) = @_;
66	if (-f $savefile) { unlink $savefile };
67	if (-f $savefile) { pre2par::exiter::exit_program("ERROR: Cannot delete existing file: $savefile", "save_file"); };
68	open( OUT, ">$savefile" );
69	print OUT @{$savecontent};
70	close( OUT);
71	if (! -f $savefile) { pre2par::exiter::exit_program("ERROR: Cannot write file: $savefile", "save_file"); }
72}
73
74sub save_hash
75{
76	my ($savefile, $hashref) = @_;
77
78	my @printcontent = ();
79
80	my ($itemkey, $itemvalue, $line);
81
82	foreach $itemkey ( keys %{$hashref} )
83	{
84		$line = "";
85		$itemvalue = $hashref->{$itemkey};
86		$line = $itemkey . "=" . $itemvalue . "\n";
87		push(@printcontent, $line);
88	}
89
90	open( OUT, ">$savefile" );
91	print OUT @printcontent;
92	close( OUT);
93}
94
95sub save_array_of_hashes
96{
97	my ($savefile, $arrayref) = @_;
98
99	my @printcontent = ();
100
101	my ($itemkey, $itemvalue, $line, $hashref);
102
103	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
104	{
105		$line = "";
106		$hashref = ${$arrayref}[$i];
107
108		foreach $itemkey ( keys %{$hashref} )
109		{
110			$itemvalue = $hashref->{$itemkey};
111
112			$line = $line . $itemkey . "=" . $itemvalue . "\t";
113		}
114
115		$line = $line . "\n";
116
117		push(@printcontent, $line);
118	}
119
120	open( OUT, ">$savefile" );
121	print OUT @printcontent;
122	close( OUT);
123}
124
1251;
126