The functions from this section are intended to read a Prolog list from C. Suppose we expect a list of atoms; the code below will print the atoms, each on a line. Please note the following:
term_t
term reference for the
elements (head). This reference is reused for each element.term_t
. As it is not allowed to
overwrite the term_t
passed in as arguments to a predicate,
we must
copy the argument term_t
.char*
. We want it to convert atoms, return the
result as a multibyte string (REP_UTF8
may also be
used) and finally we want an exception on type, instantiation or
representation errors (if the system's default encoding cannot represent
some characters of the Unicode atom). This may create temporary copies
of the atom text - PL_STRINGS_MARK() ... PL_STRINGS_RELEASE()
handles that._ex
suffix, but they raise type, domain,
or instantiation errors when the input is invalid; whereas the plain
version may only raise resource exceptions if the request cannot be
fullfilled due to resource exhaustion.foreign_t pl_write_atoms(term_t l) { term_t head = PL_new_term_ref(); /* the elements */ term_t tail = PL_copy_term_ref(l); /* copy (we modify tail) */ int rc = TRUE; while( rc && PL_get_list_ex(tail, head, tail) ) { PL_STRINGS_MARK(); char *s; if (rc=PL_get_chars(head, &s, CVT_ATOM|REP_MB|CVT_EXCEPTION)) ) rc = Sfprintf(Scurrent_output, "%s\n", s); PL_STRINGS_RELEASE(); } return rc && PL_get_nil_ex(tail); /* test end for [] */ }
Note that as of version 7, lists have a new representation unless the option --traditional is used. see section 5.1.
It is allowed to pass 0 for tail and NULL
for len.