-
Notifications
You must be signed in to change notification settings - Fork 0
/
int80_api.c
executable file
·109 lines (78 loc) · 2.55 KB
/
int80_api.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
#include "int80_api.h"
short int80_parse_message(json_t *json, int80_message *msg) {
json_t *command, *params;
if (! msg) return 0;
if (! json_is_object(json)) return 0;
command = json_object_get(json, "command");
if (command == NULL || ! json_is_string(command))
return 0;
printf("command=%X\n", command);
params = json_object_get(json, "params");
// cool
int80_set_message_command_copy(msg, json_string_value(command));
if (params) {
//int80_set_message_params_ref(msg, params);
}
return 1;
}
int80_message* int80_alloc_message(void) {
int80_message *msg = (int80_message *)malloc(sizeof(int80_message));
if (! msg) {
printf("failed to alloc msg!\n");
return NULL;
}
printf("alloc msg=%lX, size=%u\n", msg, sizeof(int80_message));
msg->command = NULL;
msg->params = NULL;
msg->params_copied = 0;
msg->command_copied = 0;
msg->params_ref = 0;
return msg;
}
void int80_free_message(int80_message *msg) {
if (msg->command_copied && msg->command)
free(msg->command);
int80_message_free_params(msg);
free(msg);
}
void int80_set_message_command(int80_message *msg, char *command) {
if (msg->command_copied && msg->command)
free(msg->command);
msg->command = command;
msg->command_copied = 0;
}
void int80_set_message_command_copy(int80_message *msg, const char *command) {
//if (msg->command == command) return;
char *copy;
if (msg->command_copied && msg->command)
free(msg->command);
copy = strdup(command);
msg->command = copy;
printf("copied %X to %X, s1=%s, s2=%s, size=%u\n", command, msg->command, command, msg->command, strlen(msg->command));
msg->command_copied = 1;
}
void int80_set_message_params(int80_message *msg, json_t *params) {
int80_message_free_params(msg);
msg->params = params;
msg->params_copied = 0;
}
void int80_set_message_params_copy(int80_message *msg, json_t *params) {
int80_message_free_params(msg);
msg->params = json_deep_copy(params);
msg->params_copied = 1;
}
void int80_set_message_params_ref(int80_message *msg, json_t *params) {
int80_message_free_params(msg);
json_incref(params);
msg->params_ref = 1;
msg->params = params;
}
void int80_message_free_params(int80_message *msg) {
if (msg->params_copied && msg->params)
free(msg->params);
else if (msg->params_ref && msg->params)
json_decref(msg->params);
msg->params_copied = 0;
msg->params_ref = 0;
msg->params = NULL;
}