-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
535 lines (465 loc) · 14.3 KB
/
main.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include <iostream>
#include <string>
#include "car_trailer.h"
#include "machine_car_trailer.h"
#include <fstream>
#include <streambuf>
#include <sstream>
#include <unordered_map>
using namespace std;
//UI supporting functions-------------------------
string questions[] = {"SVG width: ", "SVG height: ","Car lenght: ","Car height: ", "Wheel radius [16,17,18]: ","Cars-per-trailer [1,2]: ","Number of floors [1,2]: "};
void print(unordered_map<string,float> umap){
for (pair<string, float> p : umap){
cout << p.first << " | " << p.second << endl;
}
}
void displaymenu(){
string com = "";
com += "----------------------------------------\n";
com += "Here's what you can do:\n";
com += "[1] - load SVG drawing from file\n";
com += "[2] - create a new trailer\n";
com += "[3] - save SVG drawing to file\n";
com += "[4] - change a parameter\n";
com += "[5] - machine mode\n";
com += "[6] - create a machine from this trailer\n";
com += "[7] - display Device Data\n";
com += "[8] - display Machine Data\n";
com += "[9] - exit";
cout << com << endl;
}
void help(){
cout << "-h | --help\t\t allows you to read this awesome guide" << endl;
cout << "-c | --create\t\t create a trailer SVG\t\t\t--create [SVG width] [SVG height] [Car length] [Car height] [Wheel radius] [Cars-per-trailer] [Floors]" << endl;
cout << "-l | --load\t\t load SVG from file\t\t\t--load [path]" << endl;
cout << "-m | --machine\t\t draw a train of trailers\t\t " << endl;
cout << "-i | --interface\t use graphic menu (for lame people)" << endl;
}
void machine_displaymenu(){
string com = "";
com += "--------------MACHINE MODE--------------\n";
com += "----------------------------------------\n";
com += "Here's what you can do:\n";
com += "[1] - load machine SVG drawing from file\n";
com += "[2] - create a new machine\n";
com += "[3] - save machine SVG drawing to file\n";
com += "[4] - change a parameter\n";
com += "[5] - display Machine Data\n";
com += "[6] - return";
cout << com << endl;
}
//------------------------------------------------
//Trailer managing functions----------------------
oselin::Trailer* trailer_load(int argc = 0, char *argv[] = NULL){
string s, filename;
if (argv != NULL && argc >=2){
filename = string(argv[2]);
}
else{
cout << "path/file [with extension]: ";
cin >> filename;
}
try{
ifstream file(filename);
stringstream buffer;
buffer << file.rdbuf();
s = buffer.str();
}
catch(const exception& e){
cout << "An error occurred here" << endl;
throw e;
}
oselin::Trailer *t = new oselin::Trailer(s);
return t;
}
oselin::Trailer* trailer_create(oselin::Trailer *trailer){
if (trailer->isempty()){
oselin::Parameters p;
try{
float answ;
for (int i=0; i<7; i++){
cout << questions[i];
cin >> answ;
switch (i)
{
case 0:
p.svg_width(answ);
break;
case 1:
p.svg_height(answ);
break;
case 2:
p.car_length(answ);
break;
case 3:
p.car_height(answ);
break;
case 4:
p.car_radius(answ);
break;
case 5:
p.n_cars(answ);
break;
case 6:
p.n_floors(answ);
break;
default:
throw out_of_range("An error occurred.");
}}
}catch(const exception& e){
throw e;
}
//Prevent data loss if something goes wrong
p.isempty(false);
oselin::Trailer *t = new oselin::Trailer(p, 0);
return t;
}
return trailer;
}
void trailer_save(oselin::Trailer *trailer, int mode=0){
char resp;
string svg;
if (!mode){
cout << "Do you want measures on the drawing?[y/n] ";
cin >> resp;
if (resp == 'y' || resp == 'Y') {
svg = trailer->svg(true, true);
}
else if (resp == 'n' || resp == 'N'){
svg = trailer->svg(true);
}
}
else svg = trailer->svg();
if (svg == "") throw logic_error("An error occurred. [TRAILER SAVE]");
string filename;
cout << "File name for saving (with extension): ";
cin >> filename;
ofstream MyFile(filename);
MyFile << (svg + "\n</svg>");
MyFile.close();
}
oselin::Trailer* trailer_change(oselin::Trailer *trailer){
oselin::Trailer *t = new oselin::Trailer();
if (trailer->isempty()) return t;
int choice; float newvalue;
system("clear");
string help = "Choose what to change:\n";
help += "[0] Set new car SVG width\n";
help += "[1] Set new car SVG height\n";
help += "[2] Set new car length\n";
help += "[3] Set new car height\n";
help += "[4] Set new radius\n";
help += "[5] Set new number of cars per trailer\n";
help += "[6] Set new number of floors\n";
help += "--- Any other number to go back\n";
cout << help << endl;
cout << "Your choice: ";
cin >> choice;
if (choice >7 || choice <0) return trailer;
cout << "New value: ";
cin >> newvalue;
oselin::Parameters buffer = trailer;
switch (choice)
{
case 0:
buffer.svg_width(newvalue);
break;
case 1:
buffer.svg_height(newvalue);
break;
case 2:
buffer.car_length(newvalue);
break;
case 3:
buffer.car_height(newvalue);
break;
case 4:
buffer.car_radius(newvalue);
break;
case 5:
buffer.n_cars(newvalue);
break;
case 6:
buffer.n_floors(newvalue);
break;
default:
break;
}
try
{
t = new oselin::Trailer(buffer);
return t;
}
catch(const std::exception& e)
{
cout << "New parameter is wrong";
}
return trailer;
}
//------------------------------------------------
//Machine managing functions----------------------
void machine_save(oselin::Machine *machine){
if (machine != NULL){
if (machine->isempty() == 0){
string filename, svgmach;
cout << "File name for saving (with extension): ";
cin >> filename;
ofstream MyFile(filename);
svgmach = machine->svg();
if (svgmach == "") throw range_error("Something went wrong.");
MyFile << (svgmach + "\n</svg>");
MyFile.close();
cout << "Saved." << endl;
}
}
else throw logic_error("Pointer is null.");
}
oselin::Machine * convertFromTrailer(oselin::Trailer *trailer){
oselin::Machine *machine = new oselin::Machine(); //worst case: return a brand new machine
if (!trailer->isempty()){
int ntrailers;
cout << "How many trailers? ";
cin >> ntrailers;
if (ntrailers > 0){
machine = new oselin::Machine(trailer, ntrailers);
}
}
return machine;
}
oselin::Machine * machine_create(){
int ntrailers;
float parameters[5];
for (int i=0; i<5; i++){
cout << questions[i+2];
cin >> parameters[i];
}
cout << "How many trailers? ";
cin >> ntrailers;
oselin::Parameters p;
p.car_length(parameters[0]);
p.car_height(parameters[1]);
p.car_radius(parameters[2]);
p.n_cars (parameters[3]);
p.n_floors (parameters[4]);
oselin::Machine *m = new oselin::Machine(p, ntrailers);
return m;
}
oselin::Machine * machine_load(){
string filename;
cout << "path/file [with extension]: ";
cin >> filename;
oselin::Machine *m;
try{
ifstream file(filename);
stringstream buffer;
buffer << file.rdbuf();
string s = buffer.str();
m = new oselin::Machine(s);
}catch(exception &e){throw invalid_argument("The file does not exist.");}
return m;
}
oselin::Machine* machine_change(oselin::Machine *machine){
oselin::Machine *m = new oselin::Machine();
if (machine->isempty()) return m;
int choice; float newvalue;
system("clear");
string help = "Choose what to change:\n";
help += "[0] Set new car length\n";
help += "[1] Set new car height\n";
help += "[2] Set new radius\n";
help += "[3] Set new number of cars per trailer\n";
help += "[4] Set new number of floors\n";
help += "[5] Set new number of trailers\n";
help += "--- Any other number to go back\n";
cout << help << endl;
cout << "Your choice: ";
cin >> choice;
if (choice >8 || choice <0) return machine;
cout << "New value: ";
cin >> newvalue;
oselin::Parameters buffer = machine;
float n_trail = machine->n_trailers();
switch (choice)
{
case 0:
buffer.car_length(newvalue);
break;
case 1:
buffer.car_height(newvalue);
break;
case 2:
buffer.car_radius(newvalue);
break;
case 3:
buffer.n_cars(newvalue);
break;
case 4:
buffer.n_floors(newvalue);
break;
case 5:
n_trail = newvalue;
break;
default:
break;
}
try
{
m = new oselin::Machine(buffer, n_trail);
cout << "Machine changed successfully." << endl;
return m;
}
catch(const std::exception& e)
{
cout << "New parameter is wrong";
}
return machine;
}
//------------------------------------------------
oselin::Machine * machine_mainloop(oselin::Machine *mach){
system("clear");
int inloop = 1, ntrailers;
string message;
float f[5];
oselin::Machine *machine = new oselin::Machine(mach);
do{
machine_displaymenu();
char choice;
cout << "Your choice: " ;
cin >> choice;
switch (choice)
{
case '1':
machine = machine_load();
message = "Machine loaded successfully.";
break;
case '2':
machine = machine_create();
message = "Machine created successfully.";
break;
case '3':
machine_save(machine);
break;
case '4':
machine = machine_change(machine);
message = "";
break;
case '5':
system("clear");
cout << machine << endl;
getchar();getchar();
break;
case '6':
inloop = 0;
break;
default:
message = "Command not found.";
break;
}
system("clear");
if (inloop) cout << message << " What's next?" << endl;
}while(inloop);
return machine;
}
void mainloop(oselin::Trailer *trailer, oselin::Machine *machine){
int inloop = 1;
string message;
cout << "Welcome to the SVG TRAILER CREATOR" << endl;
do{
displaymenu();
char choice;
cout << "Your choice: " ;
cin >> choice;
switch (choice)
{
case '1':
trailer = trailer_load();
message = "File loaded successfully.";
break;
case '2':
trailer->isempty(true);
trailer = trailer_create(trailer);
message = "Created successfully.";
break;
case '3':
trailer_save(trailer);
message = "Saved successfully.";
break;
case '4':
trailer = trailer_change(trailer);
message = "Changed successfully.";
break;
case '5':
machine = machine_mainloop(machine);
message = "";
break;
case '6':
machine = convertFromTrailer(trailer);
break;
case '7':
system("clear");
cout << trailer;
getchar(); getchar();
break;
case '8':
system("clear");
cout << machine;
getchar(); getchar();
break;
case '9':
inloop = 0;
break;
default:
cout << "Command not found." << endl;
break;
}
system("clear");
if (inloop) cout << message << " What's next?" << endl;
}while(inloop);
}
int main(int argc, char * argv[]) {
oselin::Trailer *trailer = new oselin::Trailer();
oselin::Machine *machine = new oselin::Machine();
if (argc==1){
cout << "Welcolme to the trailer-to-svg tool. Use '-h' to display commands.\n" << endl;
}
else{
if (string(argv[1]) == "-h" || string(argv[1]) == "--help") help();
else if (string(argv[1]) == "-c" || string(argv[1]) == "--create"){
if (argc < 9){
cout << "Missing some parameters. Please check and try again." << endl;
}
else if (argc > 9){
cout << "Too many parameters. Please check and try again." << endl;
}
else{
trailer = new oselin::Trailer(oselin::Parameters(argv),0);
cout << "Trailer created successfully." << endl;
mainloop(trailer, machine);
}
}
else if (string(argv[1]) == "-l" || string(argv[1]) == "--load"){
if (argc < 3){
cout << "Missing some parameters. Please check and try again." << endl;
}
else if (argc > 3){
cout << "Too many parameters. Please check and try again." << endl;
}
else{
trailer = trailer_load(argc,argv);
cout << "Trailer loaded successfully." << endl;
mainloop(trailer, machine);
}
}
else if (string(argv[1]) == "-m" || string(argv[1]) == "--machine"){;
machine = machine_mainloop(machine);
mainloop(trailer, machine);
}
else if (string(argv[1]) == "-i" || string(argv[1]) == "--interface"){
mainloop(trailer, machine);
}
}
delete trailer;
delete machine;
return 0;
}