0ee84bdba5a99ac348baf500e421f63aad3ad9d2
[p5sagit/p5-mst-13.2.git] / lib / File / Compare.pm
1 package File::Compare;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $Too_Big *FROM *TO);
5
6 require Exporter;
7 use Carp;
8
9 $VERSION = '1.1002';
10 @ISA = qw(Exporter);
11 @EXPORT = qw(compare);
12 @EXPORT_OK = qw(cmp compare_text);
13
14 $Too_Big = 1024 * 1024 * 2;
15
16 sub VERSION {
17     # Version of File::Compare
18     return $File::Compare::VERSION;
19 }
20
21 sub compare {
22     croak("Usage: compare( file1, file2 [, buffersize]) ")
23       unless(@_ == 2 || @_ == 3);
24
25     my ($from,$to,$size) = @_;
26     my $text_mode = defined($size) && $size < 0;
27
28     my ($fromsize,$closefrom,$closeto);
29     local (*FROM, *TO);
30
31     croak("from undefined") unless (defined $from);
32     croak("to undefined") unless (defined $to);
33
34     if (ref($from) && 
35         (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) {
36         *FROM = *$from;
37     } elsif (ref(\$from) eq 'GLOB') {
38         *FROM = $from;
39     } else {
40         open(FROM,"<$from") or goto fail_open1;
41         unless ($text_mode) {
42             binmode FROM;
43             $fromsize = -s FROM;
44         }
45         $closefrom = 1;
46     }
47
48     if (ref($to) &&
49         (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) {
50         *TO = *$to;
51     } elsif (ref(\$to) eq 'GLOB') {
52         *TO = $to;
53     } else {
54         open(TO,"<$to") or goto fail_open2;
55         binmode TO unless $text_mode;
56         $closeto = 1;
57     }
58
59     if (!$text_mode && $closefrom && $closeto) {
60         # If both are opened files we know they differ if their size differ
61         goto fail_inner if $fromsize != -s TO;
62     }
63
64     if ($text_mode) {
65         local $/ = "\n";
66         my ($fline,$tline);
67         while (defined($fline = <FROM>)) {
68             unless (defined($tline = <TO>) && $fline eq $tline) {
69                 goto fail_inner;
70             }
71         }
72         goto fail_inner if defined($tline = <TO>);
73     }
74     else {
75         unless (defined($size) && $size > 0) {
76             $size = $fromsize;
77             $size = 1024 if $size < 512;
78             $size = $Too_Big if $size > $Too_Big;
79         }
80
81         my ($fr,$tr,$fbuf,$tbuf);
82         $fbuf = $tbuf = '';
83         while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
84             unless (defined($tr = read(TO,$tbuf,$fr)) && $tbuf eq $fbuf) {
85                 goto fail_inner;
86             }
87         }
88         goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0;
89     }
90
91     close(TO) || goto fail_open2 if $closeto;
92     close(FROM) || goto fail_open1 if $closefrom;
93
94     return 0;
95     
96   # All of these contortions try to preserve error messages...
97   fail_inner:
98     close(TO) || goto fail_open2 if $closeto;
99     close(FROM) || goto fail_open1 if $closefrom;
100
101     return 1;
102
103   fail_open2:
104     if ($closefrom) {
105         my $status = $!;
106         $! = 0;
107         close FROM;
108         $! = $status unless $!;
109     }
110   fail_open1:
111     return -1;
112 }
113
114 *cmp = \&compare;
115
116 # Using a negative buffer size puts compare into text_mode
117 sub compare_text { compare(@_[0..1], -1) }
118
119 1;
120
121 __END__
122
123 =head1 NAME
124
125 File::Compare - Compare files or filehandles
126
127 =head1 SYNOPSIS
128
129         use File::Compare;
130
131         if (compare("file1","file2") == 0) {
132             print "They're equal\n";
133         }
134
135 =head1 DESCRIPTION
136
137 The File::Compare::compare function compares the contents of two
138 sources, each of which can be a file or a file handle.  It is exported
139 from File::Compare by default.
140
141 File::Compare::cmp is a synonym for File::Compare::compare.  It is
142 exported from File::Compare only by request.
143
144 File::Compare::compare_text does a line by line comparison of the two
145 files. It stops as soon as a difference is detected.
146
147 =head1 RETURN
148
149 File::Compare::compare return 0 if the files are equal, 1 if the
150 files are unequal, or -1 if an error was encountered.
151
152 =head1 AUTHOR
153
154 File::Compare was written by Nick Ing-Simmons.
155 Its original documentation was written by Chip Salzenberg.
156
157 =cut
158