--- trunk/lib/WebPAC/Normalize.pm 2006/01/08 20:32:06 368 +++ trunk/lib/WebPAC/Normalize.pm 2006/01/08 21:16:27 371 @@ -308,7 +308,7 @@ sub parse { my $self = shift; - my ($rec, $format_utf8, $i) = @_; + my ($rec, $format_utf8, $i, $rec_size) = @_; return if (! $format_utf8); @@ -357,7 +357,7 @@ } my $found = 0; - my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); + my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found,$rec_size); if ($found) { $found_any->{$fld_type} += $found; @@ -431,8 +431,11 @@ my $i = 0; my @arr; - while (my $v = $self->parse($rec,$format_utf8,$i++)) { + my $rec_size = { '_' => '_' }; + + while (my $v = $self->parse($rec,$format_utf8,$i++,\$rec_size)) { push @arr, $v; + warn "parse rec_size = ", Dumper($rec_size); } $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); @@ -462,6 +465,11 @@ This method will automatically decode UTF-8 string to local code page if needed. +There is optional parametar C<$record_size> which can be used to get sizes of +all C combinations in this format. + + my $text = $webpac->fill_in($rec,'got: v900^a v900^x',0,\$rec_size); + =cut sub fill_in { @@ -469,10 +477,13 @@ my $log = $self->_get_logger(); - my $rec = shift || $log->logconfess("need data record"); - my $format = shift || $log->logconfess("need format to parse"); + my ($rec,$format,$i,$rec_size) = @_; + + $log->logconfess("need data record") unless ($rec); + $log->logconfess("need format to parse") unless($format); + # iteration (for repeatable fields) - my $i = shift || 0; + $i ||= 0; $log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); @@ -496,12 +507,12 @@ # do actual replacement of placeholders # repeatable fields - if ($format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges) { + if ($format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found,$rec_size)/ges) { $just_single = 0; } # non-repeatable fields - if ($format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges) { + if ($format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found,$rec_size)/ges) { return if ($i > 0 && $just_single); } @@ -558,8 +569,11 @@ my $i = 0; my @arr; - while (my $v = $self->fill_in($rec,$format_utf8,$i++)) { + my $rec_size; + + while (my $v = $self->fill_in($rec,$format_utf8,$i,\$rec_size)) { push @arr, $v; + warn "rec_size = ", Dumper($rec_size); } $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); @@ -572,7 +586,7 @@ Returns value from record. - my $text = $self->get_data(\$rec,$f,$sf,$i,\$found,\$fld_occurances); + my $text = $self->get_data(\$rec,$f,$sf,$i,\$found,\$rec_size); Required arguments are: @@ -592,20 +606,20 @@ =item C<$i> -index offset for repeatable values ( 0 ... $#occurances ) +index offset for repeatable values ( 0 ... $rec_size->{'400^a'} ) =item C<$found> optional variable that will be incremeted if preset -=item C<$fld_occurances> +=item C<$rec_size> -hash to hold maximum occurances of C combinations +hash to hold maximum occurances of C combinations (which can be accessed using keys in same format) =back -Returns value or empty string, updates C<$found> and C +Returns value or empty string, updates C<$found> and C if present. =cut @@ -618,7 +632,7 @@ return '' unless ($$rec->{$f} && ref($$rec->{$f}) eq 'ARRAY'); if (defined($$cache)) { - $$cache->{"$f\t$sf"} ||= $$#rec->{$f}; + $$cache->{ $f . ( $sf ? '^' . $sf : '' ) } ||= scalar @{ $$rec->{$f} }; } return '' unless ($$rec->{$f}->[$i]);