--- trunk/IsisDB.pm 2004/12/29 15:17:59 8 +++ trunk/IsisDB.pm 2004/12/29 17:03:52 11 @@ -7,7 +7,7 @@ BEGIN { use Exporter (); use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); - $VERSION = 0.02; + $VERSION = 0.03; @ISA = qw (Exporter); #Give a hoot don't pollute, do not export more than needed by default @EXPORT = qw (); @@ -22,16 +22,29 @@ =head1 SYNOPSIS - use IsisDB + use IsisDB; + my $isis = new IsisDB( isisdb => './cds/cds', ); + for(my $mfn = 1; $mfn <= $isis->{'maxmfn'}; $mfn++) { + print $isis->to_ascii($mfn),"\n"; + } + =head1 DESCRIPTION This module will read CDS/ISIS databases and create hash values out of it. It can be used as perl-only alternative to OpenIsis module. +This will module will always be slower that OpenIsis module which use C +library. However, since it's written in perl, it's platform independent (so +you don't need C compiler), and can be easily modified. + +Unique feature of this module is ability to C records. +It will also skip zero sized fields (OpenIsis has a bug in XS bindings, so +fields which are zero sized will be filled with random junk from memory). + =head1 METHODS =cut @@ -50,14 +63,6 @@ # some binary reads # -sub Read32 { - my $self = shift; - - my $f = shift || die "Read32 needs file handle"; - read($$f,$b,4) || die "can't read 4 bytes from $$f from position ".tell($f); - return unpack("l",$b); -} - =head2 new Open CDS/ISIS database @@ -66,6 +71,7 @@ isisdb => './cds/cds', read_fdt => 1, debug => 1, + include_deleted => 1, ); Options are described below: @@ -86,6 +92,10 @@ Dump a C of debugging output. +=item include_deleted + +Don't skip logically deleted records in ISIS. + =back It will also set C<$isis-E{'maxmfn'}> which is maximum MFN stored in database. @@ -97,9 +107,11 @@ my $self = {}; bless($self, $class); - $self->{isisdb} = {@_}->{isisdb} || croak "new needs database name as argument!"; + croak "new needs database name (isisdb) as argument!" unless ({@_}->{isisdb}); - $self->{debug} = {@_}->{debug}; + foreach my $v (qw{isisdb debug include_deleted}) { + $self->{$v} = {@_}->{$v}; + } # if you want to read .FDT file use read_fdt argument when creating class! if ({@_}->{read_fdt} && -e $self->{isisdb}.".FDT") { @@ -140,7 +152,11 @@ # NXTMFP offset to next available position in last block # MFTYPE always 0 for user db file (1 for system) seek(fileMST,4,0); - $self->{'NXTMFN'}=$self->Read32(\*fileMST) || carp "NXTNFN is zero"; + + my $buff; + + read(fileMST, $buff, 4); + $self->{'NXTMFN'}=unpack("l",$buff) || carp "NXTNFN is zero"; # save maximum MFN $self->{'maxmfn'} = $self->{'NXTMFN'} - 1; @@ -173,13 +189,14 @@ my $buff = shift || return; my @arr = unpack("ssssssllls", $buff); + print "unpack_cnt: ",join(" ",@arr),"\n" if ($self->{'debug'}); + my $IDTYPE = shift @arr; foreach (@flds) { $self->{$IDTYPE}->{$_} = abs(shift @arr); } } - my $buff; read(fileCNT, $buff, 26); $self->unpack_cnt($buff); @@ -223,8 +240,11 @@ print "seeking to $mfnpos in file '$self->{isisdb}.XRF'\n" if ($self->{debug}); seek($self->{'fileXRF'},$mfnpos,0); + my $buff; + # read XRFMFB abd XRFMFP - my $pointer=$self->Read32(\*{$self->{'fileXRF'}}); + read($self->{'fileXRF'}, $buff, 4); + my $pointer=unpack("l",$buff) || carp "pointer is null"; my $XRFMFB = int($pointer/2048); my $XRFMFP = $pointer - ($XRFMFB*2048); @@ -249,7 +269,8 @@ seek($self->{'fileMST'},$offset4,0); - my $value=$self->Read32(\*{$self->{'fileMST'}}); + read($self->{'fileMST'}, $buff, 4); + my $value=unpack("l",$buff); if ($value!=$mfn) { print ("Error: The MFN:".$mfn." is not found in MST(".$value.")"); @@ -263,13 +284,20 @@ # $NVF=$self->Read16($fileMST); # $STATUS=$self->Read16($fileMST); - my $buff; read($self->{'fileMST'}, $buff, 14); my ($MFRL,$MFBWB,$MFBWP,$BASE,$NVF,$STATUS) = unpack("slssss", $buff); print "MFRL: $MFRL MFBWB: $MFBWB MFBWP: $MFBWP BASE: $BASE NVF: $NVF STATUS: $STATUS\n" if ($self->{debug}); + # delete old record + delete $self->{record}; + + if (! $self->{'include_deleted'} && $MFRL < 0) { + print "## logically deleted record $mfn, skipping...\n" if ($self->{debug}); + return; + } + # Get Directory Format my @FieldPOS; @@ -309,20 +337,16 @@ # Get Variable Fields - delete $self->{record}; - read($self->{'fileMST'},$buff,$fld_len); for (my $i = 0 ; $i < $NVF ; $i++) { + # skip zero-sized fields + next if ($FieldLEN[$i] == 0); + push @{$self->{record}->{$FieldTAG[$i]}}, substr($buff,$FieldPOS[$i],$FieldLEN[$i]); } close(fileMST); - # The record is marked for deletion - if ($STATUS==1) { - return -1; - } - print Dumper($self) if ($self->{debug}); return $self->{'record'};