1# -*- cperl -*- 2# Copyright (c) 2004-2008 MySQL AB, 2008 Sun Microsystems, Inc. 3# Use is subject to license terms. 4# 5# This program is free software; you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; version 2 of the License. 8# 9# This program is distributed in the hope that it will be useful, 10# but WITHOUT ANY WARRANTY; without even the implied warranty of 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12# GNU General Public License for more details. 13# 14# You should have received a copy of the GNU General Public License 15# along with this program; if not, write to the Free Software 16# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA 17 18# This is a library file used by the Perl version of mysql-test-run, 19# and is part of the translation of the Bourne shell script with the 20# same name. 21 22use strict; 23use Carp; 24use My::Platform; 25 26sub mtr_fromfile ($); 27sub mtr_tofile ($@); 28sub mtr_tonewfile($@); 29sub mtr_appendfile_to_file ($$); 30sub mtr_grab_file($); 31sub mtr_printfile($); 32sub mtr_lastlinesfromfile ($$); 33 34# Read a whole file, stripping leading and trailing whitespace. 35sub mtr_fromfile ($) { 36 my $file= shift; 37 38 open(FILE,"<",$file) or mtr_error("can't open file \"$file\": $!"); 39 my $text= join('', <FILE>); 40 close FILE; 41 $text =~ s/^\s+//; # Remove starting space, incl newlines 42 $text =~ s/\s+$//; # Remove ending space, incl newlines 43 return $text; 44} 45 46 47sub mtr_tofile ($@) { 48 my $file= shift; 49 my $fh= open_for_append $file; 50 mtr_error("can't open file \"$file\": $!") unless defined($fh); 51 print $fh join("", @_); 52 close $fh; 53} 54 55 56sub mtr_tonewfile ($@) { 57 my $file= shift; 58 59 open(FILE,">",$file) or mtr_error("can't open file \"$file\": $!"); 60 print FILE join("", @_); 61 close FILE; 62} 63 64 65sub mtr_appendfile_to_file ($$) { 66 my $from_file= shift; 67 my $to_file= shift; 68 69 open(TOFILE,">>",$to_file) or mtr_error("can't open file \"$to_file\": $!"); 70 open(FROMFILE,"<",$from_file) 71 or mtr_error("can't open file \"$from_file\": $!"); 72 print TOFILE while (<FROMFILE>); 73 close FROMFILE; 74 close TOFILE; 75} 76 77 78# Read a whole file verbatim. 79sub mtr_grab_file($) { 80 my $file= shift; 81 open(FILE, '<', $file) 82 or return undef; 83 local $/= undef; 84 my $data= scalar(<FILE>); 85 close FILE; 86 return $data; 87} 88 89 90# Print the file to STDOUT 91sub mtr_printfile($) { 92 my $file= shift; 93 open(FILE, '<', $file) 94 or warn $!; 95 print while(<FILE>); 96 close FILE; 97 return; 98} 99 100sub mtr_lastlinesfromfile ($$) { 101 croak "usage: mtr_lastlinesfromfile(file,numlines)" unless (@_ == 2); 102 my ($file, $num_lines)= @_; 103 my $text; 104 open(FILE,"<",$file) or mtr_error("can't open file \"$file\": $!"); 105 my @lines= reverse <FILE>; 106 close FILE; 107 my $size= scalar(@lines); 108 $num_lines= $size unless ($size >= $num_lines); 109 return join("", reverse(splice(@lines, 0, $num_lines))); 110} 111 1121; 113