Ejemplo n.º 1
0
/*
 * 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);
}
Ejemplo n.º 2
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;
  }
}
Ejemplo n.º 3
0
/*
 * 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;
}
Ejemplo n.º 4
0
bool Type::is_object() const {
    Class* cls = clazz();
    return is_object_proto() || is_functor()
        || (cls && cls->proto()->is_object_proto())
        || (cls && !cls->proto()->is_proto()); 
    // Note: If the user specified an invalid proto, then error out
}
Ejemplo n.º 5
0
//todo: need to refactor this code.
int callpy(CTXTdecl)
{
	setenv("PYTHONPATH", ".", 1);
	PyObject *pName = NULL, *pModule = NULL, *pFunc = NULL;
	PyObject *pArgs = NULL, *pValue = NULL;
	//PyObject *pArgs, *pValue;
	prolog_term V, temp;
	Py_Initialize();
	char *module = ptoc_string(CTXTdeclc 1);
	//char *function = ptoc_string(CTXTdeclc 2);
	pName = PyString_FromString(module);
	pModule = PyImport_Import(pName);
	if(pModule == NULL)
	{
		return FALSE;	
	}
	Py_DECREF(pName);
	V = extern_reg_term(2);
	char *function = p2c_functor(V);
	if(is_functor(V))
	{
		int args_count = p2c_arity(V);

		pFunc = PyObject_GetAttrString(pModule, function);
		Py_DECREF(pModule);
		if(pFunc && PyCallable_Check(pFunc))
		{
			pArgs = PyTuple_New(args_count);
			int i;
			for(i = 1; i <= args_count; i++)
			{
				temp = p2p_arg(V, i);
				if(!(set_python_argument(temp, pArgs, i)))
				{
					return FALSE;
				}
			}

		}
		else
		{
			return FALSE;
		}
		
		pValue = PyObject_CallObject(pFunc, pArgs);
		//printf("return call : %p\n",pValue); 
		if(return_to_prolog(pValue))
			return TRUE;
		else
			return FALSE;
	}
	else
	{
		return FALSE;
	}
	return TRUE;
}
Ejemplo n.º 6
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");
}
Ejemplo n.º 7
0
/*
 * call as: is_datime(+T)
 *  Returns true if T is a datime (with or without counter)
 */
int is_datime(CTXTdecl)
{
    prolog_term t = reg_term(CTXTc 1);
    if(is_functor(t))
    {
        char *func = p2c_functor(t);
        if(strcmp(func,"datime")==0)
            return TRUE;
    }
    return FALSE;
}
Ejemplo n.º 8
0
/*
 * 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;
}
Ejemplo n.º 9
0
/*
 * 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);
}
Ejemplo n.º 10
0
/* call as: current_datime(-D)
    Returns the predicate datime(D) where D is the current time in ms*/
int current_datime(CTXTdecl)
{
    time_t now;
    time(&now);
   
    prolog_term t = reg_term(CTXTc 1);
    if (is_functor(t))
    {
        char *func = p2c_functor(t);
        if(strcmp(func,"datime")==0)
        {
            c2p_int(CTXTc now, p2p_arg(reg_term(CTXTc 1),1));
            return TRUE;
        }
        return FALSE;
    }
    c2p_functor(CTXTc "datime",1,reg_term(CTXTc 1));
    c2p_int(CTXTc now, p2p_arg(reg_term(CTXTc 1),1));
    return TRUE;
}
Ejemplo n.º 11
0
int find_prolog_term_type(prolog_term term)
{
	if(is_float(term))
		return FLOAT;
	else if(is_int(term))
		return INT;
	else if(is_string(term)){	
		//printf("found string");
		return STRING;
	}
	else if(is_list(term))
		return LIST;
	else if(is_var(term))
		return VAR;
	else if(is_nil(term))
		return NIL;
	else if(is_functor(term))
		return FUNCTOR;
	else 
		return -1;
}
Ejemplo n.º 12
0
/*
 * 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;
}
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;
}
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;
}