-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfutils.h
613 lines (544 loc) · 14.7 KB
/
futils.h
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/**
* @author Francesco Wanderlingh
* @date Year 2017
*
* @brief A set of useful C++ utilities ironically called "futils".
*
* @details The utilities implemented, meant primarily for Linux-based OS, include:
* - Elapsing Spinner Command-Line Animation
* - Waiting Dotter Command-Line Animation
* - Executable file self path retrieval for path-safe file saving, loading
* - STL Vector Printing
* - Debug print macro
*/
#ifndef FUTILS_H_
#define FUTILS_H_
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <unistd.h>
#include <map>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pwd.h>
#include <iomanip>
#include <algorithm>
#include <memory>
#include <stdexcept>
#include <array>
#ifdef DEBUG_PRINT
# define dout std::cerr
# define d_out(x) (std::cerr << x << std::endl)
#else
# define dout 0 && std::cerr
# define d_out(x) 0 && std::cerr
#endif
//#define CTRL_out(x) (std::cout << tc::bluL << "[controller] " << tc::none << x)
//#define DRIVER_out(x) (std::cout << tc::magL << "[driver] " << tc::none << x)
//#define LOGGER_out(x) (std::cout << tc::grnL << "[logger] " << tc::none << x)
/** Escape sequence
* \033[<code>m or \e[<code>m
*
* color : \[\033[ <code>m\] \]
*
* e.g.
* bg=Blue, Bold, fg=Red
* 1. \033[44;1;31m
* 2. \033[44m\033[1;31m
*
*
* Text attributes
* 0 All attributes off
* 1 Bold on
* 4 Underscore (on monochrome display adapter only)
* 5 Blink on
* 7 Reverse video on
* 8 Concealed on
*
*
* Foreground colors
* 0;30 Black 1;30 Dark Gray
* 0;31 Red 1;31 Light Red
* 0;32 Green 1;32 Light Green
* 0;33 Brown 1;33 Yellow
* 0;34 Blue 1;34 Light Blue
* 0;35 Purple 1;35 Light Purple
* 0;36 Cyan 1;36 Light Cyan
* 0;37 Light Gray 1;37 White
*/
#if defined(__linux__) || defined(linux)
#define TC_RED "\033[5;31m"
#define TC_CYAN "\033[1;36m"
#define TC_GREEN "\033[1;32m"
#define TC_BLUE "\033[1;34m"
#define TC_YELLOW "\033[1;33m"
#define TC_BLACK "\033[0;30m"
#define TC_BROWN "\033[0;33m"
#define TC_MAGENTA "\033[1;35m"
#define TC_GRAY "\033[1;37m"
#define TC_NONE "\033[0m"
namespace tc
{
const char* const none = "\033[0m";
const char* const blck = "\033[0;30m";
const char* const grayD = "\033[1;30m";
const char* const red = "\033[0;31m";
const char* const redL = "\033[1;31m";
const char* const grn = "\033[0;32m";
const char* const grnL = "\033[1;32m";
const char* const brwn = "\033[0;33m";
const char* const yel = "\033[1;33m";
const char* const blu = "\033[0;34m";
const char* const bluL = "\033[1;34m";
const char* const mag = "\033[0;35m";
const char* const magL = "\033[1;35m";
const char* const cyan = "\033[0;36m";
const char* const cyanL = "\033[1;36m";
const char* const grayL = "\033[0;37m";
const char* const white = "\033[1;37m";
}
#endif
enum class LogEntities {
Controller, Driver, Logger, UDPReceiver, UDPSender, Generic
};
inline std::string DebugMsg(const LogEntities entity, const std::string inputMsg, const std::string generic = ""){
std::stringstream strstr;
switch (entity) {
case LogEntities::Controller:
strstr << tc::cyanL << "[controller] ";
break;
case LogEntities::Driver:
strstr << tc::magL << "[driver] ";
break;
case LogEntities::Logger:
strstr << tc::grnL << "[logger] ";
break;
case LogEntities::UDPReceiver:
strstr << tc::bluL << "[udpReceiver] ";
break;
case LogEntities::UDPSender:
strstr << tc::bluL << "[udpSender] ";
break;
case LogEntities::Generic:
strstr << tc::white << "[" << generic << "] ";
break;
}
strstr << tc::none << inputMsg;
return strstr.str();
}
namespace FUTILS
{
/**
* @brief Create folder if not existing
*
* @param path of the folder
* @return 0 if success (or folder exists) -1 otherwise (sets errno)
*/
inline int MakeDir(const char *path) {
/**
* S_IRWXU | S_IRWXG | S_IRWXO
* Read/write/search permissions for owner and group and others. Since mkdir() masks
* the mode with umask(), a further chmod() is needed.
*/
int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
chmod(path, S_IRWXU | S_IRWXG | S_IRWXO);
if (ret != 0) {
if (errno == EEXIST) {
ret = 0;
} else {
std::cerr << tc::redL << "Could not create log directory " << path << " (error: " << strerror(errno) << ")\n";
}
}
return ret;
}
/// My first functor!!! :D
/**
* Prints a spinner that, put in a while loop with negligible execution time,
* spins at a given frequency.
*
* @param freq Animation update frequency
*/
struct Spinner
{
Spinner(int frequency) :
freq(frequency), spinIndex(0), spin_chars("/-\\|")
{
clock_gettime(CLOCK_MONOTONIC, &last);
period = 1 / static_cast<double>(freq + 1E-6);
//std::cout << "period: " << period << "s" << std::endl;
}
void operator()(void)
{
clock_gettime(CLOCK_MONOTONIC, &now);
double timeElapsed = (now.tv_sec - last.tv_sec) + (now.tv_nsec - last.tv_nsec) / 1E9;
//std::cout << "timeElapsed: " << timeElapsed << std::endl;
if (period - timeElapsed < 1E-3) {
//std::cout << "fabs(freq - timeElapsed): " << fabs(freq - timeElapsed) << std::endl;
//printf("\e[?25l"); /* hide the cursor */
putchar(' ');
putchar(spin_chars[spinIndex % spin_chars.length()]);
putchar(' ');
fflush(stdout);
putchar('\r');
spinIndex++;
last = now;
}
}
private:
struct timespec last, now;
int freq;
double period;
unsigned long spinIndex;
std::string spin_chars;
};
/**
* Prints a classic dot animation that, put in a while loop with negligible execution time,
* plays at a given frequency.
*
* @param freq Animation update frequency
*/
struct Dotter
{
Dotter(int freq) :
freq(freq), spinIndex(0), spin_chars("... .. .. .. .... .... .")
{
clock_gettime(CLOCK_MONOTONIC, &last);
period = 1 / static_cast<double>(freq);
//std::cout << "period: " << period << "s" << std::endl;
}
void operator()(void)
{
clock_gettime(CLOCK_MONOTONIC, &now);
double timeElapsed = (now.tv_sec - last.tv_sec) + (now.tv_nsec - last.tv_nsec) / 1E9;
//std::cout << "timeElapsed: " << timeElapsed << std::endl;
if (period - timeElapsed < 1E-3) {
//std::cout << "fabs(freq - timeElapsed): " << fabs(freq - timeElapsed) << std::endl;
//putchar(' ');
putchar(spin_chars[spinIndex % spin_chars.length()]);
putchar(spin_chars[(spinIndex % spin_chars.length()) + 1]);
putchar(spin_chars[(spinIndex % spin_chars.length()) + 2]);
putchar(spin_chars[(spinIndex % spin_chars.length()) + 3]);
putchar(' ');
fflush(stdout);
putchar('\r');
spinIndex += 4;
last = now;
}
}
private:
struct timespec last, now;
int freq;
double period;
unsigned long spinIndex;
std::string spin_chars;
};
/**
* Simple timer for getting time elapsed and intermediate laps.
* All values are returned in seconds, with nanosecond precision.
*/
struct Timer
{
Timer() :
running(false), elapsedTime(0), lapTime(0)
{
}
void Start()
{
clock_gettime(CLOCK_MONOTONIC, &start);
lap = start;
running = true;
}
void Stop()
{
lapTime = 0;
running = false;
}
/**
* @return elapsed time since start in seconds, with nanosecond precision (if timer is running)
*/
double Elapsed()
{
if (running) {
clock_gettime(CLOCK_MONOTONIC, &now);
elapsedTime = (now.tv_sec - start.tv_sec) + (now.tv_nsec - start.tv_nsec) / 1E9;
return elapsedTime;
} else {
return 0.0;
}
}
/**
* @return elapsed time since last Lap in seconds, with nanosecond precision
*/
double Lap()
{
clock_gettime(CLOCK_MONOTONIC, &now);
lapTime = (now.tv_sec - lap.tv_sec) + (now.tv_nsec - lap.tv_nsec) / 1E9;
laps.push_back(lapTime);
lap = now;
return lapTime;
}
double GetCurrentLapTime()
{
clock_gettime(CLOCK_MONOTONIC, &now);
lapTime = (now.tv_sec - lap.tv_sec) + (now.tv_nsec - lap.tv_nsec) / 1E9;
return lapTime;
}
std::vector<double> laps;
bool running;
private:
struct timespec start, lap, now;
double elapsedTime, lapTime;
};
template<typename T>
inline std::string toStringPointDecimal(T val){
std::string s = std::to_string(val);
std::replace(s.begin(), s.end(), ',', '.');
return s;
}
/*
template<typename T>
inline std::string toStringPointDecimal(T val, const int precision = 0){
std::streamsize defaultPrecision = std::cout.precision();
if(precision != 0){
std::setprecision(static_cast<std::streamsize>(precision));
}
std::ostringstream oss;
oss << std::setprecision(precision) << val;
std::string s = oss.str();
std::replace(s.begin(), s.end(), ',', '.');
std::setprecision(defaultPrecision);
return s;
}
*/
template<typename T>
void PrintArray(T arr, const int size, const char delimiter)
{
for (int i = 0; i < size; ++i) {
std::cout << arr[i];
if (i < (size - 1)) {
std::cout << delimiter << " ";
}
}
}
template<typename T>
void PrintCMATArray(T arr, const int size, const char delimiter)
{
for (int i = 1; i <= size; ++i) {
std::cout << arr(i);
if (i < size) {
std::cout << delimiter << " ";
}
}
}
template<typename T>
std::string ArrayToString(T arr, const int size, const char delimiter, const int precision = 0)
{
std::string arrayText;
for (int i = 0; i < size; ++i) {
arrayText = arrayText + toStringPointDecimal(arr[i]);
if (i < (size - 1)) {
arrayText = arrayText + delimiter + " ";
}
}
return arrayText;
}
template<typename T>
std::string CMATArrayToString(T arr, const int size, const char delimiter)
{
std::string arrayText;
for (int i = 1; i <= size; ++i) {
arrayText = arrayText + toStringPointDecimal(arr(i));
if (i < size) {
arrayText = arrayText + delimiter + " ";
}
}
return arrayText;
}
template<typename T>
void PrintSTLVector(T vecObj, const char delimiter, const std::string preText = "")
{
std::cout << preText << " ";
for (typename T::iterator itr = vecObj.begin(); itr != vecObj.end(); ++itr) {
std::cout << *itr;
if (itr != (vecObj.end() - 1)) {
std::cout << delimiter << " ";
}
}
}
template<typename T>
std::string STLVectorToString(T vecObj, const char delimiter, const std::string preText = "")
{
std::string vectorText;
vectorText = preText + " ";
for (typename T::iterator itr = vecObj.begin(); itr != vecObj.end(); ++itr) {
vectorText = vectorText + toStringPointDecimal(*itr);
if (itr != (vecObj.end() - 1)) {
vectorText = vectorText + delimiter + " ";
}
}
return vectorText;
}
template<typename T>
void PrintSTLVectOfVects(T vecObj, const char delimiter)
{
for (typename T::iterator itr = vecObj.begin(); itr != vecObj.end(); ++itr) {
std::cout << "#" << itr - vecObj.begin() << ": ";
PrintSTLVector(*itr, delimiter);
std::cout << "\n";
}
}
/**
* Returns the current date and time formatted as %Y-%m-%d_%H.%M.%S
*
* @return Current date
*/
inline std::string GetCurrentDateFormatted()
{
std::time_t t = std::time(NULL);
char mbstr[20];
std::strftime(mbstr, sizeof(mbstr), "%Y-%m-%d_%H.%M.%S", std::localtime(&t));
std::string currentDate(mbstr);
return currentDate;
}
template<typename K, typename V>
using MapIterator = typename std::map<K,V>::const_iterator;
template<typename K, typename V>
bool FindMapKeyByValue(const std::map<K, V> myMap, const V value, K &key)
{
bool keyWasFound = false;
MapIterator<K, V> it;
for (it = myMap.begin(); it != myMap.end(); ++it) {
if (it->second == value) {
key = it->first;
keyWasFound = true;
break;
}
}
return keyWasFound;
}
/// Functor for getting sum of previous result and square of current element (innerproduct can be used instead)
template<typename T>
struct square
{
T operator()(const T& Left, const T& Right) const
{
return (Left + Right * Right);
}
};
#if defined(__linux__) || defined(linux)
/**
* Returns the path of the folder containing executable that calls this functions
*
* @return String with the folder path
*/
inline std::string get_selfpath()
{
char buff[2048];
ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff) - 1);
if (len != -1) {
buff[len] = '\0';
std::string path(buff); ///Here the executable name is still in
std::string::size_type t = path.find_last_of("/"); // Here we find the last "/"
path = path.substr(0, t); // and remove the rest (exe name)
return path;
} else {
printf("Cannot determine executable path! [Exiting]\n");
exit(-1);
}
}
/**
* Return user "home" directory
*/
inline std::string get_homepath()
{
const char *homedir;
if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}
return homedir;
}
inline bool does_file_exists(const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
inline std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != NULL)
result += buffer.data();
}
return result;
}
inline void ParseIPString(const std::string input_Str, unsigned char ip[4]){
std::string ipString = input_Str, ip_chunk;
std::string::size_type t1;
for (int i = 0; i < 3; ++i) {
t1 = ipString.find_first_of(".");
ip_chunk = ipString.substr(0, t1);
ip[i] = static_cast<unsigned char>(stod(ip_chunk, &t1));
ipString = ipString.substr(t1 + 1, ipString.size());
}
ip[3] = static_cast<unsigned char>(stod(ipString, &t1));
}
inline void paddr(unsigned char *a)
{
printf("%d.%d.%d.%d\n", a[0], a[1], a[2], a[3]);
}
inline void die(std::string s)
{
perror(s.c_str());
exit(1);
}
inline bool ConfigureSenderSocket(struct sockaddr_in &si_out, char *ip, uint16_t port){
// zero out the structure
memset((char *) &si_out, 0, sizeof(si_out));
si_out.sin_family = AF_INET;
si_out.sin_port = htons(port);
if (inet_aton(ip , &si_out.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
return true;
}
inline bool ConfigureReceiverSocket(int &sockfd, struct sockaddr_in &si_in, uint16_t port, bool nonBlocking){
///
/// SOCKET Initialization
///
if ((sockfd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
// zero out the structure
memset((char *) &si_in, 0, sizeof(si_in));
si_in.sin_family = AF_INET;
si_in.sin_port = htons(port);
si_in.sin_addr.s_addr = htonl(INADDR_ANY);
//bind socket to port
if(bind(sockfd , (struct sockaddr*)&si_in, sizeof(si_in) ) == -1)
{
die("bind");
}
if(nonBlocking){
// Put the socket in non-blocking mode:
if(fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK) < 0) {
die("Error in putting the socket in non blocking mode");
}
}
return true;
}
#endif /* Linux functions*/
} /* namespace FUTILS */
#endif /* FUTILS_H_ */