diff --git a/.gitignore b/.gitignore index 4914d5327..f64f8090e 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ log __pycache__ test.bin release +event_log.bin diff --git a/docs/changes.md b/docs/changes.md index 038499076..89b710f1d 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -3,6 +3,7 @@ * 2020/06/13 * 完善资源生成脚本,兼容 AWTK 和 Designer(感谢朝泽提供补丁)。 * 完善 str\_count\_sub\_str(感谢朝泽提供补丁)。 + * 增加 data\_reader\_asset。 * 2020/06/12 * 增加 app\_conf\_set\_wstr/app\_conf\_get\_wstr diff --git a/src/awtk_global.c b/src/awtk_global.c index a11783b73..c2dc26f48 100644 --- a/src/awtk_global.c +++ b/src/awtk_global.c @@ -33,8 +33,14 @@ #include "base/window_manager.h" #include "base/widget_factory.h" #include "base/assets_manager.h" + +#ifdef WITH_DATA_READER_WRITER #include "tkc/data_reader_factory.h" #include "tkc/data_writer_factory.h" +#include "tkc/data_writer_file.h" +#include "tkc/data_reader_file.h" +#include "base/data_reader_asset.h" +#endif/*WITH_DATA_READER_WRITER*/ #include "base/widget_animator_manager.h" #include "font_loader/font_loader_bitmap.h" @@ -141,6 +147,7 @@ ret_t tk_init_internal(void) { data_reader_factory_set(data_reader_factory_create()); data_writer_factory_register(data_writer_factory(), "file", data_writer_file_create); data_reader_factory_register(data_reader_factory(), "file", data_reader_file_create); + data_reader_factory_register(data_reader_factory(), "asset", data_reader_asset_create); #endif /*WITH_DATA_READER_WRITER*/ #ifdef WITH_STB_IMAGE diff --git a/src/base/data_reader_asset.c b/src/base/data_reader_asset.c new file mode 100644 index 000000000..6616b7cc6 --- /dev/null +++ b/src/base/data_reader_asset.c @@ -0,0 +1,86 @@ +/** + * File: data_reader.c + * Author: AWTK Develop Team + * Brief: data_reader asset + * + * Copyright (c) 2020 - 2020 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License asset for more details. + * + */ + +/** + * History: + * ================================================================ + * 2020-06-13 Li XianJing created + * + */ + +#include "tkc/mem.h" +#include "tkc/asset_info.h" +#include "base/assets_manager.h" +#include "base/data_reader_asset.h" + +typedef struct _data_reader_asset_t { + data_reader_t data_reader; + + const asset_info_t* info; +} data_reader_asset_t; + +static int32_t data_reader_asset_read(data_reader_t* reader, uint64_t offset, void* data, + uint32_t size) { + const void* src = NULL; + uint32_t max_size = 0; + data_reader_asset_t* asset = (data_reader_asset_t*)reader; + return_value_if_fail(asset != NULL && asset->info != NULL, 0); + return_value_if_fail(offset < asset->info->size, 0); + + src = asset->info->data + offset; + max_size = asset->info->size - offset; + size = tk_min(size, max_size); + + memcpy(data, src, size); + + return size; +} + +static uint64_t data_reader_asset_get_size(data_reader_t* reader) { + data_reader_asset_t* asset = (data_reader_asset_t*)reader; + return_value_if_fail(asset != NULL && asset->info != NULL, 0); + + return asset->info->size; +} + +static ret_t data_reader_asset_destroy(data_reader_t* reader) { + data_reader_asset_t* asset = (data_reader_asset_t*)reader; + + assets_manager_unref(assets_manager(), (asset_info_t*)(asset->info)); + TKMEM_FREE(asset); + + return RET_OK; +} + +static data_reader_vtable_t s_data_reader_asset_vtable = { + .read = data_reader_asset_read, + .get_size = data_reader_asset_get_size, + .destroy = data_reader_asset_destroy, +}; + +data_reader_t* data_reader_asset_create(const char* assetname) { + data_reader_asset_t* asset = NULL; + return_value_if_fail(assetname != NULL, NULL); + asset = TKMEM_ZALLOC(data_reader_asset_t); + return_value_if_fail(asset != NULL, NULL); + + asset->data_reader.vt = &s_data_reader_asset_vtable; + asset->info = assets_manager_ref(assets_manager(), ASSET_TYPE_DATA, assetname); + + if (asset->info == NULL) { + TKMEM_FREE(asset); + } + + return (data_reader_t*)asset; +} diff --git a/src/base/data_reader_asset.h b/src/base/data_reader_asset.h new file mode 100644 index 000000000..78b0d5ac1 --- /dev/null +++ b/src/base/data_reader_asset.h @@ -0,0 +1,42 @@ +/** + * File: data_reader_asset.h + * Author: AWTK Develop Team + * Brief: data_reader asset + * + * Copyright (c) 2020 - 2020 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License asset for more details. + * + */ + +/** + * History: + * ================================================================ + * 2020-06-13 Li XianJing created + * + */ + +#ifndef TK_DATA_READER_ASSET_H +#define TK_DATA_READER_ASSET_H + +#include "tkc/data_reader.h" + +BEGIN_C_DECLS + +/** + * @method data_reader_asset_create + * 创建基于asset的data reader。 + * @param {const char*} assetname 资源的名称。 + * + * @return {data_reader_t*} 返回data reader对象。 + */ +data_reader_t* data_reader_asset_create(const char* assetname); + +#define DATA_READER_ASSET(reader) ((data_reader_t*)(reader)) + +END_C_DECLS + +#endif /*TK_DATA_READER_ASSET_H*/ diff --git a/src/tkc/data_reader_file.h b/src/tkc/data_reader_file.h index 582691a53..980e973f5 100644 --- a/src/tkc/data_reader_file.h +++ b/src/tkc/data_reader_file.h @@ -29,9 +29,9 @@ BEGIN_C_DECLS /** * @method data_reader_file_create * 创建基于文件的data reader。 - * @param {data_reader_t*} reader reader对象。 + * @param {const char*} filename 文件名。 * - * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + * @return {data_reader_t*} 返回data reader对象。 */ data_reader_t* data_reader_file_create(const char* filename); diff --git a/src/tkc/data_writer_file.h b/src/tkc/data_writer_file.h index 608fe2249..a0e3da68a 100644 --- a/src/tkc/data_writer_file.h +++ b/src/tkc/data_writer_file.h @@ -29,9 +29,9 @@ BEGIN_C_DECLS /** * @method data_writer_file_create * 创建基于文件的data writer。 - * @param {data_writer_t*} writer writer对象。 + * @param {const char*} filename 文件名。 * - * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + * @return {data_writer_t*} 返回data writer对象。 */ data_writer_t* data_writer_file_create(const char* filename); diff --git a/tests/data_reader_asset_test.cc b/tests/data_reader_asset_test.cc new file mode 100644 index 000000000..e510dd3f7 --- /dev/null +++ b/tests/data_reader_asset_test.cc @@ -0,0 +1,21 @@ +#include "gtest/gtest.h" +#include "tkc/data_reader_factory.h" +#include "base/data_reader_asset.h" + +TEST(DataReaderAsset, basic) { + char data[256]; + data_reader_t* reader = NULL; + data_reader_factory_t* f = data_reader_factory_create(); + + ASSERT_EQ(data_reader_factory_register(f, "asset", data_reader_asset_create), RET_OK); + + reader = data_reader_factory_create_reader(f, "asset://test.json"); + ASSERT_EQ(reader != NULL, true); + + memset(data, 0x00, sizeof(data)); + ASSERT_EQ(data_reader_get_size(reader), 27); + ASSERT_EQ(data_reader_read(reader, 0, data, sizeof(data)), 27); + + data_reader_destroy(reader); + data_reader_factory_destroy(f); +}