-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
566 lines (522 loc) · 17.6 KB
/
main.cpp
File metadata and controls
566 lines (522 loc) · 17.6 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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
--- Code Quality TODOs
TODO: Code auf mehrere files verteilen (vorallem selection und parser Klasse in cpp und h Dateien aufteilen)
TODO: Console Farben testen und vllt sogar rausnehmen
--- Funktionale TODOs
TODO: list files in directory
TODO: Bluetooth?
TODO: dirent.h auf Windows ausprobieren (list items in directory)
TODO: parameter like onlypng (with no value) or somth for dir coommand so that only png is listed (or for removing all selected)
x TODO: parameter sollten entweder so "set=" oder auch so "s=" ansprechbar sein
*/
#include <iostream>
#include <sstream>
#include "pixel_reader.h"
#include <Map>
#include "filesys.h"
// set to 'DEBUG' for more verbose output
#define PROD
// typedefinition for map that holds commands, description and parameters
// key: commandname, value: description, parameterlist
typedef std::map<std::string, std::pair<std::string, std::vector<std::string>>> cmdMap_t;
// cwd in pov enviroment
#ifdef DEBUG
std::string currentPath = "/Users/i519401/Development/personal/C++/VPRProjekt/pov_system/resources/";
#else
std::string currentPath = "NO PATH";
#endif
// rootcommand for all other commands, like a prefix
const std::string rootCommand = "pov";
// command for quitting the main loop
const std::string quitCommand = "q";
// get operation system
int getOs(){
#ifdef _WIN32
return 1;
#elif _WIN64
return 2;
#elif __APPLE__ || __MACH__
return 3;
#elif __linux__
return 4;
#elif __FreeBSD__
return 5;
#elif __unix || __unix__
return 6;
#else
return 7;
#endif
}
/**
* @brief cuts whitespaces from left and right
* Source: https://stackoverflow.com/questions/25829143/trim-whitespace-from-a-string/25829178
* @param str string to be trimmed
* @return std::string trimmed string
*/
std::string trimString(const std::string str)
{
size_t first = str.find_first_not_of(' ');
if (std::string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}
/**
* @brief replaces backslashes with slashes and sets slash to end if not there
*
* @param path path to be corrected
*/
void correctPath(std::string &path) {
// used characters
char lastChar = 'a';
const char ch1 = '\\';
const char ch2 = '/';
// loop through all chars in path
for (int i = 0; i < path.length(); ++i) {
if (lastChar == ch1 && path[i] == ch1){
// remove double backslashes
path.erase(i, 1);
continue;
}
lastChar = path[i];
if (path[i] == ch1){
// replace backslash with slash
path[i] = ch2;
}
}
// append slash if not there
if (path[path.length()-1] != '/')
path.append("/");
}
/**
* @brief advanced spliiting method
*
* @param s to be split string
* @param delimiter split by which character
* @param ignore_inside_string if false, will not split parts in string eclosed by "", only works if delimiter is one space
* @return std::vector<std::string> the split parts of the string
*/
std::vector<std::string> split(std::string s, std::string delimiter, bool ignore_inside_string = false){
// temporary replacement char, should be somethin unique
const auto string_space_replacement = '$';
// automatically check if there is a need to check for strings
if (s.find('"') == std::string::npos || delimiter != " ")
ignore_inside_string = true;
if (!ignore_inside_string)
{
// replace space inside string with replacement char
auto is_inside_string = false;
for(auto i = 0; i<s.length(); i++)
{
const auto c = s[i];
if (c == '"')
{
is_inside_string = !is_inside_string;
s.erase(i, 1);
}
if (c == ' ' && is_inside_string)
s[i] = string_space_replacement;
}
}
// append delimiter to iterate through all split string not all-1
s.append(delimiter);
std::vector<std::string> list;
size_t pos;
// loop through all found delimiter chars
while ((pos = s.find(delimiter)) != std::string::npos) {
// create string from 0 to current delimiter pos
auto token = s.substr(0, pos);
if (!token.empty())
{
if (!ignore_inside_string)
{
// replace replacement char with space again
size_t space_replace_pos;
while ((space_replace_pos = token.find(string_space_replacement)) != std::string::npos)
{
token[space_replace_pos] = ' ';
}
}
// append to return list
list.push_back(token);
}
// remove from char 0 to last position
s.erase(0, pos + delimiter.length());
}
return list;
}
/**
* @brief selection of images and handler for that
*
*/
class ImageSelection_c{
public:
ImageSelection_c(){
// init
m_pixelSelection = std::map<std::string, std::string>();
}
~ImageSelection_c(){}
/**
* @brief adds image by filename
*
* @param fileName name of file
*/
void addImage(const std::string fileName){
if (m_pixelSelection.size() >= MAX_SELECTED){
std::cout << "maximum number of selected items reached. Cannot add more than " << MAX_SELECTED << std::endl;
return;
}
// build full pathname
auto completeFileName = trimString(currentPath + fileName);
auto exists = filesys::fileExists(completeFileName);
// file existent?
if (!exists){
std::cout << completeFileName << " does not exist!\n";
return;
}
// file already in selection
if (m_pixelSelection.find(completeFileName) != m_pixelSelection.end()){
std::cout << completeFileName << " is already selected\n";
return;
}
// extract pixel data
auto pixels = std::vector<color>();
bool succeeded = pixel::decodeWithState(completeFileName.c_str(), pixels);
// check ob pixel extrahiert werden konnten
if (!succeeded){
std::cout << "cannot add " << completeFileName << " to selection\n";
return;
}
// get correct sending format
auto transformed = pixelsToPixelsString(pixels);
// add to selection
m_pixelSelection.insert({completeFileName, transformed});
std::cout << "added image " << fileName << " to selection\n";
}
/**
* @brief removes image from selection by index
*
* @param atIndex index of item (visible when typed pov status)
*/
void removeImage(const int atIndex){
if (atIndex >= m_pixelSelection.size()){
std::cout << "index out of bounds\n";
return;
}
auto index = 0;
for(std::map<std::string, std::string>::iterator it = m_pixelSelection.begin(); it != m_pixelSelection.end(); ++it){
if (atIndex == index){
m_pixelSelection.erase(it);
std::cout << "removed image " << it->first << " from selection\n";
return;
}
++index;
}
}
/**
* @brief pretty prints all items in selection
*
*/
void printSelection(){
int index = 0;
std::cout << "---- current image selection: ----\n";
for(std::map<std::string, std::string>::iterator it = m_pixelSelection.begin(); it != m_pixelSelection.end(); ++it){
std::cout << index << ": " << it->first << "\nFarben: " << it->second << std::endl;
++index;
}
std::cout << "----------------------------------\n";
}
private:
// all selected items are stored here
std::map<std::string, std::string> m_pixelSelection;
// maximum selected items possible
const size_t MAX_SELECTED = 1;
/**
* @brief converts pixel RGB list to string with bools, seperated by char
* 3 values RGB > 1 value bool
* @param pix list of pixels from image
* @return std::string
*/
std::string pixelsToBoolString(std::vector<color> pix){
std::string asstring = "";
for (auto &a:pix){
size_t rgbscalar = a.R + a.B + a.G; // ignore alpha
bool t = rgbscalar != 0;
asstring.push_back(t ? '1' : '0');
asstring.push_back(';');
}
//return transformed;
return asstring;
}
/**
* @brief turns pixel RGB list into string with RGB values
*
* @param pix pixel lsit
* @return std::string
*/
std::string pixelsToPixelsString(std::vector<color> pix){
std::string asstring = "";
for (auto &a:pix){
// build string containing RGB
std::ostringstream os;
os << a.R << "," << a.G << "," << a.B << ";";
asstring.append(os.str());
}
// append ending char
asstring.push_back('.');
return asstring;
}
};
/**
* @brief string parameter parser
*
*/
class ParameterParser_c{
public:
// the base (consisting of 2 words: <root><action cmd>)
std::string baseCommand;
// the original input
std::string originalCommand;
ParameterParser_c(std::string _cmd) : originalCommand(_cmd), baseCommand(""){
m_parameters = std::map<std::string, std::string>();
getParametersFromCommand();
}
~ParameterParser_c(){}
/**
* @brief checks whether string contains a parameter and returns it's value
*
* @param param_name the parameters name
* @param param_value the to be returned value
* @param alt_param_name an alternative parameter name (optional)
* @return true the parameter exists
* @return false does not exist
*/
bool getParameter(std::string param_name, std::string ¶m_value, std::string alt_param_name = "") const{
if (m_parameters.empty())
return false;
std::map<std::string, std::string>::const_iterator it = m_parameters.find(param_name);
// meaning key non existent
if (it == m_parameters.end()){
it = m_parameters.find(alt_param_name);
auto alt_param_empty = alt_param_name.empty();
if (!alt_param_empty && it != m_parameters.end()){
// alt param exists, return value
param_value = it->second;
return true;
}
// param doesnt exist
std::cout << "parameter " << param_name << (alt_param_empty ? "" : " or alternatively ") << (alt_param_empty ? "" : alt_param_name) << " does not exist!\n";
return false;
}
// param exists, return value
param_value = it->second;
return true;
}
private:
// the list of parameters
std::map<std::string, std::string> m_parameters;
// parameter <splitSymbol> value
const std::string splitSymbol = "=";
/**
* @brief parses string and retrieves all parameters and base command
*
*/
void getParametersFromCommand(){
auto rawSplit = split(this->originalCommand, " ");
#ifdef DEBUG
for (auto e : rawSplit){
std::cout << e << std::endl;
}
#endif
// not even base exists, cannot parse
if (rawSplit.size() < 2)
return;
this->baseCommand = (trimString(rawSplit[0]) + " " + trimString(rawSplit[1]));
// if only basecommand exists
if (rawSplit.size() <= 2)
return;
// retrieve all params
for(std::vector<std::string>::iterator it = rawSplit.begin()+2; it != rawSplit.end(); ++it){
auto paramSplit = split(*it, this->splitSymbol, true);
#ifdef DEBUG
std::cout << "param: " << *it << std::endl;
for (auto e : paramSplit){
std::cout << "paramsplit " << e << std::endl;
}
#endif
if (paramSplit.size() > 2)
continue;
auto name = trimString(paramSplit.at(0));
std::string value;
if (paramSplit.size() == 1)
value = "NONE";
else
value = trimString(paramSplit.at(1));
#ifdef DEBUG
std::cout << "found Parameter: " << name << "='" << value << "'"<< std::endl;
#endif
m_parameters.insert({name, value});
}
}
};
#pragma region not_used
// system spezifische commands
char* listItemsCommand = "";
char* gotoDirectoryCommand = "";
const int os = getOs();
void setCommandIdentifiers(){
if (os == 1 || os == 2){
listItemsCommand = "dir";
gotoDirectoryCommand = "cd ";
}else if (os == 3 || os == 4){
listItemsCommand = "ls";
gotoDirectoryCommand = "cd ";
}
}
void listItemsInDirectory(){
if (listItemsCommand == "")
return;
std::system(listItemsCommand);
}
void listItemsInDirectory(char* dir){
if (listItemsCommand == "")
return;
std::system(strcat(listItemsCommand, dir));
}
void gotoDirectory(char* dir){
if (gotoDirectoryCommand == "")
return;
std::system(strcat(gotoDirectoryCommand, dir));
}
#pragma endregion
// commands as map (only used in help screen!)
// key: string as command, value: string as description
cmdMap_t commandMap = {
{"help", {"lists all commands", {}}},
{"dir", {"sets the current working path or lists items in current dir", {"optional: <set/s=[path]>: path as value, sets working dir"}}},
{"add", {"adds image to selection", {"mandatory: <file/f=[filename]>: file in current path"}}},
{"remove", {"removes image from selection either by index or by name", {"mandatory: <index/i=[index]>: which index to delete from selection"}}},
{"status", {"lists all selected images and index", {}}},
{"start", {"uploads selection to arduino", {}}}
};
/**
* @brief prints all commands
*
*/
void printHelp(){
std::cout << "-----------POV Help Page------------\n" << "Current Path: " << currentPath << std::endl;
for (cmdMap_t::iterator it = commandMap.begin(); it != commandMap.end(); ++it){
std::cout << "\033[1;31m"<< it->first <<": \033[0m" << it->second.first << std::endl;
for (int i = 0; i<it->second.second.size(); i++){
std::cout << "\t" << it->second.second[i] << std::endl;
}
}
std::cout << "--------------------------------------\n";
}
// current selection, later multiple selection can be implemented
auto selection = ImageSelection_c();
/**
* @brief parses command and acts
*
* @param cmd input by user
* @return true command exists
* @return false doesnt exist
*/
bool evaluateCommand(std::string cmd){
// parse cmd
auto paramParser = ParameterParser_c(cmd);
// load base command
auto base = paramParser.baseCommand;
#ifdef DEBUG
std::cout << base << std::endl;
#endif
if (base == (rootCommand+" help")){
// print commands
printHelp();
return true;
} else if (base == (rootCommand+" dir")){
std::string path = "";
bool gotValue = paramParser.getParameter("set", path, "s");
// if error, parameter was not found, meaning invalid parameter command
if (gotValue){
// set path param passed
correctPath(path);
auto exists = filesys::directoryExists(path);
if (exists){
currentPath = path;
filesys::listItemsInDirectoryAlt(path);
}
}else{
// no params passed -> list all items in current dir
filesys::listItemsInDirectoryAlt(currentPath);
}
return true;
} else if (base == (rootCommand+" add")){
// adds file to selection
std::string filename = "";
bool gotValue = paramParser.getParameter("file", filename, "f");
if (!gotValue)
return false;
auto splitfilename = split(filename, ".");
auto fileextension = splitfilename[splitfilename.size()-1];
#ifdef DEBUG
for (auto a : splitfilename){
std::cout << a << std::endl;
}
#endif
if (fileextension != "png")
filename += ".png";
#ifdef DEBUG
std::cout << fileextension << std::endl;
std::cout << filename << std::endl;
#endif
selection.addImage(filename);
return true;
} else if (base == (rootCommand+" remove")){
// remove file from selection
std::string index;
bool gotValue = paramParser.getParameter("index", index, "i");
if (!gotValue)
return false;
int index_from_string = std::stoi(index);
selection.removeImage(index_from_string);
return true;
} else if (base == (rootCommand+" status")){
// print status
selection.printSelection();
return true;
} else if (base == (rootCommand+" start")){
// not implemented
std::cout << "starting upload to arduino\n";
return true;
}
return false;
}
int main(int, char**) {
// check os
if (os > 4)
return 1;
setCommandIdentifiers();
// init enviroment screen
std::cout << "\033[1m >>>>> POV Enviroment Start\n";
std::cout << "enter '"<< quitCommand <<"' to quit. enter 'pov help' to list available commands\033[0m\n";
std::cout << "\033[33m INFO: please input path and filenames enclosed by quotation marks (\")\033[0m\n";
// last user input
std::string lastInput = "";
// main loop
do{
std::cout << "\033[1;32m<" << currentPath << ">: \033[0m";
// get user input
std::getline(std::cin, lastInput);
lastInput = trimString(lastInput);
if (lastInput == quitCommand)
break;
auto cmdfound = evaluateCommand(lastInput);
if (!cmdfound)
std::cout << "Command "<< lastInput <<" not found\n";
}while(lastInput != quitCommand);
std::cout << "\033[1mQuitting...\n";
std::cout << ">>>>> POV Enviroment Exit\033[0m\n";
}