Debian Codegraph @

bin/marcdump

raw
1 #!/usr/bin/perl -w
2 # vi:et:sw=4 ts=4
3
4 =head1 NAME
5
6 marcdump - MARC record dump utility
7
8 =head1 SYNOPSIS
9
10 B<marcdump> [options] file(s)
11
12 =cut
13
14 use strict;
15 use integer;
16
17 use Encode;
18 use MARC::File;
19 use MARC::File::USMARC;
20 use MARC::Record;
21 use Getopt::Long;
22
23 ## flag STDOUT for UTF8
24
25 my $opt_print = 1;
26 my $opt_hex = 0;
27 my $opt_quiet = 0;
28 my $opt_stats = 1;
29 my @opt_field = ();
30 my $opt_help = 0;
31 my $opt_lif = 0;
32
33 my $rc =
34 GetOptions(
35 "version" => sub { print "$0, using MARC::Record v$MARC::Record::VERSION\n"; exit 1; },
36 "print!" => \$opt_print,
37 "hex!" => \$opt_hex,
38 "lif!" => \$opt_lif,
39 "quiet!" => \$opt_quiet,
40 "stats!" => \$opt_stats,
41 "field=s" => \@opt_field,
42 "debug!" => \$MARC::Record::DEBUG,
43 "help" => \$opt_help,
44 );
45
46 my @files = @ARGV;
47 if ( $opt_help || !@files || !$rc ) {
48 print <DATA>;
49 exit 1;
50 }
51
52 my $wants_leader = grep { /LDR/ } @opt_field;
53
54 my $class = $opt_lif ? "MARC::File::MicroLIF" : "MARC::File::USMARC";
55 eval "require $class"; # Must be quoted to get path searching
56
57 my %counts;
58 my %errors;
59 for my $filename ( @files ) {
60 $counts{$filename} = 0;
61 $errors{$filename} = 0;
62
63 warn "$filename\n" unless $opt_quiet;
64
65 my $file = $class->in( $filename ) or die $MARC::File::ERROR;
66
67 while ( my $marc = $file->next() ) {
68 ++$counts{$filename};
69 warn "$counts{$filename} records\n" if ( !$opt_quiet && ($counts{$filename} % 1000 == 0) );
70
71 if ( @opt_field ) {
72 $marc = $marc->clone( @opt_field );
73 $marc->leader('') unless $wants_leader;
74 }
75
76 if ( $opt_print ) {
77 if ( $opt_hex ) {
78 print_hex( $marc );
79 }
80 else {
81 # stifle warnings here in case there's utf8 data being printed
82 no warnings;
83 print $marc->as_formatted, "\n\n";
84 }
85 }
86
87 if ( my @warnings = $marc->warnings() ) {
88 ++$errors{$filename};
89 print join( "\n", @warnings, "" );
90 }
91 } # while
92 $file->close();
93 } # for
94
95 if ( $opt_stats ) {
96 print " Recs Errs Filename\n";
97 print "----- ----- --------\n";
98 for my $key ( sort keys %counts ) {
99 printf( "%5d %5d %s\n", $counts{$key}, $errors{$key}, $key );
100 } # for
101 } # if stats
102
103
104 sub print_hex {
105 my $marc = shift;
106 my $raw = $marc->as_usmarc();
107
108 print "\n";
109
110 my $offset = 0;
111
112 # dump the leader
113 my $leader = bytes::substr( $raw, 0, MARC::Record::LEADER_LEN );
114 my $part1 = bytes::substr( $leader, 0, 12 );
115 my $part2 = bytes::substr( $leader, 12 );
116 _hex_line_output( $offset, _to_hex($part1), _to_ascii($part1), 48 );
117 _hex_line_output( $offset+12, _to_hex($part2), _to_ascii($part2), 48 );
118 $offset += MARC::Record::LEADER_LEN;
119
120 # dump the directory. If we can't find end-of-field character that
121 # follows the directory, everything following the leader (which we
122 # have already dumped) will be dumped as part of the data section.
123 my $dir_end = bytes::index( $raw, MARC::File::USMARC::END_OF_FIELD, MARC::Record::LEADER_LEN );
124 if ( $dir_end >= 0 ) {
125 for ( my $n = $offset; $n < $dir_end; $n += MARC::File::USMARC::DIRECTORY_ENTRY_LEN ) {
126
127 my $dir_entry = bytes::substr( $raw, $n, MARC::File::USMARC::DIRECTORY_ENTRY_LEN );
128
129 my $hex = _to_hex( $dir_entry );
130
131 my $ascii =
132 bytes::substr( $dir_entry, 0, 3 ) . ' ' .
133 bytes::substr( $dir_entry, 3, 4 ) . ' ' .
134 bytes::substr( $dir_entry, 7, 5 )
135 ;
136
137 _hex_line_output( $offset, $hex, $ascii, 48 );
138 $offset += MARC::File::USMARC::DIRECTORY_ENTRY_LEN;
139 }
140
141 # dump the end-of-field character that follows the directory
142 _hex_line_output( $offset, _to_hex(MARC::File::USMARC::END_OF_FIELD), '.', 48 );
143 ++$offset;
144 }
145
146 # dump the data
147 my $data_offset = 0;
148 while ( $offset < bytes::length($raw) ) {
149 my $chunk = bytes::substr( $raw, $offset, 16 );
150 _hex_line_output( $data_offset, _to_hex($chunk), _to_ascii($chunk), 48 );
151 $offset += 16;
152 $data_offset += 16;
153 }
154 }
155
156 sub _to_ascii {
157 my $raw = shift;
158 if ( defined $raw ) {
159 $raw =~ s/[\x00-\x1f\x7f-\xff]/./g;
160 }
161 return $raw;
162 }
163
164 sub _to_hex {
165 my $raw = shift;
166 my $result = '';
167
168 if ( defined $raw ) {
169 for ( my $n = 0; $n < bytes::length($raw); $n++ ) {
170 $result .= sprintf( '%02x ', ord(bytes::substr($raw, $n, 1)) );
171 }
172 }
173 $result =~ s/ $//;
174
175 return $result;
176 }
177
178 sub _hex_line_output {
179 my $offset = shift;
180 my $hex = shift;
181 my $ascii = shift;
182 my $width = shift;
183
184 printf( "%05d: %-$width.${width}s %s\n", $offset, $hex, $ascii );
185 }
186
187
188 __END__
189 Usage: marcdump [options] file(s)
190
191 Options:
192 --[no]print Print a MicroLIF-style dump of each record
193 --[no]hex If --print active, make the output hexadecimal
194 --lif Input files are MicroLIF, not USMARC
195 --field=spec Specify a field spec to include. There may be many.
196 Examples:
197 --field=245 --field=1XX
198 --[no]quiet Print status messages
199 --[no]stats Print a statistical summary by file at the end
200 --version Print version information
201 --help Print this summary