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