UserInterface.h 1.87 KB
Newer Older
Francois Gygi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
////////////////////////////////////////////////////////////////////////////////
//
// UserInterface.h:
//
////////////////////////////////////////////////////////////////////////////////
// $ Id: $

#ifndef USER_INTERFACE_H
#define USER_INTERFACE_H

#include <iostream>
#include <string>
#include <iomanip>
#include <list>
#include <algorithm>

class UserInterface;

class Cmd
{
  public:
  UserInterface *ui;
  virtual char *name(void) const = 0;
  virtual char *help_msg(void) const = 0;
  virtual int action(int argc, char **argv) = 0;
};

class Var
{
  public:
  UserInterface *ui;
  virtual char *name ( void ) const = 0;
  virtual int set ( int argc, char **argv ) = 0;
34
  virtual std::string print ( void ) const = 0;
Francois Gygi committed
35 36 37 38 39
};

class UserInterface
{
  private:
40

41
  char *readCmd(char *s, int max, std::istream &fp, bool echo);
Francois Gygi committed
42 43 44
  bool terminate_;
  bool onpe0_;

45
  public:
Francois Gygi committed
46

47 48
  std::list<Cmd*> cmdlist;
  std::list<Var*> varlist;
Francois Gygi committed
49 50 51 52 53 54 55 56 57

  void addCmd(Cmd *newcmd)
  {
    newcmd->ui = this;
    cmdlist.push_back( newcmd );
  };

  Cmd *findCmd(char *cmdname)
  {
58
    std::list<Cmd*>::iterator cmd;
Francois Gygi committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    for ( cmd = cmdlist.begin();
          (cmd != cmdlist.end() && (strcmp((*cmd)->name(),cmdname)));
          cmd++ );

    if ( cmd != cmdlist.end() )
    {
      return (*cmd);
    }
    else
    {
      return 0;
    }
  };

  void addVar(Var *newvar)
  {
    newvar->ui = this;
    varlist.push_back( newvar );
  };

  Var *findVar(char *varname)
  {
81
    std::list<Var*>::iterator var;
Francois Gygi committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
    for ( var = varlist.begin();
          (var != varlist.end() && (strcmp((*var)->name(),varname)));
          var++ );

    if ( var != varlist.end() )
    {
      return (*var);
    }
    else
    {
      return 0;
    }
  };

96
  void processCmds(std::istream &cmdstream, char *prompt, bool echo);
97

Francois Gygi committed
98
  void terminate(void) { terminate_ = true; }
99

Francois Gygi committed
100
  bool onpe0(void) const { return onpe0_; }
101

Francois Gygi committed
102 103 104
  UserInterface(void);
};
#endif