-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl.c
118 lines (87 loc) · 1.86 KB
/
pl.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tl2/list.h"
#include "pl.h"
static list *msg_list = NULL;
void pl_init_list() {
msg_list = list_init_node(NULL);
}
void pl_send(char *name, void *value, size_t size) {
static char is_first = 0;
list *last = NULL;
msg *message = NULL;
if (!is_first) {
pl_init_list();
is_first = 1;
}
list_add_node(msg_list);
last = list_get_last(msg_list);
message = (msg*)malloc(sizeof(msg));
strcpy(message->name, name);
message->value = malloc(size);
memcpy(message->value, value, size);
list_set_data(last, message);
}
void pl_free() {
list_free_list(msg_list);
}
msg *pl_get(char *msg_name) {
list *lptr = NULL;
msg *message = NULL;
lptr = msg_list;
while (lptr) {
if (lptr && lptr->data)
message = (msg*)lptr->data;
if (message && !strcmp(msg_name, message->name))
return message;
lptr = lptr->next;
}
return NULL;
}
void *pl_read(char *msg_name) {
msg *message = NULL;
message = pl_get(msg_name);
if (message)
return message->value;
return NULL;
}
list *pl_get_node(char *msg_name) {
list *lptr = NULL;
msg *message = NULL;
lptr = msg_list;
while (lptr) {
if (lptr && lptr->data)
message = (msg*)lptr->data;
if (message && !strcmp(msg_name, message->name))
return lptr;
lptr = lptr->next;
}
return NULL;
}
void pl_remove(char *msg_name) {
list *lptr = NULL;
msg *message = NULL;
if (!pl_is_exist(msg_name))
return ;
lptr = pl_get_node(msg_name);
// free data
message = (msg*)lptr->data;
free(message->value);
free(message);
// unlink node
if (lptr->prev)
lptr->prev->next = lptr->next;
if (lptr->next)
lptr->next->prev = lptr->prev;
free(lptr);
}
int pl_is_exist(char *msg_name) {
return pl_read(msg_name) ? 1 : 0;
}
list *pl_get_msg_list() {
return msg_list;
}
void pl_set_msg_list(list *new_msg_list) {
msg_list = new_msg_list;
}