Add ZOOM::Log::module_level() function
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.25 2005-12-22 12:48:15 mike Exp $
2
3 use strict;
4 use warnings;
5 use Net::Z3950::ZOOM;
6
7
8 package ZOOM;
9
10
11 # Member naming convention: hash-element names which begin with an
12 # underscore represent underlying ZOOM-C object descriptors; those
13 # which lack them represent Perl's ZOOM objects.  (The same convention
14 # is used in naming local variables where appropriate.)
15 #
16 # So, for example, the ZOOM::Connection class has an {_conn} element,
17 # which is a pointer to the ZOOM-C Connection object; but the
18 # ZOOM::ResultSet class has a {conn} element, which is a reference to
19 # the Perl-level Connection object by which it was created.  (It may
20 # be that we find we have no need for these references, but for now
21 # they are retained.)
22 #
23 # To get at the underlying ZOOM-C connection object of a result-set
24 # (if you ever needed to do such a thing, which you probably don't)
25 # you'd use $rs->{conn}->_conn().
26
27 # ----------------------------------------------------------------------------
28
29 # The "Error" package contains constants returned as error-codes.
30 package ZOOM::Error;
31 sub NONE { Net::Z3950::ZOOM::ERROR_NONE }
32 sub CONNECT { Net::Z3950::ZOOM::ERROR_CONNECT }
33 sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY }
34 sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE }
35 sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE }
36 sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST }
37 sub INIT { Net::Z3950::ZOOM::ERROR_INIT }
38 sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL }
39 sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT }
40 sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL }
41 sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
42 sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
43 sub CQL_PARSE { Net::Z3950::ZOOM::ERROR_CQL_PARSE }
44 sub CQL_TRANSFORM { Net::Z3950::ZOOM::ERROR_CQL_TRANSFORM }
45 # The following are added specifically for this OO interface
46 sub CREATE_QUERY { 20001 }
47 sub QUERY_CQL { 20002 }
48 sub QUERY_PQF { 20003 }
49 sub SORTBY { 20004 }
50 sub CLONE { 20005 }
51 sub PACKAGE { 20006 }
52 sub SCANTERM { 20007 }
53 sub LOGLEVEL { 20008 }
54
55 # The "Event" package contains constants returned by last_event()
56 package ZOOM::Event;
57 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
58 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
59 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
60 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
61 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
62 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
63 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
64 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
65 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
66 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
67
68 # ----------------------------------------------------------------------------
69
70 package ZOOM;
71
72 sub diag_str {
73     my($code) = @_;
74
75     # Special cases for error specific to the OO layer
76     if ($code == ZOOM::Error::CREATE_QUERY) {
77         return "can't create query object";
78     } elsif ($code == ZOOM::Error::QUERY_CQL) {
79         return "can't set CQL query";
80     } elsif ($code == ZOOM::Error::QUERY_PQF) {
81         return "can't set prefix query";
82     } elsif ($code == ZOOM::Error::SORTBY) {
83         return "can't set sort-specification";
84     } elsif ($code == ZOOM::Error::CLONE) {
85         return "can't clone record";
86     } elsif ($code == ZOOM::Error::PACKAGE) {
87         return "can't create package";
88     } elsif ($code == ZOOM::Error::SCANTERM) {
89         return "can't retrieve term from scan-set";
90     } elsif ($code == ZOOM::Error::LOGLEVEL) {
91         return "unregistered log-level";
92     }
93
94     return Net::Z3950::ZOOM::diag_str($code);
95 }
96
97 sub _oops {
98     my($code, $addinfo, $diagset) = @_;
99
100     die new ZOOM::Exception($code, diag_str($code), $addinfo, $diagset);
101 }
102
103 # ----------------------------------------------------------------------------
104
105 package ZOOM::Exception;
106
107 sub new {
108     my $class = shift();
109     my($code, $message, $addinfo, $diagset) = @_;
110
111     return bless {
112         code => $code,
113         message => $message,
114         addinfo => $addinfo,
115         diagset => $diagset || "ZOOM",
116     }, $class;
117 }
118
119 sub code {
120     my $this = shift();
121     return $this->{code};
122 }
123
124 sub message {
125     my $this = shift();
126     return $this->{message};
127 }
128
129 sub addinfo {
130     my $this = shift();
131     return $this->{addinfo};
132 }
133
134 sub diagset {
135     my $this = shift();
136     return $this->{diagset};
137 }
138
139 sub render {
140     my $this = shift();
141     my $res = "ZOOM error " . $this->code() . ' "' . $this->message() . '"';
142     $res .= ' (addinfo: "' . $this->addinfo() . '")' if $this->addinfo();
143     $res .= " from diag-set '" . $this->diagset() . "'" if $this->diagset();
144     return $res;
145 }
146
147 # This means that untrapped exceptions render nicely.
148 use overload '""' => \&render;
149
150 # ----------------------------------------------------------------------------
151
152 package ZOOM::Options;
153
154 sub new {
155     my $class = shift();
156     my($p1, $p2) = @_;
157
158     my $opts;
159     if (@_ == 0) {
160         $opts = Net::Z3950::ZOOM::options_create();
161     } elsif (@_ == 1) {
162         $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
163     } elsif (@_ == 2) {
164         $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
165                                                               $p2->_opts());
166     } else {
167         die "can't make $class object with more than 2 parents";
168     }
169
170     return bless {
171         _opts => $opts,
172     }, $class;
173 }
174
175 # PRIVATE to this class and ZOOM::Connection::create() and
176 # ZOOM::Connection::package()
177 #
178 sub _opts {
179     my $this = shift();
180
181     my $_opts = $this->{_opts};
182     die "{_opts} undefined: has this Options block been destroy()ed?"
183         if !defined $_opts;
184
185     return $_opts;
186 }
187
188 sub option {
189     my $this = shift();
190     my($key, $value) = @_;
191
192     my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
193     Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
194         if defined $value;
195
196     return $oldval;
197 }
198
199 sub option_binary {
200     my $this = shift();
201     my($key, $value) = @_;
202
203     my $dummylen = 0;
204     my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
205                                                 $key, $dummylen);
206     Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
207                                    $value, length($value))
208         if defined $value;
209
210     return $oldval;
211 }
212
213 # This is a bit stupid, since the scalar values that Perl returns from
214 # option() can be used as a boolean; but it's just possible that some
215 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
216 # interpretation of what constitutes truth.
217 #
218 sub bool {
219     my $this = shift();
220     my($key, $default) = @_;
221
222     return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
223 }
224
225 # .. and the next two are even more stupid
226 sub int {
227     my $this = shift();
228     my($key, $default) = @_;
229
230     return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
231 }
232
233 sub set_int {
234     my $this = shift();
235     my($key, $value) = @_;
236
237     Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
238 }
239
240 #   ### Feel guilty.  Feel very, very guilty.  I've not been able to
241 #       get the callback memory-management right in "ZOOM.xs", with
242 #       the result that the values of $function and $udata passed into
243 #       this function, which are on the stack, have sometimes been
244 #       freed by the time they're used by __ZOOM_option_callback(),
245 #       with hilarious results.  To avoid this, I copy the values into
246 #       module-scoped globals, and pass _those_ into the extension
247 #       function.  To avoid overwriting those globals by subsequent
248 #       calls, I keep all the old ones, pushed onto the @_function and
249 #       @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
250 #       LIKE IT'S GOING OUT OF FASHION.  Not nice.  One day, I should
251 #       fix this, but for now there's more important fish to fry.
252 #
253 my(@_function, @_udata);
254 sub set_callback {
255     my $o1 = shift();
256     my($function, $udata) = @_;
257
258     push @_function, $function;
259     push @_udata, $udata;
260     Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
261                                            $_function[-1], $_udata[-1]);
262 }
263
264 sub destroy {
265     my $this = shift();
266
267     Net::Z3950::ZOOM::options_destroy($this->_opts());
268     $this->{_opts} = undef;
269 }
270
271
272 # ----------------------------------------------------------------------------
273
274 package ZOOM::Connection;
275
276 sub new {
277     my $class = shift();
278     my($host, $port) = @_;
279
280     my $_conn = Net::Z3950::ZOOM::connection_new($host, $port || 0);
281     my $conn = bless {
282         host => $host,
283         port => $port,
284         _conn => $_conn,
285     };
286     $conn->_check();
287     return $conn;
288 }
289
290 # PRIVATE to this class and to ZOOM::Query::CQL2RPN::new()
291 sub _conn {
292     my $this = shift();
293
294     my $_conn = $this->{_conn};
295     die "{_conn} undefined: has this Connection been destroy()ed?"
296         if !defined $_conn;
297
298     return $_conn;
299 }
300
301 sub _check {
302     my $this = shift();
303
304     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x");
305     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
306                                                     $addinfo, $diagset);
307     die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset)
308         if $errcode;
309 }
310
311 sub create {
312     my $class = shift();
313     my($options) = @_;
314
315     my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
316     return bless {
317         host => undef,
318         port => undef,
319         _conn => $_conn,
320     };
321 }
322
323 sub error_x {
324     my $this = shift();
325
326     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
327     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
328                                                     $addinfo, $diagset);
329     return ($errcode, $errmsg, $addinfo, $diagset);
330 }
331
332 sub errcode {
333     my $this = shift();
334     return Net::Z3950::ZOOM::connection_errcode($this->_conn());
335 }
336
337 sub errmsg {
338     my $this = shift();
339     return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
340 }
341
342 sub addinfo {
343     my $this = shift();
344     return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
345 }
346
347 sub diagset {
348     my $this = shift();
349     return Net::Z3950::ZOOM::connection_diagset($this->_conn());
350 }
351
352 sub connect {
353     my $this = shift();
354     my($host, $port) = @_;
355
356     Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
357     $this->_check();
358     # No return value
359 }
360
361 sub option {
362     my $this = shift();
363     my($key, $value) = @_;
364
365     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
366     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
367         if defined $value;
368
369     return $oldval;
370 }
371
372 sub option_binary {
373     my $this = shift();
374     my($key, $value) = @_;
375
376     my $dummylen = 0;
377     my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
378                                                           $key, $dummylen);
379     Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
380                                              $value, length($value))
381         if defined $value;
382
383     return $oldval;
384 }
385
386 sub search {
387     my $this = shift();
388     my($query) = @_;
389
390     my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
391                                                   $query->_query());
392     $this->_check();
393     return _new ZOOM::ResultSet($this, $query, $_rs);
394 }
395
396 sub search_pqf {
397     my $this = shift();
398     my($pqf) = @_;
399
400     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
401     $this->_check();
402     return _new ZOOM::ResultSet($this, $pqf, $_rs);
403 }
404
405 sub scan_pqf {
406     my $this = shift();
407     my($startterm) = @_;
408
409     my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm);
410     $this->_check();
411     return _new ZOOM::ScanSet($this, $startterm, $_ss);
412 }
413
414 sub scan {
415     my $this = shift();
416     my($query) = @_;
417
418     my $_ss = Net::Z3950::ZOOM::connection_scan1($this->_conn(),
419                                                  $query->_query());
420     $this->_check();
421     return _new ZOOM::ScanSet($this, $query, $_ss);
422 }
423
424 sub package {
425     my $this = shift();
426     my($options) = @_;
427
428     my $_o = defined $options ? $options->_opts() :
429         Net::Z3950::ZOOM::options_create();
430     my $_p = Net::Z3950::ZOOM::connection_package($this->_conn(), $_o)
431         or ZOOM::_oops(ZOOM::Error::PACKAGE);
432
433     return _new ZOOM::Package($this, $options, $_p);
434 }
435
436 sub destroy {
437     my $this = shift();
438
439     Net::Z3950::ZOOM::connection_destroy($this->_conn());
440     $this->{_conn} = undef;
441 }
442
443
444 # ----------------------------------------------------------------------------
445
446 package ZOOM::Query;
447
448 sub new {
449     my $class = shift();
450     die "You can't create $class objects: it's a virtual base class";
451 }
452
453 # PRIVATE to this class and ZOOM::Connection::search()
454 sub _query {
455     my $this = shift();
456
457     my $_query = $this->{_query};
458     die "{_query} undefined: has this Query been destroy()ed?"
459         if !defined $_query;
460
461     return $_query;
462 }
463
464 sub sortby {
465     my $this = shift();
466     my($sortby) = @_;
467
468     Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
469         or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
470 }
471
472 sub destroy {
473     my $this = shift();
474
475     Net::Z3950::ZOOM::query_destroy($this->_query());
476     $this->{_query} = undef;
477 }
478
479
480 package ZOOM::Query::CQL;
481 our @ISA = qw(ZOOM::Query);
482
483 sub new {
484     my $class = shift();
485     my($string) = @_;
486
487     my $q = Net::Z3950::ZOOM::query_create()
488         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
489     Net::Z3950::ZOOM::query_cql($q, $string) == 0
490         or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
491
492     return bless {
493         _query => $q,
494     }, $class;
495 }
496
497
498 package ZOOM::Query::CQL2RPN;
499 our @ISA = qw(ZOOM::Query);
500
501 sub new {
502     my $class = shift();
503     my($string, $conn) = @_;
504
505     my $q = Net::Z3950::ZOOM::query_create()
506         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
507     # check() throws the exception we want; but we only want it on failure!
508     Net::Z3950::ZOOM::query_cql2rpn($q, $string, $conn->_conn()) == 0
509         or $conn->_check();
510
511     return bless {
512         _query => $q,
513     }, $class;
514 }
515
516
517 package ZOOM::Query::PQF;
518 our @ISA = qw(ZOOM::Query);
519
520 sub new {
521     my $class = shift();
522     my($string) = @_;
523
524     my $q = Net::Z3950::ZOOM::query_create()
525         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
526     Net::Z3950::ZOOM::query_prefix($q, $string) == 0
527         or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
528
529     return bless {
530         _query => $q,
531     }, $class;
532 }
533
534
535 # ----------------------------------------------------------------------------
536
537 package ZOOM::ResultSet;
538
539 sub new {
540     my $class = shift();
541     die "You can't create $class objects directly";
542 }
543
544 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
545 sub _new {
546     my $class = shift();
547     my($conn, $query, $_rs) = @_;
548
549     return bless {
550         conn => $conn,
551         query => $query,        # This is not currently used, which is
552                                 # just as well since it could be
553                                 # either a string (when the RS is
554                                 # created with search_pqf()) or a
555                                 # ZOOM::Query object (when it's
556                                 # created with search())
557         _rs => $_rs,
558     }, $class;
559 }
560
561 # PRIVATE to this class
562 sub _rs {
563     my $this = shift();
564
565     my $_rs = $this->{_rs};
566     die "{_rs} undefined: has this ResultSet been destroy()ed?"
567         if !defined $_rs;
568
569     return $_rs;
570 }
571
572 sub option {
573     my $this = shift();
574     my($key, $value) = @_;
575
576     my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
577     Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
578         if defined $value;
579
580     return $oldval;
581 }
582
583 sub size {
584     my $this = shift();
585
586     return Net::Z3950::ZOOM::resultset_size($this->_rs());
587 }
588
589 sub record {
590     my $this = shift();
591     my($which) = @_;
592
593     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
594     $this->{conn}->_check();
595
596     # Even if no error has occurred, I think record() might
597     # legitimately return undef if we're running in asynchronous mode
598     # and the record just hasn't been retrieved yet.  This goes double
599     # for record_immediate().
600     return undef if !defined $_rec;
601
602     # For some reason, I have to use the explicit "->" syntax in order
603     # to invoke the ZOOM::Record constructor here, even though I don't
604     # have to do the same for _new ZOOM::ResultSet above.  Weird.
605     return ZOOM::Record->_new($this, $which, $_rec);
606 }
607
608 sub record_immediate {
609     my $this = shift();
610     my($which) = @_;
611
612     my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
613                                                             $which);
614     $this->{conn}->_check();
615     # The record might legitimately not be there yet
616     return undef if !defined $_rec;
617
618     return ZOOM::Record->_new($this, $which, $_rec);
619 }
620
621 sub cache_reset {
622     my $this = shift();
623
624     Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
625 }
626
627 sub records {
628     my $this = shift();
629     my($start, $count, $return_records) = @_;
630
631     my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
632                                                   $return_records);
633     # By design, $raw may be undefined (if $return_records is true)
634     return undef if !defined $raw;
635
636     # We need to package up the returned records in ZOOM::Record objects
637     my @res = ();
638     for my $i (0 .. @$raw-1) {
639         my $_rec = $raw->[$i];
640         if (!defined $_rec) {
641             push @res, undef;
642         } else {
643             push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
644         }
645     }
646
647     return \@res;
648 }
649
650 sub sort {
651     my $this = shift();
652     my($sort_type, $sort_spec) = @_;
653
654     return Net::Z3950::ZOOM::resultset_sort1($this->_rs(),
655                                              $sort_type, $sort_spec);
656 }
657
658 sub destroy {
659     my $this = shift();
660
661     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
662     $this->{_rs} = undef;
663 }
664
665
666 # ----------------------------------------------------------------------------
667
668 package ZOOM::Record;
669
670 sub new {
671     my $class = shift();
672     die "You can't create $class objects directly";
673 }
674
675 # PRIVATE to ZOOM::ResultSet::record(),
676 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
677 # ZOOM::Record::clone()
678 #
679 sub _new {
680     my $class = shift();
681     my($rs, $which, $_rec) = @_;
682
683     return bless {
684         rs => $rs,
685         which => $which,
686         _rec => $_rec,
687     }, $class;
688 }
689
690 # PRIVATE to this class
691 sub _rec {
692     my $this = shift();
693
694     my $_rec = $this->{_rec};
695     die "{_rec} undefined: has this Record been destroy()ed?"
696         if !defined $_rec;
697
698     return $_rec;
699 }
700
701 sub render {
702     my $this = shift();
703
704     my $len = 0;
705     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
706     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
707     # glue code should use the value of `len' as well as the opaque
708     # data-pointer returned, to ensure that the SV contains all of the
709     # returned data and does not stop at the first NUL character in
710     # binary data.  Carefully check the ZOOM_record_get() documentation.
711     return $string;
712 }
713
714 sub raw {
715     my $this = shift();
716
717     my $len = 0;
718     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
719     # See comment about $len in render()
720     return $string;
721 }
722
723 sub clone {
724     my $this = shift();
725
726     my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
727         or ZOOM::_oops(ZOOM::Error::CLONE);
728
729     # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
730     return _new ZOOM::Record(undef, undef, $raw);
731 }
732
733 sub destroy {
734     my $this = shift();
735
736     Net::Z3950::ZOOM::record_destroy($this->_rec());
737     $this->{_rec} = undef;
738 }
739
740
741 # ----------------------------------------------------------------------------
742
743 package ZOOM::ScanSet;
744
745 sub new {
746     my $class = shift();
747     die "You can't create $class objects directly";
748 }
749
750 # PRIVATE to ZOOM::Connection::scan(),
751 sub _new {
752     my $class = shift();
753     my($conn, $startterm, $_ss) = @_;
754
755     return bless {
756         conn => $conn,
757         startterm => $startterm,# This is not currently used, which is
758                                 # just as well since it could be
759                                 # either a string (when the SS is
760                                 # created with scan()) or a
761                                 # ZOOM::Query object (when it's
762                                 # created with scan1())
763         _ss => $_ss,
764     }, $class;
765 }
766
767 # PRIVATE to this class
768 sub _ss {
769     my $this = shift();
770
771     my $_ss = $this->{_ss};
772     die "{_ss} undefined: has this ScanSet been destroy()ed?"
773         if !defined $_ss;
774
775     return $_ss;
776 }
777
778 sub option {
779     my $this = shift();
780     my($key, $value) = @_;
781
782     my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key);
783     Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value)
784         if defined $value;
785
786     return $oldval;
787 }
788
789 sub size {
790     my $this = shift();
791
792     return Net::Z3950::ZOOM::scanset_size($this->_ss());
793 }
794
795 sub term {
796     my $this = shift();
797     my($which) = @_;
798
799     my($occ, $len) = (0, 0);
800     my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
801                                               $occ, $len)
802         or ZOOM::_oops(ZOOM::Error::SCANTERM);
803
804     die "length of term '$term' differs from returned len=$len"
805         if length($term) != $len;
806
807     return ($term, $occ);
808 }
809
810 sub display_term {
811     my $this = shift();
812     my($which) = @_;
813
814     my($occ, $len) = (0, 0);
815     my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
816                                                       $occ, $len)
817         or ZOOM::_oops(ZOOM::Error::SCANTERM);
818
819     die "length of display term '$term' differs from returned len=$len"
820         if length($term) != $len;
821
822     return ($term, $occ);
823 }
824
825 sub destroy {
826     my $this = shift();
827
828     Net::Z3950::ZOOM::scanset_destroy($this->_ss());
829     $this->{_ss} = undef;
830 }
831
832
833 # ----------------------------------------------------------------------------
834
835 package ZOOM::Package;
836
837 sub new {
838     my $class = shift();
839     die "You can't create $class objects directly";
840 }
841
842 # PRIVATE to ZOOM::Connection::package(),
843 sub _new {
844     my $class = shift();
845     my($conn, $options, $_p) = @_;
846
847     return bless {
848         conn => $conn,
849         options => $options,
850         _p => $_p,
851     }, $class;
852 }
853
854 # PRIVATE to this class
855 sub _p {
856     my $this = shift();
857
858     my $_p = $this->{_p};
859     die "{_p} undefined: has this Package been destroy()ed?"
860         if !defined $_p;
861
862     return $_p;
863 }
864
865 sub option {
866     my $this = shift();
867     my($key, $value) = @_;
868
869     my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
870     Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
871         if defined $value;
872
873     return $oldval;
874 }
875
876 sub send {
877     my $this = shift();
878     my($type) = @_;
879
880     Net::Z3950::ZOOM::package_send($this->_p(), $type);
881     $this->{conn}->_check();
882 }
883
884 sub destroy {
885     my $this = shift();
886
887     Net::Z3950::ZOOM::package_destroy($this->_p());
888     $this->{_p} = undef;
889 }
890
891
892 # There follows trivial support for YAZ logging.  This is wired out
893 # into the Net::Z3950::ZOOM package, and we here provide wrapper
894 # functions -- nothing more than aliases, really -- in the ZOOM::Log
895 # package.  There really is no point in inventing an OO interface.
896 #
897 # Passing @_ directly to the underlying Net::Z3950::ZOOM::* functions
898 # doesn't work, for reasons that I can't begin to fathom, and that
899 # don't particularly interest me.  Unpacking into scalars and passing
900 # those _does_ work, so that's what we do.
901
902 package ZOOM::Log;
903
904 sub mask_str      { my($a) = @_; Net::Z3950::ZOOM::yaz_log_mask_str($a); }
905 sub module_level  { my($a) = @_; Net::Z3950::ZOOM::yaz_log_module_level($a); }
906 sub init          { my($a, $b, $c) = @_;
907                     Net::Z3950::ZOOM::yaz_log_init($a, $b, $c) }
908 sub init_file     { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_file($a) }
909 sub init_level    { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_level($a) }
910 sub init_prefix   { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_prefix($a) }
911 sub time_format   { my($a) = @_; Net::Z3950::ZOOM::yaz_log_time_format($a) }
912 sub init_max_size { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_max_size($a) }
913
914 sub log {
915     my($level, @message) = @_;
916
917     if ($level !~ /^(0x)?\d+$/) {
918         # Assuming its log-level name, we look it up.
919         my $num = module_level($level);
920         ZOOM::_oops(ZOOM::Error::LOGLEVEL, $level)
921             if $num == 0;
922         $level = $num;
923     }
924
925     Net::Z3950::ZOOM::yaz_log($level, join("", @message));
926 }
927
928
929 1;