--- trunk/lib/WebPAC/Normalize.pm 2006/01/08 20:32:06 368 +++ trunk/lib/WebPAC/Normalize.pm 2006/01/08 21:50:34 372 @@ -198,9 +198,9 @@ my @v; if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { - @v = $self->fill_in_to_arr($rec,$format); + @v = $self->_rec_to_arr($rec,$format,'fill_in'); } else { - @v = $self->parse_to_arr($rec,$format); + @v = $self->_rec_to_arr($rec,$format,'parse'); } if (! @v) { $log->debug("$field <",$self->{tag},"> format: $format no values"); @@ -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; @@ -410,37 +410,6 @@ return $out; } -=head2 parse_to_arr - -Similar to C, but returns array of all repeatable fields - - my @arr = $webpac->parse_to_arr($rec,'v250^a'); - -=cut - -sub parse_to_arr { - my $self = shift; - - my ($rec, $format_utf8) = @_; - - my $log = $self->_get_logger(); - - $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); - return if (! $format_utf8); - - my $i = 0; - my @arr; - - while (my $v = $self->parse($rec,$format_utf8,$i++)) { - push @arr, $v; - } - - $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); - - return @arr; -} - - =head2 fill_in Workhourse of all: takes record from in-memory structure of database and @@ -462,6 +431,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 +443,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 +473,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); } @@ -535,20 +512,20 @@ } -=head2 fill_in_to_arr +=head2 _rec_to_arr -Similar to C, but returns array of all repeatable fields. Usable +Similar to C and C, but returns array of all repeatable fields. Usable for fields which have lookups, so they shouldn't be parsed but rather -Ced. +Cd or Ced. Last argument is name of operation: C or C. - my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); + my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]','paste'); =cut -sub fill_in_to_arr { +sub _rec_to_arr { my $self = shift; - my ($rec, $format_utf8) = @_; + my ($rec, $format_utf8, $code) = @_; my $log = $self->_get_logger(); @@ -556,10 +533,20 @@ return if (! $format_utf8); my $i = 0; + my $max = 0; my @arr; + my $rec_size = {}; - while (my $v = $self->fill_in($rec,$format_utf8,$i++)) { + while ($i <= $max) { + my $v = $self->$code($rec,$format_utf8,$i++,\$rec_size) || next; push @arr, $v; + if ($rec_size) { + foreach my $f (keys %{ $rec_size }) { + $max = $rec_size->{$f} if ($rec_size->{$f} > $max); + } + warn "max set to $max, rec_size = ", Dumper($rec_size); + undef $rec_size; + } } $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); @@ -572,7 +559,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 +579,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 +605,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]);