summaryrefslogtreecommitdiff
path: root/py/modstruct.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-19 03:13:15 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-19 03:22:08 +0300
commit6204460461cc21b27861fa89b2423119f8cdce88 (patch)
treea47370140e792aa9722bced667b0922e22122647 /py/modstruct.c
parent504e23388ce68fcb1ac80a7c411979718686af7c (diff)
modstruct: Initial implementation of struct.pack().
Diffstat (limited to 'py/modstruct.c')
-rw-r--r--py/modstruct.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/py/modstruct.c b/py/modstruct.c
index 0be194fee..81afd94d1 100644
--- a/py/modstruct.c
+++ b/py/modstruct.c
@@ -6,6 +6,7 @@
#include "obj.h"
#include "builtin.h"
#include "objtuple.h"
+#include "objstr.h"
#include "binary.h"
#if MICROPY_ENABLE_MOD_STRUCT
@@ -69,9 +70,26 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
}
MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack);
+STATIC mp_obj_t struct_pack(uint n_args, mp_obj_t *args) {
+ // TODO: "The arguments must match the values required by the format exactly."
+ const char *fmt = mp_obj_str_get_str(args[0]);
+ char fmt_type = get_fmt_type(&fmt);
+ int size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
+ byte *p;
+ mp_obj_t res = mp_obj_str_builder_start(&mp_type_bytes, size, &p);
+ memset(p, 0, size);
+
+ for (uint i = 1; i < n_args; i++) {
+ mp_binary_set_val(fmt_type, *fmt++, args[i], &p);
+ }
+ return res;
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, -1, struct_pack);
+
STATIC const mp_map_elem_t mp_module_struct_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_pack), (mp_obj_t)&struct_pack_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj },
};