/usr/share/perl5/MARC/File/USMARC.pm is in libmarc-record-perl 2.0.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | package MARC::File::USMARC;
=head1 NAME
MARC::File::USMARC - USMARC-specific file handling
=cut
use strict;
use warnings;
use integer;
use vars qw( $ERROR );
use MARC::File::Encode qw( marc_to_utf8 );
use MARC::File;
use vars qw( @ISA ); @ISA = qw( MARC::File );
use MARC::Record qw( LEADER_LEN );
use MARC::Field;
use constant SUBFIELD_INDICATOR => "\x1F";
use constant END_OF_FIELD => "\x1E";
use constant END_OF_RECORD => "\x1D";
use constant DIRECTORY_ENTRY_LEN => 12;
=head1 SYNOPSIS
use MARC::File::USMARC;
my $file = MARC::File::USMARC->in( $filename );
while ( my $marc = $file->next() ) {
# Do something
}
$file->close();
undef $file;
=head1 EXPORT
None.
=head1 METHODS
=cut
sub _next {
my $self = shift;
my $fh = $self->{fh};
my $reclen;
return if eof($fh);
local $/ = END_OF_RECORD;
my $usmarc = <$fh>;
# remove illegal garbage that sometimes occurs between records
$usmarc =~ s/^[ \x00\x0a\x0d\x1a]+//;
return $usmarc;
}
=head2 decode( $string [, \&filter_func ] )
Constructor for handling data from a USMARC file. This function takes care of
all the tag directory parsing & mangling.
Any warnings or coercions can be checked in the C<warnings()> function.
The C<$filter_func> is an optional reference to a user-supplied function
that determines on a tag-by-tag basis if you want the tag passed to it
to be put into the MARC record. The function is passed the tag number
and the raw tag data, and must return a boolean. The return of a true
value tells MARC::File::USMARC::decode that the tag should get put into
the resulting MARC record.
For example, if you only want title and subject tags in your MARC record,
try this:
sub filter {
my ($tagno,$tagdata) = @_;
return ($tagno == 245) || ($tagno >= 600 && $tagno <= 699);
}
my $marc = MARC::File::USMARC->decode( $string, \&filter );
Why would you want to do such a thing? The big reason is that creating
fields is processor-intensive, and if your program is doing read-only
data analysis and needs to be as fast as possible, you can save time by
not creating fields that you'll be ignoring anyway.
Another possible use is if you're only interested in printing certain
tags from the record, then you can filter them when you read from disc
and not have to delete unwanted tags yourself.
=cut
sub decode {
my $text;
my $location = '';
## decode can be called in a variety of ways
## $object->decode( $string )
## MARC::File::USMARC->decode( $string )
## MARC::File::USMARC::decode( $string )
## this bit of code covers all three
my $self = shift;
if ( ref($self) =~ /^MARC::File/ ) {
$location = 'in record '.$self->{recnum};
$text = shift;
} else {
$location = 'in record 1';
$text = $self=~/MARC::File/ ? shift : $self;
}
my $filter_func = shift;
# ok this the empty shell we will fill
my $marc = MARC::Record->new();
# Check for an all-numeric record length
($text =~ /^(\d{5})/)
or return $marc->_warn( "Record length \"", substr( $text, 0, 5 ), "\" is not numeric $location" );
my $reclen = $1;
my $realLength = bytes::length( $text );
$marc->_warn( "Invalid record length $location: Leader says $reclen " .
"bytes but it's actually $realLength" ) unless $reclen == $realLength;
(substr($text, -1, 1) eq END_OF_RECORD)
or $marc->_warn( "Invalid record terminator $location" );
$marc->leader( substr( $text, 0, LEADER_LEN ) );
# bytes 12 - 16 of leader give offset to the body of the record
my $data_start = 0 + bytes::substr( $text, 12, 5 );
# immediately after the leader comes the directory (no separator)
my $dir = substr( $text, LEADER_LEN, $data_start - LEADER_LEN - 1 ); # -1 to allow for \x1e at end of directory
# character after the directory must be \x1e
(substr($text, $data_start-1, 1) eq END_OF_FIELD)
or $marc->_warn( "No directory found $location" );
# all directory entries 12 bytes long, so length % 12 must be 0
(length($dir) % DIRECTORY_ENTRY_LEN == 0)
or $marc->_warn( "Invalid directory length $location" );
# go through all the fields
my $nfields = length($dir)/DIRECTORY_ENTRY_LEN;
for ( my $n = 0; $n < $nfields; $n++ ) {
my ( $tagno, $len, $offset ) = unpack( "A3 A4 A5", substr($dir, $n*DIRECTORY_ENTRY_LEN, DIRECTORY_ENTRY_LEN) );
# Check directory validity
($tagno =~ /^[0-9A-Za-z]{3}$/)
or $marc->_warn( "Invalid tag in directory $location: \"$tagno\"" );
($len =~ /^\d{4}$/)
or $marc->_warn( "Invalid length in directory $location tag $tagno: \"$len\"" );
($offset =~ /^\d{5}$/)
or $marc->_warn( "Invalid offset in directory $location tag $tagno: \"$offset\"" );
($offset + $len <= $reclen)
or $marc->_warn( "Directory entry $location runs off the end of the record tag $tagno" );
my $tagdata = bytes::substr( $text, $data_start+$offset, $len );
# if utf8 the we encode the string as utf8
if ( $marc->encoding() eq 'UTF-8' ) {
$tagdata = marc_to_utf8( $tagdata );
}
$marc->_warn( "Invalid length in directory for tag $tagno $location" )
unless ( $len == bytes::length($tagdata) );
if ( substr($tagdata, -1, 1) eq END_OF_FIELD ) {
# get rid of the end-of-tag character
chop $tagdata;
--$len;
} else {
$marc->_warn( "field does not end in end of field character in tag $tagno $location" );
}
warn "Specs: ", join( "|", $tagno, $len, $offset, $tagdata ), "\n" if $MARC::Record::DEBUG;
if ( $filter_func ) {
next unless $filter_func->( $tagno, $tagdata );
}
if ( MARC::Field->is_controlfield_tag($tagno) ) {
$marc->append_fields( MARC::Field->new( $tagno, $tagdata ) );
} else {
my @subfields = split( SUBFIELD_INDICATOR, $tagdata );
my $indicators = shift @subfields;
my ($ind1, $ind2);
if ( length( $indicators ) > 2 or length( $indicators ) == 0 ) {
$marc->_warn( "Invalid indicators \"$indicators\" forced to blanks $location for tag $tagno\n" );
($ind1,$ind2) = (" ", " ");
} else {
$ind1 = substr( $indicators,0, 1 );
$ind2 = substr( $indicators,1, 1 );
}
# Split the subfield data into subfield name and data pairs
my @subfield_data;
for ( @subfields ) {
if ( length > 0 ) {
push( @subfield_data, substr($_,0,1),substr($_,1) );
} else {
$marc->_warn( "Entirely empty subfield found in tag $tagno" );
}
}
if ( !@subfield_data ) {
$marc->_warn( "no subfield data found $location for tag $tagno" );
next;
}
my $field = MARC::Field->new($tagno, $ind1, $ind2, @subfield_data );
if ( $field->warnings() ) {
$marc->_warn( $field->warnings() );
}
$marc->append_fields( $field );
}
} # looping through all the fields
return $marc;
}
=head2 update_leader()
If any changes get made to the MARC record, the first 5 bytes of the
leader (the length) will be invalid. This function updates the
leader with the correct length of the record as it would be if
written out to a file.
=cut
sub update_leader {
my $self = shift;
my (undef,undef,$reclen,$baseaddress) = $self->_build_tag_directory();
$self->_set_leader_lengths( $reclen, $baseaddress );
}
=head2 _build_tag_directory()
Function for internal use only: Builds the tag directory that gets
put in front of the data in a MARC record.
Returns two array references, and two lengths: The tag directory, and the data fields themselves,
the length of all data (including the Leader that we expect will be added),
and the size of the Leader and tag directory.
=cut
sub _build_tag_directory {
my $marc = shift;
$marc = shift if (ref($marc)||$marc) =~ /^MARC::File/;
die "Wanted a MARC::Record but got a ", ref($marc) unless ref($marc) eq "MARC::Record";
my @fields;
my @directory;
my $dataend = 0;
for my $field ( $marc->fields() ) {
# Dump data into proper format
my $str = $field->as_usmarc;
push( @fields, $str );
# Create directory entry
my $len = bytes::length( $str );
my $direntry = sprintf( "%03s%04d%05d", $field->tag, $len, $dataend );
push( @directory, $direntry );
$dataend += $len;
}
my $baseaddress =
LEADER_LEN + # better be 24
( @directory * DIRECTORY_ENTRY_LEN ) +
# all the directory entries
1; # end-of-field marker
my $total =
$baseaddress + # stuff before first field
$dataend + # Length of the fields
1; # End-of-record marker
return (\@fields, \@directory, $total, $baseaddress);
}
=head2 encode()
Returns a string of characters suitable for writing out to a USMARC file,
including the leader, directory and all the fields.
=cut
sub encode {
my $marc = shift;
$marc = shift if (ref($marc)||$marc) =~ /^MARC::File/;
my ($fields,$directory,$reclen,$baseaddress) = _build_tag_directory($marc);
$marc->set_leader_lengths( $reclen, $baseaddress );
# Glomp it all together
return join("",$marc->leader, @$directory, END_OF_FIELD, @$fields, END_OF_RECORD);
}
1;
__END__
=head1 RELATED MODULES
L<MARC::Record>
=head1 TODO
Make some sort of autodispatch so that you don't have to explicitly
specify the MARC::File::X subclass, sort of like how DBI knows to
use DBD::Oracle or DBD::Mysql.
Create a toggle-able option to check inside the field data for
end of field characters. Presumably it would be good to have
it turned on all the time, but it's nice to be able to opt out
if you don't want to take the performance hit.
=head1 LICENSE
This code may be distributed under the same terms as Perl itself.
Please note that these modules are not products of or supported by the
employers of the various contributors to the code.
=head1 AUTHOR
Andy Lester, C<< <andy@petdance.com> >>
=cut
|