next up previous contents index
Next: 2.2.3 Exchanging Complex Data Up: 2.2 Lower-Level Foreign Language Previous: 2.2.1 Context Parameters   Contents   Index

2.2.2 Exchanging Basic Data Types

The basic interface assumes that correct modes (i.e., input or output parameters) and types are being passed between the C and Prolog levels. As a result, output unification should be explicitly performed in the Prolog level. The prototypes for the conversion functions between Prolog and C should be declared before the corresponding functions are used. This is done by including the "cinterf.h" header file. Under Unix, the XSB foreign C interface automatically finds this file in the XSB/emu directory. Under Windows (including Cygwin), the user must compile and create the DLL out of the C file manually, so the compiler option `/I...\XSB\emu' is necessary.

The following C functions are used to convert basic types between Prolog and C.

int ptoc_int(CTXTdeclc int N)

CTXTdeclc is a context parameter; N is assumed to hold a Prolog integer corresponding to the Nth argument of a Prolog predicate. This function returns the value of that argument in as a C int.
double ptoc_float(CTXTdeclc int N)

CTXTdeclc is a context parameter; N is assumed to hold a Prolog integer corresponding to the Nth argument of a Prolog predicate. This function returns the value of that argument as a C double. By default, XSB provides double precision, but if XSB was configured with -enable-fast-floats less than single precision can be provided 2.5.
char *ptoc_string(CTXTdeclc int N)

CTXTdeclc is a context parameter; N is assumed to hold a Prolog integer corresponding to the Nth argument of a Prolog predicate. This function returns the value the C string (of type char *) that corresponds to this interned Prolog atom. WARNING: the string should be copied before being manipulated in any way: otherwise unexpected results may arise whenever the interned Prolog atom is unified.
void ctop_int(CTXTdeclc int N, int V)

CTXTdeclc is a context parameter; argument N is assumed to hold a Prolog free variable, and this function binds that variable to an integer of value V.
void ctop_float(CTXTdeclc int N, float V)

CTXTdeclc is a context parameter; argument N is assumed to hold a Prolog free variable, and this function binds that variable to a floating point number of value V.
void extern_ctop_string(CTXTdeclc int N, char * V)

CTXTdeclc is a context parameter; argument N is assumed to hold a Prolog free variable. If needed, this function interns the string to which V points as a Prolog atom and then binds the variable in argument N to that atom.


next up previous contents index
Next: 2.2.3 Exchanging Complex Data Up: 2.2 Lower-Level Foreign Language Previous: 2.2.1 Context Parameters   Contents   Index
Terrance Swift 2007-10-06