mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-30 11:08:34 +08:00
add data_writer_truncate/data_writer_clear
This commit is contained in:
parent
e0835a2a75
commit
78deb87eb9
@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
#include "tkc/data_writer.h"
|
||||
#include "tkc/data_writer_factory.h"
|
||||
|
||||
int32_t data_writer_write(data_writer_t* writer, uint64_t offset, const void* data, uint32_t size) {
|
||||
return_value_if_fail(writer != NULL && writer->vt != NULL && writer->vt->write != NULL, -1);
|
||||
@ -28,9 +29,28 @@ int32_t data_writer_write(data_writer_t* writer, uint64_t offset, const void* da
|
||||
return writer->vt->write(writer, offset, data, size);
|
||||
}
|
||||
|
||||
ret_t data_writer_truncate(data_writer_t* writer, uint64_t size) {
|
||||
return_value_if_fail(writer != NULL && writer->vt != NULL && writer->vt->truncate != NULL,
|
||||
RET_BAD_PARAMS);
|
||||
|
||||
return writer->vt->truncate(writer, size);
|
||||
}
|
||||
|
||||
ret_t data_writer_destroy(data_writer_t* writer) {
|
||||
return_value_if_fail(writer != NULL && writer->vt != NULL && writer->vt->destroy != NULL,
|
||||
RET_BAD_PARAMS);
|
||||
|
||||
return writer->vt->destroy(writer);
|
||||
}
|
||||
|
||||
ret_t data_writer_clear(const char* url) {
|
||||
ret_t ret = RET_OK;
|
||||
data_writer_t* writer = data_writer_factory_create_writer(data_writer_factory(), url);
|
||||
return_value_if_fail(writer != NULL, RET_BAD_PARAMS);
|
||||
|
||||
ret = data_writer_truncate(writer, 0);
|
||||
data_writer_destroy(writer);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -31,10 +31,12 @@ typedef struct _data_writer_t data_writer_t;
|
||||
|
||||
typedef int32_t (*data_writer_write_t)(data_writer_t* writer, uint64_t offset, const void* data,
|
||||
uint32_t size);
|
||||
typedef ret_t (*data_writer_truncate_t)(data_writer_t* writer, uint64_t size);
|
||||
typedef ret_t (*data_writer_destroy_t)(data_writer_t* writer);
|
||||
|
||||
typedef struct _data_writer_vtable_t {
|
||||
data_writer_write_t write;
|
||||
data_writer_truncate_t truncate;
|
||||
data_writer_destroy_t destroy;
|
||||
} data_writer_vtable_t;
|
||||
|
||||
@ -62,6 +64,16 @@ struct _data_writer_t {
|
||||
*/
|
||||
int32_t data_writer_write(data_writer_t* writer, uint64_t offset, const void* data, uint32_t size);
|
||||
|
||||
/**
|
||||
* @method data_writer_truncate
|
||||
* 截去指定长度之后的数据。
|
||||
* @param {data_writer_t*} writer writer对象。
|
||||
* @param {uint64_t} size 保留长度。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t data_writer_truncate(data_writer_t* writer, uint64_t size);
|
||||
|
||||
/**
|
||||
* @method data_writer_destroy
|
||||
* 销毁writer对象。
|
||||
@ -71,6 +83,15 @@ int32_t data_writer_write(data_writer_t* writer, uint64_t offset, const void* da
|
||||
*/
|
||||
ret_t data_writer_destroy(data_writer_t* writer);
|
||||
|
||||
/**
|
||||
* @method data_writer_clear
|
||||
* 清除指定URL的数据。
|
||||
* @param {const char*} url URL。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t data_writer_clear(const char* url);
|
||||
|
||||
#define DATA_WRITER(writer) ((data_writer_t*)(writer))
|
||||
|
||||
END_C_DECLS
|
||||
|
@ -46,8 +46,15 @@ static ret_t data_writer_file_destroy(data_writer_t* writer) {
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t data_writer_file_truncate(data_writer_t* writer, uint64_t size) {
|
||||
data_writer_file_t* file = (data_writer_file_t*)writer;
|
||||
|
||||
return fs_file_truncate(file->file, size);
|
||||
}
|
||||
|
||||
static const data_writer_vtable_t s_data_writer_file_vtable = {
|
||||
.write = data_writer_file_write,
|
||||
.truncate = data_writer_file_truncate,
|
||||
.destroy = data_writer_file_destroy,
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user