From e94f928a6881013b6eaec6f057b5a41ee364bb6d Mon Sep 17 00:00:00 2001 From: lixianjing Date: Wed, 6 Jan 2021 15:49:03 +0800 Subject: [PATCH] improve fscript_ext --- docs/changes.md | 4 + src/fscript_ext/fscript_ext.c | 18 +- src/fscript_ext/fscript_ext.h | 3 + src/fscript_ext/fscript_iostream.c | 48 ++++ src/fscript_ext/fscript_iostream.h | 35 +++ src/fscript_ext/fscript_iostream_file.c | 54 ++++ src/fscript_ext/fscript_iostream_file.h | 35 +++ src/fscript_ext/fscript_istream.c | 312 ++++++++++++++++++++++++ src/fscript_ext/fscript_istream.h | 35 +++ src/fscript_ext/fscript_ostream.c | 246 +++++++++++++++++++ src/fscript_ext/fscript_ostream.h | 35 +++ src/fscript_ext/fscript_rbuffer.h | 2 +- src/fscript_ext/fscript_wbuffer.c | 175 ++++++------- src/fscript_ext/fscript_wbuffer.h | 2 +- tests/testdata/demo_stream_file.fs | 60 +++++ tests/testdata/demo_stream_file1.fs | 57 +++++ tests/testdata/demo_wbuffer3.fs | 62 +++++ 17 files changed, 1083 insertions(+), 100 deletions(-) create mode 100644 src/fscript_ext/fscript_iostream.c create mode 100644 src/fscript_ext/fscript_iostream.h create mode 100644 src/fscript_ext/fscript_iostream_file.c create mode 100644 src/fscript_ext/fscript_iostream_file.h create mode 100644 src/fscript_ext/fscript_istream.c create mode 100644 src/fscript_ext/fscript_istream.h create mode 100644 src/fscript_ext/fscript_ostream.c create mode 100644 src/fscript_ext/fscript_ostream.h create mode 100644 tests/testdata/demo_stream_file.fs create mode 100644 tests/testdata/demo_stream_file1.fs create mode 100644 tests/testdata/demo_wbuffer3.fs diff --git a/docs/changes.md b/docs/changes.md index 3b3f75955..6dbbb85ac 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,5 +1,9 @@ # 最新动态 +2021/01/06 + * 完善fscript和fscript\_ext。 + * 将istream/ostream/iostream包装成fscript的函数。 + 2021/01/05 * 完善fscript和fscript\_ext。 * 支持修改输入候选字的个数或者候选字个数自动扩展功能(感谢智明提供补丁) diff --git a/src/fscript_ext/fscript_ext.c b/src/fscript_ext/fscript_ext.c index 92c666a8e..f898b6b76 100644 --- a/src/fscript_ext/fscript_ext.c +++ b/src/fscript_ext/fscript_ext.c @@ -25,17 +25,29 @@ #include "fscript_ext/fscript_wbuffer.h" #include "fscript_ext/fscript_typed_array.h" -static ret_t func_value_reset(fscript_t* fscript, fscript_args_t* args, value_t* result) { +#include "fscript_ext/fscript_istream.h" +#include "fscript_ext/fscript_ostream.h" +#include "fscript_ext/fscript_iostream.h" +#include "fscript_ext/fscript_iostream_file.h" + +static ret_t func_value_is_valid(fscript_t* fscript, fscript_args_t* args, value_t* result) { FSCRIPT_FUNC_CHECK(args->size == 1, RET_BAD_PARAMS); - value_reset(args->args); + value_set_bool(result, args->args->type != VALUE_TYPE_INVALID); return RET_OK; } ret_t fscript_ext_init(void) { - ENSURE(fscript_register_func("value_reset", func_value_reset) == RET_OK); + ENSURE(fscript_register_func("value_is_valid", func_value_is_valid) == RET_OK); fscript_object_register(); +#ifdef FSCRIPT_WITH_STREAM + fscript_istream_register(); + fscript_ostream_register(); + fscript_iostream_register(); + fscript_iostream_file_register(); +#endif /*FSCRIPT_WITH_STREAM*/ + #ifdef FSCRIPT_WITH_CRC fscript_crc_register(); #endif /*FSCRIPT_WITH_CRC*/ diff --git a/src/fscript_ext/fscript_ext.h b/src/fscript_ext/fscript_ext.h index 07dbabaec..4bafd44a1 100644 --- a/src/fscript_ext/fscript_ext.h +++ b/src/fscript_ext/fscript_ext.h @@ -37,6 +37,9 @@ ret_t fscript_ext_init(void); #define FSCRIPT_WITH_BUFFER 1 #define FSCRIPT_WITH_TYPED_ARRAY 1 #define WITH_FSCRIPT_EXT 1 +#define FSCRIPT_WITH_STREAM 1 +#define FSCRIPT_WITH_STREAM_FILE 1 + #endif /*PC*/ END_C_DECLS diff --git a/src/fscript_ext/fscript_iostream.c b/src/fscript_ext/fscript_iostream.c new file mode 100644 index 000000000..d4f2174b7 --- /dev/null +++ b/src/fscript_ext/fscript_iostream.c @@ -0,0 +1,48 @@ +/** + * File: fscript_iostream.c + * Author: AWTK Develop Team + * Brief: iostream functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-04 Li XianJing created + * + */ + +#include "tkc/buffer.h" +#include "tkc/fscript.h" +#include "tkc/iostream.h" + +static ret_t func_iostream_get_istream(fscript_t* fscript, fscript_args_t* args, value_t* result) { + tk_iostream_t* iostream = NULL; + FSCRIPT_FUNC_CHECK(args->size == 1, RET_BAD_PARAMS); + iostream = TK_IOSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(iostream != NULL, RET_BAD_PARAMS); + + value_set_object(result, OBJECT(tk_iostream_get_istream(iostream))); + + return RET_OK; +} + +static ret_t func_iostream_get_ostream(fscript_t* fscript, fscript_args_t* args, value_t* result) { + tk_iostream_t* iostream = NULL; + FSCRIPT_FUNC_CHECK(args->size == 1, RET_BAD_PARAMS); + iostream = TK_IOSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(iostream != NULL, RET_BAD_PARAMS); + + value_set_object(result, OBJECT(tk_iostream_get_ostream(iostream))); + + return RET_OK; +} + +ret_t fscript_iostream_register(void) { + ENSURE(fscript_register_func("iostream_get_istream", func_iostream_get_istream) == RET_OK); + ENSURE(fscript_register_func("iostream_get_ostream", func_iostream_get_ostream) == RET_OK); + + return RET_OK; +} diff --git a/src/fscript_ext/fscript_iostream.h b/src/fscript_ext/fscript_iostream.h new file mode 100644 index 000000000..c92488f2a --- /dev/null +++ b/src/fscript_ext/fscript_iostream.h @@ -0,0 +1,35 @@ +/** + * File: fscript_iostream.h + * Author: AWTK Develop Team + * Brief: iostream functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-06 Li XianJing created + * + */ + +#ifndef TK_FSCRIPT_IOSTREAM_H +#define TK_FSCRIPT_IOSTREAM_H + +#include "tkc/str.h" +#include "tkc/object.h" + +BEGIN_C_DECLS + +/** + * @method fscript_iostream_register + * 注册iostream相关函数。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t fscript_iostream_register(void); + +END_C_DECLS + +#endif /*TK_FSCRIPT_IOSTREAM_H*/ diff --git a/src/fscript_ext/fscript_iostream_file.c b/src/fscript_ext/fscript_iostream_file.c new file mode 100644 index 000000000..36f959c77 --- /dev/null +++ b/src/fscript_ext/fscript_iostream_file.c @@ -0,0 +1,54 @@ +/** + * File: fscript_iostream_file.c + * Author: AWTK Develop Team + * Brief: iostream_file functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-06 Li XianJing created + * + */ + +#include "tkc/buffer.h" +#include "tkc/fscript.h" +#include "streams/file/istream_file.h" +#include "streams/file/ostream_file.h" + +static ret_t func_istream_file_create(fscript_t* fscript, fscript_args_t* args, value_t* result) { + const char* mode = NULL; + const char* filename = NULL; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + filename = value_str(args->args); + mode = args->size > 1 ? value_str(args->args + 1) : "rb"; + return_value_if_fail(filename != NULL && mode != NULL, RET_BAD_PARAMS); + + value_set_object(result, OBJECT(tk_istream_file_create_ex(filename, mode))); + result->free_handle = TRUE; + + return RET_OK; +} + +static ret_t func_ostream_file_create(fscript_t* fscript, fscript_args_t* args, value_t* result) { + const char* mode = NULL; + const char* filename = NULL; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + filename = value_str(args->args); + mode = args->size > 1 ? value_str(args->args + 1) : "wb+"; + return_value_if_fail(filename != NULL && mode != NULL, RET_BAD_PARAMS); + + value_set_object(result, OBJECT(tk_ostream_file_create_ex(filename, mode))); + result->free_handle = TRUE; + + return RET_OK; +} + +ret_t fscript_iostream_file_register(void) { + ENSURE(fscript_register_func("istream_file_create", func_istream_file_create) == RET_OK); + ENSURE(fscript_register_func("ostream_file_create", func_ostream_file_create) == RET_OK); + return RET_OK; +} diff --git a/src/fscript_ext/fscript_iostream_file.h b/src/fscript_ext/fscript_iostream_file.h new file mode 100644 index 000000000..016a00e3e --- /dev/null +++ b/src/fscript_ext/fscript_iostream_file.h @@ -0,0 +1,35 @@ +/** + * File: fscript_iostream_file.h + * Author: AWTK Develop Team + * Brief: iostream_file functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-06 Li XianJing created + * + */ + +#ifndef TK_FSCRIPT_IOSTREAM_FILE_H +#define TK_FSCRIPT_IOSTREAM_FILE_H + +#include "tkc/str.h" +#include "tkc/object.h" + +BEGIN_C_DECLS + +/** + * @method fscript_iostream_file_register + * 注册iostream_file相关函数。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t fscript_iostream_file_register(void); + +END_C_DECLS + +#endif /*TK_FSCRIPT_IOSTREAM_FILE_H*/ diff --git a/src/fscript_ext/fscript_istream.c b/src/fscript_ext/fscript_istream.c new file mode 100644 index 000000000..91965aa89 --- /dev/null +++ b/src/fscript_ext/fscript_istream.c @@ -0,0 +1,312 @@ +/** + * File: fscript_istream.c + * Author: AWTK Develop Team + * Brief: istream functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-06 Li XianJing created + * + */ + +#include "tkc/mem.h" +#include "tkc/buffer.h" +#include "tkc/fscript.h" +#include "tkc/istream.h" + +static ret_t func_istream_seek(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t offset = 0; + tk_istream_t* istream = NULL; + FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + offset = value_uint32(args->args + 1); + + value_set_bool(result, tk_istream_seek(istream, offset) == RET_OK); + + return RET_OK; +} + +static ret_t func_istream_tell(fscript_t* fscript, fscript_args_t* args, value_t* result) { + tk_istream_t* istream = NULL; + FSCRIPT_FUNC_CHECK(args->size == 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + + value_set_uint32(result, tk_istream_tell(istream)); + + return RET_OK; +} + +static ret_t func_istream_is_eos(fscript_t* fscript, fscript_args_t* args, value_t* result) { + tk_istream_t* istream = NULL; + FSCRIPT_FUNC_CHECK(args->size == 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + + value_set_bool(result, tk_istream_eos(istream)); + + return RET_OK; +} + +static ret_t func_istream_read_uint8(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint8_t v = 0; + int32_t ret = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + timeout = args->size > 1 ? value_uint32(args->args + 1) : 0; + + ret = tk_istream_read_len(istream, &v, sizeof(v), timeout); + if (ret == sizeof(v)) { + value_set_uint8(result, v); + } + + return ret == sizeof(v) ? RET_OK : RET_FAIL; +} + +static ret_t func_istream_read_uint16(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint16_t v = 0; + int32_t ret = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + timeout = args->size > 1 ? value_uint32(args->args + 1) : 0; + + ret = tk_istream_read_len(istream, &v, sizeof(v), timeout); + if (ret == sizeof(v)) { + value_set_uint16(result, v); + } + + return ret == sizeof(v) ? RET_OK : RET_FAIL; +} + +static ret_t func_istream_read_uint32(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t v = 0; + int32_t ret = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + timeout = args->size > 1 ? value_uint32(args->args + 1) : 0; + + ret = tk_istream_read_len(istream, &v, sizeof(v), timeout); + if (ret == sizeof(v)) { + value_set_uint32(result, v); + } + + return ret == sizeof(v) ? RET_OK : RET_FAIL; +} + +static ret_t func_istream_read_uint64(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint64_t v = 0; + int32_t ret = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + timeout = args->size > 1 ? value_uint32(args->args + 1) : 0; + + ret = tk_istream_read_len(istream, &v, sizeof(v), timeout); + if (ret == sizeof(v)) { + value_set_uint64(result, v); + } + + return ret == sizeof(v) ? RET_OK : RET_FAIL; +} + +static ret_t func_istream_read_int8(fscript_t* fscript, fscript_args_t* args, value_t* result) { + ret_t ret = func_istream_read_uint8(fscript, args, result); + if (result->type != VALUE_TYPE_INVALID) { + result->type = VALUE_TYPE_INT8; + } + return ret; +} + +static ret_t func_istream_read_int16(fscript_t* fscript, fscript_args_t* args, value_t* result) { + ret_t ret = func_istream_read_uint16(fscript, args, result); + if (result->type != VALUE_TYPE_INVALID) { + result->type = VALUE_TYPE_INT16; + } + return ret; +} + +static ret_t func_istream_read_int32(fscript_t* fscript, fscript_args_t* args, value_t* result) { + ret_t ret = func_istream_read_uint32(fscript, args, result); + if (result->type != VALUE_TYPE_INVALID) { + result->type = VALUE_TYPE_INT32; + } + return ret; +} + +static ret_t func_istream_read_int64(fscript_t* fscript, fscript_args_t* args, value_t* result) { + ret_t ret = func_istream_read_uint64(fscript, args, result); + if (result->type != VALUE_TYPE_INVALID) { + result->type = VALUE_TYPE_INT64; + } + return ret; +} + +static ret_t func_istream_read_float(fscript_t* fscript, fscript_args_t* args, value_t* result) { + float v = 0; + int32_t ret = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + timeout = args->size > 1 ? value_uint32(args->args + 1) : 0; + + ret = tk_istream_read_len(istream, &v, sizeof(v), timeout); + if (ret == sizeof(v)) { + value_set_float(result, v); + } + + return ret == sizeof(v) ? RET_OK : RET_FAIL; +} + +static ret_t func_istream_read_double(fscript_t* fscript, fscript_args_t* args, value_t* result) { + double v = 0; + int32_t ret = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + timeout = args->size > 1 ? value_uint32(args->args + 1) : 0; + + ret = tk_istream_read_len(istream, &v, sizeof(v), timeout); + if (ret == sizeof(v)) { + value_set_double(result, v); + } + + return ret == sizeof(v) ? RET_OK : RET_FAIL; +} + +static ret_t func_istream_read_string(fscript_t* fscript, fscript_args_t* args, value_t* result) { + char* v = 0; + int32_t ret = 0; + uint32_t size = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + size = args->size > 1 ? value_uint32(args->args + 1) : 0; + timeout = args->size > 2 ? value_uint32(args->args + 2) : 0; + FSCRIPT_FUNC_CHECK(size > 0, RET_BAD_PARAMS); + v = TKMEM_ALLOC(size + 1); + FSCRIPT_FUNC_CHECK(v != NULL, RET_BAD_PARAMS); + + ret = tk_istream_read_len(istream, v, size, timeout); + if (ret >= 0) { + v[ret] = '\0'; + value_set_str(result, v); + result->free_handle = TRUE; + } else { + TKMEM_FREE(v); + } + + return ret >= 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_istream_read_binary(fscript_t* fscript, fscript_args_t* args, value_t* result) { + char* v = 0; + int32_t ret = 0; + uint32_t size = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + size = args->size > 1 ? value_uint32(args->args + 1) : 0; + timeout = args->size > 2 ? value_uint32(args->args + 2) : 0; + FSCRIPT_FUNC_CHECK(size > 0, RET_BAD_PARAMS); + v = TKMEM_ALLOC(size + 1); + FSCRIPT_FUNC_CHECK(v != NULL, RET_BAD_PARAMS); + + ret = tk_istream_read_len(istream, v, size, timeout); + if (ret >= 0) { + v[ret] = '\0'; + value_set_binary_data(result, v, ret); + result->free_handle = TRUE; + } else { + TKMEM_FREE(v); + } + + return ret >= 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_istream_read_line(fscript_t* fscript, fscript_args_t* args, value_t* result) { + char* v = 0; + int32_t ret = 0; + uint32_t size = 0; + uint32_t timeout = 0; + tk_istream_t* istream = NULL; + + result->type = VALUE_TYPE_INVALID; + FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS); + istream = TK_ISTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(istream != NULL && istream != NULL, RET_BAD_PARAMS); + size = args->size > 1 ? value_uint32(args->args + 1) : 0; + timeout = args->size > 2 ? value_uint32(args->args + 2) : 0; + FSCRIPT_FUNC_CHECK(size > 0, RET_BAD_PARAMS); + v = TKMEM_ALLOC(size + 1); + FSCRIPT_FUNC_CHECK(v != NULL, RET_BAD_PARAMS); + + ret = tk_istream_read_line(istream, v, size, timeout); + if (ret >= 0) { + v[ret] = '\0'; + value_set_str(result, v); + result->free_handle = TRUE; + } else { + TKMEM_FREE(v); + } + + return ret >= 0 ? RET_OK : RET_FAIL; +} + +ret_t fscript_istream_register(void) { + ENSURE(fscript_register_func("istream_seek", func_istream_seek) == RET_OK); + ENSURE(fscript_register_func("istream_tell", func_istream_tell) == RET_OK); + + ENSURE(fscript_register_func("istream_read_uint8", func_istream_read_uint8) == RET_OK); + ENSURE(fscript_register_func("istream_read_uint16", func_istream_read_uint16) == RET_OK); + ENSURE(fscript_register_func("istream_read_uint32", func_istream_read_uint32) == RET_OK); + ENSURE(fscript_register_func("istream_read_uint64", func_istream_read_uint64) == RET_OK); + ENSURE(fscript_register_func("istream_read_int8", func_istream_read_int8) == RET_OK); + ENSURE(fscript_register_func("istream_read_int16", func_istream_read_int16) == RET_OK); + ENSURE(fscript_register_func("istream_read_int32", func_istream_read_int32) == RET_OK); + ENSURE(fscript_register_func("istream_read_int64", func_istream_read_int64) == RET_OK); + + ENSURE(fscript_register_func("istream_read_float", func_istream_read_float) == RET_OK); + ENSURE(fscript_register_func("istream_read_double", func_istream_read_double) == RET_OK); + ENSURE(fscript_register_func("istream_read_string", func_istream_read_string) == RET_OK); + ENSURE(fscript_register_func("istream_read_binary", func_istream_read_binary) == RET_OK); + ENSURE(fscript_register_func("istream_read_line", func_istream_read_line) == RET_OK); + + ENSURE(fscript_register_func("istream_is_eos", func_istream_is_eos) == RET_OK); + + return RET_OK; +} diff --git a/src/fscript_ext/fscript_istream.h b/src/fscript_ext/fscript_istream.h new file mode 100644 index 000000000..829c11b5f --- /dev/null +++ b/src/fscript_ext/fscript_istream.h @@ -0,0 +1,35 @@ +/** + * File: fscript_istream.h + * Author: AWTK Develop Team + * Brief: wrap istream for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-06 Li XianJing created + * + */ + +#ifndef TK_FSCRIPT_ISTREAM_H +#define TK_FSCRIPT_ISTREAM_H + +#include "tkc/str.h" +#include "tkc/object.h" + +BEGIN_C_DECLS + +/** + * @method fscript_istream_register + * 注册istream相关等函数。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t fscript_istream_register(void); + +END_C_DECLS + +#endif /*TK_FSCRIPT_ISTREAM_H*/ diff --git a/src/fscript_ext/fscript_ostream.c b/src/fscript_ext/fscript_ostream.c new file mode 100644 index 000000000..5a44be097 --- /dev/null +++ b/src/fscript_ext/fscript_ostream.c @@ -0,0 +1,246 @@ +/** + * File: fscript_ostream.c + * Author: AWTK Develop Team + * Brief: ostream functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-04 Li XianJing created + * + */ + +#include "tkc/buffer.h" +#include "tkc/fscript.h" +#include "tkc/ostream.h" + +static int32_t my_ostream_write(tk_ostream_t* ostream, const void* data, uint32_t size) { + return tk_ostream_write_len(ostream, data, size, 1000); +} + +static ret_t func_ostream_seek(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t offset = 0; + tk_ostream_t* ostream = NULL; + FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + offset = value_uint32(args->args + 1); + + value_set_bool(result, tk_ostream_seek(ostream, offset) == RET_OK); + + return RET_OK; +} + +static ret_t func_ostream_tell(fscript_t* fscript, fscript_args_t* args, value_t* result) { + tk_ostream_t* ostream = NULL; + FSCRIPT_FUNC_CHECK(args->size == 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + value_set_uint32(result, tk_ostream_tell(ostream)); + + return RET_OK; +} + +static ret_t func_ostream_flush(fscript_t* fscript, fscript_args_t* args, value_t* result) { + tk_ostream_t* ostream = NULL; + FSCRIPT_FUNC_CHECK(args->size == 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + value_set_bool(result, tk_ostream_flush(ostream) == RET_OK); + + return RET_OK; +} + +static ret_t func_ostream_write_uint8(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t i = 0; + int32_t ret = 0; + tk_ostream_t* ostream = NULL; + value_set_uint32(result, 0); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + uint8_t v = value_uint8(iter); + ret += my_ostream_write(ostream, &v, sizeof(v)); + } + value_set_int32(result, ret); + + return ret > 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_ostream_write_uint16(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t i = 0; + int32_t ret = 0; + tk_ostream_t* ostream = NULL; + value_set_uint32(result, 0); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + uint16_t v = value_uint16(iter); + ret += my_ostream_write(ostream, &v, sizeof(v)); + } + value_set_int32(result, ret); + + return ret > 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_ostream_write_uint32(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t i = 0; + int32_t ret = 0; + tk_ostream_t* ostream = NULL; + value_set_uint32(result, 0); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + uint32_t v = value_uint32(iter); + ret += my_ostream_write(ostream, &v, sizeof(v)); + } + value_set_int32(result, ret); + + return ret > 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_ostream_write_uint64(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t i = 0; + int32_t ret = 0; + tk_ostream_t* ostream = NULL; + value_set_uint32(result, 0); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + uint64_t v = value_uint64(iter); + ret += my_ostream_write(ostream, &v, sizeof(v)); + } + value_set_int32(result, ret); + + return ret > 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_ostream_write_float(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t i = 0; + int32_t ret = 0; + tk_ostream_t* ostream = NULL; + value_set_uint32(result, 0); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + float v = value_float(iter); + ret += my_ostream_write(ostream, &v, sizeof(v)); + } + value_set_int32(result, ret); + + return ret > 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_ostream_write_double(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t i = 0; + int32_t ret = 0; + tk_ostream_t* ostream = NULL; + value_set_uint32(result, 0); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + double v = value_double(iter); + ret += my_ostream_write(ostream, &v, sizeof(v)); + } + value_set_int32(result, ret); + + return ret > 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_ostream_write_string(fscript_t* fscript, fscript_args_t* args, value_t* result) { + uint32_t i = 0; + int32_t ret = 0; + tk_ostream_t* ostream = NULL; + value_set_uint32(result, 0); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + const char* v = value_str(iter); + if (v != NULL) { + ret += my_ostream_write(ostream, v, strlen(v)); + } + } + + value_set_int32(result, ret); + + return ret > 0 ? RET_OK : RET_FAIL; +} + +static ret_t func_ostream_write_binary(fscript_t* fscript, fscript_args_t* args, value_t* result) { + int32_t ret = 0; + uint32_t size = 0; + value_t* v = NULL; + const uint8_t* data = NULL; + tk_ostream_t* ostream = NULL; + value_set_uint32(result, 0); + FSCRIPT_FUNC_CHECK(args->size == 2 || args->size == 3, RET_BAD_PARAMS); + ostream = TK_OSTREAM(value_object(args->args)); + FSCRIPT_FUNC_CHECK(ostream != NULL, RET_BAD_PARAMS); + + v = args->args + 1; + if (v->type == VALUE_TYPE_BINARY) { + binary_data_t* bin = value_binary_data(v); + if (bin != NULL) { + data = bin->data; + size = bin->size; + } + } else { + data = value_pointer(v); + size = value_uint32(args->args + 2); + } + return_value_if_fail(data != NULL && size > 0, RET_BAD_PARAMS); + + ret = my_ostream_write(ostream, data, size); + value_set_uint32(result, ret); + + return ret > 0 ? RET_OK : RET_FAIL; +} + +ret_t fscript_ostream_register(void) { + ENSURE(fscript_register_func("ostream_seek", func_ostream_seek) == RET_OK); + ENSURE(fscript_register_func("ostream_tell", func_ostream_tell) == RET_OK); + ENSURE(fscript_register_func("ostream_flush", func_ostream_flush) == RET_OK); + + ENSURE(fscript_register_func("ostream_write_uint8", func_ostream_write_uint8) == RET_OK); + ENSURE(fscript_register_func("ostream_write_uint16", func_ostream_write_uint16) == RET_OK); + ENSURE(fscript_register_func("ostream_write_uint32", func_ostream_write_uint32) == RET_OK); + ENSURE(fscript_register_func("ostream_write_uint64", func_ostream_write_uint64) == RET_OK); + ENSURE(fscript_register_func("ostream_write_int8", func_ostream_write_uint8) == RET_OK); + ENSURE(fscript_register_func("ostream_write_int16", func_ostream_write_uint16) == RET_OK); + ENSURE(fscript_register_func("ostream_write_int32", func_ostream_write_uint32) == RET_OK); + ENSURE(fscript_register_func("ostream_write_int64", func_ostream_write_uint64) == RET_OK); + + ENSURE(fscript_register_func("ostream_write_float", func_ostream_write_float) == RET_OK); + ENSURE(fscript_register_func("ostream_write_double", func_ostream_write_double) == RET_OK); + ENSURE(fscript_register_func("ostream_write_string", func_ostream_write_string) == RET_OK); + ENSURE(fscript_register_func("ostream_write_binary", func_ostream_write_binary) == RET_OK); + + return RET_OK; +} diff --git a/src/fscript_ext/fscript_ostream.h b/src/fscript_ext/fscript_ostream.h new file mode 100644 index 000000000..b095c981d --- /dev/null +++ b/src/fscript_ext/fscript_ostream.h @@ -0,0 +1,35 @@ +/** + * File: fscript_ostream.h + * Author: AWTK Develop Team + * Brief: ostream functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-06 Li XianJing created + * + */ + +#ifndef TK_FSCRIPT_OSTREAM_H +#define TK_FSCRIPT_OSTREAM_H + +#include "tkc/str.h" +#include "tkc/object.h" + +BEGIN_C_DECLS + +/** + * @method fscript_ostream_register + * 注册ostream相关函数。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t fscript_ostream_register(void); + +END_C_DECLS + +#endif /*TK_FSCRIPT_OSTREAM_H*/ diff --git a/src/fscript_ext/fscript_rbuffer.h b/src/fscript_ext/fscript_rbuffer.h index 4fcd09094..d8ea734bd 100644 --- a/src/fscript_ext/fscript_rbuffer.h +++ b/src/fscript_ext/fscript_rbuffer.h @@ -24,7 +24,7 @@ BEGIN_C_DECLS /** * @method fscript_rbuffer_register - * 注册rbuffer16/rbuffer32/cksum等函数。 + * 注册rbuffer相关函数。 * * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 */ diff --git a/src/fscript_ext/fscript_wbuffer.c b/src/fscript_ext/fscript_wbuffer.c index 066cdab01..e4a6ea551 100644 --- a/src/fscript_ext/fscript_wbuffer.c +++ b/src/fscript_ext/fscript_wbuffer.c @@ -40,64 +40,6 @@ static ret_t func_wbuffer_attach(fscript_t* fscript, fscript_args_t* args, value return RET_OK; } -static ret_t func_wbuffer_write(fscript_t* fscript, fscript_args_t* args, value_t* result) { - uint32_t i = 0; - object_wbuffer_t* obj = NULL; - FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); - obj = OBJECT_WBUFFER(value_object(args->args)); - FSCRIPT_FUNC_CHECK(obj != NULL && obj->wbuffer != NULL, RET_BAD_PARAMS); - - for (i = 1; i < args->size; i++) { - value_t* iter = args->args + i; - switch (iter->type) { - case VALUE_TYPE_INT8: - case VALUE_TYPE_UINT8: { - wbuffer_write_uint8(obj->wbuffer, value_uint8(iter)); - break; - } - case VALUE_TYPE_INT16: - case VALUE_TYPE_UINT16: { - wbuffer_write_uint16(obj->wbuffer, value_uint16(iter)); - break; - } - case VALUE_TYPE_INT32: - case VALUE_TYPE_UINT32: { - wbuffer_write_uint32(obj->wbuffer, value_uint32(iter)); - break; - } - case VALUE_TYPE_INT64: - case VALUE_TYPE_UINT64: { - wbuffer_write_uint64(obj->wbuffer, value_uint64(iter)); - break; - } - case VALUE_TYPE_FLOAT: - case VALUE_TYPE_FLOAT32: { - wbuffer_write_float(obj->wbuffer, value_float32(iter)); - break; - } - case VALUE_TYPE_DOUBLE: { - wbuffer_write_double(obj->wbuffer, value_double(iter)); - break; - } - case VALUE_TYPE_STRING: { - wbuffer_write_string(obj->wbuffer, value_str(iter)); - break; - } - case VALUE_TYPE_BINARY: { - binary_data_t* data = value_binary_data(iter); - FSCRIPT_FUNC_CHECK(data != NULL, RET_BAD_PARAMS); - wbuffer_write_binary(obj->wbuffer, data->data, data->size); - break; - } - default: { - FSCRIPT_FUNC_CHECK(FALSE, RET_BAD_PARAMS); - } - } - } - - return RET_OK; -} - static ret_t func_wbuffer_skip(fscript_t* fscript, fscript_args_t* args, value_t* result) { object_wbuffer_t* obj = NULL; FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); @@ -155,94 +97,134 @@ static ret_t func_wbuffer_get_capacity(fscript_t* fscript, fscript_args_t* args, } static ret_t func_wbuffer_write_uint8(fscript_t* fscript, fscript_args_t* args, value_t* result) { - ret_t ret = RET_OK; + uint32_t i = 0; object_wbuffer_t* obj = NULL; - FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); obj = OBJECT_WBUFFER(value_object(args->args)); FSCRIPT_FUNC_CHECK(obj != NULL && obj->wbuffer != NULL, RET_BAD_PARAMS); - ret = wbuffer_write_uint8(obj->wbuffer, value_uint8(args->args + 1)); - value_set_bool(result, ret == RET_OK); + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + if (wbuffer_write_uint8(obj->wbuffer, value_uint8(iter)) != RET_OK) { + break; + } + } + value_set_int32(result, (i - 1) * sizeof(uint8_t)); - return ret; + return RET_OK; } static ret_t func_wbuffer_write_uint16(fscript_t* fscript, fscript_args_t* args, value_t* result) { - ret_t ret = RET_OK; + uint32_t i = 0; object_wbuffer_t* obj = NULL; - FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); obj = OBJECT_WBUFFER(value_object(args->args)); FSCRIPT_FUNC_CHECK(obj != NULL && obj->wbuffer != NULL, RET_BAD_PARAMS); - ret = wbuffer_write_uint16(obj->wbuffer, value_uint16(args->args + 1)); - value_set_bool(result, ret == RET_OK); + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + if (wbuffer_write_uint16(obj->wbuffer, value_uint16(iter)) != RET_OK) { + break; + } + } + value_set_int32(result, (i - 1) * sizeof(uint16_t)); - return ret; + return RET_OK; } static ret_t func_wbuffer_write_uint32(fscript_t* fscript, fscript_args_t* args, value_t* result) { - ret_t ret = RET_OK; + uint32_t i = 0; object_wbuffer_t* obj = NULL; - FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); obj = OBJECT_WBUFFER(value_object(args->args)); FSCRIPT_FUNC_CHECK(obj != NULL && obj->wbuffer != NULL, RET_BAD_PARAMS); - ret = wbuffer_write_uint32(obj->wbuffer, value_uint32(args->args + 1)); - value_set_bool(result, ret == RET_OK); + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + if (wbuffer_write_uint32(obj->wbuffer, value_uint32(iter)) != RET_OK) { + break; + } + } + value_set_int32(result, (i - 1) * sizeof(uint32_t)); - return ret; + return RET_OK; } static ret_t func_wbuffer_write_uint64(fscript_t* fscript, fscript_args_t* args, value_t* result) { - ret_t ret = RET_OK; + uint32_t i = 0; object_wbuffer_t* obj = NULL; - FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); obj = OBJECT_WBUFFER(value_object(args->args)); FSCRIPT_FUNC_CHECK(obj != NULL && obj->wbuffer != NULL, RET_BAD_PARAMS); - ret = wbuffer_write_uint64(obj->wbuffer, value_uint64(args->args + 1)); - value_set_bool(result, ret == RET_OK); + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + if (wbuffer_write_uint64(obj->wbuffer, value_uint64(iter)) != RET_OK) { + break; + } + } + value_set_int32(result, (i - 1) * sizeof(uint64_t)); - return ret; + return RET_OK; } static ret_t func_wbuffer_write_float(fscript_t* fscript, fscript_args_t* args, value_t* result) { - ret_t ret = RET_OK; + uint32_t i = 0; object_wbuffer_t* obj = NULL; - FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); obj = OBJECT_WBUFFER(value_object(args->args)); FSCRIPT_FUNC_CHECK(obj != NULL && obj->wbuffer != NULL, RET_BAD_PARAMS); - ret = wbuffer_write_float(obj->wbuffer, value_float32(args->args + 1)); - value_set_bool(result, ret == RET_OK); + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + if (wbuffer_write_float(obj->wbuffer, value_float32(iter)) != RET_OK) { + break; + } + } + value_set_int32(result, (i - 1) * sizeof(float)); - return ret; + return RET_OK; } static ret_t func_wbuffer_write_double(fscript_t* fscript, fscript_args_t* args, value_t* result) { - ret_t ret = RET_OK; + uint32_t i = 0; object_wbuffer_t* obj = NULL; - FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); obj = OBJECT_WBUFFER(value_object(args->args)); FSCRIPT_FUNC_CHECK(obj != NULL && obj->wbuffer != NULL, RET_BAD_PARAMS); - ret = wbuffer_write_double(obj->wbuffer, value_double(args->args + 1)); - value_set_bool(result, ret == RET_OK); + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + if (wbuffer_write_double(obj->wbuffer, value_double(iter)) != RET_OK) { + break; + } + } + value_set_int32(result, (i - 1) * sizeof(double)); - return ret; + return RET_OK; } static ret_t func_wbuffer_write_string(fscript_t* fscript, fscript_args_t* args, value_t* result) { - ret_t ret = RET_OK; + uint32_t i = 0; + uint32_t size = 0; object_wbuffer_t* obj = NULL; - FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); + FSCRIPT_FUNC_CHECK(args->size > 1, RET_BAD_PARAMS); obj = OBJECT_WBUFFER(value_object(args->args)); FSCRIPT_FUNC_CHECK(obj != NULL && obj->wbuffer != NULL, RET_BAD_PARAMS); - ret = wbuffer_write_string(obj->wbuffer, value_str(args->args + 1)); - value_set_bool(result, ret == RET_OK); + for (i = 1; i < args->size; i++) { + value_t* iter = args->args + i; + const char* str = value_str(iter); + if (str != NULL) { + if (wbuffer_write_string(obj->wbuffer, str) != RET_OK) { + break; + } + size += strlen(str) + 1; + } + } + value_set_int32(result, size); - return ret; + return RET_OK; } static ret_t func_wbuffer_write_binary(fscript_t* fscript, fscript_args_t* args, value_t* result) { @@ -267,7 +249,11 @@ static ret_t func_wbuffer_write_binary(fscript_t* fscript, fscript_args_t* args, FSCRIPT_FUNC_CHECK(data != NULL && size > 0, RET_BAD_PARAMS); } ret = wbuffer_write_binary(obj->wbuffer, data, size); - value_set_bool(result, ret == RET_OK); + if (ret == RET_OK) { + value_set_uint32(result, size); + } else { + value_set_uint32(result, 0); + } return ret; } @@ -275,7 +261,6 @@ static ret_t func_wbuffer_write_binary(fscript_t* fscript, fscript_args_t* args, ret_t fscript_wbuffer_register(void) { ENSURE(fscript_register_func("wbuffer_create", func_wbuffer_create) == RET_OK); ENSURE(fscript_register_func("wbuffer_attach", func_wbuffer_attach) == RET_OK); - ENSURE(fscript_register_func("wbuffer_write", func_wbuffer_write) == RET_OK); ENSURE(fscript_register_func("wbuffer_skip", func_wbuffer_skip) == RET_OK); ENSURE(fscript_register_func("wbuffer_rewind", func_wbuffer_rewind) == RET_OK); diff --git a/src/fscript_ext/fscript_wbuffer.h b/src/fscript_ext/fscript_wbuffer.h index 89a4bda47..78c827ca4 100644 --- a/src/fscript_ext/fscript_wbuffer.h +++ b/src/fscript_ext/fscript_wbuffer.h @@ -24,7 +24,7 @@ BEGIN_C_DECLS /** * @method fscript_wbuffer_register - * 注册wbuffer16/wbuffer32/cksum等函数。 + * 注册wbuffer相关函数。 * * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 */ diff --git a/tests/testdata/demo_stream_file.fs b/tests/testdata/demo_stream_file.fs new file mode 100644 index 000000000..f5103c44d --- /dev/null +++ b/tests/testdata/demo_stream_file.fs @@ -0,0 +1,60 @@ +a = ostream_file_create("test.bin", "wb+") +assert(ostream_write_uint8(a, 1) == 1) +assert(ostream_write_int8(a, -1) == 1) +assert(ostream_tell(a), 2) + +assert(ostream_write_uint16(a, 2) == 2) +assert(ostream_write_int16(a, -2) == 2) +assert(ostream_tell(a), 6) + +assert(ostream_write_uint32(a, 3) == 4) +assert(ostream_write_int32(a, -3) == 4) +assert(ostream_tell(a), 14) + +assert(ostream_write_uint64(a, 4) == 8) +assert(ostream_write_int64(a, -4) == 8) +assert(ostream_tell(a), 30) + +assert(ostream_write_float(a, 5) == 4) +assert(ostream_write_double(a, -5) == 8) +assert(ostream_tell(a), 42) + +assert(ostream_write_string(a, "hello") == 5) +assert(ostream_write_binary(a, "world", 5) == 5) +assert(ostream_tell(a), 52) + +assert(ostream_flush(a)) + +a = istream_file_create("test.bin", "rb") +assert(istream_read_uint8(a)==1) +assert(istream_read_int8(a)==-1) +assert(istream_tell(a), 2) + +assert(istream_read_uint16(a)==2) +assert(istream_read_int16(a)==-2) +assert(istream_tell(a), 6) + +assert(istream_read_uint32(a)==3) +assert(istream_read_int32(a)==-3) +assert(istream_tell(a), 14) + +assert(istream_read_uint64(a)==4) +assert(istream_read_int64(a)==-4) +assert(istream_tell(a), 30) + +assert(istream_read_float(a)==5) +assert(istream_read_double(a)==-5) +assert(istream_tell(a), 42) + +assert(istream_read_string(a, 5)=="hello") +assert(istream_tell(a), 47) + +istream_read_binary(a, 5) +assert(istream_tell(a), 52) +assert(istream_seek(a, 0)) +assert(istream_read_uint8(a)==1) +assert(istream_read_int8(a)==-1) +assert(istream_tell(a) == 2) + +unset(a) + diff --git a/tests/testdata/demo_stream_file1.fs b/tests/testdata/demo_stream_file1.fs new file mode 100644 index 000000000..a8723566b --- /dev/null +++ b/tests/testdata/demo_stream_file1.fs @@ -0,0 +1,57 @@ +b = 0 +a = ostream_file_create("test.bin", "wb+") + +assert(ostream_write_uint8(a, 1, 2, 3, 4) == 4) +assert(ostream_write_int8(a, -1, -2, -3, -4) == 4) +assert(ostream_tell(a), 8) + +assert(ostream_write_uint16(a, 1, 2) == 4) +assert(ostream_write_int16(a, -1, -2) == 4) +assert(ostream_tell(a), 16) + +assert(ostream_write_uint32(a, 1, 2) == 8) +assert(ostream_write_int32(a, -1, -2) == 8) +assert(ostream_tell(a), 32) + +assert(ostream_write_uint64(a, 1, 2) == 16) +assert(ostream_write_int64(a, -1, -2) == 16) +assert(ostream_tell(a), 64) + +assert(ostream_write_float(a, 1, 2) == 8) +assert(ostream_tell(a), 72) + +assert(ostream_write_double(a, 1, 2) == 16) +assert(ostream_tell(a), 88) + +a = istream_file_create("test.bin", "rb") +assert(istream_read_uint8(a)==1) +assert(istream_read_uint8(a)==2) +assert(istream_read_uint8(a)==3) +assert(istream_read_uint8(a)==4) +assert(istream_read_int8(a)==-1) +assert(istream_read_int8(a)==-2) +assert(istream_read_int8(a)==-3) +assert(istream_read_int8(a)==-4) + +assert(istream_read_uint16(a)==1) +assert(istream_read_uint16(a)==2) +assert(istream_read_int16(a)==-1) +assert(istream_read_int16(a)==-2) + +assert(istream_read_uint32(a)==1) +assert(istream_read_uint32(a)==2) +assert(istream_read_int32(a)==-1) +assert(istream_read_int32(a)==-2) + +assert(istream_read_uint64(a)==1) +assert(istream_read_uint64(a)==2) +assert(istream_read_int64(a)==-1) +assert(istream_read_int64(a)==-2) + +assert(istream_read_float(a) == 1) +assert(istream_read_float(a) == 2) +assert(istream_read_double(a) == 1) +assert(istream_read_double(a) == 2) + +unset(a) + diff --git a/tests/testdata/demo_wbuffer3.fs b/tests/testdata/demo_wbuffer3.fs new file mode 100644 index 000000000..3905c7508 --- /dev/null +++ b/tests/testdata/demo_wbuffer3.fs @@ -0,0 +1,62 @@ + +a=wbuffer_create() + +assert(wbuffer_write_int8(a, -10, -20) == 2); +assert(wbuffer_write_uint8(a, 10, 20) == 2); +assert(wbuffer_get_cursor(a)==4) + +assert(wbuffer_write_int16(a, -10, -20) == 4); +assert(wbuffer_write_uint16(a, 10, 20) == 4); +assert(wbuffer_get_cursor(a)==12) + +assert(wbuffer_write_int32(a, -10, -20) == 8); +assert(wbuffer_write_uint32(a, 10, 20) == 8); +assert(wbuffer_get_cursor(a)==28) + +assert(wbuffer_write_int64(a, -10, -20) == 16); +assert(wbuffer_write_uint64(a, 10, 20) == 16); +assert(wbuffer_get_cursor(a)==60) + +assert(wbuffer_write_float(a, -10, -20) == 8); +assert(wbuffer_write_double(a, 10, 20) == 16); +assert(wbuffer_get_cursor(a)==84) + +assert(wbuffer_write_string(a, "ab", "cd") == 6); +assert(wbuffer_write_binary(a, "1234", 4) == 4); +assert(wbuffer_get_cursor(a)==94) + +b=rbuffer_create(wbuffer_get_data(a), wbuffer_get_cursor(a)) + +assert(rbuffer_get_cursor(b)==0) +assert(rbuffer_read_int8(b) == -10) +assert(rbuffer_read_int8(b) == -20) +assert(rbuffer_read_uint8(b) == 10) +assert(rbuffer_read_uint8(b) == 20) + +assert(rbuffer_read_int16(b) == -10) +assert(rbuffer_read_int16(b) == -20) +assert(rbuffer_read_uint16(b) == 10) +assert(rbuffer_read_uint16(b) == 20) + +assert(rbuffer_read_int32(b) == -10) +assert(rbuffer_read_int32(b) == -20) +assert(rbuffer_read_uint32(b) == 10) +assert(rbuffer_read_uint32(b) == 20) + +assert(rbuffer_read_int64(b) == -10) +assert(rbuffer_read_int64(b) == -20) +assert(rbuffer_read_uint64(b) == 10) +assert(rbuffer_read_uint64(b) == 20) + +assert(rbuffer_read_float(b) == -10) +assert(rbuffer_read_float(b) == -20) +assert(rbuffer_read_double(b) == 10) +assert(rbuffer_read_double(b) == 20) + +assert(rbuffer_read_string(b) == "ab") +assert(rbuffer_read_string(b) == "cd") + +unset(a) +unset(b) + +