packet.c
2.52 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <json/json.h>
#include "token/cclass.h"
#include "token/packet.h"
#include "token/dyntype.h"
#include "token/dyntype/hash.h"
void
setHashString(DYNTYPE_HASH hash, const char * const key, const char * value)
{
DYNTYPE dyn;
dyn = new(DYNTYPE, DYNTYPE_TYPE_STRING, strlen(value), value);
dyntype_hash_set(hash, key, dyn);
}
void
setHashInt(DYNTYPE_HASH hash, const char * const key, const int value)
{
DYNTYPE dyn;
dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), value);
dyntype_hash_set(hash, key, dyn);
}
void
deleteHashValue(DYNTYPE_HASH hash, const char * const key)
{
DYNTYPE dyn = dyntype_hash_get(hash, key);
delete(&dyn);
}
int
main(int argc, char * argv[])
{
PACKET packet;
DYNTYPE_HASH data;
DYNTYPE _clear;
struct json_object * json;
char * json_str;
packet = new(PACKET, NULL);
packet_setHeader(
packet,
new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), time(NULL)));
data = new(DYNTYPE_HASH, NULL);
setHashString(data, "#C#", "this comes from C");
setHashString(data, "usr", "ppohg");
setHashString(data, "pas", "yyyyy");
setHashInt(data, "val", 321);
packet_setData(
packet,
new(DYNTYPE,
DYNTYPE_HASH,
sizeof(struct DYNTYPE_HASH *),
data));
json = toJson(packet);
json_str = calloc(
strlen(json_object_to_json_string(json)) + 1,
sizeof(char));
memcpy(json_str,
json_object_to_json_string(json),
strlen(json_object_to_json_string(json)));
printf("%s\n", json_str);
deleteHashValue(data, "#C#");
deleteHashValue(data, "usr");
deleteHashValue(data, "pas");
deleteHashValue(data, "val");
_clear = packet_getHeader(packet);
delete(&_clear);
_clear = packet_getData(packet);
delete(&_clear);
delete(&packet);
json_object_put(json);
json = json_tokener_parse(json_str);
packet = newFromJson(PACKET, json);
printf("%s\n", json_object_to_json_string(json));
data = (packet_getData(packet)->data)._hash;
deleteHashValue(data, "#C#");
deleteHashValue(data, "usr");
deleteHashValue(data, "pas");
deleteHashValue(data, "val");
_clear = packet_getHeader(packet);
delete(&_clear);
_clear = packet_getData(packet);
delete(&_clear);
delete(&packet);
free(json_str);
json_object_put(json);
return 0;
}
// vim: set et ts=4 sw=4: