UserInterface.h 2.6 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 24
// UserInterface.h:
//
////////////////////////////////////////////////////////////////////////////////
// $ Id: $

#ifndef USER_INTERFACE_H
#define USER_INTERFACE_H

#include <iostream>
#include <string>
Francois Gygi committed
25
#include <cstring>
Francois Gygi committed
26 27 28 29 30 31 32 33 34 35
#include <iomanip>
#include <list>
#include <algorithm>

class UserInterface;

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

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

class UserInterface
{
  private:
55

56
  int readCmd(char *s, int max, std::istream &fp, bool echo);
Francois Gygi committed
57 58 59
  bool terminate_;
  bool onpe0_;

60
  public:
Francois Gygi committed
61

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

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

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

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

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

111 112 113
  void processCmds(std::istream &cmdstream, const char *prompt, bool echo);
  void processCmdsServer(std::string inputfilename, std::string outputfilename,
                         const char *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