-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize.c
355 lines (334 loc) · 7.91 KB
/
serialize.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <assert.h>
#include <ctype.h>
#include "dalloc.h"
#include "pary.h"
#include "mc_protocol.h"
typedef struct serialize_ret_s serialize_ret_t;
//序列化函数的返回值
struct serialize_ret_s
{
pary_type_t type; //类型
buff_t *buff;//数据
int item_num;//有多少个元素,数组
};
//序列化一个节点
static serialize_ret_t *serialize_node(const pary_node_t *node);
//序列化一个一个对象
static serialize_ret_t *serialize_object(const pary_object_t *object);
/**
* @desc 将序列化过的数据反序列化
* @param pary_node_t *node 最后返回节点
* @param char *str 序列化过的字符串
* @return char *
*/
char *unserialize(pary_node_t *node,char *str)
{
assert(node && str);
char *curr_pos = str;
bool find_begin_mark = true;
bool find_key = false;
bool find_value = false;
char *key = NULL;
char *value = NULL;
while(NULL != curr_pos || '\0' != *curr_pos)
{
if(true == find_begin_mark)
{
curr_pos = strstr(curr_pos,"{");
if(NULL == curr_pos) return false;
curr_pos++;
find_key = true;
find_begin_mark = false;
find_value = false;
}
else if(true == find_key)
{
char type;
GET_STR(&type,1,curr_pos,":",1);
if('s' == type)
{
//字符串的情况
int len = 0;
GET_INT(len,curr_pos,":",1);
curr_pos++;
DMALLOC(key,char *,len+1);
memset(key,0,len+1);
strncpy(key,curr_pos,len);
key[len] = '\0';
curr_pos += len + 2;
}
else if('i' == type )
{
//整数的情况
//整数最大长度19
DMALLOC(key,char *,20);
memset(key,0,20);
int i = 0;
GET_INT(i,curr_pos,";",1);
sprintf(key,"%d",i);
}
else if('}' == type)//空数组
{
curr_pos++;
return curr_pos;
}
else
{
printf("unserialize bad type ==%c==\n",type);
return NULL;
}
find_key = false;
find_begin_mark = false;
find_value = true;
}
else if(true == find_value)
{
//找值
char type;
GET_STR(&type,1,curr_pos,":",1);
if('s' == type)
{
//字符串的情况
int len = 0;
GET_INT(len,curr_pos,":",1);
curr_pos++;
DMALLOC(value,char *,len+1);
memset(value,0,len+1);
strncpy(value,curr_pos,len);
value[len] = '\0';
buff_t *buff = create_buff(len);
append_buff(buff,value,len);
DFREE(value);
pary_node_t *tmp_node = create_pary_node_buff(buff);
set_pary_node(node,key,tmp_node,true);
curr_pos += len + 1;
if('}' == *curr_pos)
{
curr_pos++;
return curr_pos;
}
curr_pos++;
find_key = true;
find_begin_mark = false;
find_value = false;
}
else if('i' == type)
{
//整形的情况
int i = 0;
GET_INT(i,curr_pos,";",1);
pary_node_t *tmp_node = create_pary_node_int(i);
set_pary_node(node,key,tmp_node,true);
find_key = true;
find_begin_mark = false;
find_value = false;
}
else if('b' == type)
{
//整形的情况
int i = 0;
GET_INT(i,curr_pos,";",1);
pary_node_t *tmp_node = create_pary_node_bool(i);
set_pary_node(node,key,tmp_node,true);
find_key = true;
find_begin_mark = false;
find_value = false;
}
else if('d' == type)
{
//double的情况
double d = 0;
GET_DOUBLE(d,curr_pos,";",1);
pary_node_t *tmp_node = create_pary_node_double(d);
set_pary_node(node,key,tmp_node,true);
find_key = true;
find_begin_mark = false;
find_value = false;
}
else if('a' == type)
{
//数组的情况
int item_num = 0;
GET_INT(item_num,curr_pos,":",1);
pary_node_t *tmp_node = create_pary_node_object();
set_pary_node(node,key,tmp_node,true);
if(item_num > 0)
{
curr_pos = unserialize(tmp_node,curr_pos);
}
else
{
curr_pos += 2; //跳过{}这样的情况
}
if(NULL == curr_pos) return NULL;
find_key = true;
find_begin_mark = false;
find_value = false;
}
if(NULL == curr_pos)
{
return NULL;
}
if(0 == strlen(curr_pos))
{
return NULL;
}
if('}' == *curr_pos)
{
curr_pos++;
return curr_pos;
}
}
}
return NULL;
}
/**
* @desc 序列化一个节点
* @param pary_node_t *node 节点
* @return buff_t *
*/
buff_t *serialize(const pary_node_t *node)
{
serialize_ret_t *ret = serialize_node(node);
buff_t *buff = ret->buff;
DFREE(ret);
return buff;
}
/**
* @desc 序列化一个节点
* @param pary_node_t *node 节点
* @return serialize_ret_t *
*/
serialize_ret_t *serialize_node(const pary_node_t *node)
{
assert(node);
serialize_ret_t *ret = NULL,*ret_item = NULL;
DMALLOC(ret,serialize_ret_t *,sizeof(serialize_ret_t));
if(NULL == ret) return NULL;
char tmp_value[56];
memset(tmp_value,0,56);
int value_buff_len = 0;
buff_t *buff = create_buff(1024);
switch(node->type)
{
case PARY_TYPE_INT:
value_buff_len = sprintf(tmp_value,"i:%d;",node->data.data_int);
append_buff(buff,tmp_value,value_buff_len);
ret->type = PARY_TYPE_INT;
break;
case PARY_TYPE_BOOL:
value_buff_len = sprintf(tmp_value,"b:%d;",node->data.data_int);
append_buff(buff,tmp_value,value_buff_len);
ret->type = PARY_TYPE_INT;
break;
case PARY_TYPE_DOUBLE:
value_buff_len = sprintf(tmp_value,"d:%lf;",node->data.data_double);
append_buff(buff,tmp_value,value_buff_len);
ret->type = PARY_TYPE_DOUBLE;
break;
case PARY_TYPE_BUFF:
value_buff_len = sprintf(tmp_value,"s:%d:",node->data.data_buff->length);
append_buff(buff,tmp_value,value_buff_len);
append_buff(buff,"\"",1);
append_buff(buff,node->data.data_buff->data,node->data.data_buff->length);
append_buff(buff,"\";",2);
ret->type = PARY_TYPE_BUFF;
break;
case PARY_TYPE_OBJECT:
ret_item = serialize_object(node->data.data_object);
value_buff_len = sprintf(tmp_value,"a:%d:",ret_item->item_num);
append_buff(buff,tmp_value,value_buff_len);
append_buff(buff,ret_item->buff->data,ret_item->buff->length);
destroy_buff(ret_item->buff);
DFREE(ret_item);
ret->type = PARY_TYPE_OBJECT;
break;
default:
printf("serialize bad type");
break;
}
ret->buff = buff;
return ret;
}
/**
* @desc 序列化一个对象
* @param const pary_object_t *object 对象
* @return serialize_ret_t *
*/
serialize_ret_t *serialize_object(const pary_object_t *object)
{
serialize_ret_t *ret = NULL;
DMALLOC(ret,serialize_ret_t *,sizeof(serialize_ret_t));
if(NULL == ret) return NULL;
buff_t *buff = create_buff(1024);
append_buff(buff,"{",1);
pary_list_t *item = NULL,*list = object->list;
item = list->pre->next;
char key[255];
int item_num = 0,key_len = 0,key_buff_len = 0;
char tmp_key[300];
char tmp_value[56];
char *p = NULL;
serialize_ret_t *ret_item = NULL;
while(item != list->next)
{
bool is_numeric = true;
memset(key,0,255);
memset(tmp_key,0,300);
memset(tmp_value,0,56);
strcpy(key,item->rbnode->key);
p = item->rbnode->key;
char c;
while('\0' != (c = *p++))
{
if(!isdigit(c)) is_numeric = false;
}
key_len = strlen(key);
if(is_numeric)
{
//数字类型
key_buff_len = sprintf(tmp_key,"i:%ld;",strtol(key,NULL,10));
}
else
{
//字符串类型
key_buff_len = sprintf(tmp_key,"s:%d:\"%s\";",key_len,key);
}
append_buff(buff,tmp_key,key_buff_len);
ret_item = serialize_node(item->rbnode->value);
append_buff(buff,ret_item->buff->data,ret_item->buff->length);
destroy_buff(ret_item->buff);
DFREE(ret_item);
ret_item = NULL;
item = item->next;
item_num++;
}
append_buff(buff,"}",1);
if(NULL != ret_item)
{
destroy_buff(ret_item->buff);
DFREE(ret_item);
}
ret->item_num = item_num;
ret->type = PARY_TYPE_OBJECT;
ret->buff = buff;
return ret;
}
/**
* @desc 判断一个字符串是否是序列化过的
* @param void *str 数据
* @param int len str的长度
* @return bool
*/
bool is_serialized(void *str,int len)
{
char a = 'a';
char b = ':';
char c = '}';
if(
0 == memcmp(&a,str,sizeof(char)) &&
0 == memcmp(&b,str+1,sizeof(char)) &&
0 == memcmp(&c,str-1+len,sizeof(char))
) return true;
else return false;
}