#include <iostream.h>
#include <string.h>
#include "ez_inter.h"
#include "app32imp.h"

void InitEzSetting()
{
  /* set various config options */
}

class MV {
private:

  void* interp;
  void  construct();
  void  destroy();

public:

  int quit_requested;

  MV();
  ~MV();

  void syncEval( char* script );

  void quit();
  void parseError() {};

  DATUM getDoubleVar( char* var_name );

  void setRWESecurity();
  void setROSecurity();
  void setNASecurity();

  static int CB_displayResult
    ( 
     long   flag , 
     DATUM* result , 
     char*  line, 
     void*  win 
    );

  static int CB_pause
    ( 
     int seconds, 
     DCONST void* win, 
     int opt,
     DCONST char* title,
     DCONST char* message 
    );

  static void printDebug( const char* message );

};

//-----------------------------------------------------------
MV::MV()
{
  interp = NULL;
  construct();
  quit_requested = 0;

  InitEzSetting();
}

//-----------------------------------------------------------
MV::~MV()
{
  destroy();
}

//-----------------------------------------------------------
void MV::quit()
{
  quit_requested = 1;
}

//-----------------------------------------------------------
int MV::CB_displayResult
( 
 long   flag, 
 DATUM* result, 
 char*  line,
 void*  client_data
)
{
  cout << line;
  return 1;
}

//-----------------------------------------------------------
int MV::CB_pause
( 
 int seconds, 
 DCONST void* client_data, 
 int opt,
 DCONST char* title,
 DCONST char* message 
)
{
  // not implemented
  return 1;
}

//-----------------------------------------------------------
void MV::construct()
{

  int result = InitEzdsp( &interp );

  if ( result == 0 ) {
    cout << "Initialization failed" << endl;
    exit(1);
  }

  // set displayResult callback
  PROC_PTR func = (PROC_PTR)MV::CB_displayResult;
  EzdspSetCallBack( interp, CALLBACK_PRINT , NULL , func );

  // set pause callback
  func = (PROC_PTR)MV::CB_pause;
  EzdspSetCallBack( interp, CALLBACK_PAUSE , NULL , func );

  // set various options
  EzdspSetOption( interp, OPTION_STRING, 
		  (long)"EnginePrint", (long)"1");

  EzdspSetOption( interp, OPTION_STRING, 
		  (long)"CrLf", (long)"\n");

  EzdspSetOption( interp, OPTION_STRING, 
		  (long)"fixedFont", (long)"1");

  EzdspSetOption( interp, OPTION_SAFETY,
		  (long)SAFETY_LOW,  0L );

}

//-----------------------------------------------------------
void MV::destroy()
{
  int result = CloseEzdsp( interp );
}

//-----------------------------------------------------------
void MV::syncEval( char* script )
{

  // EzdspScript2 is routine called to evaluated m-scripts
  int ez_status = EzdspScript2( interp, script, NULL, 0, 1 , NULL );

  if ( ez_status == QUIT_CODE ) {

    quit();

  }
  else if ( ez_status != 0 ) {
  
    char message[ MAX_ERROR_MESSAGE ];
    EzdspGetError( interp , message );

    parseError();
  }

}

//---------------------------------------------------------------
DATUM MV::getDoubleVar( char* var_name )
{
  DATUM ez_res;

  int ez_status = EzdspGetValue( interp,
				 PROGRAM_VAR,
				 var_name,
				 &ez_res );

  if ( ez_status != 0  ) {
    cout << "Variable " << var_name << " not found." << endl;
  }


  if ( ez_res.Type != APP_DARRAY &&
       ez_res.Type != APP_NUMBER ) {
    cout << "Variable " << var_name << " not found." << endl;
  }

  return ez_res;
}

//---------------------------------------------------------------
int main()
{
  char* buf = new char[1024];
  MV mv;

  while (1) {

    cout << ">> ";
    cin.gets(&buf);

    mv.syncEval(buf);

    if ( mv.quit_requested == 1 )
      break;

  }
  return 0;

}



