-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNexusMenuData.cpp
More file actions
190 lines (170 loc) · 4.37 KB
/
NexusMenuData.cpp
File metadata and controls
190 lines (170 loc) · 4.37 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <cctype>
#include "NexusMenuData.h"
#include "EditLineHist.h"
#include "mpl.h"
CNexusMenuData::CNexusMenuData(string strMenuTitle)
{
bMenuShown = false;
m_strMenuTitle = strMenuTitle;
}
CNexusMenuData::~CNexusMenuData()
{
vector<CNexusMenuBase*>::iterator it;
CNexusMenuBase* pMenuItem;
for (it = m_vMenu.begin(); it < m_vMenu.end(); it++)
{
pMenuItem = *it;
if (pMenuItem)
{
delete(pMenuItem);
}
}
m_vMenu.clear();
}
void CNexusMenuData::AddMenuItem(CNexusMenuBase* pMenuItem)
{
m_vMenu.push_back(pMenuItem);
}
bool CNexusMenuData::RunSelections(string strInput, CNexusUserInterface *pNexusUserInterface)
{
bool ret = true;
vector<string> commands = GetCommandList(strInput);
vector<string>::reverse_iterator it;
for (it = commands.rbegin(); it != commands.rend(); ++it)
{
ret = RunSelection(*it, pNexusUserInterface);
if (ret == false)
{
break;
}
}
return ret;
}
bool CNexusMenuData::RunSelection(string strInput, CNexusUserInterface *pNexusUserInterface)
{
vector<CNexusMenuBase*> pMenuItems;
string command;
string value;
SplitInput(strInput, &command, &value);
pMenuItems = GetMenuSelection(command);
if (pMenuItems.size() == 1)
{
bool bRet = true;
cout<<endl;
try
{
switch (pMenuItems[0]->RunCommand(pNexusUserInterface, value))
{
case ENXS_MCS_COMMAND_FAIL:
bRet = false;
break;
case ENXS_MCS_INVALID_PARAM:
PrintError(strInput, pMenuItems);
break;
default:
break;
}
}
catch (const char *e)
{
cout<<"NUI Error: "<<e<<endl;
}
cout<<endl;
return bRet;
}
PrintError(strInput, pMenuItems);
return true;
}
bool CNexusMenuData::Help(bool bForceShowMenu)
{
if ((bMenuShown == false) || (bForceShowMenu == true))
{
vector<CNexusMenuBase*>::iterator it;
CNexusMenuBase* pMenu;
string output;
for (it = m_vMenu.begin(); it < m_vMenu.end(); it++)
{
pMenu = *it;
if (pMenu)
{
pMenu->GetMenuOutput(&output);
cout<<output<<endl;
}
}
cout<<endl;
bMenuShown = true;
}
return true;
}
string CNexusMenuData::GetPrompt()
{
return m_strMenuTitle;
}
vector<string> split(const string &s, char delim)
{
vector<string> elems;
stringstream ss(s);
string item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
vector<string> CNexusMenuData::GetCommandList(string strInput)
{
vector<string> ret = split(strInput, ';');
return ret;
}
void CNexusMenuData::SplitInput(string strInput, string *command, string *value)
{
*command = CEditLineHist::trim(strInput);
size_t index = strInput.find('=');
if (index != string::npos)
{
*command = strInput.substr(0, index);
*value = strInput.substr(index+1);
CEditLineHist::trim(*command);
CEditLineHist::trim(*value);
}
}
void CNexusMenuData::PrintError(string strInput, vector<CNexusMenuBase*> pMenuItems)
{
vector<CNexusMenuBase*>::iterator it;
cout<<" Unknown command: '"<<strInput<<"'"<<endl<<endl;
CNexusMenuBase* pMenuItem;
string params;
string output;
for (it = pMenuItems.begin(); it < pMenuItems.end(); it++)
{
pMenuItem = *it;
pMenuItem->GetMenuOutput(&output);
pMenuItem->GetValidParams(¶ms);
cout<<output<<endl;
if (params.length() > 0)
{
cout<<setw(COMMAND_WIDTH - 2)<<" "<<"Possible values:"<<endl;
cout<<setw(COMMAND_WIDTH)<<" "<<params<<endl;
}
}
cout<<endl;
}
vector<CNexusMenuBase*> CNexusMenuData::GetMenuSelection(string strInput)
{
vector<CNexusMenuBase*>::iterator it;
CNexusMenuBase* pMenuItem;
vector<CNexusMenuBase*> ret;
for (it = m_vMenu.begin(); it < m_vMenu.end(); it++)
{
pMenuItem = *it;
if ((pMenuItem) && (pMenuItem->IsSelection(strInput)))
{
ret.push_back(pMenuItem);
}
}
return ret;
}