/[webpac]/trunk/parse_format.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/parse_format.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 176 by dpavlin, Mon Nov 24 01:16:04 2003 UTC revision 678 by dpavlin, Sun Feb 27 23:07:35 2005 UTC
# Line 10  sub parse_format { Line 10  sub parse_format {
10          my $i = shift || 0;     # isis repeatable number          my $i = shift || 0;     # isis repeatable number
11          my $codepage = shift || die "parse_format must be called with codepage!";          my $codepage = shift || die "parse_format must be called with codepage!";
12          if ($type eq "isis") {          if ($type eq "isis") {
13                  return parse_iso_format($format,$row,$i,$codepage,'isis_sf');                  return parse_iso_format($format,$row,$i,$codepage,'hash_sf');
14          } elsif ($type eq "excel") {          } elsif ($type eq "excel") {
15                  return parse_excel_format($format,$row,$i,$codepage);                  return parse_excel_format($format,$row,$i,$codepage);
16          } elsif ($type eq "marc") {          } elsif ($type eq "marc") {
17                  return parse_iso_format($format,$row,$i,$codepage,'marc_sf');                  return parse_iso_format($format,$row,$i,$codepage,'marc_sf');
18          } elsif ($type eq "feed") {          } elsif ($type eq "feed") {
19                  return parse_feed_format($format,$row,$i,$codepage);                  return parse_feed_format($format,$row,$i,$codepage);
20            } elsif ($type eq "dbf") {
21                    return parse_iso_format($format,$row,$i,$codepage,'hash_sf');
22            } else {
23                    confess "FATAL: unknown type '$type'";
24          }          }
25  }  }
26    
# Line 55  sub parse_iso_format { Line 59  sub parse_iso_format {
59    
60                  my $f = $format;                  my $f = $format;
61    
62                    my $eval;
63                    $eval = $1 if ($f =~ s/^eval{([^}]+?)}//);
64    
65                  if ($f =~ s/^([^\d]+)//) {                  if ($f =~ s/^([^\d]+)//) {
66                          if ($f) {       # there is more to parse                          if ($f) {       # there is more to parse
67                                  push @fmt,$1;                                  push @fmt,$1;
# Line 80  sub parse_iso_format { Line 87  sub parse_iso_format {
87                          # this might be our local scpeciality -- fields 10 and 11                          # this might be our local scpeciality -- fields 10 and 11
88                          # (as opposed to 010 and 011) so they are strictly listed                          # (as opposed to 010 and 011) so they are strictly listed
89                          # here                          # here
90                          } elsif ($f =~ s/^(1[01])//) {                          } elsif ($f =~ s/^(1[01]\w?)//) {
91                                  push @fmt,$1;                                  push @fmt,$1;
92                                  push @fmt,undef;                                  push @fmt,undef;
93                          } elsif ($f =~ s/^mfn//i) {                          } elsif ($f =~ s/^mfn//i) {
# Line 111  sub parse_iso_format { Line 118  sub parse_iso_format {
118                          }                          }
119                  }                  }
120                  push @fmt,'' if ($#fmt % 3 != 0);       # add empty suffix                  push @fmt,'' if ($#fmt % 3 != 0);       # add empty suffix
121    
122                    $cache->{format_eval}->{$format} = $eval; # store eval string (if any)
123    
124                  $cache->{format}->{$format} = \@fmt;                  $cache->{format}->{$format} = \@fmt;
125                                    
126                  print STDERR "storing format for '$format': [",join("|",@fmt),"]\n";  #               print STDERR "storing format for '$format': [",join("|",@fmt),"]\n";
127  #               print STDERR "storing format for '$format':",Dumper(@fmt),"\n";  #               print STDERR "storing format for '$format':",Dumper(@fmt),"\n";
128  #               print STDERR Dumper($cache->{format}->{$format});  #               print STDERR Dumper($cache->{format}->{$format});
129          }          }
# Line 151  sub parse_iso_format { Line 161  sub parse_iso_format {
161                  $sufix = shift @fmt;                  $sufix = shift @fmt;
162          }          }
163          $display = $prefix.$display.$sufix if ($display);          $display = $prefix.$display.$sufix if ($display);
         print STDERR "format left unused: [",join("|",@fmt),"]\n" if (@fmt);  
