diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-06-02 16:04:26 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-06-02 16:35:56 +0300 |
commit | 62798831bea83f7c75810064115e7f3a65892ca5 (patch) | |
tree | 6dbe8f6159917ee41ec53e353b23ed4940444415 /py | |
parent | b55a59de4c988b3d783f4d6ddaa95ebcb2538c62 (diff) | |
download | micropython-62798831bea83f7c75810064115e7f3a65892ca5.tar.gz micropython-62798831bea83f7c75810064115e7f3a65892ca5.zip |
modstruct: Add one more extension to typecodes - 'S', a pointer to C string.
Also, add comment with description of extension to CPython's typecodes.
Diffstat (limited to 'py')
-rw-r--r-- | py/binary.c | 5 | ||||
-rw-r--r-- | py/modstruct.c | 16 |
2 files changed, 20 insertions, 1 deletions
diff --git a/py/binary.c b/py/binary.c index cec2374460..833d9c85ad 100644 --- a/py/binary.c +++ b/py/binary.c @@ -26,6 +26,7 @@ #include <stdint.h> #include <stdlib.h> +#include <string.h> #include <assert.h> #include "misc.h" @@ -75,7 +76,7 @@ int mp_binary_get_size(char struct_type, char val_type, uint *palign) { case 'q': case 'Q': // TODO: This is for x86 align = sizeof(int); size = sizeof(long long); break; - case 'P': case 'O': + case 'P': case 'O': case 'S': align = size = sizeof(void*); break; } } @@ -161,6 +162,8 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) { *ptr += size; if (val_type == 'O') { return (mp_obj_t)val; + } else if (val_type == 'S') { + return mp_obj_new_str((char*)val, strlen((char*)val), false); } else if (is_signed(val_type)) { return mp_obj_new_int(val); } else { diff --git a/py/modstruct.c b/py/modstruct.c index 39571e3efa..a45181852c 100644 --- a/py/modstruct.c +++ b/py/modstruct.c @@ -39,6 +39,22 @@ #if MICROPY_PY_STRUCT +/* + This module implements most of character typecodes from CPython, with + some extensions: + + O - (Pointer to) an arbitrary Python object. This is useful for callback + data, etc. Note that you must keep reference to passed object in + your Python application, otherwise it may be garbage-collected, + and then when you get back this value from callback it may be + invalid (and lead to crash). + S - Pointer to a string (returned as a Python string). Note the + difference from "Ns", - the latter says "in this place of structure + is character data of up to N bytes length", while "S" means + "in this place of a structure is a pointer to zero-terminated + character data". + */ + STATIC char get_fmt_type(const char **fmt) { char t = **fmt; switch (t) { |