next up previous contents index
Next: Examples of Using the Up: Passing Data between XSB Previous: Passing Data between XSB   Contents   Index

Exchanging Basic Data Types

The basic interface assumes that correct modes (i.e., input or output) and types are being passed between C and the Prolog level. So, output unification should be explicitly performed in the Prolog level. The function prototypes 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 Prolog and C data types to each other.

int ptoc_int(int N)

Argument N is assumed to hold a Prolog integer, and this function returns its integer value in C format.
float ptoc_float(int N)

Argument N is assumed to hold a Prolog floating point number, and this function returns its floating point value in C format. (Precision is less than single word floating point).
char *ptoc_string(int N)

Argument N is assumed to hold a Prolog atom, and this function returns the C string (of type char *) that corresponds to this Prolog atom.
void ctop_int(int N, int V)

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(int N, float V)

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 ctop_string(int N, char * V)

Argument N is assumed to hold a Prolog free variable, and this function binds that variable to a Prolog atom of value V. In C, V is of type char *.

Note that the atom of value V is not interned, i.e. it is not inserted into the Prolog atom table. For that reason, the string_find(char *V, int Insert) function should be used. Function string_find() searches the symbol table for the symbol, and if the symbol does not appear there and the value of Insert is non-zero, it inserts it. Thus, the most common use of this function is as follows:

ctop_string(N, string_find(V, 1))
Refer to the example simple_foreign in the examples directory to see a use of this function.



Subsections
next up previous contents index
Next: Examples of Using the Up: Passing Data between XSB Previous: Passing Data between XSB   Contents   Index
Luis Fernando P. de Castro 2003-06-27