/[webpac]/branches/hidra/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 /branches/hidra/parse_format.pm

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

trunk/parse_format.pm revision 67 by dpavlin, Fri Jul 4 23:29:27 2003 UTC branches/hidra/parse_format.pm revision 260 by dpavlin, Thu Mar 11 20:32:55 2004 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 37  sub parse_iso_format { Line 36  sub parse_iso_format {
36          my $out;          my $out;
37          my $out_swish;          my $out_swish;
38    
         my $prefix = "";  
         if ($format =~ s/^([^\d]+)//) {  
                 $prefix = $1;  
         }  
   
39          my $display;          my $display;
40          my $swish;          my $swish;
41    
42          sub cnv_cp {          sub cnv_cp {
43                  my $tmp = shift;                  my $codepage = shift;
44                    my $tmp = shift || return;
45                  if ($codepage) {                  if ($codepage) {
46                          $tmp = $codepage->convert($tmp) || print STDERR "$1$2 = '$tmp' can't convert";                          $tmp = $codepage->convert($tmp) || print STDERR "iso: '$tmp' can't convert\n";
47                  }                  }
48                  return $tmp;                  return $tmp;
49          }          }
50    
51          while ($format) {          # if format doesn't exits, store it in cache
52  #print STDERR "\n#### $format";          if (! defined($cache->{format}->{$format})) {
53                  # this is EBSCO special to support numeric subfield in  #               print STDERR "parsing format for '$format'\n";
54                  # form of 856#3                  my @fmt;
55                  if ($format =~ s/^(\d\d\d)#*(\w?)//) {  
56                          my $tmp = get_sf($row,$1,$2,$i);                  my $f = $format;
57                          if ($tmp) {  
58                                  $display .= $prefix.cnv_cp($tmp);                  my $eval;
59                                  $swish .= $tmp." ";                  $eval = $1 if ($f =~ s/^eval{([^}]+)}//);
60  #print STDERR " == $tmp";  
61                    if ($f =~ s/^([^\d]+)//) {
62                            if ($f) {       # there is more to parse
63                                    push @fmt,$1;
64                            } else {
65                                    @fmt = ('',$1,undef,'');
66    #print STDERR "just one field: $1\n";
67                          }                          }
68                          $prefix = "";                  } else {
69                  # this might be our local scpeciality -- fields 10 and 11                          push @fmt,'';
70                  # (as opposed to 010 and 011) so they are strictly listed                  }
71                  # here  
72                  } elsif ($format =~ s/^(1[01])//) {                  while ($f) {
73                          my $tmp = get_sf($row,$1,undef,$i);  #       print STDERR "\n#### $f";
74                          if ($tmp) {                          # this is EBSCO special to support numeric subfield in
75                                  $display .= $prefix.cnv_cp($tmp);                          # form of 856#3
76                            if ($f =~ s/^(\d\d\d)#*(\w?)//) {
77                                    push @fmt,$1;
78                                    if ($2) {
79                                            push @fmt,$2;
80                                    } else {
81                                            push @fmt,undef;
82                                    }
83                            # this might be our local scpeciality -- fields 10 and 11
84                            # (as opposed to 010 and 011) so they are strictly listed
85                            # here
86                            } elsif ($f =~ s/^(1[01]\w?)//) {
87                                    push @fmt,$1;
88                                    push @fmt,undef;
89                            } elsif ($f =~ s/^mfn//i) {
90                                    push @fmt,'mfn';
91                                    push @fmt,'';
92                            } elsif ($f =~ s/^([^\d]+)(\d{0,3})/$2/) {
93                                    # still prefix?
94                                    if ($#fmt == 0) {
95                                            $fmt[0] .= $1;
96                                    } else {
97                                            push @fmt,$1;
98                                    }
99                            } elsif ($f =~ s/^([^\d]+\d{0,2})//) {
100                                    if ($#fmt == 0) {
101                                            $fmt[0] .= $1;
102                                    } else {
103                                            push @fmt,$1;
104                                    }
105                            } elsif ($f =~ s/^(\d{1,2})//) {
106                                    if ($#fmt == 0) {
107                                            $fmt[0] .= $1;
108                                    } else {
109                                            push @fmt,$1;
110                                    }
111                            } else {
112                                    print STDERR "unparsed format: $f\n";
113                                    $f = "";
114                            }
115                    }
116                    push @fmt,'' if ($#fmt % 3 != 0);       # add empty suffix
117    
118                    $cache->{format_eval}->{$format} = $eval; # store eval string (if any)
119    
120                    $cache->{format}->{$format} = \@fmt;
121                    
122    #               print STDERR "storing format for '$format': [",join("|",@fmt),"]\n";
123    #               print STDERR "storing format for '$format':",Dumper(@fmt),"\n";
124    #               print STDERR Dumper($cache->{format}->{$format});
125            }
126    
127            # now produce actual record
128            my $tmp = $cache->{format}->{$format} || die "no format cache for '$format'";
129            my @fmt = @{$tmp};
130    #       print STDERR "using format for '$format':",Dumper(@fmt),"\n";
131    #       print STDERR "tmp ",Dumper($tmp);
132    #       print STDERR "cache: ",Dumper($cache->{format}->{$format});
133    
134            # prefix
135            my $prefix = shift @fmt;
136            my $sufix;
137            while($#fmt > 1) {
138                    my $f = shift @fmt || die "BUG: field name can't be empty!";
139                    my $sf = shift @fmt;
140    
141                    if ($f eq 'mfn' && $i == 0) {
142                            $display .= $sufix if ($display);
143                            $display .= $row->{mfn};
144                    } else {
145                            my $val = &$func($row,$f,$sf,$i);
146                            if ($val) {
147    #                               print STDERR "val: $val\n";
148                                    my $tmp = cnv_cp($codepage,$val);
149                                    if ($display) {
150                                            $display .= $sufix.$tmp;
151                                    } else {
152                                            $display = $tmp;
153                                    }
154                                  $swish .= $tmp." ";                                  $swish .= $tmp." ";
155                          }                          }
156                          $prefix = "";                  }
157                  } elsif ($format =~ s/^mfn//i) {                  $sufix = shift @fmt;
158                          $display .= $prefix . $row->{mfn};          }
159                          $prefix = "";          $display = $prefix.$display.$sufix if ($display);
160                  } elsif ($format =~ s/^([^\d]+)(\d{0,3})/$2/) {  
161                          $prefix .= $1 if ($display);          my $eval = $cache->{format_eval}->{$format};
162                  } elsif ($format =~ s/^([^\d]+\d{0,2})//) {          if ($eval) {
163                          $prefix .= $1 if ($display);                  sub fld2str {
164                  } elsif ($format =~ s/^(\d{1,2})//) {                          my ($func,$row,$f,$sf,$i) = @_;
165                          $prefix .= $1 if ($display);  #print STDERR "## in fld2str\n";
166                            my $tmp = &$func($row,$f,$sf,$i) || '';
167                            return "'$tmp'";
168                    }
169    
170                    $eval =~ s/v(\d+)\^(\w*)/fld2str($func,$row,$1,$2,$i)/eg;
171    #print STDERR "## eval: $eval\n";
172                    if (eval "$eval") {
173                            return ($swish,$display);
174                  } else {                  } else {
175                          print STDERR "unparsed format: $format\n";                          return (undef,undef);
                         $prefix .= $format;  
                         $format = "";  
176                  }                  }
177          }          }
178          # add suffix  
179          $display .= $prefix if ($display);          if (@fmt) {
180                    print STDERR "format left unused: [",join("|",@fmt),"]\n";
181                    print STDERR "format: [",join("|",@{$tmp}),"]\n";
182            }
183    
184    #       print STDERR "format: {",$format || '',"} display: {",$display || '',"} swish: {",$swish || '',"}\n";
185    
186          return ($swish,$display);          return ($swish,$display);
187  }  }
# Line 104  sub parse_excel_format { Line 194  sub parse_excel_format {
194          my $i = shift;          my $i = shift;
195          my $codepage = shift;          my $codepage = shift;
196    
197            return if ($i > 0);     # Excel doesn't support repeatable fields
198    
199          my $out;          my $out;
200          my $out_swish;          my $out_swish;
201    
# Line 152  sub parse_feed_format { Line 244  sub parse_feed_format {
244          my $i = shift;          my $i = shift;
245          my $codepage = shift;          my $codepage = shift;
246    
247            # XXX feed doesn't support repeatable fields, but they really
248            # should, This is a bug. It should be fixed!
249            return if ($i > 0);
250    
251          my $out;          my $out;
252          my $out_swish;          my $out_swish;
253    

Legend:
Removed from v.67  
changed lines
  Added in v.260

  ViewVC Help
Powered by ViewVC 1.1.26