-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_item.cpp
More file actions
318 lines (287 loc) · 6.3 KB
/
menu_item.cpp
File metadata and controls
318 lines (287 loc) · 6.3 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "menu_item.h"
#include <sstream>
#include <string>
#include <iostream>
#include <stdexcept>
using std::runtime_error;
using std::ostringstream;
using std::stringstream;
using std::string;
using std::getline;
MenuItem::MenuItem(){
attr = special_type::NORMAL;
}
MenuItem::MenuItem(special_type type){
attr = type;
}
MenuItem::MenuItem(const GURI& url){
ostringstream oss;
oss << "1\t" << url.get_resource()
<< "\t" << url.get_formatted_host('\t');
init_by_line(oss.str());
}
MenuItem::MenuItem(const string& gopher_line){
init_by_line(gopher_line);
}
MenuItem::MenuItem(char type, const string& gopher_line){
init_by_type(type, gopher_line);
}
void MenuItem::init_by_line(const string& gopher_line){
const string DELIMITER("\t");
attr = special_type::NORMAL;
if(!gopher_line.size()){
item_type = 'i';
item_text = "";
selector_string = "NULL\tNULL";
return;
}
size_t firsttab = gopher_line.find(DELIMITER);
if(firsttab == string::npos){
throw runtime_error("Couldn't parse menu item (no tabs)");
}
size_t secondtab = gopher_line.find(DELIMITER, firsttab + 1);
if(secondtab == string::npos){
throw runtime_error("Couldn't parse menu item (need at least 2 tabs)");
}
item_type = gopher_line.at(0);
item_text = gopher_line.substr(1, firsttab - 1);
selector_string = gopher_line.substr(firsttab + 1);
}
bool MenuItem::can_select() const{
switch(item_type){
case '\0':
case '0':
case '1':
case '2':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '+':
case 'T':
case 'g':
case 'I':
case 'h':
case 'p':
return true;
case '3':
case 'i':
default:
return false;
}
}
bool MenuItem::is_supported_type() const{
switch(item_type){
case '0':
case '1':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '+':
case 'g':
case 'I':
case 'p':
return true;
case '\0':
case '2':
case '3':
case 'i':
case 'T':
case 'h':
default:
return false;
}
}
bool MenuItem::is_binary() const{
switch(item_type){
case '4':
case '5':
case '6':
case '9':
case 'g':
case 'I':
case 'h':
case 'p':
return true;
case '\0':
case '3':
case 'i':
case '0':
case '1':
case '2':
case '7':
case '8':
case '+':
case 'T':
default:
return false;
}
}
void MenuItem::build_request(string& req, string& host) const{
string port;
stringstream ss(selector_string);
getline(ss, req, '\t');
getline(ss, host, '\t');
getline(ss, port, '\t');
host.push_back('\t');
host.append(port);
switch(item_type){
case '0':
case '1':
case '3':
case '4':
case '5':
case '6':
case '9':
case '+':
case 'g':
case 'I':
case 'p':
break;
case '7':
req.push_back('\t');
req.append(search);
break;
case '\0':
default:
ostringstream err;
err << "Menu item type " << describe_item() << " not supported";
throw runtime_error(err.str());
}
req.append("\r\n");
}
void MenuItem::init_by_type(char type, const string& gopher_line){
item_type = type;
switch(type){
case '0':
item_type = 'i';
item_text = gopher_line;
selector_string = "";
break;
case '7':
case '3':
case '1':
init_by_line(gopher_line);
break;
default:
ostringstream oss;
oss << "Couldn't load menu for document type \"" << type << "\"";
throw runtime_error(oss.str());
break;
}
}
string MenuItem::describe_item() const{
switch(item_type){
case '\0':
return string("Internal Debug (Something is wrong)");
case '0':
return string("File");
case '1':
return string("Gopher menu");
case '2':
return string("CSO server");
case '3':
return string("Error message");
case '4':
return string("Macintosh file");
case '5':
return string("DOS binary archive");
case '6':
return string("UNIX uuencoded file");
case '7':
return string("Index-Search server");
case '8':
return string("Telnet session");
case '9':
return string("Binary file");
case '+':
return string("Redundant server");
case 'T':
return string("tn3270 session");
case 'g':
return string("GIF image");
case 'i':
return string("Text/Information");
case 'I':
return string("Image");
case 'h':
return string("HTTP link");
case 'p':
return string("PNG image");
default:
string s("Unknown item: ");
s.push_back(item_type);
return s;
}
}
bool MenuItem::operator==(const MenuItem other) const{
if(attr != special_type::NORMAL){
return (attr == other.attr);
}
return other.item_type == item_type &&
other.item_text == item_text &&
other.selector_string == selector_string;
}
bool MenuItem::operator!=(const MenuItem other) const{
return !(*this == other);
}
MenuItem MenuItem::operator=(const MenuItem other){
item_type = other.item_type;
item_text = other.item_text;
selector_string = other.selector_string;
attr = other.attr;
return other;
}
string MenuItem::get_selector() const{
return selector_string;
}
string MenuItem::get_text() const {
return item_text;
}
char MenuItem::get_type() const{
return item_type;
}
void MenuItem::set_search(const std::string& query){
search = query;
}
string MenuItem::get_search() const{
return search;
}
int MenuItem::get_line_width(char type, int scrn_width){
switch(type){
case '0':
return scrn_width;
case '7':
case '3':
case '1':
default:
return -1;
}
}
string MenuItem::get_terminator(char type){
switch(type){
case '0':
return string("");
case '7':
case '3':
case '1':
default:
return string(".");
}
}
string MenuItem::get_delimiter(char type){
switch(type){
case '0':
return string("\n");
case '7':
case '3':
case '1':
default:
return string("\r\n");
}
}