-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfone_subscribers.h
92 lines (76 loc) · 1.54 KB
/
fone_subscribers.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Subscriber {
char *keyword;
int fs2a_fd;
int fa2s_fd;
struct Subscriber *next;
} Subscriber;
typedef struct SubscriberQueue {
Subscriber *head;
Subscriber *tail;
} SubscriberQueue;
SubscriberQueue sq;
/*
*
* Helper Functions
*
*/
void sq_init() {
sq.head = NULL;
sq.tail = NULL;
}
void sq_push(char *keyword, int fs2a_fd, int fa2s_fd) {
Subscriber *new_subscriber = calloc(1, sizeof(Subscriber));
new_subscriber->keyword = calloc(strlen(keyword) + 1, sizeof(char));
strncpy(new_subscriber->keyword, keyword, strlen(keyword));
new_subscriber->keyword[strlen(keyword)] = 0;
new_subscriber->fs2a_fd = fs2a_fd;
new_subscriber->fa2s_fd = fa2s_fd;
new_subscriber->next = NULL;
if(sq.head == NULL) {
sq.head = new_subscriber;
sq.tail = new_subscriber;
} else {
sq.tail->next = new_subscriber;
sq.tail = new_subscriber;
}
}
void subscription_remove_by_fd(int fs2a_fd) {
Subscriber *s = sq.head;
Subscriber *p = NULL;
while(s != NULL) {
if(fs2a_fd == s->fs2a_fd) {
free(s->keyword);
if(p == NULL) {
sq.head = s->next;
} else {
p->next = s->next;
}
free(s);
break;
}
p = s;
s = s->next;
}
}
void sq_cleanup() {
Subscriber *i = sq.head;
Subscriber *j = NULL;
while(i != NULL) {
j = i;
free(i->keyword);
i = i->next;
free(j);
}
sq_init();
}
void sq_print() {
Subscriber *i = sq.head;
printf("Subscriber Queue:\n");
while(i != NULL) {
printf("[%d, %d] %s\n", i->fs2a_fd, i->fa2s_fd, i->keyword);
i = i->next;
}
}