File Coverage

blib/lib/Biblio/bp/lib/bp-auto.pl
Criterion Covered Total %
statement 28 54 51.8
branch 13 52 25.0
condition n/a
subroutine 2 4 50.0
pod n/a
total 43 110 39.0


line stmt bran cond sub pod time code
1             #
2             # bibliography package for Perl
3             #
4             # Auto format recognizer routines.
5             #
6             # Note that this package is intimately tied to the internals of the main
7             # package. Most format packages will _not_ look like this one. Since this
8             # does automatic recognition, it needs to change what the main package
9             # thinks the format of the file is.
10             #
11             # The basic idea is that we define open and openwrite, which they call on
12             # some file. We then determine the real format of the file (either by name,
13             # or by slogging through it trying to guess at the type )
14             # and then change the main package's pointers for this file to point to the
15             # real type. So we shouldn't ever be called again for that file.
16             #
17             # Dana Jacobsen (dana@acm.org)
18             # 14 January 1995 (last modified on 21 Jan 1996)
19             #
20              
21             package bp_auto;
22              
23             ######
24              
25             &bib'reg_format(
26             'auto', # name
27             'aut', # short name
28             'bp_auto', # package name
29             'auto', # default character set
30             'suffix is bib', # <--- This must match the default format.
31             # functions
32             'options',
33             'open',
34             'close is unsupported',
35             'read is unsupported',
36             'write is unsupported',
37             'explode is unsupported',
38             'implode is unsupported',
39             'tocanon is unsupported',
40             'fromcanon is unsupported',
41             'clear',
42             );
43              
44             ######
45              
46             $opt_complex = 1;
47              
48             $opt_default_format = 'bibtex';
49              
50             ######
51              
52             sub options {
53 0     0   0 local($opts) = @_;
54              
55 0         0 print "setting options to $opts\n";
56             }
57              
58             ######
59              
60             sub autoformat {
61 2     2   5 local($file) = @_;
62 2         44 local($fmt) = undef;
63              
64 2 50       9 return $opt_default_format if $opt_complex == 0;
65 2 50       7 if ($opt_complex == 1) {
    0          
66             # XXXXX We should use the i_suffix fields from each format for this.
67             # But... that would mean loading in _every_ format just so we
68             # can check these out. That's too painful.
69 2 50       28 $file =~ /\.bib$/ && return 'bibtex';
70 0 0       0 $file =~ /\.ref$/ && return 'refer';
71 0 0       0 $file =~ /\.tib$/ && return 'tib';
72 0 0       0 $file =~ /\.pow$/ && return 'powells';
73 0 0       0 $file =~ /\.pro$/ && return 'procite';
74 0 0       0 $file =~ /\.med$/ && return 'medline';
75 0 0       0 $file =~ /\.html?$/ && return 'html';
76 0 0       0 $file =~ /\.mel$/ && return 'melvyl';
77 0 0       0 $file =~ /\.txt$/ && return 'text';
78 0 0       0 $file =~ /\.rfc1807$/ && return 'rfc1807';
79             } elsif ($opt_complex == 2) {
80             # autorecognize by open, read, close
81             # XXXXX This will be pretty complicated....
82             # First, we should check all the formats we have already loaded.
83             # Next, call find_bp_files and get the list of supported formats,
84             # then call their auto-recognize functions if they have one.
85 0         0 return &bib'goterror("auto format complexity level 2 is not implemented");
86             } else {
87 0         0 &bib'panic("format auto has invalid complexity level");
88             }
89              
90 0         0 $fmt;
91             }
92              
93             ######
94              
95             sub open {
96 2     2   6 local($file) = @_;
97 2         5 local($name, $mode);
98 2         4 local($func, $fmt, $cset);
99              
100 2 50       8 &bib'panic("auto format open called with no arguments") unless defined $file;
101              
102             # get the name and mode
103 2 50       13 if ($file =~ /^>>(.*)/) {
    50          
104 0         0 $mode = 'append'; $name = $1;
  0         0  
105             } elsif ($file =~ /^>(.*)/) {
106 0         0 $mode = 'write'; $name = $1;
  0         0  
107             } else {
108 2         5 $mode = 'read'; $name = $file;
  2         3  
109             }
110              
111             #
112             # 1) determine the format of the file
113             #
114 2 50       9 if ($mode eq 'read') {
115 2         7 $fmt = &autoformat($name);
116 2 50       7 $fmt = $bib'glb_Irfmt{$bib'glb_Ifilename} unless defined $fmt;
117             } else {
118 0 0       0 if ( $opt_complex == 1 ) {
119 0         0 $fmt = &autoformat($name);
120             } else {
121 0         0 $fmt = undef;
122             }
123             # Try the format we last wrote
124 0 0       0 $fmt = $bib'glb_Orfmt{$bib'glb_Ofilename} unless defined $fmt;
125             # Hmm. How about the format we just read?
126 0 0       0 $fmt = $bib'glb_Irfmt{$bib'glb_Ifilename} unless defined $fmt;
127             }
128 2 50       7 if (!defined $fmt) {
129 0         0 $fmt = $opt_default_format;
130 0         0 &bib'gotwarn("auto format using default format $fmt for $file")
131             }
132              
133             # if there is no default, and we don't know the type, then we lose.
134 2 50       7 return &bib'goterror("Auto format cannot determine type of name")
135             unless defined $fmt;
136              
137 2         14 &bib'debugs("auto step 1: $name<$fmt> with mode $mode", 8192, 'module');
138              
139             #
140             # 2) make sure the real format is loaded
141             #
142             # This also a) makes sure we have the right name
143             # and b) sets the character set to the format's default
144             #
145             # XXXXX sure this is the right cset to load?
146 2 50       8 return undef unless ($fmt, $cset) = &bib'load_format($fmt);
147              
148 2 50       12 &panic("auto charset recognition is unimplemented") if $cset eq 'auto';
149              
150 2         14 &bib'debugs("auto step 2: $name<$fmt:$cset>", 32, 'module');
151              
152             #
153             # 3) open the file using the real format
154             #
155 2         4 $bib'glb_current_fmt = $fmt;
156 2         8 $func = $bib'formats{$fmt, "open"};
157 2         15 $fmt = &$func($file);
158 2 50       12 return undef unless defined $fmt;
159              
160 2         15 &bib'debugs("auto step 3: $name<$fmt:$cset>", 32, 'module');
161              
162             #
163             # 4) return the real format and character set
164             #
165              
166 2         5 $fmt .= ':' . $cset;
167              
168 2         11 $fmt;
169             }
170              
171             ######
172              
173             ######
174              
175             sub clear {
176             # for now, we don't do anything
177 0     0     1;
178             }
179              
180              
181              
182              
183              
184             #######################
185             # end of package
186             #######################
187              
188             1;