UserInterface.h 2.69 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17 18 19 20 21 22 23
// UserInterface.h:
//
////////////////////////////////////////////////////////////////////////////////

#ifndef USER_INTERFACE_H
#define USER_INTERFACE_H

#include <iostream>
#include <string>
24 25
#include <cstring> // strncpy(), strtok()
#include <cstdlib> // free(), system()
Francois Gygi committed
26 27 28 29 30 31 32 33
#include <list>

class UserInterface;

class Cmd
{
  public:
  UserInterface *ui;
34 35
  virtual const char *name(void) const = 0;
  virtual const char *help_msg(void) const = 0;
Francois Gygi committed
36
  virtual int action(int argc, char **argv) = 0;
Francois Gygi committed
37
  virtual ~Cmd(void) {}
Francois Gygi committed
38 39 40 41 42 43
};

class Var
{
  public:
  UserInterface *ui;
44
  virtual const char *name ( void ) const = 0;
Francois Gygi committed
45
  virtual int set ( int argc, char **argv ) = 0;
46
  virtual std::string print ( void ) const = 0;
Francois Gygi committed
47
  virtual ~Var(void) {}
Francois Gygi committed
48 49 50 51 52
};

class UserInterface
{
  private:
53

54 55
  int readCmd(std::string& s, std::istream &is, bool echo);
  void execCmd(std::string s, std::string prompt);
Francois Gygi committed
56 57 58
  bool terminate_;
  bool onpe0_;

59
  public:
Francois Gygi committed
60

61 62
  std::list<Cmd*> cmdlist;
  std::list<Var*> varlist;
Francois Gygi committed
63 64 65 66 67 68 69

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

70
  Cmd *findCmd(const char *cmdname)
Francois Gygi committed
71
  {
72
    std::list<Cmd*>::iterator cmd;
Francois Gygi committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    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 );
  };

93
  Var *findVar(const char *varname)
Francois Gygi committed
94
  {
95
    std::list<Var*>::iterator var;
Francois Gygi committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    for ( var = varlist.begin();
          (var != varlist.end() && (strcmp((*var)->name(),varname)));
          var++ );

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

110 111
  void processCmds(std::istream &cmdstream, const std::string prompt,
                   bool echo);
112
  void processCmdsServer(std::string inputfilename, std::string outputfilename,
113
                         const std::string prompt, bool echo);
114

Francois Gygi committed
115
  void terminate(void) { terminate_ = true; }
116

Francois Gygi committed
117
  bool onpe0(void) const { return onpe0_; }
118

Francois Gygi committed
119
  UserInterface(void);
Francois Gygi committed
120
  ~UserInterface(void);
Francois Gygi committed
121 122
};
#endif