blob: b53dd5183648cf543c06fbc996c2690da2d60ea6 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 Sokolovskye9db8402014-04-10 03:45:38 +030027#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 Sokolovsky62044602014-04-19 03:13:15 +030035#include "objstr.h"
Paul Sokolovskye9db8402014-04-10 03:45:38 +030036#include "binary.h"
37
38#if MICROPY_ENABLE_MOD_STRUCT
39
40STATIC 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
59STATIC uint calcsize_items(const char *fmt) {
60 // TODO
61 return strlen(fmt);
62}
63
64STATIC 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 Georgebf8ae4d2014-04-10 13:53:31 +010067 machine_uint_t size;
Paul Sokolovskye9db8402014-04-10 03:45:38 +030068 for (size = 0; *fmt; fmt++) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030069 uint align;
70 int sz = mp_binary_get_size(fmt_type, *fmt, &align);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030071 // TODO
72 assert(sz != -1);
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030073 // Apply alignment
74 size = (size + align - 1) & ~(align - 1);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030075 size += sz;
76 }
77 return MP_OBJ_NEW_SMALL_INT(size);
78}
79MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
80
81STATIC 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 Sokolovskye9db8402014-04-10 03:45:38 +030085 uint size = calcsize_items(fmt);
86 mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
Damien George57a4b4f2014-04-18 22:29:21 +010087 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +010088 mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030089 byte *p = bufinfo.buf;
90
91 for (uint i = 0; i < size; i++) {
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +030092 mp_obj_t item = mp_binary_get_val(fmt_type, *fmt++, &p);
Paul Sokolovskye9db8402014-04-10 03:45:38 +030093 res->items[i] = item;
94 }
95 return res;
96}
97MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack);
98
Paul Sokolovsky62044602014-04-19 03:13:15 +030099STATIC 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 Sokolovsky147c80b2014-05-11 22:50:27 +0300113MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300114
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300115STATIC 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 Sokolovsky62044602014-04-19 03:13:15 +0300118 { MP_OBJ_NEW_QSTR(MP_QSTR_pack), (mp_obj_t)&struct_pack_obj },
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300119 { MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj },
120};
121
122STATIC 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 George6d3c5e42014-04-26 10:47:29 +0100127 .used = ARRAY_SIZE(mp_module_struct_globals_table),
128 .alloc = ARRAY_SIZE(mp_module_struct_globals_table),
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300129 .table = (mp_map_elem_t*)mp_module_struct_globals_table,
130 },
131};
132
133const 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