Now we have all the enumerations and structures declared we now have to wrap the C functions. In general, to convert a C function declaration to Pyrex you:
cdef extern to the beginning
of the line,
const keywords, ; from the end of the line,
and(void) with
().Consider the following C function declaration.
xosd *xosd_init (char *font, char *colour, int timeout,
xosd_pos pos, int offset,
int shadow_offset);
It declares a function, xosd_init, which takes
six arguments including one enumeration (pos).
The Pyrex declaration of the same function looks very
similar.
cdef extern xosd *xosd_init(char *font, char *colour,
int timeout, xosd_pos pos,
int offset, int shadow_offset)
.h file.C, like Python, can handle variable length arguments. The conversion between C var-args and Pyrex var-args is fairly strait forward. Consider the function declaration:
int xosd_display (xosd *osd, int line,
xosd_command command, ...);
After the three compulsory arguments (osd,
line and command), the function
xosd_display can take a variable number of
arguments.
To emulate this in Pyrex we also write ellipsis:
cdef extern int xosd_display(xosd *osd, int line,
xosd_command command,
...)
Be warned, things get more complicated later on.
Call by reference is done in a similar way to call by value. Consider the following.
extern int cupsGetDests(cups_dest_t **dests);
Here dests will be modified when
cupsGetDests returns.
In Pyrex, the call would look like the following.
cdef extern int cupsGetDests(cups_dest_t **dests)
We have to have declared the argument, cups_dest_t,
before we declared the function.
| Previous | Contents | Next |