mirror of
https://github.com/fumiama/simple-protobuf.git
synced 2026-06-19 09:40:24 +08:00
增加实际读取长度
This commit is contained in:
@@ -33,11 +33,13 @@ 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 struct_len = read_num(fp);
|
uint32_t struct_len = read_num(fp);
|
||||||
if(struct_len > 1) {
|
if(struct_len > 1) {
|
||||||
SIMPLE_PB* spb = malloc(struct_len + sizeof(uint32_t));
|
SIMPLE_PB* spb = malloc(struct_len + sizeof(uint32_t));
|
||||||
if(spb) {
|
if(spb) {
|
||||||
spb->len = struct_len;
|
spb->struct_len = struct_len;
|
||||||
|
spb->real_len = 0;
|
||||||
char* p = spb->target;
|
char* p = spb->target;
|
||||||
char* end = p + struct_len;
|
char* end = p + struct_len;
|
||||||
memset(p, 0, struct_len);
|
memset(p, 0, struct_len);
|
||||||
@@ -47,6 +49,7 @@ SIMPLE_PB* get_pb(FILE* fp) {
|
|||||||
fread(p, data_len, 1, fp);
|
fread(p, data_len, 1, fp);
|
||||||
p += offset;
|
p += offset;
|
||||||
}
|
}
|
||||||
|
spb->real_len = ftell(fp) - init_pos;
|
||||||
return spb;
|
return spb;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
struct SIMPLE_PB {
|
struct SIMPLE_PB {
|
||||||
uint32_t len;
|
uint32_t struct_len, real_len;
|
||||||
char target[];
|
char target[];
|
||||||
};
|
};
|
||||||
typedef struct SIMPLE_PB SIMPLE_PB;
|
typedef struct SIMPLE_PB SIMPLE_PB;
|
||||||
|
|||||||
1
test.c
1
test.c
@@ -34,6 +34,7 @@ int main() {
|
|||||||
SIMPLE_PB* spb = get_pb(fp);
|
SIMPLE_PB* spb = get_pb(fp);
|
||||||
memcpy(&t, spb->target, sizeof(struct TEST));
|
memcpy(&t, spb->target, sizeof(struct TEST));
|
||||||
printf("a:%u\nb:%u\nc:%u\nd:%llu\ne:%s\n", t.a, t.b, t.c, t.d, t.e);
|
printf("a:%u\nb:%u\nc:%u\nd:%llu\ne:%s\n", t.a, t.b, t.c, t.d, t.e);
|
||||||
|
printf("Struct size: %u, read: %u\n", spb->struct_len, spb->real_len);
|
||||||
} else perror("[SPB]");
|
} else perror("[SPB]");
|
||||||
} else perror("[SPB]");
|
} else perror("[SPB]");
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user