add some csv functions

This commit is contained in:
lixianjing 2023-09-25 18:01:54 +08:00
parent d77e905ea4
commit 385fcf20b6
5 changed files with 125 additions and 6 deletions

View File

@ -1,4 +1,7 @@
# 最新动态 # 最新动态
2023/09/25
* 增加函数csv\_file\_object\_load/csv\_file\_object\_load\_from\_buff
* 增加函数csv\_file\_object\_save\_to\_buff/csv\_file\_object\_save\_as
2023/09/25 2023/09/25
* 增加函数path\_prepend\_temp\_path * 增加函数path\_prepend\_temp\_path

View File

@ -3,7 +3,7 @@
* Author: AWTK Develop Team * Author: AWTK Develop Team
* Brief: csv file * Brief: csv file
* *
* Copyright (c) 2020 - 2023 Guangzhou ZHIYUAN Electronics Co.,Ltd. * Copyright (c) 2020 - 2022 Guangzhou ZHIYUAN Electronics Co.,Ltd.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -21,6 +21,8 @@
#include "tkc/mem.h" #include "tkc/mem.h"
#include "tkc/utils.h" #include "tkc/utils.h"
#include "tkc/data_writer.h"
#include "tkc/data_writer_factory.h"
#include "csv_file.h" #include "csv_file.h"
#include "streams/mem/istream_mem.h" #include "streams/mem/istream_mem.h"
@ -624,6 +626,24 @@ ret_t csv_file_remove_row(csv_file_t* csv, uint32_t row) {
return csv_rows_remove(&(csv->rows), row); return csv_rows_remove(&(csv->rows), row);
} }
ret_t csv_file_save_to_buff(csv_file_t* csv, wbuffer_t* buff) {
str_t str;
uint32_t i = 0;
csv_row_t* r = NULL;
return_value_if_fail(csv != NULL, RET_BAD_PARAMS);
return_value_if_fail(buff != NULL, RET_BAD_PARAMS);
return_value_if_fail(str_init(&str, 512) != NULL, RET_OOM);
for (i = 0; i < csv->rows.size; i++) {
r = csv->rows.rows + i;
csv_row_to_str(r, &str, csv->sep);
ENSURE(wbuffer_write_binary(buff, str.str, str.size) == RET_OK);
}
str_reset(&str);
return RET_OK;
}
ret_t csv_file_save(csv_file_t* csv, const char* filename) { ret_t csv_file_save(csv_file_t* csv, const char* filename) {
str_t str; str_t str;
uint32_t i = 0; uint32_t i = 0;
@ -651,8 +671,7 @@ ret_t csv_file_save(csv_file_t* csv, const char* filename) {
} }
} }
str_reset(&str); str_reset(&str);
return RET_OK;
return RET_NOT_IMPL;
} }
const char* csv_file_get_title(csv_file_t* csv) { const char* csv_file_get_title(csv_file_t* csv) {

View File

@ -3,7 +3,7 @@
* Author: AWTK Develop Team * Author: AWTK Develop Team
* Brief: csv file * Brief: csv file
* *
* Copyright (c) 2020 - 2023 Guangzhou ZHIYUAN Electronics Co.,Ltd. * Copyright (c) 2020 - 2022 Guangzhou ZHIYUAN Electronics Co.,Ltd.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -23,6 +23,7 @@
#define TK_CSV_FILE_H #define TK_CSV_FILE_H
#include "tkc/istream.h" #include "tkc/istream.h"
#include "tkc/buffer.h"
BEGIN_C_DECLS BEGIN_C_DECLS
@ -313,6 +314,18 @@ ret_t csv_file_insert_row(csv_file_t* csv, uint32_t row, const char* data);
*/ */
ret_t csv_file_save(csv_file_t* csv, const char* filename); ret_t csv_file_save(csv_file_t* csv, const char* filename);
/**
* @method csv_file_save_to_buff
*
*
*
* @param {csv_file_t*} csv csv对象
* @param {wbuffer_t*} buff
*
* @return {ret_t} RET_OK表示成功
*/
ret_t csv_file_save_to_buff(csv_file_t* csv, wbuffer_t* buff);
/** /**
* @method csv_file_clear * @method csv_file_clear
* *

View File

@ -3,7 +3,7 @@
* Author: AWTK Develop Team * Author: AWTK Develop Team
* Brief: csv file object * Brief: csv file object
* *
* Copyright (c) 2020 - 2023 Guangzhou ZHIYUAN Electronics Co.,Ltd. * Copyright (c) 2020 - 2022 Guangzhou ZHIYUAN Electronics Co.,Ltd.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -23,7 +23,10 @@
#include "tkc/mem.h" #include "tkc/mem.h"
#include "tkc/utils.h" #include "tkc/utils.h"
#include "tkc/object.h" #include "tkc/object.h"
#include "tkc/data_reader.h"
#include "csv_file.h" #include "csv_file.h"
#include "csv_file_object.h"
typedef struct _csv_file_object_t { typedef struct _csv_file_object_t {
tk_object_t object; tk_object_t object;
@ -249,3 +252,35 @@ csv_file_t* csv_file_object_get_csv(tk_object_t* obj) {
return o->csv; return o->csv;
} }
tk_object_t* csv_file_object_load(const char* filename, char sep) {
csv_file_t* csv = csv_file_create(filename, sep);
return_value_if_fail(csv != NULL, NULL);
return csv_file_object_create(csv);
}
tk_object_t* csv_file_object_load_from_buff(const void* buff, uint32_t size, char sep) {
csv_file_t* csv = NULL;
if (buff != NULL) {
csv = csv_file_create_with_buff(buff, size, sep);
} else {
csv = csv_file_create_empty(sep, NULL, NULL);
}
return_value_if_fail(csv != NULL, NULL);
return csv_file_object_create(csv);
}
ret_t csv_file_object_save_to_buff(tk_object_t* obj, wbuffer_t* wb) {
csv_file_object_t* o = CSV_FILE_OBJECT(obj);
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
return csv_file_save_to_buff(o->csv, wb);
}
ret_t csv_file_object_save_as(tk_object_t* obj, const char* filename) {
csv_file_object_t* o = CSV_FILE_OBJECT(obj);
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
return csv_file_save(o->csv, filename);
}

View File

@ -3,7 +3,7 @@
* Author: AWTK Develop Team * Author: AWTK Develop Team
* Brief: csv file object * Brief: csv file object
* *
* Copyright (c) 2020 - 2023 Guangzhou ZHIYUAN Electronics Co.,Ltd. * Copyright (c) 2020 - 2022 Guangzhou ZHIYUAN Electronics Co.,Ltd.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -56,6 +56,55 @@ tk_object_t* csv_file_object_create(csv_file_t* csv);
*/ */
csv_file_t* csv_file_object_get_csv(tk_object_t* obj); csv_file_t* csv_file_object_get_csv(tk_object_t* obj);
/**
* @method csv_file_object_load
* CSV对象
*
* @annotation ["constructor"]
*
* @param {const char*} filename
* @param {char} sep
*
* @return {tk_object_t*}
*/
tk_object_t* csv_file_object_load(const char* filename, char sep);
/**
* @method csv_file_object_load_from_buff
* CSV对象
* @annotation ["constructor"]
*
* @param {const void*} buff
* @param {uint32_t} size
* @param {char} sep
*
* @return {tk_object_t*}
*/
tk_object_t* csv_file_object_load_from_buff(const void* buff, uint32_t size, char sep);
/**
* @method csv_file_object_save_to_buff
* obj保存为CSV格式到内存
*
* @param {tk_object_t*} obj doc对象
* @param {wbuffer_t*} wb (使wbuffer_deinit)
*
* @return {ret_t} RET_OK表示成功
*/
ret_t csv_file_object_save_to_buff(tk_object_t* obj, wbuffer_t* wb);
/**
* @method csv_file_object_save_as
* doc对象保存到指定文件
* @annotation ["static"]
*
* @param {tk_object_t*} obj doc对象
* @param {const char*} filename
*
* @return {ret_t} RET_OK表示成功
*/
ret_t csv_file_object_save_as(tk_object_t* obj, const char* filename);
END_C_DECLS END_C_DECLS
#endif /*TK_CSV_FILE_OBJECT_H*/ #endif /*TK_CSV_FILE_OBJECT_H*/