Advertisement
quantumech

Untitled

May 23rd, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <cinterf.h>
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6.     // 1) Create a 'fixedString' called 'queryValue' with 10 characters
  7.     //    You MUST use malloc(). Setting 'queryValue' to a string literal will throw a segfault.
  8.     const int QUERY_SIZE = 10;
  9.     char* queryValue = malloc(QUERY_SIZE);
  10.    
  11.     // 2) Initialize XSB
  12.     if(xsb_init_string("/home/quantumech/Desktop/XSB"))
  13.     {
  14.         printf("++XSB failed to initialize with error: %s\n", xsb_get_init_error_message());
  15.         exit(-1);
  16.     }
  17.     else
  18.     {
  19.         // 3) Load XSB script with defined terms such as 'f(a,b,c)' 'f(1,2,3)' etc.
  20.         xsb_command_string("consult('./script.P').");
  21.  
  22.         // 'actualValueLength' gets set to what ever the actual query size turns out to be.
  23.         // So if 10 characters are allocated for 'queryValue' but only 5 are used, then 'actualValueLength' would be 5
  24.         int actualValueLength;
  25.  
  26.         // The extra '_b' means the XSB method works on 'fixedBuffer' strings (aka. C strings)
  27.         // xsb_query_string_string_b(command, outputStr, outputStrSize, &actualValueLength, seperator)
  28.         if(xsb_query_string_string_b("f(X,Y,Z).", queryValue, QUERY_SIZE, &actualValueLength,",") == XSB_SUCCESS)      
  29.         {
  30.             // Keep passing query results into 'queryValue' until xsb_next_string() doesnt return anymore values
  31.             while(xsb_next_string_b(queryValue, QUERY_SIZE, &actualValueLength, ",") == XSB_SUCCESS)
  32.             {
  33.                 // Print result of query
  34.                 printf("Query result: %s\n", queryValue);  
  35.             }
  36.         }
  37.  
  38.         // Terminate XSB
  39.         xsb_close();
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement