Logo
~Sockets~
~Examples~
~Contact~

Command Class Reference
[Commands]

Command base class. More...

#include <Command.h>

Inheritance diagram for Command:

Inheritance graph
[legend]
Collaboration diagram for Command:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Command ()
 Command (const std::string &cmd, bool need_wiz=false)
 Command (const std::string &cmd, char cc, bool need_wiz=false)
virtual ~Command ()
void AddArgument (const std::string &, bool wiz=false)
bool CheckArgument (InSocket *, const std::string &, std::string &)
strbool_mGetArguments ()
int Match (InSocket *, const std::string &cmd, const std::string &params)
 0-no match,1-partial match,2-exact match
virtual void Execute (InSocket *, const std::string &arg, Parse &)=0
 Execute command.
virtual void OnLine (InSocket *, const std::string &line, int state)
 Additional callback, reply to command prompt.
virtual std::string Category ()=0
 Get command category.
virtual std::string Description ()
 Command description.
const std::string & GetCommand ()
char GetCommandChar ()
bool NeedWiz ()

Static Public Member Functions

static CommandGetCommandFromChar (char cc)

Private Attributes

std::string m_cmd
char m_cc
strbool_m m_args
bool m_need_wiz

Static Private Attributes

static std::map< char, Command * > m_ccs

Detailed Description

Command base class.

Definition at line 35 of file Command.h.


Constructor & Destructor Documentation

Command::Command (  ) 

Definition at line 31 of file Command.cpp.

00031                  : m_cmd(""),m_cc(0),m_need_wiz(false)
00032 {
00033 }

Command::Command ( const std::string &  cmd,
bool  need_wiz = false 
)

Definition at line 36 of file Command.cpp.

00036                                             : m_cmd(cmd),m_cc(0),m_need_wiz(w)
00037 {
00038 }

Command::Command ( const std::string &  cmd,
char  cc,
bool  need_wiz = false 
)

Definition at line 41 of file Command.cpp.

References m_ccs.

00041                                                     : m_cmd(cmd),m_cc(cc),m_need_wiz(w)
00042 {
00043         if (m_ccs[cc] != NULL)
00044         {
00045                 fprintf(stderr, "Two commands using the same char: %c - exiting...\n", cc);
00046                 exit(-1);
00047         }
00048         m_ccs[cc] = this;
00049 }

Command::~Command (  )  [virtual]

Definition at line 52 of file Command.cpp.

00053 {
00054 }


Member Function Documentation

void Command::AddArgument ( const std::string &  ,
bool  wiz = false 
)

Definition at line 89 of file Command.cpp.

References m_args.

Referenced by Create::Create(), List::List(), and Setcmd::Setcmd().

00090 {
00091         m_args[arg] = wiz;
00092 }

bool Command::CheckArgument ( InSocket ,
const std::string &  ,
std::string &   
)

Definition at line 95 of file Command.cpp.

References Player::IsWizard(), and m_args.

00096 {
00097         Player pl(from -> GetDatabase(),from -> GetAccountName());
00098         bool wiz = pl.IsWizard();
00099         if (!m_args.size())
00100         {
00101                 complete = arg;
00102                 return true;
00103         }
00104         std::string found;
00105         int count = 0;
00106         for (strbool_m::iterator it = m_args.begin(); it != m_args.end(); it++)
00107         {
00108                 std::string str = (*it).first;
00109                 bool need_wiz = (*it).second;
00110                 if (!need_wiz || wiz)
00111                 {
00112                         if (str.size() >= arg.size() &&
00113                                 !strncasecmp(str.substr(0, arg.size()).c_str(), arg.c_str(), arg.size()))
00114                         {
00115                                 found = str;
00116                                 count++;
00117                         }
00118                 }
00119         }
00120         if (count == 1)
00121         {
00122                 complete = found;
00123                 return true;
00124         }
00125         if (!count)
00126         {
00127                 from -> Send("Please specify:\n");
00128                 for (strbool_m::iterator it = m_args.begin(); it != m_args.end(); it++)
00129                 {
00130                         std::string str = (*it).first;
00131                         bool need_wiz = (*it).second;
00132                         if (!need_wiz || wiz)
00133                         {
00134                                 from -> Send("  " + str + "\n");
00135                         }
00136                 }
00137         }
00138         else
00139         {
00140                 from -> Send("Please be more specific:\n");
00141                 for (strbool_m::iterator it = m_args.begin(); it != m_args.end(); it++)
00142                 {
00143                         std::string str = (*it).first;
00144                         bool need_wiz = (*it).second;
00145                         if (!need_wiz || wiz)
00146                         {
00147                                 if (str.size() >= arg.size() &&
00148                                         !strncasecmp(str.substr(0, arg.size()).c_str(), arg.c_str(), arg.size()))
00149                                 {
00150                                         from -> Send("  " + str + "\n");
00151                                 }
00152                         }
00153                 }
00154         }
00155         from -> SetPrompt();
00156         return false;
00157 }

