1 #!/usr/local/bin/perl -ws
5 use Fcntl qw( :seek ) ;
6 use File::ReadBackwards ;
9 use vars qw( $opt_v ) ;
11 my $file = 'bw.data' ;
13 my $is_crlf = ( $^O =~ /win32/i || $^O =~ /vms/i ) ;
16 my @nl_data = init_data( "\n" ) ;
17 plan( tests => 10 * @nl_data + 1 ) ;
18 test_read_backwards( \@nl_data ) ;
21 my @crlf_data = init_data( "\015\012" ) ;
22 test_read_backwards( \@crlf_data, "\015\012" ) ;
31 my ( $rec_sep ) = @_ ;
33 return map { ( my $data = $_ ) =~ s/RS/$rec_sep/g ; $data }
44 'a' x (8 * 1024) . '0',
45 '0' x (8 * 1024) . '0',
47 join( 'RS', '00' .. '99', '' ),
48 join( 'RS', '00' .. '99' ),
49 join( 'RS', '0000' .. '9999', '' ),
50 join( 'RS', '0000' .. '9999' ),
54 sub test_read_backwards {
56 my( $data_list_ref, $rec_sep ) = @_ ;
58 foreach my $data ( @$data_list_ref ) {
60 # write the test data to a file in text or bin_mode
62 if ( defined $rec_sep ) {
64 write_bin_file( $file, $data ) ;
66 # print "cnt: ${\scalar @rev_file_lines}\n" ;
70 write_file( $file, $data ) ;
74 test_data( $rec_sep ) ;
76 test_tell_handle( $rec_sep ) ;
84 # slurp in the file and reverse the list of lines to get golden data
86 my @rev_file_lines = reverse read_bin_file( $file ) ;
88 # convert CR/LF to \n if needed - based on OS or we are testing CR/LF
90 if ( $is_crlf || $rec_sep && $rec_sep eq "\015\012" ) {
91 s/\015\012\z/\n/ for @rev_file_lines ;
94 # open the file with backwards and read in the lines
96 my $bw = File::ReadBackwards->new( $file, $rec_sep ) or
97 die "can't open $file: $!" ;
99 my( @bw_file_lines ) ;
102 my $line = $bw->readline() ;
103 last unless defined( $line ) ;
104 push( @bw_file_lines, $line ) ;
106 $line = $bw->getline() ;
107 last unless defined( $line ) ;
108 push( @bw_file_lines, $line ) ;
111 # while ( defined( my $line = $bw->readline() ) ) {
112 # push( @bw_file_lines, $line) ;
115 # see if we close cleanly
117 ok( $bw->close(), 'close' ) ;
119 # compare the golden lines to the backwards lines
121 if ( eq_array( \@rev_file_lines, \@bw_file_lines ) ) {
127 # test failed so dump the different lines if verbose
131 return unless $opt_v ;
133 print "[$rev_file_lines[0]]\n" ;
134 print unpack( 'H*', $rev_file_lines[0] ), "\n" ;
135 print unpack( 'H*', $bw_file_lines[0] ), "\n" ;
137 #print "REV ", unpack( 'H*', join '',@rev_file_lines ), "\n" ;
138 #print "BW ", unpack( 'H*', join '',@bw_file_lines ), "\n" ;
142 sub test_tell_handle {
144 my( $rec_sep ) = @_ ;
146 # open the file backwards again to test tell and get_handle methods
148 my $bw = File::ReadBackwards->new( $file, $rec_sep ) or
149 die "can't open $file: $!" ;
151 # read the last line in
153 my $bw_line = $bw->readline() ;
155 # get the current seek position
157 my $pos = $bw->tell() ;
159 #print "BW pos = $pos\n" ;
163 ok( 1, "skip tell - at eof" ) ;
164 ok( 1, "skip get_handle - at eof" ) ;
168 # save the current $/ so we can reassign it if it $rec_sep isn't set
170 my $old_rec_sep = $/ ;
171 local $/ = $rec_sep || $old_rec_sep ;
173 # open a new regular file and seek to this spot.
175 open FH, $file or die "tell open $!" ;
176 seek FH, $pos, SEEK_SET or die "tell seek $!" ;
178 # read in the next line and clean up the ending CR/LF
181 $fw_line =~ s/\015\012\z/\n/ ;
183 # print "BW [", unpack( 'H*', $bw_line ),
184 # "] TELL [", unpack( 'H*', $fw_line), "]\n" if $bw_line ne $fw_line ;
186 # compare the backwards and forwards lines
188 is ( $bw_line, $fw_line, "tell check" ) ;
190 # get the handle and seek to the current spot
192 my $fh = $bw->get_handle() ;
194 # read in the next line and clean up the ending CR/LF
196 my $fh_line = <$fh> ;
197 $fh_line =~ s/\015\012\z/\n/ ;
199 # print "BW [", unpack( 'H*', $bw_line ),
200 # "] HANDLE [", unpack( 'H*', $fh_line), "]\n" if $bw_line ne $fh_line ;
202 # compare the backwards and forwards lines
204 is ( $bw_line, $fh_line, "get_handle" ) ;
207 ok( $bw->close(), 'close2' ) ;
213 write_file( $file, <<BW ) ;
218 my $bw = File::ReadBackwards->new( $file ) or
219 die "can't open $file: $!" ;
221 my $line = $bw->readline() ;
225 if ( $bw->readline() ) {
236 my( $file_name ) = shift ;
240 open( FH, $file_name ) || carp "can't open $file_name $!" ;
242 local( $/ ) unless wantarray ;
247 # utility sub to write a file. takes a file name and a list of strings
251 my( $file_name ) = shift ;
255 open( FH, ">$file_name" ) || carp "can't create $file_name $!" ;
262 my( $file_name ) = shift ;
265 open( FH, $file_name ) || carp "can't open $file_name $!" ;
268 local( $/ ) = shift if @_ ;
270 local( $/ ) unless wantarray ;
275 # utility sub to write a file. takes a file name and a list of strings
279 my( $file_name ) = shift ;
282 open( FH, ">$file_name" ) || carp "can't create $file_name $!" ;