int main(int argc, char *argv[]) { #ifdef MULTI_THREAD static th_context *p_th, *r_th; #endif char init_string[MAXPATHLEN]; int rcp, rcr; XSB_StrDefine(p_return_string); XSB_StrDefine(r_return_string); /* xsb_init_string 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. */ strcpy(init_string,strip_names_from_path(xsb_executable_full_path(argv[0]),3)); if (xsb_init_string(init_string)) { fprintf(stderr,"%s initializing XSB: %s\n",xsb_get_error_type(xsb_get_main_thread()), xsb_get_error_message(xsb_get_main_thread())); exit(XSB_ERROR); } p_th = xsb_get_main_thread(); /* Create command to consult a file: edb.P, and send it. */ if (xsb_command_string(p_th, "consult('edb.P').") == XSB_ERROR) fprintf(stderr,"++Error consulting edb.P: %s/%s\n",xsb_get_error_type(p_th), xsb_get_error_message(p_th)); xsb_ccall_thread_create(p_th,&r_th); if (xsb_command_string(r_th,"import thread_exit/0 from thread.") == XSB_ERROR) fprintf(stderr,"++Error exiting r: %s/%s\n",xsb_get_error_type(r_th), xsb_get_error_message(r_th)); rcp = xsb_query_string_string(p_th,"p(X,Y,Z).",&p_return_string,"|"); rcr = xsb_query_string_string(r_th,"r(X,Y,Z).",&r_return_string,"|"); while (rcp == XSB_SUCCESS && rcr == XSB_SUCCESS) { printf("Return p %s\n",(p_return_string.string)); rcp = xsb_next_string(p_th, &p_return_string,"|"); printf("Return r %s\n",(r_return_string.string)); rcr = xsb_next_string(r_th, &r_return_string,"|"); } if (rcp == XSB_ERROR) fprintf(stderr,"++Query Error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); if (rcr == XSB_ERROR) fprintf(stderr,"++Query Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); // xsb_kill_thread(r_th); xsb_close(xsb_get_main_thread()); /* Close connection */ return(0); }
int main (int argc, char** argv) { XSB_StrDefine(foo); XSB_StrDefine(boo); XSB_StrDefine(goo); VarString *loo; XSB_StrSet(&foo, "abc"); printf("foo1: %s %d/%d\n", foo.string, foo.length, foo.size); foo.op->append(&foo, "123"); printf("foo2: %s %d/%d\n", foo.string, foo.length, foo.size); foo.op->prepend(&foo, "098"); printf("foo3: %s %d/%d\n", foo.string, foo.length, foo.size); boo.op->prepend(&boo, "booooooo"); printf("boo: %s %d/%d\n", boo.string, boo.length, boo.size); boo.op->shrink(&boo, 3); boo.op->appendv(&boo, &foo); loo = &boo; printf("boo2: %s %d/%d\n", loo->string, boo.length, boo.size); foo.op->shrink(&foo,60); if (foo.length > 0) printf("foo4: %s %d/%d\n", foo.string, foo.length, foo.size); boo.op->set(&boo,"123"); XSB_StrAppend(loo,"---"); printf("boo3: %s %d/%d\n", loo->string, boo.length, boo.size); boo.op->append(&boo,"4567"); printf("boo4: %s %d/%d\n", loo->string, boo.length, boo.size); printf("initialized: boo=%p foo=%p goo=%p\n", boo.string, foo.string, goo.string); XSB_StrSetV(loo, &foo); printf("boo5: %s %d/%d\n", loo->string, boo.length, boo.size); XSB_StrEnsureSize(loo, 1000); XSB_StrAppendBlk(loo,NULL,5); XSB_StrAppend(loo,NULL); XSB_StrDestroy(&foo); XSB_StrDestroy(loo); goo.op->destroy(&goo); }
int main(int argc, char *argv[]) { int rc; /* xsb_init_string 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. */ int myargc = 1; char *myargv[1]; #ifdef WIN_NT VarString return_string, *rs; XSB_StrCreate(&rs); XSB_StrInit(rs); return_string = *rs; #else XSB_StrDefine(return_string); #endif myargv[0] = argv[1]; if (xsb_init(myargc,myargv)) { fprintf(stderr,"%s initializing XSB: %s\n",xsb_get_init_error_type(),xsb_get_init_error_message()); exit(XSB_ERROR); } #ifdef MULTI_THREAD th_context *th = xsb_get_main_thread(); #endif /* Create command to consult a file: edb.P, and send it. */ if (xsb_command_string(CTXTc "consult('edb.P').") == XSB_ERROR) fprintf(stderr,"++Error consulting edb.P: %s/%s\n",xsb_get_error_type(CTXT), xsb_get_error_message(CTXT)); rc = xsb_query_string_string(CTXTc "p(X,Y,Z).",&return_string,"|"); while (rc == XSB_SUCCESS) { printf("cvsreturn %s\n",(return_string.string)); rc = xsb_next_string(CTXTc &return_string,"|"); } if (rc == XSB_ERROR) fprintf(stderr,"++Query Error: %s/%s\n",xsb_get_error_type(CTXT),xsb_get_error_message(CTXT)); xsb_close(CTXT); /* Close connection */ return(0); }
void *query_rs_err(void * arg) { int rc; th_context *r_th; XSB_StrDefine(r_return_string); r_th = (th_context *)arg; rc = xsb_query_string_string(r_th,"r_err(X,Y,Z).",&r_return_string,"|"); while (rc == XSB_SUCCESS) { printf("Pre-err return r %s\n",(r_return_string.string)); rc = xsb_next_string(r_th, &r_return_string,"|"); } if (rc == XSB_ERROR) printf("Planned query Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); return NULL; }
void *close_query_rs(void * arg) { int rc; th_context *r_th; XSB_StrDefine(r_return_string); r_th = (th_context *)arg; rc = xsb_query_string_string(r_th,"r(X,Y,Z).",&r_return_string,"|"); if (rc == XSB_ERROR) printf("++Closed query Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); printf("Closed return r %s\n",(r_return_string.string)); rc = xsb_close_query(r_th); if (rc == XSB_FAILURE) printf("++Failure on closed query (r)!\n"); return NULL; }
/******************************************************************* Successful queries *********************************************************************/ void *query_ps(void * arg) { int rc; th_context *p_th; XSB_StrDefine(p_return_string); p_th = (th_context *)arg; rc = xsb_query_string_string(p_th,"p(X,Y,Z).",&p_return_string,"|"); while (rc == XSB_SUCCESS) { printf("Return p %s\n",(p_return_string.string)); rc = xsb_next_string(p_th, &p_return_string,"|"); } if (rc == XSB_ERROR) printf("++Query Error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); return NULL; }