Skip to content

Commit

Permalink
[component/python/os] add read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
versaloon committed Sep 7, 2023
1 parent ef66623 commit 08659fe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ typedef mp_obj_t vsf_pyal_obj_t;
#define vsf_pyal_intobj_get_int(__intobj) mp_obj_get_int(__intobj)
#define vsf_pyal_newobj_str(__str) mp_obj_new_str((const char *)(__str), strlen(__str))
#define vsf_pyal_strobj_get_str(__strojb) mp_obj_str_get_str(__strojb)
#define vsf_pyal_newobj_bytes(__data, __len) mp_obj_new_bytes((const byte *)(__data), __len)
#define vsf_pyal_bytesobj_get_data(__bytesobj, __len_ptr) \
(uint8_t *)mp_obj_str_get_data((__bytesobj), (__len_ptr))

typedef struct _mp_obj_file_t {
mp_obj_base_t base;
Expand All @@ -74,6 +77,7 @@ extern const mp_obj_type_t mp_type_textio;
fileobj->f = (__file); \
MP_OBJ_FROM_PTR(fileobj); \
})
#define vsf_pyal_fileobj_get_file(__fileobj) ((mp_obj_file_t *)(__fileobj))->f

// typle

Expand Down Expand Up @@ -123,6 +127,7 @@ typedef mp_obj_t vsf_pyal_dict_key_t;
#define vsf_pyal_funcarg_var_is_str(__name, __idx) mp_obj_is_str((__name ## _arr)[__idx])
#define vsf_pyal_funcarg_var_get_str(__name, __idx) vsf_pyal_strobj_get_str((__name ## _arr)[__idx])
#define vsf_pyal_funcarg_var_get_int(__name, __idx) vsf_pyal_intobj_get_int((__name ## _arr)[__idx])
#define vsf_pyal_funcarg_var_get_obj(__name, __idx) ((__name ## _arr)[__idx])

#define vsf_pyal_func_void_return_t mp_obj_t
#define vsf_pyal_func_void_return() return mp_const_none
Expand Down
62 changes: 62 additions & 0 deletions source/component/script/python/module/os/vsf_python_module_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,66 @@ vsf_pyal_module_func_var_imp(os, open, vsf_pyal_obj_t, 2, 3, vsf_pyal_funcarg_va
}
}

vsf_pyal_module_func_var_imp(os, read, vsf_pyal_obj_t, 2, 2, vsf_pyal_funcarg_var(arg))
{
// the 2nd arg is length
ssize_t length = vsf_pyal_funcarg_var_get_int(arg, 1);
int fd;

// if the first arg is int, it's fd of read
// if the first arg if file_obj, it's FILE of fread
if (vsf_pyal_funcarg_var_is_int(arg, 0)) {
fd = vsf_pyal_funcarg_var_get_int(arg, 0);
} else {
vsf_pyal_obj_t fileobj = vsf_pyal_funcarg_var_get_obj(arg, 0);
FILE *f = vsf_pyal_fileobj_get_file(fileobj);
fd = ((vsf_linux_fd_t *)f)->fd;
}

uint8_t *buffer = malloc(length);
if (NULL == buffer) {
vsf_pyal_raise("not enough memory\n");
return VSF_PYAL_OBJ_NULL;
}

length = read(fd, buffer, length);
if (length <= 0) {
vsf_pyal_raise("fail to read fd %d\n", fd);
free(buffer);
return VSF_PYAL_OBJ_NULL;
}

vsf_pyal_obj_t result = vsf_pyal_newobj_bytes(buffer, length);
free(buffer);
return result;
}

vsf_pyal_module_func_var_imp(os, write, vsf_pyal_func_void_return_t, 2, 2, vsf_pyal_funcarg_var(arg))
{
// the 2nd arg is bytesobj
vsf_pyal_obj_t bytesobj = vsf_pyal_funcarg_var_get_obj(arg, 1);
size_t length;
uint8_t *buffer = vsf_pyal_bytesobj_get_data(bytesobj, &length);
int fd;

// if the first arg is int, it's fd of read
// if the first arg if file_obj, it's FILE of fread
if (vsf_pyal_funcarg_var_is_int(arg, 0)) {
fd = vsf_pyal_funcarg_var_get_int(arg, 0);
} else {
vsf_pyal_obj_t fileobj = vsf_pyal_funcarg_var_get_obj(arg, 0);
FILE *f = vsf_pyal_fileobj_get_file(fileobj);
fd = ((vsf_linux_fd_t *)f)->fd;
}

length = write(fd, buffer, length);
if (length <= 0) {
vsf_pyal_raise("fail to write fd %d\n", fd);
}

vsf_pyal_func_void_return();
}

#ifdef vsf_pyal_module
vsf_pyal_module(os,
vsf_pyal_module_func(os, __init__),
Expand All @@ -230,6 +290,8 @@ vsf_pyal_module(os,
vsf_pyal_module_func(os, rename),
vsf_pyal_module_func(os, remove),
vsf_pyal_module_func(os, open),
vsf_pyal_module_func(os, read),
vsf_pyal_module_func(os, write),
vsf_pyal_module_int(os, O_RDONLY, O_RDONLY),
vsf_pyal_module_int(os, O_WRONLY, O_WRONLY),
vsf_pyal_module_int(os, O_RDWR, O_RDWR),
Expand Down

0 comments on commit 08659fe

Please sign in to comment.