add data_reader_asset

This commit is contained in:
lixianjing 2020-06-13 12:06:54 +08:00
parent 9101fa6b8b
commit 49c71325c8
8 changed files with 162 additions and 4 deletions

1
.gitignore vendored
View File

@ -28,3 +28,4 @@ log
__pycache__
test.bin
release
event_log.bin

View File

@ -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

View File

@ -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

View File

@ -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 <xianjimli@hotmail.com> 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;
}

View File

@ -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 <xianjimli@hotmail.com> 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*/

View File

@ -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);

View File

@ -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);

View File

@ -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);
}