示例#1
0
/* calculate approximate length of a printed term.  For space alloc. */
DWORD clenpterm(prolog_term term)
{
  int i, clen;

  if (is_var(term)) return 11;
  else if (is_int(term)) return 12;
  else if (is_float(term)) return 12;
  else if (is_nil(term)) return 2;
  else if (is_string(term)) return strlen(p2c_string(term))+5;
  else if (is_list(term)) {
      clen = 1;
      clen += clenpterm(p2p_car(term)) + 1;
      while (is_list(term)) {
          clen += clenpterm(p2p_car(term)) + 1;
          term = p2p_cdr(term);
      }
      if (!is_nil(term)) {
          clen += clenpterm(term) + 1;
      }
      return clen+1;
  } else if (is_functor(term)) {
      clen = strlen(p2c_functor(term))+5;
      if (p2c_arity(term) > 0) {
          clen += clenpterm(p2p_arg(term,1)) + 1;
          for (i = 2; i <= p2c_arity(term); i++) {
              clen += clenpterm(p2p_arg(term,i)) + 1;
          }
          return clen + 1;
      } else return clen;
  } else {
      fprintf(stderr,"error, unrecognized type");
      return 0;
  }
}
示例#2
0
DllExport int call_conv set_nq_prefix(CTXTdecl)	
{
	prolog_term arg = reg_term(CTXTdecl 1);
	prefix = p2c_string(CTXTdecl arg);

	return 1;
}
示例#3
0
DllExport int call_conv load_nquad_file(CTXTdecl)
{
	char *filename = p2c_string(reg_term(CTXTdecl 1));

	unsigned char *uri_string;
	raptor_uri *uri, *base_uri;

	world = raptor_new_world();

	rdf_parser = raptor_new_parser(world, "nquads");

	raptor_parser_set_statement_handler(rdf_parser, NULL, handle_term);

	uri_string = raptor_uri_filename_to_uri_string(filename);
	uri = raptor_new_uri(world, uri_string);
	base_uri = raptor_uri_copy(uri);

	raptor_parser_parse_file(rdf_parser, uri, base_uri);

	raptor_free_parser(rdf_parser);

	raptor_free_uri(base_uri);
	raptor_free_uri(uri);
	raptor_free_memory(uri_string);

	raptor_free_world(world);

	return 1;
}
示例#4
0
int is_reference(prolog_term term)
{
	char *result = p2c_string(term);
	if(strncmp("ref_", result, 4)== 0 ){
		return 1;
	}
	return 0;
}
示例#5
0
/* 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");
}
示例#6
0
/*******************************************************************
Error-throwing queries
*********************************************************************/
void *query_ps_err(void * arg) {
  int rc;
  th_context *p_th;
  pthread_mutex_lock(&user_p_ready_mut); 
  p_th = (th_context *)arg;
  
  c2p_functor(p_th, "pregs_err",3,reg_term(p_th, 1));
  rc = xsb_query(p_th);
  while (rc == XSB_SUCCESS) {
    printf("Pre-err return p(%s,%s,%s)\n",
	   (p2c_string(p2p_arg(reg_term(p_th, 2),1))),
	   (p2c_string(p2p_arg(reg_term(p_th, 2),2))),
	   (p2c_string(p2p_arg(reg_term(p_th, 2),3))));
    rc = xsb_next(p_th);
  }

 if (rc == XSB_ERROR) 
   fprintf(stderr,"Planned query Error p: %s/%s\n",
	   xsb_get_error_type(p_th),xsb_get_error_message(p_th));
  pthread_mutex_unlock(&user_p_ready_mut); 
 return NULL;
}
示例#7
0
/*******************************************************************
Closed queries
*********************************************************************/
void *close_query_ps(void * arg) {
  int rc;
  th_context *p_th;
  pthread_mutex_lock(&user_p_ready_mut); 
  p_th = (th_context *)arg;
  
  c2p_functor(p_th, "pregs",3,reg_term(p_th, 1));
  rc = xsb_query(p_th);
  if (rc == XSB_ERROR) 
    fprintf(stderr,"++Closed query Error p: %s/%s\n",
	    xsb_get_error_type(p_th),xsb_get_error_message(p_th));
  printf("Closed return p(%s,%s,%s)\n",
	   (p2c_string(p2p_arg(reg_term(p_th, 2),1))),
	   (p2c_string(p2p_arg(reg_term(p_th, 2),2))),
	   (p2c_string(p2p_arg(reg_term(p_th, 2),3))));
  rc = xsb_close_query(p_th);
  if (rc == XSB_FAILURE) 
    fprintf(stderr,"++Failure on closed query (p)!\n");

 pthread_mutex_unlock(&user_p_ready_mut); 
 return NULL;
}
示例#8
0
文件: pita_lib.c 项目: jianqiao/code
void create_dotc(int node,prolog_term filenameatom)
{
    char * onames[]= {"Out"};
    char ** inames;
    DdNode * array[1];
    int i,b,index;
    variable v;
    char numberVar[10],numberBit[10];
    FILE * file;
    char * filename;

    filename=p2c_string(filenameatom);
    inames= (char **) malloc(sizeof(char *)*boolVars);
    index=0;
    for (i=0; i<nVars; i++)
    {
        v=vars[i];
        for (b=0; b<v.nVal-1; b++)
        {
            inames[b+index]=(char *) malloc(sizeof(char)*20);
            strcpy(inames[b+index],"X");
            sprintf(numberVar,"%d",i);
            strcat(inames[b+index],numberVar);
            strcat(inames[b+index],"_");
            sprintf(numberBit,"%d",b);
            strcat(inames[b+index],numberBit);
        }
        index=index+v.nVal-1;
    }
    array[0]=(DdNode *)node;
    file = open_file(filename, "w");

    Cudd_DumpDot(mgr,1,array,inames,onames,file);
    fclose(file);
    index=0;
    for (i=0; i<nVars; i++)
    {
        v=vars[i];
        for (b=0; b<v.nVal-1; b++)
            free(inames[b+index]);
        index=index+v.nVal-1;
    }
    free(inames);
}
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;
}
示例#12
0
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);
}
示例#13
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;
}
示例#14
0
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;
}