-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandHandler.cpp
More file actions
71 lines (54 loc) · 1.79 KB
/
CommandHandler.cpp
File metadata and controls
71 lines (54 loc) · 1.79 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* File: CommandHandler.cpp
* Author: kmcqueen
*
* Created on September 11, 2013, 12:25 PM
*/
#include "CommandHandler.h"
#include <iostream>
#include "QuitHandler.h"
#include "HelpHandler.h"
#include "SendHandler.h"
#include "main.h"
#include "ListHandler.h"
#include "ReadHandler.h"
#include "ResetHandler.h"
CommandHandler::CommandHandler() {
}
CommandHandler::~CommandHandler() {
}
list<CommandHandler*> CommandHandler::getHandlers() {
list<CommandHandler*> handlers;
// add the "send" command handler
handlers.push_back(SendHandler::instance());
// add the "list" command handler
handlers.push_back(ListHandler::instance());
// add the "read" command handler
handlers.push_back(ReadHandler::instance());
// add the "quit" command handler
handlers.push_back(QuitHandler::instance());
// add the "reset" command handler
//handlers.push_back(ResetHandler::instance());
// order is important here -- the HelpHandler *must* be the last in the list
handlers.push_back(HelpHandler::instance());
return handlers;
}
bool CommandHandler::canHandle(string commandLine) {
size_t found = commandLine.find(this->getCommandPrefix());
if (found == string::npos) {
return false;
}
return 0 == found;
}
void CommandHandler::handleCommand(string commandLine) {
debug(this->getName() + " is handling command line: " + commandLine);
if (not this->canHandle(commandLine)) {
this->printUnexpectedCommand(commandLine);
return;
}
this->doHandleCommand(commandLine);
}
void CommandHandler::printUnexpectedCommand(string commandLine) {
cout << "Unexpected command line: " << commandLine << endl;
cout << "Command should be of the form:\n" << this->getHelpString() << endl;
}