Several Routines in one CSUB Context
|
This example shows how to combine several routines into one CSUB context. It shows five C routines and five HTBasic SUB routines all defined in one CSUB context. The entire CSUB context can be loaded by one LOADSUB and deleted by one DELSUB statement that mentions the first CSUB’s name.
Prototype SUB program lines:
BASIC Program
10 ! prototype SUB program lines:
20 END
30 SUB Integ(INTEGER A)
40 SUBEND
50 SUB Real(A)
60 SUBEND
70 SUB Isum(INTEGER A(*),S)
80 SUBEND
90 SUB Strint(A$)
100 SUBEND
110 SUB Streint(A$(*),INTEGER E)
120 SUBEND
C Programs
#include "csub.h"
#include <string.h>
integ( npar, a ) /* INTEGER C SUB routine */
int npar; /* number of parameters */
intptr a; /* integer data pointer */
{
*a *= 3; /* return the new value */
return( 0 ); /* no errors */
}
real( npar, a ) /* REAL C SUB routine */
int npar; /* number of parameters */
realptr a; /* real data pointer */
{
*a /= 4.0; /* return a new value */
return( 0 ); /* no errors */
}
isum( npar, a, d, s ) /* Integer Array C SUB routine */
int npar; /* number of parameters */
intptr a; /* array data pointer */
dimptr d; /* array dim pointer */
intptr s; /* sum data pointer */
{
int i; /* element counter */
int max = d->cae; /* get number of elements */
int sum = 0L; /* clear the sum */
for(i = 0; i < max; ++i) /* scan all elements of array */
sum += a[i];
*s = (T_INT)sum; /* return the sum value */
return( 0 ); /* no error */
}
strint( npar, a, d ) /* String C SUB routine */
int npar; /* number of parameters */
strptr a; /* string data pointer */
dimptr d; /* string dimension pointer */
{
if( d->elen < 25 ) /* check variable length */
return( 18 ); /* too small, return error */
memcpy( a->str, "This is a test of strings", 25);
a->clen = 25; /* set the length */
return( 0 ); /* no error */
}
streint( npar, a, d, e ) /* String Array C SUB routine */
int npar; /* number of parameters */
strptr a; /* string data pointer */
dimptr d; /* dimension pointer */
intptr e; /* element number to set */
{
strptr t; /* string element pointer */
int maxsize = d->elen; /* get element size */
int zbe; /* zero base element number */
zbe = *e - d->sbs[0].base; /* zero based element number */
if( zbe > d-cae ) /* check element number */
return( 18 ); /* too large, return error */
if( maxsize > 25 ) /* check variable length */
return( 18 ); /* too small, return error */
/* Notice the string element address calculation */
if( maxsize & 1) /* if odd length */
++maxsize; /* round up to next even length */
t = (strptr)((U_CHAR *)a + ((maxsize + sizeof(T_SUBS)) * zbe));
memcpy( t->str, "This is a test of strings", 25);
t->clen = 25; /* set the length */
return( 0 ); /* no error */
}
All the preceding C language examples are on the distribution media. The files used to create and test the CSUB are included for each example. You may use these routines as a model to implement CSUB routines.