/[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 92 by dpavlin, Sun Jul 13 13:42:17 2003 UTC revision 678 by dpavlin, Sun Feb 27 23:07:35 2005 UTC
# Line 3  Line 3 
3  # parse_format(...)  # parse_format(...)
4  #  #
5    
   
6  sub parse_format {  sub parse_format {
7          my $type = shift || die "parset_format must be called with type!";          my $type = shift || die "parset_format must be called with type!";
8          my $format = shift || die "parse_format must be called with format!";          my $format = shift || die "parse_format must be called with format!";
# Line 11  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 37  sub parse_iso_format { Line 40  sub parse_iso_format {
40          my $out;          my $out;
41          my $out_swish;          my $out_swish;
42    
         my $prefix = "";  
         if ($format =~ s/^([^\d]+)//) {  
                 $prefix = $1;  
         }  
   
43          my $display;          my $display;
44          my $swish;          my $swish;
45    
# Line 54  sub parse_iso_format { Line 52  sub parse_iso_format {
52                  return $tmp;                  return $tmp;
53          }          }
54    
55          while ($format) {          # if format doesn't exits, store it in cache
56  #print STDERR "\n#### $format";          if (! defined($cache->{format}->{$format})) {
57                  # this is EBSCO special to support numeric subfield in  #               print STDERR "parsing format for '$format'\n";
58                  # form of 856#3                  my @fmt;
59                  if ($format =~ s/^(\d\d\d)#*(\w?)//) {  
60                          my $tmp = cnv_cp($codepage,get_sf($row,$1,$2,$i));                  my $f = $format;
61                          if ($tmp) {  
62                                  $display .= $prefix.$tmp;                  my $eval;
63                                  $swish .= $tmp." ";                  $eval = $1 if ($f =~ s/^eval{([^}]+?)}//);
64  #print STDERR " == $tmp";  
65                    if ($f =~ s/^([^\d]+)//) {
66                            if ($f) {       # there is more to parse
67                                    push @fmt,$1;
68                            } else {
69                                    @fmt = ('',$1,undef,'');
70    #print STDERR "just one field: $1\n";
71                          }                          }
72                          $prefix = "";                  } else {
73                  # this might be our local scpeciality -- fields 10 and 11                          push @fmt,'';
74                  # (as opposed to 010 and 011) so they are strictly listed                  }
75                  # here  
76                  } elsif ($format =~ s/^(1[01])//) {                  while ($f) {
77                          my $tmp = cnv_cp($codepage,get_sf($row,$1,undef,$i));  #       print STDERR "\n#### $f";
78                          if ($tmp) {                          # this is EBSCO special to support numeric subfield in
79                                  $display .= $prefix.$tmp;                          # form of 856#3
80                            if ($f =~ s/^(\d\d\d)#*(\w?)//) {
81                                    push @fmt,$1;
82                                    if ($2) {
83                                            push @fmt,$2;
84                                    } else {
85                                            push @fmt,undef;
86                                    }
87                            # this might be our local scpeciality -- fields 10 and 11
88                            # (as opposed to 010 and 011) so they are strictly listed
89                            # here
90                            } elsif ($f =~ s/^(1[01]\w?)//) {
91                                    push @fmt,$1;
92                                    push @fmt,undef;
93                            } elsif ($f =~ s/^mfn//i) {
94                                    push @fmt,'mfn';
95                                    push @fmt,'';
96                            } elsif ($f =~ s/^([^\d]+)(\d{0,3})/$2/) {
97                                    # still prefix?
98                                    if ($#fmt == 0) {
99                                            $fmt[0] .= $1;
100                                    } else {
101                                            push @fmt,$1;
102                                    }
103                            } elsif ($f =~ s/^([^\d]+\d{0,2})//) {
104                                    if ($#fmt == 0) {
105                                            $fmt[0] .= $1;
106                                    } else {
107                                            push @fmt,$1;
108                                    }
109                            } elsif ($f =~ s/^(\d{1,2})//) {
110                                    if ($#fmt == 0) {
111                                            $fmt[0] .= $1;
112                                    } else {
113                                            push @fmt,$1;
114                                    }
115                            } else {
116                                    print STDERR "unparsed format: $f\n";
117                                    $f = "";
118                            }
119                    }
120                    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;
125                    
126    #               print STDERR "storing format for '$format': [",join("|",@fmt),"]\n";
127    #               print STDERR "storing format for '$format':",Dumper(@fmt),"\n";
128    #               print STDERR Dumper($cache->{format}->{$format});
129            }
130    
131            # now produce actual record
132            my $tmp = $cache->{format}->{$format} || die "no format cache for '$format'";
133            my @fmt = @{$tmp};
134    #       print STDERR "using format for '$format':",Dumper(@fmt),"\n";
135    #       print STDERR "tmp ",Dumper($tmp);
136    #       print STDERR "cache: ",Dumper($cache->{format}->{$format});
137    
138            # prefix
139            my $prefix = shift @fmt;
140            my $sufix;
141            while($#fmt > 1) {
142                    my $f = shift @fmt || die "BUG: field name can't be empty!";
143                    my $sf = shift @fmt;
144    
145                    if ($f eq 'mfn' && $i == 0) {
146                            $display .= $sufix if ($display);
147                            $display .= $row->{mfn};
148                    } else {
149                            my $val = &$func($row,$f,$sf,$i);
150                            if ($val) {
151    #                               print STDERR "val: $val\n";
152                                    my $tmp = cnv_cp($codepage,$val);
153                                    if ($display) {
154                                            $display .= $sufix.$tmp;
155                                    } else {
156                                            $display = $tmp;
157                                    }
158                                  $swish .= $tmp." ";                                  $swish .= $tmp." ";
159                          }                          }
160                          $prefix = "";                  }
161                  } elsif ($format =~ s/^mfn//i) {                  $sufix = shift @fmt;
162                          $display .= $prefix . $row->{mfn};          }
163                          $prefix = "";          $display = $prefix.$display.$sufix if ($display);
164                  } elsif ($format =~ s/^([^\d]+)(\d{0,3})/$2/) {  
165                          $prefix .= $1 if ($display);          my $eval = $cache->{format_eval}->{$format};
166                  } elsif ($format =~ s/^([^\d]+\d{0,2})//) {          if ($eval) {
167                          $prefix .= $1 if ($display);                  sub fld2str {
168                  } elsif ($format =~ s/^(\d{1,2})//) {                          my ($func,$row,$f,$sf,$i) = @_;
169                          $prefix .= $1 if ($display);  #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 {                  } else {
180                          print STDERR "unparsed format: $format\n";                          die "eval error: eval{$eval}: $@" if ($@);
181                          $prefix .= $format;                          return (undef,undef);
                         $format = "";  
182                  }                  }
183          }          }
184          # add suffix  
185          $display .= $prefix if ($display);          if (@fmt) {
186                    print STDERR "format left unused: [",join("|",@fmt),"]\n";
187                    print STDERR "format: [",join("|",@{$tmp}),"]\n";
188            }
189    
190    #       print STDERR "format: {",$format || '',"} display: {",$display || '',"} swish: {",$swish || '',"}\n";
191    
192          return ($swish,$display);          return ($swish,$display);
193  }  }
# Line 103  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 124  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 135  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.92  
changed lines
  Added in v.678

  ViewVC Help
Powered by ViewVC 1.1.26