1 #-------------------------------------------------------------
7 my $type = shift || die "parset_format must be called with type!";
8 my $format = shift || die "parse_format must be called with format!";
9 my $row = shift || die "parse_format must be called with row!";
10 my $i = shift || 0; # isis repeatable number
11 my $codepage = shift || die "parse_format must be called with codepage!";
12 if ($type eq "isis") {
13 return parse_iso_format($format,$row,$i,$codepage,'isis_sf');
14 } elsif ($type eq "excel") {
15 return parse_excel_format($format,$row,$i,$codepage);
16 } elsif ($type eq "marc") {
17 return parse_iso_format($format,$row,$i,$codepage,'marc_sf');
18 } elsif ($type eq "feed") {
19 return parse_feed_format($format,$row,$i,$codepage);
23 #-------------------------------------------------------------
25 sub parse_iso_format {
32 my $func = shift || die "need to know which sub-field function to use";
44 my $tmp = shift || return;
46 $tmp = $codepage->convert($tmp) || print STDERR "iso: '$tmp' can't convert\n";
51 # if format doesn't exits, store it in cache
52 if (! defined($cache->{format}->{$format})) {
53 # print STDERR "parsing format for '$format'\n";
59 $eval = $1 if ($f =~ s/^eval{([^}]+?)}//);
61 if ($f =~ s/^([^\d]+)//) {
62 if ($f) { # there is more to parse
65 @fmt = ('',$1,undef,'');
66 #print STDERR "just one field: $1\n";
73 # print STDERR "\n#### $f";
74 # this is EBSCO special to support numeric subfield in
76 if ($f =~ s/^(\d\d\d)#*(\w?)//) {
83 # this might be our local scpeciality -- fields 10 and 11
84 # (as opposed to 010 and 011) so they are strictly listed
86 } elsif ($f =~ s/^(1[01]\w?)//) {
89 } elsif ($f =~ s/^mfn//i) {
92 } elsif ($f =~ s/^([^\d]+)(\d{0,3})/$2/) {
99 } elsif ($f =~ s/^([^\d]+\d{0,2})//) {
105 } elsif ($f =~ s/^(\d{1,2})//) {
112 print STDERR "unparsed format: $f\n";
116 push @fmt,'' if ($#fmt % 3 != 0); # add empty suffix
118 $cache->{format_eval}->{$format} = $eval; # store eval string (if any)
120 $cache->{format}->{$format} = \@fmt;
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});
127 # now produce actual record
128 my $tmp = $cache->{format}->{$format} || die "no format cache for '$format'";
130 # print STDERR "using format for '$format':",Dumper(@fmt),"\n";
131 # print STDERR "tmp ",Dumper($tmp);
132 # print STDERR "cache: ",Dumper($cache->{format}->{$format});
135 my $prefix = shift @fmt;
138 my $f = shift @fmt || die "BUG: field name can't be empty!";
141 if ($f eq 'mfn' && $i == 0) {
142 $display .= $sufix if ($display);
143 $display .= $row->{mfn};
145 my $val = &$func($row,$f,$sf,$i);
147 # print STDERR "val: $val\n";
148 my $tmp = cnv_cp($codepage,$val);
150 $display .= $sufix.$tmp;
159 $display = $prefix.$display.$sufix if ($display);
161 my $eval = $cache->{format_eval}->{$format};
164 my ($func,$row,$f,$sf,$i) = @_;
165 #print STDERR "## in fld2str\n";
166 my $tmp = $codepage->convert(&$func($row,$f,$sf,$i)) || $codepage->convert(&$func($row,$f,$sf,0)) || '';
170 $eval =~ s/v(\d+)\^(\w*)/fld2str($func,$row,$1,$2,$i)/eg;
171 #print STDERR "## eval: $eval\n";
173 die "eval error: eval{$eval}: $@" if ($@);
174 return ($swish,$display);
176 die "eval error: eval{$eval}: $@" if ($@);
177 return (undef,undef);
182 print STDERR "format left unused: [",join("|",@fmt),"]\n";
183 print STDERR "format: [",join("|",@{$tmp}),"]\n";
186 # print STDERR "format: {",$format || '',"} display: {",$display || '',"} swish: {",$swish || '',"}\n";
188 return ($swish,$display);
191 #-------------------------------------------------------------
193 sub parse_excel_format {
197 my $codepage = shift;
199 return if ($i > 0); # Excel doesn't support repeatable fields
205 if ($format =~ s/^([^A-Z\|]{1,3})//) {
212 while ($format && length($format) > 0) {
213 #print STDERR "\n#### $format #";
214 if ($format =~ s/^\|([A-Z]{1,2})\|//) {
215 #print STDERR "--$1-> $format -[",length($format),"] ";
217 my $tmp = $row->{$1};
219 $tmp = $codepage->convert($tmp) || warn "excel: $1 '$tmp' can't convert";
221 $display .= $prefix . $tmp;
223 #print STDERR " == $tmp";
226 } elsif ($format =~ s/^([^A-Z\|]+)(\|[A-Z]{1,2}\|)/$2/) {
227 $prefix .= $1 if ($display);
229 #print STDERR "unparsed format: $format\n";
233 #print STDERR " display: $display swish: $swish [format: $format]";
236 $display .= $prefix if ($display);
238 return ($swish,$display);
241 #-------------------------------------------------------------
243 sub parse_feed_format {
247 my $codepage = shift;
249 # XXX feed doesn't support repeatable fields, but they really
250 # should, This is a bug. It should be fixed!
257 if ($format =~ s/^([^\d\|]{1,3})//) {
264 while ($format && length($format) > 0) {
265 #print STDERR "\n#### $format #";
266 if ($format =~ s/^\|(\d+)\|//) {
267 #print STDERR "--$1-> $format -[",length($format),"] ";
269 my $tmp = $data->{$1};
271 $tmp = $codepage->convert($tmp) || warn "feed: $1 '$tmp' can't convert\n";
273 $display .= $prefix . $tmp;
275 #print STDERR " == $tmp";
278 } elsif ($format =~ s/^([^\d\|]+)(\|\d+\|)/$2/) {
279 $prefix .= $1 if ($display);
281 print STDERR "unparsed format: $format\n";
285 #print STDERR " display: $display swish: $swish [format: $format]";
288 $display .= $prefix if ($display);
290 return ($swish,$display);
293 #-------------------------------------------------------------