/* * PVVMUD a 3D MUD * Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include "commandparser.H" CCommandParser::CCommand::CCommand(string & commandString,CommandFunc_t commandFunc){ m_commandString = commandString; m_commandFunc = commandFunc; } string & CCommandParser::CCommand::getCommandString(){ return m_commandString; } void CCommandParser::CCommand::executeCommand(argv_t & argv, CCommandParser * parser) { (parser->*m_commandFunc)(argv); } CCommandParser::CCommandParser(){ m_commandPrefix = '/'; } /////////////////////////////////////////////////////////////////////////////// // parseCommand // Parse commands on the form "/command", "/command arg...", "Message..." // Side effects on commandString: // Any command with arguments will be null terminated before arguments /////////////////////////////////////////////////////////////////////////////// void CCommandParser::parseCommand(const string & commandString){ string command = commandString; if (command[0] != m_commandPrefix){ // Is this a message? commandChat(command); } else { // No it is a command command.erase(0,1); // Don't need leading '/' argv_t argv; createArgv(command,argv); for (int index = 0; index < m_commandList.size(); index++){ if (m_commandList[index].getCommandString() == argv[0]){ m_commandList[index].executeCommand(argv,this); return; } } commandUnknown(commandString); } } void CCommandParser::setCommandPrefix(char commandPrefix){ m_commandPrefix = commandPrefix; } /////////////////////////////////////////////////////////////////////////////// // createArgv // Convert string to table of strings /////////////////////////////////////////////////////////////////////////////// void CCommandParser::createArgv(string & argString, argv_t & argv){ string::size_type pos; // Remove all double spaces while ((pos = argString.find(" ")) != string::npos) argString.replace(pos,2," "); // Create argv string::size_type last = 0; string::size_type next; while ((next = argString.find(" ",last)) != string::npos) { argv.push_back(string(argString,last,next-last)); last = next + 1; } next = argString.size(); argv.push_back(string(argString,last,next)); } /////////////////////////////////////////////////////////////////////////////// // addCommand /////////////////////////////////////////////////////////////////////////////// void CCommandParser::addCommand(string commandString, CommandFunc_t commandFunc) { m_commandList.push_back( CCommand(commandString,commandFunc)); } void CCommandParser::commandChat(const string & chatMessage){ commandUnknown(chatMessage); } void CCommandParser::commandUnknown(const string & commandString){ }