Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 27 | #include <assert.h> |
| 28 | #include <string.h> |
| 29 | #include "misc.h" |
| 30 | #include "mpconfig.h" |
| 31 | #include "qstr.h" |
| 32 | #include "obj.h" |
| 33 | #include "builtin.h" |
| 34 | #include "objtuple.h" |
Paul Sokolovsky | 6204460 | 2014-04-19 03:13:15 +0300 | [diff] [blame] | 35 | #include "objstr.h" |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 36 | #include "binary.h" |
| 37 | |
| 38 | #if MICROPY_ENABLE_MOD_STRUCT |
| 39 | |
| 40 | STATIC char get_fmt_type(const char **fmt) { |
| 41 | char t = **fmt; |
| 42 | switch (t) { |
| 43 | case '!': |
| 44 | t = '>'; |
| 45 | break; |
| 46 | case '@': |
| 47 | case '=': |
| 48 | case '<': |
| 49 | case '>': |
| 50 | break; |
| 51 | default: |
| 52 | return '@'; |
| 53 | } |
| 54 | // Skip type char |
| 55 | (*fmt)++; |
| 56 | return t; |
| 57 | } |
| 58 | |
| 59 | STATIC uint calcsize_items(const char *fmt) { |
| 60 | // TODO |
| 61 | return strlen(fmt); |
| 62 | } |
| 63 | |
| 64 | STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) { |
| 65 | const char *fmt = mp_obj_str_get_str(fmt_in); |
| 66 | char fmt_type = get_fmt_type(&fmt); |
Damien George | bf8ae4d | 2014-04-10 13:53:31 +0100 | [diff] [blame] | 67 | machine_uint_t size; |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 68 | for (size = 0; *fmt; fmt++) { |
Paul Sokolovsky | 1355cf4 | 2014-04-19 01:25:49 +0300 | [diff] [blame] | 69 | uint align; |
| 70 | int sz = mp_binary_get_size(fmt_type, *fmt, &align); |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 71 | // TODO |
| 72 | assert(sz != -1); |
Paul Sokolovsky | 1355cf4 | 2014-04-19 01:25:49 +0300 | [diff] [blame] | 73 | // Apply alignment |
| 74 | size = (size + align - 1) & ~(align - 1); |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 75 | size += sz; |
| 76 | } |
| 77 | return MP_OBJ_NEW_SMALL_INT(size); |
| 78 | } |
| 79 | MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize); |
| 80 | |
| 81 | STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) { |
| 82 | // TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))." |
| 83 | const char *fmt = mp_obj_str_get_str(fmt_in); |
| 84 | char fmt_type = get_fmt_type(&fmt); |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 85 | uint size = calcsize_items(fmt); |
| 86 | mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL); |
Damien George | 57a4b4f | 2014-04-18 22:29:21 +0100 | [diff] [blame] | 87 | mp_buffer_info_t bufinfo; |
Damien George | b11b85a | 2014-04-18 22:59:24 +0100 | [diff] [blame] | 88 | mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ); |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 89 | byte *p = bufinfo.buf; |
| 90 | |
| 91 | for (uint i = 0; i < size; i++) { |
Paul Sokolovsky | 0c43cf9 | 2014-04-11 03:47:21 +0300 | [diff] [blame] | 92 | mp_obj_t item = mp_binary_get_val(fmt_type, *fmt++, &p); |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 93 | res->items[i] = item; |
| 94 | } |
| 95 | return res; |
| 96 | } |
| 97 | MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack); |
| 98 | |
Paul Sokolovsky | 6204460 | 2014-04-19 03:13:15 +0300 | [diff] [blame] | 99 | STATIC mp_obj_t struct_pack(uint n_args, mp_obj_t *args) { |
| 100 | // TODO: "The arguments must match the values required by the format exactly." |
| 101 | const char *fmt = mp_obj_str_get_str(args[0]); |
| 102 | char fmt_type = get_fmt_type(&fmt); |
| 103 | int size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0])); |
| 104 | byte *p; |
| 105 | mp_obj_t res = mp_obj_str_builder_start(&mp_type_bytes, size, &p); |
| 106 | memset(p, 0, size); |
| 107 | |
| 108 | for (uint i = 1; i < n_args; i++) { |
| 109 | mp_binary_set_val(fmt_type, *fmt++, args[i], &p); |
| 110 | } |
| 111 | return res; |
| 112 | } |
Paul Sokolovsky | 147c80b | 2014-05-11 22:50:27 +0300 | [diff] [blame^] | 113 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack); |
Paul Sokolovsky | 6204460 | 2014-04-19 03:13:15 +0300 | [diff] [blame] | 114 | |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 115 | STATIC const mp_map_elem_t mp_module_struct_globals_table[] = { |
| 116 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) }, |
| 117 | { MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj }, |
Paul Sokolovsky | 6204460 | 2014-04-19 03:13:15 +0300 | [diff] [blame] | 118 | { MP_OBJ_NEW_QSTR(MP_QSTR_pack), (mp_obj_t)&struct_pack_obj }, |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 119 | { MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj }, |
| 120 | }; |
| 121 | |
| 122 | STATIC const mp_obj_dict_t mp_module_struct_globals = { |
| 123 | .base = {&mp_type_dict}, |
| 124 | .map = { |
| 125 | .all_keys_are_qstrs = 1, |
| 126 | .table_is_fixed_array = 1, |
Damien George | 6d3c5e4 | 2014-04-26 10:47:29 +0100 | [diff] [blame] | 127 | .used = ARRAY_SIZE(mp_module_struct_globals_table), |
| 128 | .alloc = ARRAY_SIZE(mp_module_struct_globals_table), |
Paul Sokolovsky | e9db840 | 2014-04-10 03:45:38 +0300 | [diff] [blame] | 129 | .table = (mp_map_elem_t*)mp_module_struct_globals_table, |
| 130 | }, |
| 131 | }; |
| 132 | |
| 133 | const mp_obj_module_t mp_module_struct = { |
| 134 | .base = { &mp_type_module }, |
| 135 | .name = MP_QSTR_struct, |
| 136 | .globals = (mp_obj_dict_t*)&mp_module_struct_globals, |
| 137 | }; |
| 138 | |
| 139 | #endif |