-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.cpp
executable file
·73 lines (62 loc) · 1.49 KB
/
utils.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
#include "utils.h"
Utils g_utils;
/* Action - Exit the program */
void Utils::handle_type1_error(int arg, string msg) {
if (arg < 0) {
msg = to_string(errno) + ": " + msg;
perror(msg.c_str());
exit(EXIT_FAILURE);
}
}
/* Action - Check for error conditions. Do not exit. */
void Utils::handle_type2_error(int arg, string msg) {
if (arg < 0) {
msg = to_string(errno) + ": " + msg;
perror(msg.c_str());
}
}
char* Utils::allocate_str_mem(int len) {
char *tem;
if (len <= 0) {
handle_type1_error(-1, "Memory length error: utils_allocatestrmem");
}
tem = (char*)malloc(len * sizeof (char));
if (tem != NULL) {
memset(tem, 0, len * sizeof (char));
return tem;
}
else {
handle_type1_error(-1, "Memory allocation error: utils_allocatestrmem");
}
}
uint8_t* Utils::allocate_uint8_mem(int len) {
uint8_t *tem;
if (len <= 0) {
handle_type1_error(-1, "Memory length error: utils_allocateuint8mem");
}
tem = (uint8_t*)malloc(len * sizeof (uint8_t));
if (tem != NULL) {
memset(tem, 0, len * sizeof (uint8_t));
return tem;
}
else {
handle_type1_error(-1, "Memory allocation error: utils_allocateuint8mem");
}
}
void Utils::time_check(time_t start_time, double dur_time, bool &time_exceeded) {
double elapsed_time;
if ((elapsed_time = difftime(time(0), start_time)) > dur_time) {
time_exceeded = true;
}
}
int Utils::max_ele(vector<int> inp) {
int ans;
int size;
int i;
ans = 0;
size = inp.size();
for (i = 0; i < size; i++) {
ans = max(ans, inp[i]);
}
return ans;
}