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