xref: /aoo42x/main/solenv/bin/modules/pre2par/files.pm (revision 9780544f)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package pre2par::files;
25
26use pre2par::exiter;
27
28############################################
29# File Operations
30############################################
31
32sub check_file
33{
34	my ($arg) = @_;
35
36	if(!( -f $arg ))
37	{
38		pre2par::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
39	}
40}
41
42sub read_file
43{
44	my ($localfile) = @_;
45
46	my @localfile = ();
47
48	open( IN, "<$localfile" ) || pre2par::exiter::exit_program("ERROR: Cannot open file: $localfile", "read_file");
49	while ( <IN> ) { push(@localfile, $_); }
50	close( IN );
51
52	return \@localfile;
53}
54
55###########################################
56# Saving files, arrays and hashes
57###########################################
58
59sub save_file
60{
61	my ($savefile, $savecontent) = @_;
62	if (-f $savefile) { unlink $savefile };
63	if (-f $savefile) { pre2par::exiter::exit_program("ERROR: Cannot delete existing file: $savefile", "save_file"); };
64	open( OUT, ">$savefile" );
65	print OUT @{$savecontent};
66	close( OUT);
67	if (! -f $savefile) { pre2par::exiter::exit_program("ERROR: Cannot write file: $savefile", "save_file"); }
68}
69
70sub save_hash
71{
72	my ($savefile, $hashref) = @_;
73
74	my @printcontent = ();
75
76	my ($itemkey, $itemvalue, $line);
77
78	foreach $itemkey ( keys %{$hashref} )
79	{
80		$line = "";
81		$itemvalue = $hashref->{$itemkey};
82		$line = $itemkey . "=" . $itemvalue . "\n";
83		push(@printcontent, $line);
84	}
85
86	open( OUT, ">$savefile" );
87	print OUT @printcontent;
88	close( OUT);
89}
90
91sub save_array_of_hashes
92{
93	my ($savefile, $arrayref) = @_;
94
95	my @printcontent = ();
96
97	my ($itemkey, $itemvalue, $line, $hashref);
98
99	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
100	{
101		$line = "";
102		$hashref = ${$arrayref}[$i];
103
104		foreach $itemkey ( keys %{$hashref} )
105		{
106			$itemvalue = $hashref->{$itemkey};
107
108			$line = $line . $itemkey . "=" . $itemvalue . "\t";
109		}
110
111		$line = $line . "\n";
112
113		push(@printcontent, $line);
114	}
115
116	open( OUT, ">$savefile" );
117	print OUT @printcontent;
118	close( OUT);
119}
120
1211;
122