mirror of
https://github.com/fumiama/simple-protobuf.git
synced 2026-06-12 12:50:30 +08:00
优化代码结构
This commit is contained in:
30
protobuf.c
30
protobuf.c
@@ -36,25 +36,19 @@ static int write_num(FILE* fp, uint32_t n) {
|
|||||||
SIMPLE_PB* get_pb(FILE* fp) {
|
SIMPLE_PB* get_pb(FILE* fp) {
|
||||||
uint32_t init_pos = ftell(fp);
|
uint32_t init_pos = ftell(fp);
|
||||||
uint32_t struct_len = read_num(fp);
|
uint32_t struct_len = read_num(fp);
|
||||||
if(struct_len > 1) {
|
if(struct_len <= 1) return NULL;
|
||||||
SIMPLE_PB* spb = malloc(struct_len + 2 * sizeof(uint32_t));
|
SIMPLE_PB* spb = malloc(struct_len + 2 * sizeof(uint32_t));
|
||||||
if(spb) {
|
if(!spb) return NULL;
|
||||||
spb->struct_len = struct_len;
|
spb->struct_len = struct_len;
|
||||||
spb->real_len = 0;
|
memset(spb->target, 0, struct_len);
|
||||||
char* p = spb->target;
|
uint32_t offset, data_len;
|
||||||
char* end = p + struct_len;
|
for(char* p = spb->target; p < spb->target+struct_len; p += offset) {
|
||||||
memset(p, 0, struct_len);
|
offset = read_num(fp);
|
||||||
while(p < end) {
|
data_len = read_num(fp);
|
||||||
uint32_t offset = read_num(fp);
|
if(data_len > 0) fread(p, data_len, 1, fp);
|
||||||
uint32_t data_len = read_num(fp);
|
|
||||||
if(data_len > 0) fread(p, data_len, 1, fp);
|
|
||||||
p += offset;
|
|
||||||
}
|
|
||||||
spb->real_len = ftell(fp) - init_pos;
|
|
||||||
return spb;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return NULL;
|
spb->real_len = ftell(fp) - init_pos;
|
||||||
|
return spb;
|
||||||
}
|
}
|
||||||
|
|
||||||
int set_pb(FILE* fp, uint32_t* items_len, uint32_t struct_len, void* target) {
|
int set_pb(FILE* fp, uint32_t* items_len, uint32_t struct_len, void* target) {
|
||||||
|
|||||||
Reference in New Issue
Block a user