From 3c0de35e2c3fa88280c3329294ead42fb109f019 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 3 May 2022 08:18:27 +0300 Subject: [PATCH] test struct & value functionality, implement struct_size --- struct.c | 15 ++++++++++++++- struct.h | 2 ++ test.c | 15 +++++++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/struct.c b/struct.c index 886c1eb..9420f40 100644 --- a/struct.c +++ b/struct.c @@ -57,8 +57,21 @@ uint value_array_size(struct cu_struct_s* st, uint idx, const void* in) { union intvalue_u count = {0}; struct cu_value_s* value = &st->members[idx]; + if (value->array.index) - value_get(st, value->array.count, in, &count); + { + if (value->array.count != idx) + value_get(st, value->array.count, in, &count); + } else count.int32 = value->array.count; + return value->array.item * count.int32; } + +uint struct_size(struct cu_struct_s* st, const void* in) +{ + uint idx = 0; + struct cu_value_s* value = st->members; + while ((value++)->type != NoValue) idx++; + return value_offset(st, idx, in); +} diff --git a/struct.h b/struct.h index 2947440..53fa995 100644 --- a/struct.h +++ b/struct.h @@ -62,4 +62,6 @@ uint value_offset(struct cu_struct_s* st, uint idx, const void* in); void value_get(struct cu_struct_s* st, uint idx, const void* in, void* out); uint value_array_size(struct cu_struct_s* st, uint idx, const void* in); +uint struct_size(struct cu_struct_s* st, const void* in); + #endif \ No newline at end of file diff --git a/test.c b/test.c index f5c8041..3fa5b16 100644 --- a/test.c +++ b/test.c @@ -15,18 +15,20 @@ struct test_s { uint32_t array1[12]; uint16_t count; item_t array2[5]; + uint64_t value; } __attribute__((packed)) test = { .seq = {0}, .array1 = {0}, .count = 5, - .array2 = {1, 2, 3, 4, 5} + .value = 123456789 }; CU_STRUCT_BEGIN(test_s, LittleEndian) CU_VALUE_SEQUENCE(6) CU_VALUE_ARRAY(4, 12) CU_VALUE_INT16() -CU_VALUE_ARRAY_INDEX(14, 3) +CU_VALUE_ARRAY_INDEX(14, 2) +CU_VALUE_INT64() CU_STRUCT_END() int main() @@ -106,8 +108,13 @@ int main() printf("%lx\t%lx\n", 0x12345678abcd8765, h64ton64(0x12345678abcd8765)); printf("[struct]\n"); - printf("value_offset\t%u\n", value_offset(CU_STRUCT(test_s), 1, &test)); - printf("value_size\t%u\n", value_size(CU_STRUCT(test_s), 1, &test)); + printf("value_offset\t%u\n", value_offset(CU_STRUCT(test_s), 4, &test)); + printf("value_size\t%u\n", value_size(CU_STRUCT(test_s), 4, &test)); + uint64_t value; + value_get(CU_STRUCT(test_s), 4, &test, &value); + printf("value\t%u\n", value); + printf("struct_size\t%u\t%u\n", sizeof(struct test_s), + struct_size(CU_STRUCT(test_s), &test)); cutil_exit(); return 0;