#include <string.h>
#include <epp_pal.h>

int pl_term2long(long *i, pl_term t) {
    return(PL_get_long(t, i));
}

int pl_term2string(const char **s, pl_term t) {
    char *tmp = NULL;
    long retval = PL_get_chars(t, &tmp, CVT_ALL | BUF_DISCARDABLE);
    *s = tmp ? strdup(tmp) : NULL;
    return(retval);
}

int pl_term2float(double *d, pl_term t) {
    return(PL_get_float(t, d));
}

int pl_term2pointer(void **p, pl_term t) {
    return(PL_get_pointer(t, p));
}

int pl_long2term(pl_term t, long i) {
    PL_put_integer(t, i);
    return(PL_TRUE);
}

int pl_string2term(pl_term t, const char *s) {
    PL_put_string_chars(t, s);
    return(PL_TRUE);
}

int pl_float2term(pl_term t, double d) {
    PL_put_float(t, d);
    return(PL_TRUE);
}

int pl_pointer2term(pl_term t, void *p) {
    PL_put_pointer(t, p);
    return(PL_TRUE);
}

int pl_longUterm(pl_term t, long i) {
    return(PL_unify_integer(t, i));
}

int pl_stringUterm(pl_term t, const char *s) {
    return(PL_unify_string_chars(t, s));
}

int pl_floatUterm(pl_term t, double d) {
    return(PL_unify_float(t, d));
}

int pl_pointerUterm(pl_term t, void *p) {
    return(PL_unify_pointer(t, p));
}

int pl_register_predicate(const char *name, unsigned int arity, pl_predicate (*function)()) {
    return(PL_register_foreign(name, arity, function, 0));
}
