1 /* $Id: ZOOM.xs,v 1.20 2005-11-02 18:23:10 mike Exp $ */
8 #include <yaz/xmalloc.h>
10 /* Used by the *_setl() functions */
11 typedef char opaquechar;
13 /* Used as the return value of the *_getl() functions */
19 /* Used to package Perl function-pointer and user-data together */
20 struct callback_block {
25 /* The callback function used for ZOOM_options_set_callback(). I do
26 * not claim to fully understand all the stack-hacking magic, and less
27 * still the reference-counting/mortality stuff. Accordingly, the
28 * memory management here is best characterised as What I Could Get To
31 const char *__ZOOM_option_callback (void *handle, const char *name)
33 struct callback_block *cb = (struct callback_block*) handle;
46 XPUSHs(sv_2mortal(newSVpv(name, 0)));
48 /* Perl_sv_dump(0, cb->function); */
50 count = call_sv(cb->function, G_SCALAR);
55 croak("callback function for ZOOM_options_get() returned %d values: should have returned exactly one", count);
60 /* ### `res' never gets freed! I think it is
61 * impossible to solve this problem "correctly"
62 * because the ZOOM-C option callback interface is
77 MODULE = Net::Z3950::ZOOM PACKAGE = Net::Z3950::ZOOM PREFIX=ZOOM_
84 ZOOM_connection_new(host, portnum)
90 ZOOM_connection_create(options)
95 ZOOM_connection_connect(c, host, portnum)
102 ZOOM_connection_destroy(c)
107 ZOOM_connection_option_get(c, key)
113 ZOOM_connection_option_getl(c, key, len)
118 RETVAL.data = (char*) ZOOM_connection_option_getl(c, key, &len);
126 ZOOM_connection_option_set(c, key, val)
131 # In ZOOM-C, the `val' parameter is const char*. However, our typemap
132 # treats this as T_PV, i.e. it's "known" that it points to a
133 # NUL-terminated string. Instead, then, I here use opaquechar*, which
134 # is an opaque pointer. The underlying C function can then use this
135 # along with `len' to Do The Right Thing.
139 ZOOM_connection_option_setl(c, key, val, len)
145 # The reference parameters, `cp' and `addinfo', need to already have
146 # values when this function is called, otherwise an "uninitialised
147 # value" warning is generated. As far as I can see, there is no way
148 # around this: no way to specify in a prototype that an argument is
149 # allowed to be undefined, for example. Since these function will
150 # never be called directly by well-behaved client code, but only by
151 # our own wrapper classes, I think we can live with that.
153 # The poxing about with cpp and caddinfo is due to Perl XS's lack of
154 # support for const char**, but who can blame it? If you ask me, the
155 # whole "const" thing was well-intentioned by ghastly mistake.
159 ZOOM_connection_error(c, cp, addinfo)
164 const char *ccp, *caddinfo;
165 RETVAL = ZOOM_connection_error(c, &ccp, &caddinfo);
167 addinfo = (char*) caddinfo;
173 # See comments for ZOOM_connection_error() above
176 ZOOM_connection_error_x(c, cp, addinfo, diagset)
179 const char * &addinfo
180 const char * &diagset
182 const char *ccp, *caddinfo, *cdset;
183 RETVAL = ZOOM_connection_error_x(c, &ccp, &caddinfo, &cdset);
185 addinfo = (char*) caddinfo;
186 diagset = (char*) cdset;
195 ZOOM_connection_errcode(c)
200 ZOOM_connection_errmsg(c)
205 ZOOM_connection_addinfo(c)
215 ZOOM_connection_last_event(cs)
220 ZOOM_connection_search(arg0, q)
226 ZOOM_connection_search_pqf(c, q)
232 ZOOM_resultset_destroy(r)
237 ZOOM_resultset_option_get(r, key)
243 ZOOM_resultset_option_set(r, key, val)
250 ZOOM_resultset_size(r)
255 ZOOM_resultset_records(r, start, count, return_values)
263 /*printf("*** ZOOM_resultset_records(r=0x%lx, start=%lu, count=%lx, return_values=%d)\n", (unsigned long) r, (unsigned long) start, (unsigned long) count, return_values);*/
265 /*printf("*** generating space for %ld records\n", (unsigned long) count);*/
266 recs = (ZOOM_record*) xmalloc(count * sizeof *recs);
268 /*printf("*** using null pointer for records array\n");*/
271 ZOOM_resultset_records(r, recs, start, count);
272 /*printf("*** returned from ZOOM_resultset_records()\n");*/
276 for (i = 0; i < count; i++) {
277 SV *tmp = sv_newmortal();
278 printf("*** recs[%d] = %lu\n", i, (unsigned long) recs[i]);
279 /* ### Next line fails. Damn and blast Perl */
280 sv_setref_pv(tmp, "ZOOM_record", (void*) recs[i]);
283 RETVAL = newRV((SV*) av);
284 printf("*** returning array %lu\n", (unsigned long) RETVAL);
286 /*printf("*** returning undef\n");*/
287 RETVAL = &PL_sv_undef;
294 ZOOM_resultset_record(s, pos)
300 ZOOM_resultset_record_immediate(s, pos)
306 ZOOM_resultset_cache_reset(r)
309 # See "typemap" for discussion of the "const char *" return-type.
312 ### but should use datachunk for in some (not all!) cases.
314 ZOOM_record_get(rec, type, len)
324 ZOOM_record_destroy(rec)
329 ZOOM_record_clone(srec)
338 ZOOM_query_destroy(s)
343 ZOOM_query_cql(s, str)
349 ZOOM_query_prefix(s, str)
355 ZOOM_query_sortby(s, criteria)
357 const char * criteria
361 ZOOM_connection_scan(c, startterm)
363 const char * startterm
367 ZOOM_scanset_term(scan, pos, occ, len)
375 ZOOM_scanset_display_term(scan, pos, occ, len)
383 ZOOM_scanset_size(scan)
388 ZOOM_scanset_destroy(scan)
393 ZOOM_scanset_option_get(scan, key)
399 ZOOM_scanset_option_set(scan, key, val)
406 ZOOM_connection_package(c, options)
412 ZOOM_package_destroy(p)
417 ZOOM_package_send(p, type)
423 ZOOM_package_option_get(p, key)
429 ZOOM_package_option_set(p, key, val)
436 ZOOM_resultset_sort(r, sort_type, sort_spec)
438 const char * sort_type
439 const char * sort_spec
441 # We ignore the return value of ZOOM_options_set_callback(), since it
442 # is always just the address of the __ZOOM_option_callback() function.
443 # The information that we actually want -- the address of the Perl
444 # function in the callback_block -- is unavailable to us, as the
445 # underlying C function doesn't give the block back.
449 ZOOM_options_set_callback(opt, function, handle)
454 /* The tiny amount of memory allocated here is never
455 * released, as options_destroy() doesn't do anything
456 * to the callback information. Not a big deal.
457 * Also, I have no idea how to drive the Perl "mortal"
458 * reference-counting stuff, so I am just allocating
459 * copies which also never get released. Don't sue!
461 struct callback_block *block = (struct callback_block*)
462 xmalloc(sizeof *block);
463 block->function = function;
464 block->handle = handle;
465 SvREFCNT(block->function);
466 SvREFCNT(block->handle);
467 ZOOM_options_set_callback(opt, __ZOOM_option_callback,
472 ZOOM_options_create()
476 ZOOM_options_create_with_parent(parent)
481 ZOOM_options_create_with_parent2(parent1, parent2)
487 ZOOM_options_get(opt, name)
493 ZOOM_options_getl(opt, name, len)
498 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
506 ZOOM_options_set(opt, name, v)
513 ZOOM_options_setl(opt, name, value, len)
521 ZOOM_options_destroy(opt)
526 ZOOM_options_get_bool(opt, name, defa)
533 ZOOM_options_get_int(opt, name, defa)
540 ZOOM_options_set_int(opt, name, value)