mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-29 18:48:09 +08:00
add data_reader_asset
This commit is contained in:
parent
9101fa6b8b
commit
49c71325c8
1
.gitignore
vendored
1
.gitignore
vendored
@ -28,3 +28,4 @@ log
|
||||
__pycache__
|
||||
test.bin
|
||||
release
|
||||
event_log.bin
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
86
src/base/data_reader_asset.c
Normal file
86
src/base/data_reader_asset.c
Normal 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;
|
||||
}
|
42
src/base/data_reader_asset.h
Normal file
42
src/base/data_reader_asset.h
Normal 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*/
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
21
tests/data_reader_asset_test.cc
Normal file
21
tests/data_reader_asset_test.cc
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user