164    
165          print STDERR "format: [",join("|",@{$tmp}),"]\n" if (@fmt);          my $eval = $cache->{format_eval}->{$format};
166            if ($eval) {
167                    sub fld2str {
168                            my ($func,$row,$f,$sf,$i) = @_;
169    #print STDERR "## in fld2str\n";
170                            my $tmp = $codepage->convert(&$func($row,$f,$sf,$i)) ||  $codepage->convert(&$func($row,$f,$sf,0)) || '';
171                            return "'$tmp'";
172                    }
173    
174                    $eval =~ s/v(\d+)\^(\w*)/fld2str($func,$row,$1,$2,$i)/eg;
175    #print STDERR "## eval: $eval\n";
176                    if (eval "$eval") {
177                            die "eval error: eval{$eval}: $@" if ($@);
178                            return ($swish,$display);
179                    } else {
180                            die "eval error: eval{$eval}: $@" if ($@);
181                            return (undef,undef);
182                    }
183            }
184    
185            if (@fmt) {
186                    print STDERR "format left unused: [",join("|",@fmt),"]\n";
187                    print STDERR "format: [",join("|",@{$tmp}),"]\n";
188            }
189    
190  #       print STDERR "display: $display swish: $swish\n";  #       print STDERR "format: {",$format || '',"} display: {",$display || '',"} swish: {",$swish || '',"}\n";
191    
192          return ($swish,$display);          return ($swish,$display);
193  }  }
# Line 166  sub parse_excel_format { Line 198  sub parse_excel_format {
198          my $format = shift;          my $format = shift;
199          my $row = shift;          my $row = shift;
200          my $i = shift;          my $i = shift;
201          my $codepage = shift;          #my $codepage = shift;
202            #
203            # data allready comes in utf-8 due to change in
204            # SpreadSheet::ParseExcel::FmtDefault line 69 from
205            #       return pack('C*', unpack('n*', $sTxt));
206            # to following which returns utf-8:
207            #       return pack('U*', unpack('n*', $sTxt));
208            #
209    
210          return if ($i > 0);     # Excel doesn't support repeatable fields          return if ($i > 0);     # Excel doesn't support repeatable fields
211    
# Line 187  sub parse_excel_format { Line 226  sub parse_excel_format {
226  #print STDERR "--$1-> $format -[",length($format),"] ";  #print STDERR "--$1-> $format -[",length($format),"] ";
227                          if ($row->{$1}) {                          if ($row->{$1}) {
228                                  my $tmp = $row->{$1};                                  my $tmp = $row->{$1};
                                 if ($codepage) {  
                                         $tmp = $codepage->convert($tmp) || warn "excel: $1 '$tmp' can't convert";  
                                 }  
229                                  $display .= $prefix . $tmp;                                  $display .= $prefix . $tmp;
230                                  $swish .= $tmp." ";                                  $swish .= $tmp." ";
231  #print STDERR " == $tmp";  #print STDERR " == $tmp";
# Line 198  sub parse_excel_format { Line 234  sub parse_excel_format {
234                  } elsif ($format =~ s/^([^A-Z\|]+)(\|[A-Z]{1,2}\|)/$2/) {                  } elsif ($format =~ s/^([^A-Z\|]+)(\|[A-Z]{1,2}\|)/$2/) {
235                          $prefix .= $1 if ($display);                          $prefix .= $1 if ($display);
236                  } else {                  } else {
237                          print STDERR "unparsed format: $format\n";                          #print STDERR "unparsed format: $format\n";
238                          $prefix .= $format;                          $prefix .= $format;
239                          $format = "";                          $format = "";
240                  }                  }

Legend:
Removed from v.176  
changed lines
  Added in v.678

  ViewVC Help
Powered by ViewVC 1.1.26