-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cpp
More file actions
121 lines (108 loc) · 2.92 KB
/
Copy pathcommand.cpp
File metadata and controls
121 lines (108 loc) · 2.92 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <stdlib.h>
#include "command.h"
void GraphCommand::execute( g * graph, vector<g*> args){
}
string GraphCommand::get_name(){
return name;
}
string GraphCommand::get_desc(){
return description;
}
GraphCommandBase & GraphCommandBase::base(){
static GraphCommandBase gcb;
return gcb;
}
void GraphCommandBase::split( string &line, char delim, vector<string> &parts ){
stringstream ss( line );
string item;
while( getline( ss, item, delim) ){
if( !item.empty() ){
parts.push_back( item );
}
}
}
GraphCommand * GraphCommandBase::lookup_command( const string & c ){
c_map::iterator it = cmdMap.find(c);
if( it == cmdMap.end() ){
throw "Error: Invalid Command " + c;
}
return it->second;
}
g * GraphCommandBase::lookup_graph( const string & c, const string & gs ){
g_map::iterator it = graphMap.find(gs);
if( it == graphMap.end() ){
throw "Error: Graph does not exist";
}
return it->second;
}
void GraphCommandBase::run(){
string command;
cout << prompt;
getline( cin, command );
while( command != "exit" && command != "quit" ){
if( !command.empty()){
vector<string> parts;
split( command, ' ', parts );
try{
if( parts[0] == "create" ){
if( parts.size() != 3 ){
throw "Error: invalid number of arguments for create";
}
else{
register_g( parts[1], new g( atoi( parts[2].c_str() ) ) );
}
}
else if( parts[0] == "mk_join" || parts[0] == "mk_conn" ||
parts[0] == "mk_aconn" || parts[0] == "mk_arconn" ){
vector<g*> graphs;
while( parts.size() > 2 ){
graphs.push_back( lookup_graph( parts[0], parts.back() ) );
parts.pop_back();
}
lookup_command(parts[0])->execute( lookup_graph(parts[0], parts[1]),
graphs );
}
else if( parts[0] == "help" ){
cout << "*************************************************" << endl;
cout << " COMMAND \t\t DESCRIPTION" << endl;
for( map< string, GraphCommand* >::iterator it = cmdMap.begin();
it != cmdMap.end(); it++ ){
string space;
string c_name = it->second->get_name();
if( c_name.length() < 8 ){
space = "\t\t";
}
else{
space = "\t";
}
cout << c_name << space << it->second->get_desc() << endl;
}
cout << "*************************************************" << endl;
}
else{
lookup_command(parts[0])->execute( lookup_graph(parts[0], parts[1]),
parts );
}
}
catch( const string error ){
cerr << error << endl;
}
catch( const char * err ){
cerr << err << endl;
}
}
cout << prompt;
getline( cin, command );
}
}
void GraphCommandBase::register_g( const string name, g * graph ){
graphMap.insert( pair<string,g*>( name, graph ) );
}
void GraphCommandBase::register_c( const string name, GraphCommand * cmd ){
cmdMap.insert( pair<string,GraphCommand*>(name, cmd ) );
}