static foreign_t pl_clingo_add(term_t ccontrol, term_t params, term_t program) { char *prog; clingo_env *ctl; atom_t name; size_t arity; char *param_buf[FAST_PARAMS]; char **prog_params = param_buf; term_t arg = PL_new_term_ref(); int rc; if (!(rc = get_clingo(ccontrol, &ctl))) { goto out; } if (!get_name_arity(params, &name, &arity)) { rc = PL_type_error("callable", params); goto out; } if (arity + 1 > FAST_PARAMS && !(prog_params = malloc(sizeof(char *) * arity))) { rc = PL_resource_error("memory"); goto out; } for (size_t i = 0; i < arity; i++) { _PL_get_arg(i + 1, params, arg); if (!(rc = get_null_terminated_string(arg, &prog_params[i], CVT_ATOM))) { goto out; } } if (!(rc = get_null_terminated_string(program, &prog, CVT_ATOM | CVT_STRING | CVT_LIST | BUF_DISCARDABLE))) { goto out; } if (!(rc = clingo_status( clingo_control_add(ctl->control, PL_atom_chars(name), (const char **)prog_params, arity, prog)))) { goto out; } out: if (prog_params != param_buf) { free(prog_params); } return rc; }
OptionalField<std::string> get_null_terminated_optional_string(const std::uint8_t* data, const std::uint32_t size) { std::string str = get_null_terminated_string(data, size); if (str.empty()) { return OptionalField<std::string>(); } return OptionalField<std::string>(str); }