blob: 2a3c3c0ad67a663b237a3492f87a2c94744b5673 [file] [log] [blame]
Paul Sokolovskye9db8402014-04-10 03:45:38 +03001#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"
Paul Sokolovsky62044602014-04-19 03:13:15 +03009#include "objstr.h"
Paul Sokolovskye9db8402014-04-10 03:45:38 +030010#include "binary.h"
11
12#if MICROPY_ENABLE_MOD_STRUCT
13
14STATIC char get_fmt_type(const char **fmt) {
15 char t = **fmt;
16 switch (t) {
17 case '!':
18 t = '>';
19 break;
20 case '@':
21 case '=':
22 case '<':
23 case '>':
24 break;
25 default:
26 return '@';
27 }
28 // Skip type char
29 (*fmt)++;
30 return t;
31}
32
33STATIC uint calcsize_items(const char *fmt) {
34 // TODO
35 return strlen(fmt);
36}
37
38STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
39 const char *fmt = mp_obj_str_get_str(fmt_in);
40 char fmt_type = get_fmt_type(&fmt);
Damien Georgebf8ae4d2014-04-10 13:53:31 +010041 machine_uint_t size;
Paul Sokolovskye9db8402014-04-10 03:45:38 +030042 for (size = 0; *fmt; fmt++) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030043 uint align;
44 int sz = mp_binary_get_size(fmt_type, *fmt, &align);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030045 // TODO
46 assert(sz != -1);
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030047 // Apply alignment
48 size = (size + align - 1) & ~(align - 1);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030049 size += sz;
50 }
51 return MP_OBJ_NEW_SMALL_INT(size);
52}
53MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
54
55STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
56 // TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))."
57 const char *fmt = mp_obj_str_get_str(fmt_in);
58 char fmt_type = get_fmt_type(&fmt);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030059 uint size = calcsize_items(fmt);
60 mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
Damien George57a4b4f2014-04-18 22:29:21 +010061 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +010062 mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030063 byte *p = bufinfo.buf;
64
65 for (uint i = 0; i < size; i++) {
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +030066 mp_obj_t item = mp_binary_get_val(fmt_type, *fmt++, &p);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030067 res->items[i] = item;
68 }
69 return res;
70}
71MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack);
72
Paul Sokolovsky62044602014-04-19 03:13:15 +030073STATIC mp_obj_t struct_pack(uint n_args, mp_obj_t *args) {
74 // TODO: "The arguments must match the values required by the format exactly."
75 const char *fmt = mp_obj_str_get_str(args[0]);
76 char fmt_type = get_fmt_type(&fmt);
77 int size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
78 byte *p;
79 mp_obj_t res = mp_obj_str_builder_start(&mp_type_bytes, size, &p);
80 memset(p, 0, size);
81
82 for (uint i = 1; i < n_args; i++) {
83 mp_binary_set_val(fmt_type, *fmt++, args[i], &p);
84 }
85 return res;
86}
87MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, -1, struct_pack);
88
Paul Sokolovskye9db8402014-04-10 03:45:38 +030089STATIC const mp_map_elem_t mp_module_struct_globals_table[] = {
90 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) },
91 { MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj },
Paul Sokolovsky62044602014-04-19 03:13:15 +030092 { MP_OBJ_NEW_QSTR(MP_QSTR_pack), (mp_obj_t)&struct_pack_obj },
Paul Sokolovskye9db8402014-04-10 03:45:38 +030093 { MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj },
94};
95
96STATIC const mp_obj_dict_t mp_module_struct_globals = {
97 .base = {&mp_type_dict},
98 .map = {
99 .all_keys_are_qstrs = 1,
100 .table_is_fixed_array = 1,
Damien George6d3c5e42014-04-26 10:47:29 +0100101 .used = ARRAY_SIZE(mp_module_struct_globals_table),
102 .alloc = ARRAY_SIZE(mp_module_struct_globals_table),
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300103 .table = (mp_map_elem_t*)mp_module_struct_globals_table,
104 },
105};
106
107const mp_obj_module_t mp_module_struct = {
108 .base = { &mp_type_module },
109 .name = MP_QSTR_struct,
110 .globals = (mp_obj_dict_t*)&mp_module_struct_globals,
111};
112
113#endif