forked from oselin/dii-tpa-hw1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
459 lines (403 loc) · 12.4 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
#include <iostream>
#include <string>
#include "car_trailer.h"
#include "Car.h"
#include "machine_car_trailer.h"
#include <fstream>
#include <streambuf>
#include <sstream>
using namespace std;
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 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] - create a machine\n";
com += "[6] - 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;
}
/**
* Load OselinDevice from file. It can work with graphical menu or inline commands
**/
string load(OselinDevice *dev, int n_args = 0, char *param[] = NULL){
string filename;
if (n_args != 0){
filename = string(param[2]);
}
else{
cout << "path/file [with extension]: ";
cin >> filename;
}
try{
ifstream file(filename);
stringstream buffer;
buffer << file.rdbuf();
string s = buffer.str();
oselin_parsing(dev, s);
return "loaded successfully";
}
catch(const std::exception& e){
return "Somethig went wrong. The file exists?";
}
}
/**
* Create OselinDevice from scratch. It can work with graphical menu or inline commands
**/
string create(OselinDevice *dev, int n_args = 0, char *param[] = NULL){
float parameters[5];
Parameters p;
if (n_args != 0){
try
{
p.svgwidth = stof(param[2]);
p.svgheight = stof(param[3]);
p.length = stof(param[4]);
p.height = stof(param[5]);
p.radius = stof(param[6]);
p.ncars = atoi(param[7]);
p.nfloors = atoi(param[8]);
//p.margin;
//for (int i=0;i<5;i++) parameters[i] = stof(param[i+4]);
}
catch(const exception& e)
{
cout << errors(8) << endl;
exit(1);
}
}
else{
try{
for (int i=0; i<7; i++){
cout << questions[i];
if (i==0) cin >> p.svgwidth;
else if (i==1) cin >> p.svgheight;
else cin >> parameters[i-2];
}}
catch(const exception& e){
return errors(8);
}
p.length = parameters[0];
p.height = parameters[1];
p.radius = parameters[2];
p.ncars = (int)parameters[3];
p.nfloors = (int)parameters[4];
//p.margin;
}
(*dev) = (*oselin_init(p));
if (dev!=NULL){
oselin_trigonometry(dev);
return "Created successfully";
}
return "Something went wrong";
}
/**
* Save an existing OselinDevice to file
**/
string save(OselinDevice *dev, int mode = 0){
char resp;
int saving = 0;
if (!mode){
cout << "Do you want measures on the drawing?[y/n] ";
cin >> resp;
if (resp == 'y' || resp == 'Y') {
oselin_to_svg(dev, true, true);
++saving ;
}
else if (resp == 'n' || resp == 'N'){
oselin_to_svg(dev);
++saving ;
}
}
else ++saving;
if (saving){
string filename;
cout << "File name for saving (with extension): ";
cin >> filename;
ofstream MyFile(filename);
MyFile << (dev->svg + "\n</svg>");
MyFile.close();
return "SAVED!";
}
return "Aborting...";
}
/**
* Change some parameters of an existing OselinDevice
**/
string change(OselinDevice *dev){
int choice; float newvalue;
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";
cout << help << endl;
cout << "Your choice: ";
cin >> choice;
cout << "New value: ";
cin >> newvalue;
switch (choice)
{
case 0:
if(oselin_set_svgwidth(dev,newvalue) != NULL){
(*dev) = (*oselin_set_svgwidth(dev,newvalue));
return "Changed successfully!";
}
case 1:
if(oselin_set_svgheight(dev,newvalue) != NULL){
(*dev) = (*oselin_set_svgheight(dev,newvalue));
return "Changed successfully!";
}
case 2:
if(oselin_set_length(dev,newvalue) != NULL){
(*dev) = (*oselin_set_length(dev,newvalue));
return "Changed successfully!";
}
case 3:
if(oselin_set_height(dev,newvalue) != NULL){
(*dev) = (*oselin_set_height(dev,newvalue));
return "Changed successfully!";
}
case 4:
if(oselin_set_radius(dev,newvalue) != NULL){
(*dev) = (*oselin_set_radius(dev,newvalue));
return "Changed successfully!";
}
case 5:
if(oselin_set_ncars(dev,(int)newvalue) != NULL){
(*dev) = (*oselin_set_ncars(dev,(int)newvalue));
return "Changed successfully!";
}
case 6:
if(oselin_set_nfloors(dev,(int)newvalue) != NULL){
(*dev) = (*oselin_set_nfloors(dev,(int)newvalue));
return "Changed successfully!";
}
default:
break;
}
return "Aborting...";
}
/**
* Just display some possible commands
**/
void machine_displaymenu(){
string com = "";
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] - return";
cout << com << endl;
}
/**
* Create a new machine from scratch
**/
string machine_create(OselinDevice *dev, OselinMachine *mach){
//PARAM
//lenght | height | radius | ncars | nfloors | ntrailers
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;
Parameters p;
p.length = parameters[0];
p.height = parameters[1];
p.radius = parameters[2];
p.ncars = (int)parameters[3];
p.nfloors = (int)parameters[4];
mach->trailerarray = new OselinDevice* [ntrailers];
mach->cararray = new coca_device* [p.nfloors * p.ncars *ntrailers];
if (oselin_machine_init(p, ntrailers) != NULL){
(*mach) = (*oselin_machine_init(p, ntrailers));
return "Machine created successfully.";
}
return "An error occurred.";
}
/**
* Change parameters of an existing machine
**/
string machine_change(OselinDevice *dev, OselinMachine *mach){
if (mach->parameters.length != 0){
int choice; float newvalue;
Parameters p = mach->parameters;
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";
cout << help << endl;
cout << "Your choice: ";
cin >> choice;
cout << "New value: ";
cin >> newvalue;
if (choice > -1 || choice < 6){
if (choice == 5) mach->length = (int)newvalue;
//else array[choice] = newvalue;
//(*mach) = (*oselin_machine_init(dev, mach->length, array));
return "Machine updated.";
}
return "Aborting.";
}
return "The machine looks empty. Did you created one first?";
}
/**
* Load a machine from file
**/
string machine_load(OselinMachine *mach){
string filename;
cout << "path/file [with extension]: ";
cin >> filename;
ifstream file(filename);
stringstream buffer;
buffer << file.rdbuf();
string s = buffer.str();
oselin_machine_parsing(mach, s);
return "al";
}
/**
* Sub loop for working in a machine environment
**/
void machine_mainloop(OselinDevice *dev, OselinMachine *mach){
int inloop = 1, ntrailers;
string message;
float f[5];
cout << "MACHINE MODE" << endl;
do{
machine_displaymenu();
char choice;
cout << "Your choice: " ;
cin >> choice;
switch (choice)
{
case '1':
//message = machine_load(mach);
message = "This feature will come soon.";
break;
case '2':
message = machine_create(dev, mach);
break;
case '3':
message = oselin_machine_save(mach);
break;
case '4':
message = machine_change(dev,mach);
break;
case '5':
inloop = 0;
message = "Closing.";
break;
default:
message = "Command not found.";
break;
}
system("clear");
cout << message << " What's next?" << endl;
}while(inloop);
}
/**
* Main loop of the program
**/
void mainloop(OselinDevice *dev, OselinMachine *mach){
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':
message = load(dev);
break;
case '2':
message = create(dev);
break;
case '3':
message = save(dev);
break;
case '4':
message = change(dev);
break;
case '5':
machine_mainloop(dev, mach);
message = "";
case '6':
inloop = 0;
break;
default:
cout << "Command not found." << endl;
break;
}
system("clear");
cout << message << " What's next?" << endl;
}while(inloop);
}
int main(int argc, char * argv[]) {
OselinDevice *device = new OselinDevice;
OselinMachine *mach = new OselinMachine;
//signal(SIGINT,quit);
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();//NO switch qua perché non si poteva fare l'enum
}
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{
cout << create(device, argc,argv) << endl;;
mainloop(device, mach);
}
}
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{
cout << load(device, argc,argv) << endl;
mainloop(device, mach);
}
}
else if (string(argv[1]) == "-m" || string(argv[1]) == "--machine"){
machine_mainloop(device, mach);
mainloop(device, mach);
}
else if (string(argv[1]) == "-i" || string(argv[1]) == "--interface"){
mainloop(device, mach);
}
}
return 0;
}