From fa08dd1d53feedf340ab554df514ab4fb7dee899 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Sun, 24 Dec 2023 09:03:49 +0800 Subject: [PATCH] improve csv --- docs/changes.md | 1 + src/csv/csv_file.h | 12 ++++++++++++ src/csv/csv_file_object.c | 19 ++++++++++++++----- src/csv/csv_file_object.h | 4 ++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 889fd13bf8..1b33e8092c 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -3,6 +3,7 @@ 2023/12/24 * 完善data reader * data\_reader\_http 支持 chunked data + * CSV 支持设置列名,通过列名获取数据。 2023/12/23 * 增加函数path\_expand\_vars diff --git a/src/csv/csv_file.h b/src/csv/csv_file.h index 78e69ec448..6a4ac43ce0 100644 --- a/src/csv/csv_file.h +++ b/src/csv/csv_file.h @@ -495,6 +495,18 @@ const char* csv_row_get(csv_row_t* row, uint32_t col); */ ret_t csv_row_set(csv_row_t* row, uint32_t col, const char* value); +/** + * @method csv_row_get_col + * + * 根据列名获取列号。 + * + * @param {csv_row_t*} row 行对象。 + * @param {const char*} value 列名。 + * + * @return {int32_t} 返回列号。 + */ +int32_t csv_row_get_col(csv_row_t* row, const char* value); + /*public for test*/ ret_t csv_row_init(csv_row_t* row, char* buff, uint32_t size, bool_t should_free_buff); ret_t csv_row_reset(csv_row_t* row); diff --git a/src/csv/csv_file_object.c b/src/csv/csv_file_object.c index b73e7d96ca..09f0fb0d57 100644 --- a/src/csv/csv_file_object.c +++ b/src/csv/csv_file_object.c @@ -35,12 +35,13 @@ typedef struct _csv_path_t { const char* col_name; } csv_path_t; -static ret_t csv_path_parse_impl(csv_path_t* path, csv_file_t* csv, const char* name) { +static ret_t csv_path_parse_impl(csv_file_object_t* o, csv_path_t* path, const char* name) { const char* p = name; - return_value_if_fail(path != NULL && csv != NULL && name != NULL, RET_BAD_PARAMS); + csv_file_t* csv = NULL; + return_value_if_fail(path != NULL && o != NULL && o->csv != NULL && name != NULL, RET_BAD_PARAMS); + csv = o->csv; memset(path, 0x00, sizeof(*path)); - while (tk_isspace(*p)) p++; if (tk_isdigit(*p)) { @@ -75,7 +76,11 @@ static ret_t csv_path_parse_impl(csv_path_t* path, csv_file_t* csv, const char* } else if (tk_isdigit(*p)) { path->col = tk_atoi(p); } else { - path->col = csv_file_get_col_of_name(csv, p); + if(o->fields.size > 0) { + path->col = csv_row_get_col(&(o->fields), p); + } else { + path->col = csv_file_get_col_of_name(csv, p); + } if (path->col < 0) { return_value_if_fail(tk_isdigit(p[0]), RET_BAD_PARAMS); path->col = tk_atoi(p); @@ -119,7 +124,7 @@ static ret_t csv_path_parse_ex(csv_file_object_t* o, csv_path_t* path, const cha ret_t ret = RET_BAD_PARAMS; return_value_if_fail(o != NULL && path != NULL && name != NULL, RET_BAD_PARAMS); - ret = csv_path_parse_impl(path, o->csv, name); + ret = csv_path_parse_impl(o, path, name); if (ret == RET_OK) { if (o->rows_map != NULL) { uint32_t index = path->row; @@ -161,6 +166,9 @@ static ret_t csv_file_object_set_prop(tk_object_t* obj, const char* name, const if (tk_str_start_with(name, CSV_QUERY_PREFIX)) { return tk_object_set_prop(o->query_args, name, v); + } else if(tk_str_eq(name, CSV_PROP_COL_NAMES)) { + csv_row_set_data(&(o->fields), value_str(v), o->csv->sep); + return RET_OK; } rows = csv_file_get_rows(o->csv); @@ -395,6 +403,7 @@ static ret_t csv_file_object_destroy(tk_object_t* obj) { csv_file_object_t* o = CSV_FILE_OBJECT(obj); return_value_if_fail(o != NULL, RET_BAD_PARAMS); + csv_row_reset(&(o->fields)); csv_file_destroy(o->csv); o->csv = NULL; TK_OBJECT_UNREF(o->query_args); diff --git a/src/csv/csv_file_object.h b/src/csv/csv_file_object.h index bfb7b2133a..7a084c2c24 100644 --- a/src/csv/csv_file_object.h +++ b/src/csv/csv_file_object.h @@ -117,6 +117,8 @@ typedef struct _csv_file_object_t { bool_t is_dirty; csv_filter_object_check_new_row_t check_new_row; void* check_new_row_ctx; + + csv_row_t fields; } csv_file_object_t; /** @@ -244,6 +246,8 @@ csv_file_object_t* csv_file_object_cast(tk_object_t* obj); #define CSV_CMD_QUERY "query" #define CSV_CMD_QUERY_ARG_CLEAR "clear" +#define CSV_PROP_COL_NAMES "col_names" + END_C_DECLS #endif /*TK_CSV_FILE_OBJECT_H*/