I've written bulk of PySWIP last summer, it is based on Nathan Denny's proolog.py
.
This is the first project that I used ctypes, a fantastic package which allows calling C functions from dynamic libraries that I use to link libpl.so
(Linux) or libpl.dll
(Windows) of SWI-Prolog.
One of the difficulties I had back then was finding the corresponding ctypes code for PL_get_chars
which is defined at SWI-Prolog.h
as follows:
PL_EXPORT(int) PL_get_chars(term_t t,
char **s, unsigned int flags);
I use that function in queryGenerator
:
answer = (c_char_p*maxsubresult)()
while PL_get_list(swipl_list, swipl_head, swipl_list):
PL_get_chars(swipl_head, answer,
CVT_ALL | CVT_WRITE | BUF_RING)
bindings.append(cstr2pystr(answer))
And, cstr2pystr
is a home-madePython function to convert from a C-string to a Python string in an awful way ;)
That was until I've seen this article in the blog of a good person called Erik Strandberg.
His article is about using ctypes, and especially using ctypes types which inspired me to write the code below (knowing that a function called addressof
existed helped much!), thanks Erik :)
answer = c_char_p("\x00"*MAXSTR)
while PL_get_list(swipl_list, swipl_head, swipl_list):
PL_get_chars(swipl_head, addressof(answer),
CVT_ALL | CVT_WRITE | BUF_RING)
bindings.append(answer.value)