-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgrabcommon.cpp
More file actions
executable file
·141 lines (132 loc) · 3.37 KB
/
grabcommon.cpp
File metadata and controls
executable file
·141 lines (132 loc) · 3.37 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
/**
* @file grabcommon.cpp
* @author Simone Comari
* @date 12 Mar 2019
* @brief This file includes definitions of functions and classes declared in
* grabcommon.h.
*/
#include "grabcommon.h"
void HandleErrorEn(const int en, const char* msg)
{
errno = en;
perror(msg);
exit(EXIT_FAILURE);
}
void PrintColor(const char color, const char* text, ...)
{
va_list args;
va_start(args, text);
std::string text_str(text);
std::string full_text;
switch (color)
{
case 'y':
full_text = ANSI_COLOR_YELLOW + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'r':
full_text = ANSI_COLOR_RED + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'g':
full_text = ANSI_COLOR_GREEN + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'm':
full_text = ANSI_COLOR_MAGENTA + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'c':
full_text = ANSI_COLOR_CYAN + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'b':
full_text = ANSI_COLOR_BLUE + (text_str + ANSI_COLOR_RESET + "\n");
break;
default:
full_text = text_str + "\n";
break;
}
vprintf(full_text.c_str(), args);
va_end(args);
}
void PrintColor(const char color, const char* text, va_list args)
{
std::string text_str(text);
std::string full_text;
switch (color)
{
case 'y':
full_text = ANSI_COLOR_YELLOW + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'r':
full_text = ANSI_COLOR_RED + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'g':
full_text = ANSI_COLOR_GREEN + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'm':
full_text = ANSI_COLOR_MAGENTA + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'c':
full_text = ANSI_COLOR_CYAN + (text_str + ANSI_COLOR_RESET + "\n");
break;
case 'b':
full_text = ANSI_COLOR_BLUE + (text_str + ANSI_COLOR_RESET + "\n");
break;
default:
full_text = text_str + "\n";
break;
}
vprintf(full_text.c_str(), args);
}
void DispRetVal(const int err, const char* msg, ...)
{
va_list args;
va_start(args, msg);
std::string full_msg = msg + GetRetValStr(err);
PrintColor(err ? 'r' : 'w', full_msg.c_str(), args);
va_end(args);
}
std::string GetRetValStr(const int err)
{
std::string description;
switch (err)
{
case OK:
description = "SUCCESS";
break;
case ECONFIG:
description = "Configuration FAILED";
break;
case EREG:
description = "Registration FAILED";
break;
case EACTIVE:
description = "Activation FAILED";
break;
case EINIT:
description = "Initialization FAILED";
break;
case EINV:
description = "Invalid value";
break;
case EFAIL:
description = "FAILED";
break;
}
return description;
}
void RunMatlabScript(const std::string& script_location, const bool display /*= false*/)
{
std::string cmd = "try,run('" + script_location + "'),catch,exit,end,exit";
char* args[7];
args[0] = (char*)("matlab");
args[1] = (char*)("-nosplash");
args[2] = (char*)(display ? "" : "-nodisplay");
args[3] = (char*)("-nodesktop");
args[4] = (char*)("-r");
args[5] = (char*)cmd.c_str();
args[6] = nullptr;
pid_t pid = vfork();
if (pid == 0)
if (execvp(args[0], args) == -1) // child
perror("exec");
if (pid > 0)
wait(nullptr); // parent
}