| Did you know ... | Search Documentation: |
| PL_get_list_slice() |
(size_t)-1, call func
for all elements from start to the end of the list. The type
of func is below. The argument index is the index
of the element in the slice, i.e., it starts at 0 for the element start.
typedef int (*pl_list_slice_f)(term_t elem, size_t index, void *closure);
The term reference elem is reused for all elements and is
only valid during the call to func. func may call
any API function, including functions that create terms or call Prolog.
If
func returns FALSE, PL_get_list_slice()
stops and returns
FALSE, leaving an exception raised by func in
place. If
count is not NULL, it is filled with the number
of elements processed.
The function returns FALSE if the list is too short, if
length is (size_t)-1 and the list is partial,
does not end in or is cyclic, or if the
list is partial, not a list or cyclic before the end of the slice. If flags
includes
[]CVT_EXCEPTION, a partial list raises an instantiation error
and a cyclic list or a term that is not a list raises a type error. A
list that is too short does not raise an exception. Cycles are detected
without extra cost for short lists. Below is the list example using this
function:
static int
write_atom(term_t a, size_t index, void *closure)
{ int rc;
char *s;
PL_STRINGS_MARK();
if ( (rc=PL_get_chars(a, &s, CVT_ATOM|REP_MB|CVT_EXCEPTION)) )
rc = Sfprintf(closure, "%s\n", s) >= 0;
PL_STRINGS_RELEASE();
return rc;
}
foreign_t
pl_write_atoms(term_t l)
{ return PL_get_list_slice(l, 0, (size_t)-1,
write_atom, Scurrent_output,
NULL, CVT_EXCEPTION);
}