-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.h
103 lines (86 loc) · 2.61 KB
/
helpers.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
/* Mirciu Andrei-Constantin */
/* 323CD */
#ifndef _HELPERS_H
#define _HELPERS_H 1
#include <stdio.h>
#include <stdlib.h>
#define BUFLEN 2000 // dimensiunea maxima a bufferului de date
#define MAX_UDP_CONTENT 1500
#define MAX_CLIENTS 5 // numarul maxim de clienti in asteptare
/*
* Macro de verificare a erorilor
* Exemplu:
* int fd = open(file_name, O_RDONLY);
* DIE(fd == -1, "open failed");
*/
#define DIE(assertion, call_description) \
do { \
if (assertion) { \
fprintf(stderr, "(%s, %d): ", \
__FILE__, __LINE__); \
perror(call_description); \
exit(EXIT_FAILURE); \
} \
} while(0)
typedef struct {
char topic[50];
uint8_t tip_date;
char continut[MAX_UDP_CONTENT];
} udp_msg;
typedef struct {
int id;
int socket;
char subscribed_topics[BUFLEN];
} tcp_client;
typedef struct {
char sign;
uint32_t content;
} payload_int;
typedef struct {
uint16_t content;
} payload_short_real;
typedef struct {
char sign;
uint32_t number;
uint8_t power;
} payload_float;
typedef struct {
char content[MAX_UDP_CONTENT];
} payload_string;
//https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word/
char *replaceWord(const char *subscribed_topics, const char *oldTopic,
const char *newTopic) {
char *result;
int i, cnt = 0;
int newTopicLen = strlen(newTopic);
int oldTopicLen = strlen(oldTopic);
// Counting the number of times old topic
// occurs in the subscribed topics
for (i = 0; subscribed_topics[i] != '\0'; i++)
{
if (strstr(&subscribed_topics[i], oldTopic) == &subscribed_topics[i])
{
cnt++;
// Jumping to index after the old topic.
i += oldTopicLen - 1;
}
}
// Making new string of enough length
result = (char *)malloc(i + cnt * (newTopicLen - oldTopicLen) + 1);
i = 0;
while (*subscribed_topics)
{
// compare the substring with the result
if (strstr(subscribed_topics, oldTopic) == subscribed_topics)
{
strcpy(&result[i], newTopic);
i += newTopicLen;
subscribed_topics += oldTopicLen;
}
else
result[i++] = *subscribed_topics++;
}
result[i] = '\0';
return result;
}
#endif