add data_writer_truncate/data_writer_clear

This commit is contained in:
lixianjing 2021-01-14 23:08:29 +08:00
parent e0835a2a75
commit 78deb87eb9
3 changed files with 48 additions and 0 deletions

View File

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

View File

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

View File

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