From: Chris W. <la...@us...> - 2005-02-08 01:12:53
|
Update of /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19832/lib/OpenInteract2 Modified Files: Util.pm Log Message: add 'read_file()' (just a slurp) and 'is_same_file()' comparison Index: Util.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Util.pm,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** Util.pm 2 Feb 2005 13:12:14 -0000 1.18 --- Util.pm 8 Feb 2005 01:12:35 -0000 1.19 *************** *** 60,63 **** --- 60,75 ---- # FILE ROUTINES + sub read_file { + my ( $class, $filename ) = @_; + unless ( -f $filename ) { + oi_error "Cannot open '$filename': file does not exist"; + } + open( MOD, '<', $filename ) + || oi_error "Cannot read '$filename': $!"; + my $contents = join( '', <MOD> ); + close( MOD ); + return $contents; + } + sub read_file_lines { my ( $class, $filename ) = @_; *************** *** 104,107 **** --- 116,130 ---- } + sub is_same_file { + my ( $class, $src_file, $dest_file ) = @_; + return 0 unless ( -f $src_file and -f $dest_file ); + my $src_size = (stat( $src_file ))[7]; + my $dest_size = (stat( $dest_file ))[7]; + return 0 unless ( $src_size == $dest_size ); + my $src_md5 = $class->digest_file( $src_file ); + my $dest_md5 = $class->digest_file( $dest_file ); + return ( $src_md5 eq $dest_md5 ); + } + sub digest_file { my ( $class, $file ) = @_; *************** *** 404,407 **** --- 427,434 ---- =head1 FILE METHODS + B<read_file( $filename )> + + Slurps in C<$filename> to scalar, returns contents. + B<read_file_lines( $filename )> *************** *** 413,416 **** --- 440,449 ---- Returns content of C<$flename> evaluated as a Perl data structure. + B<is_same_file( $path_a, $path_b )> + + Returns result of comparing content in C<$path_a> and content in + C<$path_b> -- that is, if their file sizes are equal and both have the + same MD5 digest value this will return true, otherwise false. + B<digest_file( $filename )> |