_Evaluate( ) API Library Routine Example

The following example has the same functionality as the Visual FoxPro EVALUATE( ) function.

Visual FoxPro Code

SET LIBRARY TO EVALUATE
? XEVAL("2 + 3")
? XEVAL("'a' + 'b'")
? XEVAL("SIN(PI()/2))")

C Code

#include <pro_ext.h>

FAR EvaluateEx(ParamBlk FAR *parm)
{
   char FAR *expr;
   Value result;

//   Null terminate character string
   if (!_SetHandSize(parm->p[0].val.ev_handle, 
      parm->p[0].val.ev_length + 1))
   {
      _Error(182); // "Insufficient memory"
   }
   _HLock(parm->p[0].val.ev_handle);
   expr = (char FAR *) _HandToPtr(parm->p[0].val.ev_handle);
   expr[parm->p[0].val.ev_length] = '\0';

   _Evaluate(&result, expr);
   _RetVal(&result);

   _HUnLock(parm->p[0].val.ev_handle);
}

FoxInfo myFoxInfo[] = {
   {"XEVAL", (FPFI) EvaluateEx, 1, "C"},
};
FoxTable _FoxTable = {
   (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};