|
| 1 | +#include <assert.h> |
| 2 | +#include <string.h> |
| 3 | +#include "misc.h" |
| 4 | +#include "mpconfig.h" |
| 5 | +#include "qstr.h" |
| 6 | +#include "obj.h" |
| 7 | +#include "builtin.h" |
| 8 | +#include "objtuple.h" |
| 9 | +#include "binary.h" |
| 10 | + |
| 11 | +#if MICROPY_ENABLE_MOD_STRUCT |
| 12 | + |
| 13 | +STATIC char get_fmt_type(const char **fmt) { |
| 14 | + char t = **fmt; |
| 15 | + switch (t) { |
| 16 | + case '!': |
| 17 | + t = '>'; |
| 18 | + break; |
| 19 | + case '@': |
| 20 | + case '=': |
| 21 | + case '<': |
| 22 | + case '>': |
| 23 | + break; |
| 24 | + default: |
| 25 | + return '@'; |
| 26 | + } |
| 27 | + // Skip type char |
| 28 | + (*fmt)++; |
| 29 | + return t; |
| 30 | +} |
| 31 | + |
| 32 | +STATIC uint calcsize_items(const char *fmt) { |
| 33 | + // TODO |
| 34 | + return strlen(fmt); |
| 35 | +} |
| 36 | + |
| 37 | +STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) { |
| 38 | + const char *fmt = mp_obj_str_get_str(fmt_in); |
| 39 | + char fmt_type = get_fmt_type(&fmt); |
| 40 | + assert(fmt_type == '<'); (void)fmt_type; |
| 41 | + uint size; |
| 42 | + for (size = 0; *fmt; fmt++) { |
| 43 | + int sz = mp_binary_get_size(*fmt); |
| 44 | + // TODO |
| 45 | + assert(sz != -1); |
| 46 | + size += sz; |
| 47 | + } |
| 48 | + return MP_OBJ_NEW_SMALL_INT(size); |
| 49 | +} |
| 50 | +MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize); |
| 51 | + |
| 52 | +STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) { |
| 53 | + // TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))." |
| 54 | + const char *fmt = mp_obj_str_get_str(fmt_in); |
| 55 | + char fmt_type = get_fmt_type(&fmt); |
| 56 | + assert(fmt_type == '<'); (void)fmt_type; |
| 57 | + uint size = calcsize_items(fmt); |
| 58 | + mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL); |
| 59 | + buffer_info_t bufinfo; |
| 60 | + mp_get_buffer_raise(data_in, &bufinfo); |
| 61 | + byte *p = bufinfo.buf; |
| 62 | + |
| 63 | + for (uint i = 0; i < size; i++) { |
| 64 | + mp_obj_t item = mp_binary_get_val_unaligned_le(*fmt++, &p); |
| 65 | + res->items[i] = item; |
| 66 | + } |
| 67 | + return res; |
| 68 | +} |
| 69 | +MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack); |
| 70 | + |
| 71 | +STATIC const mp_map_elem_t mp_module_struct_globals_table[] = { |
| 72 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) }, |
| 73 | + { MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj }, |
| 74 | + { MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj }, |
| 75 | +}; |
| 76 | + |
| 77 | +STATIC const mp_obj_dict_t mp_module_struct_globals = { |
| 78 | + .base = {&mp_type_dict}, |
| 79 | + .map = { |
| 80 | + .all_keys_are_qstrs = 1, |
| 81 | + .table_is_fixed_array = 1, |
| 82 | + .used = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t), |
| 83 | + .alloc = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t), |
| 84 | + .table = (mp_map_elem_t*)mp_module_struct_globals_table, |
| 85 | + }, |
| 86 | +}; |
| 87 | + |
| 88 | +const mp_obj_module_t mp_module_struct = { |
| 89 | + .base = { &mp_type_module }, |
| 90 | + .name = MP_QSTR_struct, |
| 91 | + .globals = (mp_obj_dict_t*)&mp_module_struct_globals, |
| 92 | +}; |
| 93 | + |
| 94 | +#endif |
0 commit comments