10.7. quadpack.sci

Here is the complete example from Section 7.3.3. Function quadpack loads, unloads, or queries the load status of a Scilab extension. In this case the extension is the famous QuadPack library.

The foremost goal in the design of quadpack was user friendliness. Therefore, we condensed the function's interface to its minimum, providing only three different actions:

load

Link library and glue code with Scilab; do nothing if the library/glue code already has been linked. Return the actual linkage status afterwards.

unload

Unload library and glue code; do nothing if the library/glue code was not linked with Scilab before. Return the actual linkage status afterwards.

query

Do nothing, but return the actual linkage status.

Additional goodies are that quadpack defaults to action query if the function's argument is omitted, that the case of the argument does not matter, and that a minimal prefix of the argument is enough to select the right action.


function state = quadpack(action)
// name:        quadpack.sci  --  load/unload QUADPACK or query 
//                                the load-status 
// author:      Lydia van Dijk
// last rev.:   Sat Mar 18 19:23:58 UTC 2000
// Scilab ver.: 2.5

// The variable 'quadpacklibs' is the *only* one that needs
// adjustment on a per-system basis.  It is safe to leave
// all the other stuff untouched.

quadpacklibs = ['/site/src/netlib/quadpack/libquadpack-1.0.so', ..
                '/site/src/netlib/quadpack/intersci/libqpif-1.0.so']

//
// No user servicable parts below this line.
//

gateway = 'quadpack_gw'  // name of the gateway function

// The order of the interface names in
// interfaces *MUST* be the same
// as in qptab in file 'quadpack-gw.c'!
interfaces = ['intals', 'intcau', 'intexc', 'intfou', ..
              'intgen', 'intgk',  'intinf', 'intosc', ..
              'intsm']

[lhs, rhs] = argn()
if rhs == 0 then
    action = 'query'                              -- Default if no args
end

if rhs >= 2 then
    error('Too many arguments; expecting 0 or 1')
end
if type(action) ~= 10 then // 10 means string
    error('Expecting a string in argument 1')
end
if size(action, '*') ~= 1 then
    error('Expecting a scalar in argument 1')
end

action = code2str( abs(str2code(action)) )        -- Convert to lowercase
[state, number] = c_link(gateway)

if strindex('query', action) == 1 then            -- Check prefex
    // do nothing
elseif strindex('load', action) == 1 then
    if state == %t then
        disp('already loaded; no action taken')
        return
    end
    addinter(quadpacklibs, gateway, interfaces)
elseif strindex('unload', action) == 1 then
    if state == %f then
        disp('not loaded; no action taken')
        return
    end
    ulink(number)
else
    error('Expecting ''query'', ''load'' or ''unload'' in arg 1')
end
state = c_link(gateway)                           -- Always return actual status