Many changes to the ZOOM::Options class:
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.8 2005-10-19 13:53:35 mike Exp $
2
3 use strict;
4 use warnings;
5 use Net::Z3950::ZOOM;
6
7
8 package ZOOM;
9
10 sub diag_str {
11     my($code) = @_;
12     return Net::Z3950::ZOOM::diag_str($code);
13 }
14
15
16 # Member naming convention: hash-element names which begin with an
17 # underscore represent underlying ZOOM-C object descriptors; those
18 # which lack them represent Perl's ZOOM objects.  (The same convention
19 # is used in naming local variables where appropriate.)
20 #
21 # So, for example, the ZOOM::Connection class has an {_conn} element,
22 # which is a pointer to the ZOOM-C Connection object; but the
23 # ZOOM::ResultSet class has a {conn} element, which is a reference to
24 # the Perl-level Connection object by which it was created.  (It may
25 # be that we find we have no need for these references, but for now
26 # they are retained.)
27 #
28 # To get at the underlying ZOOM-C connection object of a result-set
29 # (if you ever needed to do such a thing, which you probably don't)
30 # you'd use $rs->{conn}->_conn().
31
32 # ----------------------------------------------------------------------------
33
34 # The "Error" package contains constants returned as error-codes.
35 package ZOOM::Error;
36 sub NONE { Net::Z3950::ZOOM::ERROR_NONE }
37 sub CONNECT { Net::Z3950::ZOOM::ERROR_CONNECT }
38 sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY }
39 sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE }
40 sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE }
41 sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST }
42 sub INIT { Net::Z3950::ZOOM::ERROR_INIT }
43 sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL }
44 sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT }
45 sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL }
46 sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
47 sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
48
49 # The "Event" package contains constants returned by last_event()
50 package ZOOM::Event;
51 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
52 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
53 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
54 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
55 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
56 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
57 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
58 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
59 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
60 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
61
62
63 # ----------------------------------------------------------------------------
64
65 package ZOOM::Exception;
66
67 sub new {
68     my $class = shift();
69     my($code, $message, $addinfo) = @_;
70     ### support diag-set, too
71
72     return bless {
73         code => $code,
74         message => $message,
75         addinfo => $addinfo,
76     }, $class;
77 }
78
79 sub code {
80     my $this = shift();
81     return $this->{code};
82 }
83
84 sub message {
85     my $this = shift();
86     return $this->{message};
87 }
88
89 sub addinfo {
90     my $this = shift();
91     return $this->{addinfo};
92 }
93
94
95 # ----------------------------------------------------------------------------
96
97 package ZOOM::Options;
98
99 sub new {
100     my $class = shift();
101     my($p1, $p2) = @_;
102
103     my $opts;
104     if (@_ == 0) {
105         $opts = Net::Z3950::ZOOM::options_create();
106     } elsif (@_ == 1) {
107         $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
108     } elsif (@_ == 2) {
109         $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
110                                                               $p2->_opts());
111     } else {
112         die "can't make $class object with more than 2 parents";
113     }
114
115     return bless {
116         _opts => $opts,
117     }, $class;
118 }
119
120 sub _opts {
121     my $this = shift();
122
123     my $_opts = $this->{_opts};
124     die "{_opts} undefined: has this Options block been destroy()ed?"
125         if !defined $_opts;
126
127     return $_opts;
128 }
129
130 sub option {
131     my $this = shift();
132     my($key, $value) = @_;
133
134     my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
135     Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
136         if defined $value;
137
138     return $oldval;
139 }
140
141 sub option_binary {
142     my $this = shift();
143     my($key, $value) = @_;
144
145     my $dummylen = 0;
146     my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
147                                                 $key, $dummylen);
148     Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
149                                    $value, length($value))
150         if defined $value;
151
152     return $oldval;
153 }
154
155 # This is a bit stupid, since the scalar values that Perl returns from
156 # option() can be used as a boolean; but it's just possible that some
157 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
158 # interpretation of what constitutes truth.
159 #
160 sub bool {
161     my $this = shift();
162     my($key, $default) = @_;
163
164     return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
165 }
166
167 # .. and the next two are even more stupid
168 sub int {
169     my $this = shift();
170     my($key, $default) = @_;
171
172     return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
173 }
174
175 sub set_int {
176     my $this = shift();
177     my($key, $value) = @_;
178
179     Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
180 }
181
182 sub destroy {
183     my $this = shift();
184
185     Net::Z3950::ZOOM::options_destroy($this->_opts());
186     $this->{_opts} = undef;
187 }
188
189
190 # ----------------------------------------------------------------------------
191
192 package ZOOM::Connection;
193
194 sub new {
195     my $class = shift();
196     my($host, $port) = @_;
197
198     my $_conn = Net::Z3950::ZOOM::connection_new($host, $port);
199     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
200     $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo);
201     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
202
203     return bless {
204         host => $host,
205         port => $port,
206         _conn => $_conn,
207     };
208 }
209
210 sub create {
211     my $class = shift();
212     my($options) = @_;
213
214     my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
215     return bless {
216         host => undef,
217         port => undef,
218         _conn => $_conn,
219     };
220 }
221
222 # PRIVATE within this class
223 sub _conn {
224     my $this = shift();
225
226     my $_conn = $this->{_conn};
227     die "{_conn} undefined: has this ResultSet been destroy()ed?"
228         if !defined $_conn;
229
230     return $_conn;
231 }
232
233 sub error_x {
234     my $this = shift();
235
236     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
237     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
238                                                     $addinfo, $diagset);
239     return ($errcode, $errmsg, $addinfo, $diagset);
240 }
241
242 sub errcode {
243     my $this = shift();
244     return Net::Z3950::ZOOM::connection_errcode($this->_conn());
245 }
246
247 sub errmsg {
248     my $this = shift();
249     return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
250 }
251
252 sub addinfo {
253     my $this = shift();
254     return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
255 }
256
257 sub connect {
258     my $this = shift();
259     my($host, $port) = @_;
260
261     Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
262     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
263     $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
264                                                   $errmsg, $addinfo);
265     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
266     # No return value
267 }
268
269 sub option {
270     my $this = shift();
271     my($key, $value) = @_;
272
273     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
274     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
275         if defined $value;
276
277     return $oldval;
278 }
279
280 sub option_binary {
281     my $this = shift();
282     my($key, $value) = @_;
283
284     my $dummylen = 0;
285     my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
286                                                           $key, $dummylen);
287     Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
288                                              $value, length($value))
289         if defined $value;
290
291     return $oldval;
292 }
293
294
295 sub search_pqf {
296     my $this = shift();
297     my($query) = @_;
298
299     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $query);
300     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
301     $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
302                                                   $errmsg, $addinfo);
303     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
304
305     return _new ZOOM::ResultSet($this, $query, $_rs);
306 }
307
308 sub destroy {
309     my $this = shift();
310
311     Net::Z3950::ZOOM::connection_destroy($this->_conn());
312     $this->{_conn} = undef;
313 }
314
315
316 # ----------------------------------------------------------------------------
317
318 package ZOOM::ResultSet;
319
320 sub new {
321     my $class = shift();
322     die "You can't create $class objects directly";
323 }
324
325 # PRIVATE to ZOOM::Connection::search()
326 sub _new {
327     my $class = shift();
328     my($conn, $query, $_rs) = @_;
329
330     return bless {
331         conn => $conn,
332         query => $query,
333         _rs => $_rs,
334     }, $class;
335 }
336
337 # PRIVATE within this class
338 sub _rs {
339     my $this = shift();
340
341     my $_rs = $this->{_rs};
342     die "{_rs} undefined: has this ResultSet been destroy()ed?"
343         if !defined $_rs;
344
345     return $_rs;
346 }
347
348 sub size {
349     my $this = shift();
350
351     return Net::Z3950::ZOOM::resultset_size($this->_rs());
352 }
353
354 sub record {
355     my $this = shift();
356     my($which) = @_;
357
358     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
359     ### Check for error -- but how?
360
361     # For some reason, I have to use the explicit "->" syntax in order
362     # to invoke the ZOOM::Record constructor here, even though I don't
363     # have to do the same for _new ZOOM::ResultSet above.  Weird.
364     return ZOOM::Record->_new($this, $which, $_rec);
365 }
366
367 sub destroy {
368     my $this = shift();
369
370     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
371     $this->{_rs} = undef;
372 }
373
374
375 # ----------------------------------------------------------------------------
376
377 package ZOOM::Record;
378
379 sub new {
380     my $class = shift();
381     die "You can't create $class objects directly";
382 }
383
384 # PRIVATE to ZOOM::ResultSet::record()
385 sub _new {
386     my $class = shift();
387     my($rs, $which, $_rec) = @_;
388
389     return bless {
390         rs => $rs,
391         which => $which,
392         _rec => $_rec,
393     }, $class;
394 }
395
396 # PRIVATE within this class
397 sub _rec {
398     my $this = shift();
399
400     return $this->{_rec};
401 }
402
403 sub render {
404     my $this = shift();
405
406     my $len = 0;
407     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
408     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
409     # glue code should use the value of `len' as well as the opaque
410     # data-pointer returned, to ensure that the SV contains all of the
411     # returned data and does not stop at the first NUL character in
412     # binary data.  Carefully check the ZOOM_record_get() documentation.
413     return $string;
414 }
415
416 sub raw {
417     my $this = shift();
418
419     my $len = 0;
420     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
421     # See comment about $len in render()
422     return $string;
423 }
424
425
426 1;