strbool_m& Command::GetArguments (  )  [inline]

Definition at line 45 of file Command.h.

References m_args.

00045 { return m_args; }

int Command::Match ( InSocket ,
const std::string &  cmd,
const std::string &  params 
)

0-no match,1-partial match,2-exact match

Definition at line 57 of file Command.cpp.

References Player::IsWizard(), m_cmd, and m_need_wiz.

00058 {
00059         Player pl(from -> GetDatabase(),from -> GetAccountName());
00060         if (m_need_wiz && !pl.IsWizard())
00061                 return 0;
00062         if (cmd.size() > m_cmd.size())
00063                 return 0;
00064         if (!strcasecmp(cmd.c_str(), m_cmd.c_str()))
00065                 return 2;
00066         if (!strncasecmp(cmd.c_str(), m_cmd.c_str(), cmd.size()))
00067                 return 1;
00068         return 0;
00069 }

virtual void Command::Execute ( InSocket ,
const std::string &  arg,
Parse &   
) [pure virtual]

Execute command.

Implemented in Clear, Create, Down, East, Edge, Editor, Enter, Fill, Forward, Go, Help, Left, List, Login, Look, Map, North, Quit, Right, Save, Setcmd, South, Up, West, and Who.

void Command::OnLine ( InSocket ,
const std::string &  line,
int  state 
) [virtual]

Additional callback, reply to command prompt.

See also:
InSocket::SetPrompt

Reimplemented in Create, Down, East, Editor, Login, North, South, Up, and West.

Definition at line 83 of file Command.cpp.

00084 {
00085         from -> SetPrompt();
00086 }

virtual std::string Command::Category (  )  [pure virtual]

Get command category.

Implemented in Clear, Create, Down, East, Edge, Editor, Enter, Fill, Forward, Go, Help, Left, List, Login, Look, Map, North, Quit, Right, Save, Setcmd, South, Up, West, and Who.

virtual std::string Command::Description (  )  [inline, virtual]

Command description.

Reimplemented in Clear, Edge, Enter, and Fill.

Definition at line 57 of file Command.h.

00057 { return ""; }

const std::string& Command::GetCommand (  )  [inline]

Definition at line 59 of file Command.h.

References m_cmd.

Referenced by Forward::Execute().

00059 { return m_cmd; }

char Command::GetCommandChar (  )  [inline]

Definition at line 60 of file Command.h.

References m_cc.

00060 { return m_cc; }

Command * Command::GetCommandFromChar ( char  cc  )  [static]

Definition at line 72 of file Command.cpp.

References m_ccs.

Referenced by FutureHandler::DoCommand().

00073 {
00074         for (std::map<char,Command *>::iterator it = m_ccs.begin(); it != m_ccs.end(); it++)
00075         {
00076                 if ((*it).first == cc)
00077                         return (*it).second;
00078         }
00079         return NULL;
00080 }

bool Command::NeedWiz (  )  [inline]

Definition at line 64 of file Command.h.

References m_need_wiz.

00064 { return m_need_wiz; }


Member Data Documentation

std::string Command::m_cmd [private]

Definition at line 67 of file Command.h.

Referenced by GetCommand(), and Match().

char Command::m_cc [private]

Definition at line 68 of file Command.h.

Referenced by GetCommandChar().

std::map< char, Command * > Command::m_ccs [static, private]

Definition at line 69 of file Command.h.

Referenced by Command(), and GetCommandFromChar().

Definition at line 70 of file Command.h.

Referenced by AddArgument(), CheckArgument(), and GetArguments().

bool Command::m_need_wiz [private]

Definition at line 71 of file Command.h.

Referenced by Match(), and NeedWiz().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4