/* * call as: equal_datime(T1,T2) * Checks if T1 and T2 are the same date */ int equal_datime(CTXTdecl) { prolog_term t1 = reg_term(CTXTc 1); prolog_term t2 = reg_term(CTXTc 2); if(!is_functor(t1) && !is_functor(t2)) return FALSE; int t1_ts = p2c_int(p2p_arg(t1,1)); int t2_ts = p2c_int(p2p_arg(t2,1)); if (t1_ts==t2_ts) return TRUE; else return FALSE; }
static inline STORAGE_HANDLE *get_storage_handle(CTXTdeclc Cell name, prolog_term trie_type) { STORAGE_HANDLE *handle_cell; handle_cell = find_or_insert_storage_handle(name); /* new buckets are filled out with 0's by the calloc in hashtable_xsb.c */ if (handle_cell->handle==(Cell)0) { /* initialize new handle */ xsb_dbgmsg((LOG_STORAGE, "GET_STORAGE_HANDLE: New trie created for %s\n", string_val(name))); if (is_int(trie_type)) handle_cell->handle= newtrie(CTXTc (int)p2c_int(trie_type)); else xsb_abort("[GET_STORAGE_HANDLE] trie type (3d arg) must be an integer"); /* Note: not necessary to initialize snapshot_number&changed: handle_cell was calloc()'ed handle_cell->snapshot_number=0; handle_cell->changed=FALSE; */ } else xsb_dbgmsg((LOG_STORAGE, "GET_STORAGE_HANDLE: Using existing trie for %s\n", string_val(name))); return handle_cell; }
DllExport int call_conv fibr(CTXTdecl) { int inarg, f1, f2; inarg = p2c_int(reg_term(CTXTc 1)); // get the input argument if (inarg <= 1) { // if small, return answer directly c2p_int(CTXTc 1,reg_term(CTXTc 2)); return TRUE; } xsb_query_save(CTXTc 2); // prepare for calling XSB c2p_functor(CTXTc "fibp",2,reg_term(CTXTc 1)); // setup call c2p_int(CTXTc inarg-1,p2p_arg(reg_term(CTXTc 1),1)); // and inp arg if (xsb_query(CTXT)) { // call XSB printf("Error calling fibp 1.\n"); fflush(stdout); return FALSE; } f1 = p2c_int(p2p_arg(reg_term(CTXTc 1),2)); // get answer if (xsb_close_query(CTXT)) { // throw away other (nonexistent) answers printf("Error closing fibp 1.\n"); fflush(stdout); return FALSE; } c2p_functor(CTXTc "fibp",2,reg_term(CTXTc 1)); // prepare for 2nd call to XSB c2p_int(CTXTc inarg-2,p2p_arg(reg_term(CTXTc 1),1)); // and its inp arg if (xsb_query(CTXT)) { // and call query printf("Error calling fibp 2.\n"); fflush(stdout); return FALSE; } f2 = p2c_int(p2p_arg(reg_term(CTXTc 1),2)); // and get its answer if (xsb_next(CTXT) != XSB_FAILURE) { // get next answer, which must NOT exist printf("Error getting next fibp 2.\n"); fflush(stdout); return FALSE; } if (xsb_query_restore(CTXT)) { // restore regs to prepare for exit printf("Error finishing.\n"); fflush(stdout); return FALSE; } c2p_int(CTXTc f1+f2,reg_term(CTXTc 2)); // set our answer return TRUE; // and return successfully }
/* print a prolog_term into a buffer. (Atoms are quoted if !toplevel and it's necessary for Prolog reading) */ void printpterm(prolog_term term, int toplevel, char *straddr, long int *ind) { int i; if (is_var(term)) { sprintf(tempstring,"_%p",term); strcpy(straddr+*ind,tempstring); *ind += strlen(tempstring); } else if (is_int(term)) { sprintf(tempstring,"%d",p2c_int(term)); strcpy(straddr+*ind,tempstring); *ind += strlen(tempstring); } else if (is_float(term)) { sprintf(tempstring,"%f",p2c_float(term)); strcpy(straddr+*ind,tempstring); *ind += strlen(tempstring); } else if (is_nil(term)) { strcpy(straddr+*ind,"[]"); *ind += 2; } else if (is_string(term)) { printpstring(p2c_string(term),toplevel,straddr,ind); } else if (is_list(term)) { strcpy(straddr+*ind,"["); *ind += 1; printpterm(p2p_car(term),FALSE,straddr,ind); term = p2p_cdr(term); while (is_list(term)) { strcpy(straddr+*ind,","); *ind += 1; printpterm(p2p_car(term),FALSE,straddr,ind); term = p2p_cdr(term); } if (!is_nil(term)) { strcpy(straddr+*ind,"|"); *ind += 1; printpterm(term,FALSE,straddr,ind); } strcpy(straddr+*ind,"]"); *ind += 1; } else if (is_functor(term)) { printpstring(p2c_functor(term),FALSE,straddr,ind); if (p2c_arity(term) > 0) { strcpy(straddr+*ind,"("); *ind += 1; printpterm(p2p_arg(term,1),FALSE,straddr,ind); for (i = 2; i <= p2c_arity(term); i++) { strcpy(straddr+*ind,","); *ind += 1; printpterm(p2p_arg(term,i),FALSE,straddr,ind); } strcpy(straddr+*ind,")"); *ind += 1; } } else fprintf(stderr,"error, unrecognized type"); }
/* * call as: datime_minus_datime(T1,T2,Sec) * Returns Sec as the diff in seconds between T1 and T2 */ int datime_minus_datime(CTXTdecl) { prolog_term t1 = reg_term(CTXTc 1); prolog_term t2 = reg_term(CTXTc 2); if(!is_functor(t1) && !is_functor(t2)) return FALSE; int t1_ts = p2c_int(p2p_arg(t1,1)); int t2_ts = p2c_int(p2p_arg(t2,1)); int diff = 0; if(t1_ts < t2_ts) diff = t2_ts - t1_ts; else diff = t1_ts - t2_ts; c2p_int(CTXTc diff,reg_term(CTXTc 3)); return TRUE; }
/* * call as: next_valid_timestamp(Ts1,Ts2,Interval,NTs) * Returns NTs as the next valid timestamp after Ts2 taking into account the * interval. This is useful for the periodics events */ int next_valid_timestamp(CTXTdecl) { int ts = p2c_int(reg_term(CTXTc 1)); int interval = p2c_int(reg_term(CTXTc 3)); int ts_now = p2c_int(reg_term(CTXTc 2)); int next_ts = 0; if (ts_now <= ts) { next_ts = ts + interval; c2p_int(CTXTc next_ts, reg_term(CTXTc 4)); return TRUE; } int diff = (ts_now - ts); int left_time = diff % interval; next_ts = ts_now + (interval - left_time); c2p_int(CTXTc next_ts, reg_term(CTXTc 4)); return TRUE; }
int epoch2date(CTXTdecl) { time_t epoch = p2c_int(reg_term(CTXTc 7)); struct tm date = *localtime(&epoch); c2p_int(CTXTc 1900 + date.tm_year, reg_term(CTXTc 1)); c2p_int(CTXTc 1 + date.tm_mon, reg_term(CTXTc 2)); c2p_int(CTXTc date.tm_mday, reg_term(CTXTc 3)); c2p_int(CTXTc date.tm_hour, reg_term(CTXTc 4)); c2p_int(CTXTc date.tm_min, reg_term(CTXTc 5)); c2p_int(CTXTc date.tm_sec, reg_term(CTXTc 6)); return TRUE; }
/* * call as: between_datime(+T1,+T2,+T3) * Checks if T1 is between T2 and T3 */ int between_datime(CTXTdecl) { prolog_term t1 = reg_term(CTXTc 1); prolog_term t2 = reg_term(CTXTc 2); prolog_term t3 = reg_term(CTXTc 3); if (!is_functor(t1) && !is_functor(t2) && !is_functor(t3)) return FALSE; int t1_arity = p2c_arity(t1); int t2_arity = p2c_arity(t2); int t3_arity = p2c_arity(t3); int t1_ts = p2c_int(p2p_arg(t1,1)); int t2_ts = p2c_int(p2p_arg(t2,1)); int t3_ts = p2c_int(p2p_arg(t3,1)); int t1_coun = 0; int t2_coun = 0; int t3_coun = 0; if ( t1_arity > 1 && t2_arity > 1 && t3_arity > 1 ) { t1_coun = p2c_int(p2p_arg(t1,2)); t2_coun = p2c_int(p2p_arg(t2,2)); t3_coun = p2c_int(p2p_arg(t3,2)); } return aux_less_datime(t2_ts,t1_ts,t2_coun,t1_coun) && aux_less_datime(t1_ts,t3_ts,t1_coun,t3_coun); }
/* * call as: datime_plus_sec(T1,Sec,T2) * Returns T2 as the result of date T1 plus Sec seconds */ int datime_plus_sec(CTXTdecl) { prolog_term t1 = reg_term(CTXTc 1); if(!is_functor(t1)) return FALSE; int t1_ts = p2c_int(p2p_arg(t1,1)); int sec = p2c_int(reg_term(CTXTc 2)); if (p2c_arity(t1) > 1) { int counter = p2p_arg(t1,2); c2p_functor(CTXTc "datime",2,reg_term(CTXTc 3)); c2p_int(CTXTc t1_ts+sec,p2p_arg(reg_term(CTXTc 3),1)); c2p_int(CTXTc counter,p2p_arg(reg_term(CTXTc 3),2)); } else { c2p_functor(CTXTc "datime",1,reg_term(CTXTc 3)); c2p_int(CTXTc t1_ts+sec,p2p_arg(reg_term(CTXTc 3),1)); } return TRUE; }
/* * call as: less_datime(T1,T2) * Return true if T1 is an older date than T2 */ int less_datime(CTXTdecl) { prolog_term t1 = reg_term(CTXTc 1); prolog_term t2 = reg_term(CTXTc 2); if (!is_functor(t1) || !is_functor(t2)) { return FALSE; } int t1_arity = p2c_arity(t1); int t2_arity = p2c_arity(t2); int t1_ts = p2c_int(p2p_arg(t1,1)); int t2_ts = p2c_int(p2p_arg(t2,1)); int t1_coun = 0; int t2_coun = 0; if ( t1_arity > 1 && t2_arity > 1 ) { t1_coun = p2c_int(p2p_arg(t1,2)); t2_coun = p2c_int(p2p_arg(t2,2)); } return aux_less_datime(t1_ts,t2_ts,t1_coun,t2_coun); }
void *query_rs_err(void * arg) { int rc; th_context *r_th; pthread_mutex_lock(&user_r_ready_mut); r_th = (th_context *)arg; c2p_functor(r_th, "rregs_err",3,reg_term(r_th, 1)); rc = xsb_query(r_th); while (rc == XSB_SUCCESS) { printf("Pre-err return r(%d,%d,%d)\n", (p2c_int(p2p_arg(reg_term(r_th, 2),1))), (p2c_int(p2p_arg(reg_term(r_th, 2),2))), (p2c_int(p2p_arg(reg_term(r_th, 2),3)))); rc = xsb_next(r_th); } if (rc == XSB_ERROR) fprintf(stderr,"Planned query Error r: %s/%s\n", xsb_get_error_type(r_th),xsb_get_error_message(r_th)); pthread_mutex_unlock(&user_r_ready_mut); return NULL; }
void *close_query_rs(void * arg) { int rc; th_context *r_th; pthread_mutex_lock(&user_r_ready_mut); r_th = (th_context *)arg; c2p_functor(r_th, "rregs",3,reg_term(r_th, 1)); rc = xsb_query(r_th); if (rc == XSB_ERROR) fprintf(stderr,"++Closed query Error r: %s/%s\n", xsb_get_error_type(r_th),xsb_get_error_message(r_th)); printf("Closed return r(%d,%d,%d)\n", (p2c_int(p2p_arg(reg_term(r_th, 2),1))), (p2c_int(p2p_arg(reg_term(r_th, 2),2))), (p2c_int(p2p_arg(reg_term(r_th, 2),3)))); rc = xsb_close_query(r_th); if (rc == XSB_FAILURE) fprintf(stderr,"++Failure on closed query (r)!\n"); pthread_mutex_unlock(&user_r_ready_mut); return NULL; }
/* * call as: epoch(+Y,+M,+D,+H,+M,+S,-Sec) * Returns the seconds until the date Y-M-D-H-M-S */ int epoch(CTXTdecl) { int time_sec; struct tm time; time.tm_year = p2c_int(reg_term(CTXTc 1)) - 1900; time.tm_mon = p2c_int(reg_term(CTXTc 2)) - 1 ; time.tm_mday = p2c_int(reg_term(CTXTc 3)); time.tm_hour = p2c_int(reg_term(CTXTc 4)); time.tm_min = p2c_int(reg_term(CTXTc 5)); time.tm_sec = p2c_int(reg_term(CTXTc 6)); time.tm_isdst = -1; time_sec = mktime(&time); c2p_int(CTXTc time_sec, reg_term(CTXTc 7)); return TRUE; }
static xsbBool list_sockfd(prolog_term list, fd_set *fdset, int *max_fd, int **fds, int * size) { int i=0; prolog_term local=list; prolog_term head; *size = getsize(local); *fds = (int*)mem_alloc(sizeof(int)*(*size),OTHER_SPACE); while (!isnil(list)) { head = p2p_car(list); (*fds)[i++] = (int)p2c_int(head); list = p2p_cdr(list); } for (i=0; i<(*size); i++) { /* turn on the bit in the fd_set */ FD_SET((*fds)[i], fdset); *max_fd = xsb_max(*max_fd, (*fds)[i]); } return TRUE; }
int convert_prObj_pyObj(prolog_term prTerm, PyObject **pyObj) { if(find_prolog_term_type(prTerm) == INT) { prolog_term argument = prTerm; int argument_int = p2c_int(argument); *pyObj = PyInt_FromLong(argument_int); return TRUE; }else if(find_prolog_term_type(prTerm) == STRING) { prolog_term argument = prTerm; char *argument_char = p2c_string(argument); *pyObj = PyString_FromString(argument_char); return TRUE; }else if(find_prolog_term_type(prTerm) == FLOAT) { prolog_term argument = prTerm; float argument_float = p2c_float(argument); *pyObj = PyFloat_FromDouble(argument_float); return TRUE; } else if(find_prolog_term_type(prTerm) == LIST) { prolog_term argument = prTerm; if(find_prolog_term_type(argument) == LIST) { int count = find_length_prolog_list(argument); PyObject *pList = PyList_New(count); if(!prlist2pyList(argument, pList, count)) return FALSE; *pyObj = pList; return TRUE; } } // else if(find_prolog_term_type(prTerm) == REF) // { //when a reference is passed. // prolog_term argument = prTerm; // char *argument_ref = p2c_string(argument); // if(strncmp("ref_", argument_ref, 4)== 0 ) // { //gets the reference id from the string and finds it in the list // argument_ref = argument_ref + 4; // size_t ref = strtoul(argument_ref, NULL, 0); // PyObject *obj = get_pyobj_ref_list(ref); // if(obj == NULL) // return FALSE; // *pyObj = obj; // return TRUE; // } // } else if (find_prolog_term_type(prTerm) == FUNCTOR) { //region begin : handle pyobject term if(strcmp(p2c_functor(prTerm),"pyObject") == 0) { prolog_term ref = p2p_arg(prTerm, 1); char *node_pointer = p2c_string(ref); pyobj_ref_node *pyobj_ref = (pyobj_ref_node *)strtol(node_pointer,NULL, 0); //pyobj_ref_node *node = (pyobj_ref_node *)(node_pointer); *pyObj = pyobj_ref->python_obj; } //end region } return FALSE; }
DllExport int call_conv pl_load_page() { prolog_term head, tail, result = 0; char *functor, *url = NULL, *data = NULL; char *username = NULL, *password = NULL; curl_opt options = init_options(); curl_ret ret_vals; check_thread_context tail = reg_term(CTXTc 1); if(!is_list(tail)) return curl2pl_error(ERR_DOMAIN, "source", tail); while(is_list(tail)){ head = p2p_car(tail); tail = p2p_cdr(tail); if(is_functor(head)){ functor = p2c_functor(head); if(!strcmp(functor,"source")){ prolog_term term_url_func, term_url = 0; term_url_func = p2p_arg(head, 1); if(is_functor(term_url_func)){ if(!strcmp(p2c_functor(term_url_func), "url")){ term_url = p2p_arg(term_url_func, 1); url = p2c_string(term_url); data = load_page(url, options, &ret_vals); } else{ return curl2pl_error(ERR_MISC, "source", term_url); } } else{ return curl2pl_error(ERR_MISC, "source", "Improper input format"); } } else if(!strcmp(functor,"options")){ prolog_term term_options = p2p_arg(head, 1); prolog_term term_option; while(is_list(term_options)){ term_option = p2p_car(term_options); if(!strcmp(p2c_functor(term_option), "redirect")) { if(!strcmp(p2c_string(p2p_arg(term_option, 1)), "true")) options.redir_flag = 1; else options.redir_flag = 0; } else if(!strcmp(p2c_functor(term_option), "secure")){ if(!strcmp(p2c_string(p2p_arg(term_option, 1)), "false")) options.secure.flag = 0; else options.secure.crt_name = p2c_string(p2p_arg(term_option, 1)); } else if(!strcmp(p2c_functor(term_option), "auth")){ username = p2c_string(p2p_arg(term_option, 1)); password = p2c_string(p2p_arg(term_option, 2)); options.auth.usr_pwd = (char *) malloc ((strlen(username) + strlen(password) + 2) * sizeof(char)); strcpy(options.auth.usr_pwd, username); strcat(options.auth.usr_pwd, ":"); strcat(options.auth.usr_pwd, password); } else if(!strcmp(p2c_functor(term_option), "timeout")){ options.timeout = (int)p2c_int(p2p_arg(term_option, 1)); } else if(!strcmp(p2c_functor(term_option), "url_prop")){ options.url_prop = options.redir_flag; } else if(!strcmp(p2c_functor(term_option), "user_agent")){ options.user_agent = p2c_string(p2p_arg(term_option, 1)); } else if(!strcmp(p2c_functor(term_option), "post")){ options.post_data = p2c_string(p2p_arg(term_option, 1)); } term_options = p2p_cdr(term_options); } } else if(!strcmp(functor,"document")){ result = p2p_arg(head, 1); } else if(!strcmp(functor,"properties")){ c2p_int(CTXTc (int) ret_vals.size, p2p_arg(head, 1)); /* the following code can be used to convert to local/UTC time, if necessary. Note: XSB uses local time, and ret_vals.modify_time is local, too. struct tm * timeinfo; timeinfo = gmtime(&(ret_vals.modify_time)); // UTC time timeinfo = localtime(&(ret_vals.modify_time)); // local time c2p_int(CTXTc (int) mktime(timeinfo), p2p_arg(head,2)); */ /* return modification time as an integer */ c2p_int(CTXTc (int) ret_vals.modify_time, p2p_arg(head,2)); /* The following converts time to string - not useful if (ctime(&ret_vals.modify_time) == NULL) c2p_string("", p2p_arg(head, 2)); else c2p_string(CTXTc (char *) ctime(&ret_vals.modify_time), p2p_arg(head, 2)); */ } } else{ return curl2pl_error(ERR_DOMAIN, "source", head); } } c2p_string(CTXTc data, result); return TRUE; }
static char* buildSQLQuery(prolog_term sqlQueryList) { prolog_term element; char* sqlQuery; char* temp; int cnt; size_t i; sqlQuery = (char *)malloc(QUERY_SIZE * sizeof(char)+1); sqlQuery[0] = '\0'; while (!is_nil(sqlQueryList)) { element = p2p_car(sqlQueryList); sqlQueryList = p2p_cdr(sqlQueryList); if (is_string(element)) { if (p2c_string(element)[0] == DB_INTERFACE_TERM_SYMBOL) { cnt = 0; temp = (char *)malloc(ELEMENT_SIZE * sizeof(char)+1); temp[cnt++] = '\''; /* protect inner quotes in Prolog terms */ for (i = 0 ; i < strlen(p2c_string(element)) ; i++) { if (p2c_string(element)[i] == '\'') { temp[cnt++] = '\\'; temp[cnt++] = p2c_string(element)[i]; } else { temp[cnt++] = p2c_string(element)[i]; } } temp[cnt++] = '\''; temp[cnt++] = '\0'; strcat(sqlQuery, temp); if (temp != NULL) { free(temp); temp = NULL; } } else { strcat(sqlQuery, p2c_string(element)); } } else if (is_int(element)) { temp = (char *)malloc(ELEMENT_SIZE * sizeof(char)); sprintf(temp, "%d", (int)p2c_int(element)); strcat(sqlQuery, temp); if (temp != NULL) { free(temp); temp = NULL; } } else if (is_float(element)) { temp = (char *)malloc(ELEMENT_SIZE * sizeof(char)); sprintf(temp, "%f", p2c_float(element)); strcat(sqlQuery, temp); if (temp != NULL) { free(temp); temp = NULL; } } else if (is_var(element)) { errorMesg = "XSB_DBI ERROR: Unbound variable in parameter list"; } } return sqlQuery; }
DllExport int call_conv executePreparedStatement(void) { struct xsb_data** (*executeStmtDriver)(struct xsb_data**, struct xsb_queryHandle*); char* (*errorMesgDriver)(); void (*freeResultDriver)(); struct xsb_queryHandle* qHandle; struct xsb_connectionHandle* cHandle; struct xsb_data** bindValues; struct xsb_data** result; prolog_term bindList, returnList, element; char *queryHandle, *chandle; int i, val; queryHandle = ptoc_string(CTXTc 1); bindList = reg_term(CTXTc 2); returnList = reg_term(CTXTc 3); qHandle = NULL; cHandle = NULL; bindValues = NULL; result = NULL; if ((qHandle = isQueryHandle(queryHandle)) == NULL) { errorMesg = "XSB_DBI ERROR: Query handle does not exist"; errorNumber = "XSB_DBI_005"; return FALSE; } if (qHandle->state == QUERY_BEGIN) { bindValues = (struct xsb_data **)malloc(qHandle->numParams * sizeof(struct xsb_data *)); for (i = 0 ; i < qHandle->numParams ; i++) bindValues[i] = NULL; for (i = 0 ; i < qHandle->numParams ; i++) { bindValues[i] = (struct xsb_data *)malloc(sizeof(struct xsb_data)); bindValues[i]->val = NULL; if (is_nil(bindList)) { errorMesg = "XSB_DBI ERROR: Not all paremeters supplied"; errorNumber = "XSB_DBI_008"; freeBindValues(bindValues,qHandle->numParams); return FALSE; } element = p2p_car(bindList); if (is_string(element)) { bindValues[i]->type = STRING_TYPE; bindValues[i]->length = strlen(p2c_string(element)); bindValues[i]->val = (union xsb_value *)malloc(sizeof(union xsb_value)); bindValues[i]->val->str_val = p2c_string(element); } else if (is_int(element)) { bindValues[i]->type = INT_TYPE; bindValues[i]->val = (union xsb_value *)malloc(sizeof(union xsb_value)); bindValues[i]->val->i_val = p2c_int(element); } else if (is_float(element)) { bindValues[i]->type = FLOAT_TYPE; bindValues[i]->val = (union xsb_value *)malloc(sizeof(union xsb_value)); bindValues[i]->val->f_val = p2c_float(element); } else if (is_functor(element)) { } else if (is_var(element)) { errorMesg = "XSB_DBI ERROR: Unbound variable in parameter list"; errorNumber = "XSB_DBI_009"; freeBindValues(bindValues,qHandle->numParams); return FALSE; } bindList = p2p_cdr(bindList); } } if (getDriverFunction(qHandle->connHandle->driver, EXEC_PREPARE) != NULL) executeStmtDriver = getDriverFunction(qHandle->connHandle->driver, EXEC_PREPARE)->executeStmtDriver; else{ freeBindValues(bindValues,qHandle->numParams); return FALSE; } result = executeStmtDriver(bindValues, qHandle); freeBindValues(bindValues,qHandle->numParams); if (result == NULL && qHandle->state == QUERY_BEGIN) { if (getDriverFunction(qHandle->connHandle->driver, ERROR_MESG) != NULL) errorMesgDriver = getDriverFunction(qHandle->connHandle->driver, ERROR_MESG)->errorMesgDriver; else return FALSE; errorMesg = errorMesgDriver(); if (errorMesg != NULL) return FALSE; } val = bindReturnList(returnList, result, qHandle); if (result == NULL) { qHandle->state = QUERY_BEGIN; } else { if (getDriverFunction(qHandle->connHandle->driver, FREE_RESULT) != NULL) freeResultDriver = getDriverFunction(qHandle->connHandle->driver, FREE_RESULT)->freeResultDriver; else return FALSE; freeResultDriver(result, qHandle->numResultCols); } if (val == TOO_MANY_RETURN_COLS || val == TOO_FEW_RETURN_COLS || val == INVALID_RETURN_LIST) return FALSE; cHandle = qHandle->connHandle; chandle = cHandle->handle; if ((cHandle = isConnectionHandle(chandle)) != NULL) { if (getDriverFunction(cHandle->driver, ERROR_MESG) != NULL) errorMesgDriver = getDriverFunction(cHandle->driver, ERROR_MESG)->errorMesgDriver; else return FALSE; errorMesg = errorMesgDriver(); errorNumber = "XSB_DBI_000"; } /* if (errorMesg == NULL && (val == RESULT_NONEMPTY_OR_NOT_REQUESTED || val == RESULT_EMPTY_BUT_REQUESTED)){ */ if (errorMesg == NULL && val == RESULT_NONEMPTY_OR_NOT_REQUESTED) { return TRUE; } else return FALSE; }
int main(int argc, char *argv[]) { int rcode; int myargc = 3; char *myargv[3]; /* xsb_init relies on the calling program to pass the absolute or relative path name of the XSB installation directory. We assume that the current program is sitting in the directory .../examples/c_calling_xsb/ To get installation directory, we strip 3 file names from the path. */ myargv[0] = strip_names_from_path(xsb_executable_full_path(argv[0]), 3); myargv[1] = "-n"; myargv[2] = "-e writeln(hello). writeln(kkk)."; /* Initialize xsb */ xsb_init(myargc,myargv); /* depend on user to put in right options (-n) */ /* Create command to consult a file: ctest.P, and send it. */ c2p_functor("consult",1,reg_term(1)); c2p_string("ctest",p2p_arg(reg_term(1),1)); if (xsb_command()) { printf("Error consulting ctest.P.\n"); fflush(stdout); } if (xsb_command_string("consult(basics).")) { printf("Error (string) consulting basics.\n"); fflush(stdout); } /* Create the query p(300,X,Y) and send it. */ c2p_functor("p",3,reg_term(1)); c2p_int(300,p2p_arg(reg_term(1),1)); rcode = xsb_query(); /* Print out answer and retrieve next one. */ while (!rcode) { if (!(is_string(p2p_arg(reg_term(2),1)) & is_string(p2p_arg(reg_term(2),2)))) printf("2nd and 3rd subfields must be atoms\n"); else printf("Answer: %d, %s(%s), %s(%s)\n", p2c_int(p2p_arg(reg_term(1),1)), p2c_string(p2p_arg(reg_term(1),2)), xsb_var_string(1), p2c_string(p2p_arg(reg_term(1),3)), xsb_var_string(2) ); fflush(stdout); rcode = xsb_next(); } /* Create the string query p(300,X,Y) and send it, use higher-level routines. */ xsb_make_vars(3); xsb_set_var_int(300,1); rcode = xsb_query_string("p(X,Y,Z)."); /* Print out answer and retrieve next one. */ while (!rcode) { if (!(is_string(p2p_arg(reg_term(2),2)) & is_string(p2p_arg(reg_term(2),3)))) printf("2nd and 3rd subfields must be atoms\n"); else printf("Answer: %d, %s, %s\n", xsb_var_int(1), xsb_var_string(2), xsb_var_string(3) ); fflush(stdout); rcode = xsb_next(); } /* Close connection */ xsb_close(); printf("cmain exit\n"); return(0); }
int set_python_argument(prolog_term temp, PyObject *pArgs,int i) { PyObject *pValue; if(find_prolog_term_type(temp) == INT) { prolog_term argument = temp; int argument_int = p2c_int(argument); pValue = PyInt_FromLong(argument_int); PyTuple_SetItem(pArgs, i-1, pValue); }else if(find_prolog_term_type(temp) == STRING) { prolog_term argument = temp; char *argument_char = p2c_string(argument); //printf("%s", argument_char); pValue = PyString_FromString(argument_char); PyTuple_SetItem(pArgs, i-1, pValue); }else if(find_prolog_term_type(temp) == FLOAT) { prolog_term argument = temp; float argument_float = p2c_float(argument); pValue = PyFloat_FromDouble(argument_float); PyTuple_SetItem(pArgs, i-1, pValue); } else if(find_prolog_term_type(temp) == LIST) { prolog_term argument = temp; int count = find_length_prolog_list(argument); PyObject *pList = PyList_New(count); if (! prlist2pyList(argument, pList, count)) return FALSE; PyTuple_SetItem(pArgs, i-1, pList); } else if (find_prolog_term_type(temp) == FUNCTOR) { //region begin : handle pyobject term if(strcmp(p2c_functor(temp),"pyObject") == 0) { prolog_term ref = p2p_arg(temp, 1); char *node_pointer = p2c_string(ref); pyobj_ref_node *pyobj_ref = (pyobj_ref_node *)strtol(node_pointer,NULL, 0); //pyobj_ref_node *node = (pyobj_ref_node *)(node_pointer); pValue = pyobj_ref->python_obj; //printf("set_argN: %p %zu" , pValue, pyobj_ref->ref_id); PyTuple_SetItem(pArgs, i - 1 , pValue); } //end region } // else if(find_prolog_term_type(temp) == REF) // { //when a reference is passed. // prolog_term argument = temp; // char *argument_ref = p2c_string(argument); // if(strncmp("ref_", argument_ref, 4)== 0 ) // { //gets the reference id from the string and finds it in the list // argument_ref = argument_ref + 4; // size_t ref = strtoul(argument_ref, NULL, 0); // PyObject *obj = get_pyobj_ref_list(ref); // if(obj == NULL) // return FALSE; // PyTuple_SetItem(pArgs, i-1, obj); // } // } return TRUE; }