-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
115 lines (90 loc) · 2.87 KB
/
utils.cpp
File metadata and controls
115 lines (90 loc) · 2.87 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
#include "utils.h"
int decode_for_tcp_clients(udp_message_t *udp_msg, tcp_message_t *tcp_msg) {
if (!udp_msg || !tcp_msg)
return -1;
tcp_msg->data_type = udp_msg->data_type;
strcpy(tcp_msg->topic, udp_msg->topic);
switch (udp_msg->data_type) {
case 0:
{
int_tcp_t *udp_int = (int_tcp_t *)udp_msg->content;
if (udp_int->sign_byte > 1) {
return 0;
}
int32_t integer = ntohl(udp_int->integer);
if (udp_int->sign_byte) {
integer *= -1;
}
sprintf(tcp_msg->payload, "%d", integer);
strcpy(tcp_msg->type, "INT");
break;
}
case 1:
{
short_real_tcp_t *udp_short = (short_real_tcp_t *)udp_msg->content;
short_real_tcp_t to_send_short_real = ntohs(*udp_short);
double to_send_mod = (double) to_send_short_real / 100;
sprintf(tcp_msg->payload, "%.2f", to_send_mod);
strcpy(tcp_msg->type, "SHORT_REAL");
break;
}
case 2:
{
float_tcp_t *udp_float = (float_tcp_t *)udp_msg->content;
if (udp_float->sign_byte > 1) {
return 0;
}
double number = ntohl(udp_float->value);
number /= pow(10, udp_float->negative_exp);
if (udp_float->sign_byte) {
number *= -1;
}
sprintf(tcp_msg->payload, "%lf", number);
strcpy(tcp_msg->type, "FLOAT");
break;
}
case 3:
{
strcpy(tcp_msg->type, "STRING");
strcpy(tcp_msg->payload, udp_msg->content);
break;
}
default:
{
return -2;
}
}
return 1;
}
void create_buffer_confirmation(char *buffer, bool connected) {
if (connected) {
buffer[0] = 's';
} else {
buffer[0] = 'f';
}
}
bool is_wildcard(std::string topic) {
std::string plus = "+";
std::string asterix = "*";
if (topic.find(plus) != std::string::npos) {
return true;
}
if (topic.find(asterix) != std::string::npos) {
return true;
}
return false;
}
std::string replace_all(std::string &string, std::string replace_word, std::string replace_by) {
size_t pos = string.find(replace_word);
while (pos != std::string::npos) {
string.replace(pos, replace_word.size(), replace_by);
pos = string.find(replace_word,
pos + replace_by.size());
}
return string;
}
std::string build_regex(std::string& wildcard) {
std::string replaced = replace_all(wildcard, "+", "[^/]+");
std::string fully_replaced = replace_all(replaced, "*", ".*");
return fully_replaced;
}