Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

MARC::File::HUNMARC, HUNMARC-specific file handling for the marcpm project

View HUNMARC.pm
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
package MARC::File::HUNMARC;
 
=head1 NAME
MARC::File::HUNMARC - HUNMARC-specific file handling
=cut
 
use strict;
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 => "\n"; #"\x1E";
use constant END_OF_RECORD => "\n\n"; #"\x1D";
use constant DIRECTORY_ENTRY_LEN => 12;
 
=head1 SYNOPSIS
use MARC::File::HUNMARC;
my $file = MARC::File::HUNMARC->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);
 
my $usmarc;
my $line;
while ($line = <$fh>)
{
$line =~ s/[\r\n]+$//;
last if (!$line);
$usmarc .= $line.END_OF_FIELD;
}
 
# remove illegal garbage that sometimes occurs between records
$usmarc =~ s/^[ \x00\x1a]+//;
 
return $usmarc;
}
 
=head2 decode( $string [, \&filter_func ] )
Constructor for handling data from a HUNMARC 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::HUNMARC::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::HUNMARC->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::HUNMARC->decode( $string )
## MARC::File::HUNMARC::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();
 
# HUNMARC specific
$text = substr($text, 4) if ($text =~ /^000 \d{5}/);
 
# Check for an all-numeric record length
($text =~ /^(\d{5})/)
or return $marc->_warn( "Record length \"", substr( $text, 0, 5 ), "\" is not numeric $location" );
 
$marc->leader( substr( $text, 0, LEADER_LEN ) );
 
# go through all the fields
my @lines = split END_OF_FIELD, $text;
shift @lines;
 
foreach my $line (@lines)
{
# HUNMARC specific
if ($line =~ m/^(00\d) (.*)$/)
{
my $field = MARC::Field->new($1, $2);
$field->{_data} = $2; # strange but necessary
$marc->append_fields($field);
next;
}
 
$line =~ m/^(\d\d\d) (.)(.) .(.*)$/;
my ($tagno, $ind1, $ind2, $tagdata) = ($1, $2, $3, $4);
 
# if utf8 the we encode the string as utf8
if ( $marc->encoding() eq 'UTF-8' ) {
$tagdata = marc_to_utf8( $tagdata );
}
 
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 );
 
# 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 HUNMARC 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
After the original USMARC.pm by Andy Lester, C<< <andy@petdance.com> >>
adapted to HUNMARC by David Takacs, C<< <takdavid@gmail.com> >>
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.