Kenigmatic
Kenigmatic

Reputation: 436

Handling CLIPS C functions' return values as string(s)

I'm calling CLIPS Eval() and other functions from my C code and need help understanding how to handle return values that are CLIPSValue or UDFValue. A simple example ...

...
CLIPSValue  cv;
UDFValue    uv;
EvalError   ee;
LoadError   le;

entry->env = CreateEnvironment();
le = Load(entry->env, filePathBuffer);

if (le != LE_NO_ERROR)
{
    // report the load error ...
}

// Tried this but having trouble handling cv: GetDeftemplateList(entry->env, &cv, NULL);
// Trying with Eval ...
ee = Eval(entry->env, "(list-deftemplates)", &cv);
printf("%d -- %hi -- %ld -- %s", ee, cv.multifieldValue->header.type, cv.multifieldValue->length, cv.multifieldValue->lexemeValue->contents);
...

... the above printf is broken because I'm not correctly understanding cv structure/union.

Also looking into using DataObjectToString(...) but can't see how to convert CLIPSValue to UDFValue which DataObjectToString(...) needs as input.

Further processing of the result is needed, so using something like WriteCLIPSValue(...) isn't sufficient.

Would it be possible to use a router other than STDOUT with WriteCLIPSValue(...) and similar functions to only format response strings sort of like sprintf(...)?

I'm open to whatever approach is best but prefer simple/minimal C code.

Upvotes: 0

Views: 66

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

This is the code fragment for iterating over the multifield value return by GetDeftemplateList and printing the string values contained in the multifield:

   GetDeftemplateList(entry->env,&cv,NULL);
   for (i = 0; i < cv.multifieldValue->length; i++)
     {
      WriteString(mainEnv,STDOUT,cv.multifieldValue->contents[i].lexemeValue->contents);
      WriteString(mainEnv,STDOUT,"\n");
     }

In the most general case, you'd want to verify that cv.header->type is actually a multifield and cv.multifieldValue->contents[i].header->type is a symbol or string before pulling values out of the multifieldValue or lexemeValue unions, but in this case we know that's what GetDeftemplateList is going to return.

Upvotes: 1

Related Questions