#include "CExEv/CExEv.h"
#include <stdio.h>
#include <string.h>


class Evaluator : public cexev::CExEvCtx
{
  
public:
	Evaluator() : CExEvCtx() {width=height=0;}
	virtual ~Evaluator() {}

  int width; int height;
    
	// Override to return values for variables
	virtual bool getValue(const char *cIdent, variant::Variant& result)
  {
    if (strcmp(cIdent, "W") == 0)
    {
      result.setInt(320);
      return true;
    }
    if (strcmp(cIdent, "H") == 0)
    {
      result.setInt(200);
      return true;
    }
    if (strcmp(cIdent, "w") == 0)
    {
      result.setInt(width);
      return true;
    }
    if (strcmp(cIdent, "h") == 0)
    {
      result.setInt(height);
      return true;
    }
    return CExEvCtx::getValue(cIdent, result);
  }  
  
	const char *eval(const char * expression)
  {
    const char *expr = expression;
    cexev::CExEv ee;
    variant::Variant vres;
    variant::String sres, sdump;

    ee.setContext(this);
    int eeres = ee.evalExpression(expr, vres);

    ee.getDump(sdump);

    if (eeres == cexev::CExEv::E_OK)
    {
      vres.asString(sres);
      return sres.c_str();
    }
    return "0:Cannot evaluate";
  }
  
};

