mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-12-02 12:08:16 +08:00
improve csv
This commit is contained in:
parent
fa08dd1d53
commit
754b9064ed
@ -35,6 +35,32 @@ typedef struct _csv_path_t {
|
||||
const char* col_name;
|
||||
} csv_path_t;
|
||||
|
||||
int32_t csv_file_object_parse_col(csv_file_object_t* o, const char* name) {
|
||||
int32_t col = -1;
|
||||
const char* p = name;
|
||||
return_value_if_fail(p != NULL && o != NULL, -1);
|
||||
|
||||
if (*p == '[') {
|
||||
return_value_if_fail(tk_isdigit(p[1]), -1);
|
||||
col = tk_atoi(p + 1);
|
||||
} else if (tk_isdigit(*p)) {
|
||||
col = tk_atoi(p);
|
||||
} else {
|
||||
if (o->col_names.size > 0) {
|
||||
col = csv_row_get_col(&(o->col_names), p);
|
||||
} else {
|
||||
col = csv_file_get_col_of_name(o->csv, p);
|
||||
}
|
||||
|
||||
if (col < 0) {
|
||||
return_value_if_fail(tk_isdigit(p[0]), -1);
|
||||
col = tk_atoi(p);
|
||||
}
|
||||
}
|
||||
|
||||
return col;
|
||||
}
|
||||
|
||||
static ret_t csv_path_parse_impl(csv_file_object_t* o, csv_path_t* path, const char* name) {
|
||||
const char* p = name;
|
||||
csv_file_t* csv = NULL;
|
||||
@ -70,22 +96,8 @@ static ret_t csv_path_parse_impl(csv_file_object_t* o, csv_path_t* path, const c
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
if (*p == '[') {
|
||||
return_value_if_fail(tk_isdigit(p[1]), RET_BAD_PARAMS);
|
||||
path->col = tk_atoi(p + 1);
|
||||
} else if (tk_isdigit(*p)) {
|
||||
path->col = tk_atoi(p);
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
path->col = csv_file_object_parse_col(o, p);
|
||||
|
||||
return_value_if_fail((path->col >= 0) && (path->col < csv_file_get_cols(csv)), RET_BAD_PARAMS);
|
||||
return_value_if_fail((path->row >= 0) && (path->row < csv_file_get_rows(csv)), RET_BAD_PARAMS);
|
||||
|
||||
@ -166,8 +178,8 @@ 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);
|
||||
} else if (tk_str_eq(name, CSV_PROP_COL_NAMES)) {
|
||||
csv_row_set_data(&(o->col_names), value_str(v), o->csv->sep);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
@ -403,7 +415,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_row_reset(&(o->col_names));
|
||||
csv_file_destroy(o->csv);
|
||||
o->csv = NULL;
|
||||
TK_OBJECT_UNREF(o->query_args);
|
||||
|
@ -118,7 +118,7 @@ typedef struct _csv_file_object_t {
|
||||
csv_filter_object_check_new_row_t check_new_row;
|
||||
void* check_new_row_ctx;
|
||||
|
||||
csv_row_t fields;
|
||||
csv_row_t col_names;
|
||||
} csv_file_object_t;
|
||||
|
||||
/**
|
||||
@ -229,6 +229,16 @@ csv_row_t* csv_file_object_find_first(tk_object_t* obj, tk_compare_t compare, vo
|
||||
ret_t csv_file_object_set_check_new_row(tk_object_t* obj,
|
||||
csv_filter_object_check_new_row_t check_new_row, void* ctx);
|
||||
|
||||
/**
|
||||
* @method csv_file_object_parse_col
|
||||
* 解析列名。
|
||||
* @param {csv_file_object_t*} o csv_file_object_t对象。
|
||||
* @param {const char*} name 列名。
|
||||
*
|
||||
* @return {int32_t} 返回列索引。
|
||||
*/
|
||||
int32_t csv_file_object_parse_col(csv_file_object_t* o, const char* name);
|
||||
|
||||
/**
|
||||
* @method csv_file_object_cast
|
||||
* 转换为csv_file_object_t。
|
||||
|
@ -28,20 +28,12 @@
|
||||
static csv_row_object_t* csv_row_object_cast(tk_object_t* obj);
|
||||
#define CSV_ROW_OBJECT(obj) csv_row_object_cast((tk_object_t*)obj)
|
||||
|
||||
static uint32_t get_col_from_name(const char* name) {
|
||||
if (name[0] == '[') {
|
||||
return tk_atoi(name + 1);
|
||||
} else {
|
||||
return tk_atoi(name);
|
||||
}
|
||||
}
|
||||
|
||||
static ret_t csv_row_object_set_prop(tk_object_t* obj, const char* name, const value_t* v) {
|
||||
uint32_t col = 0;
|
||||
char buff[64] = {0};
|
||||
csv_row_object_t* o = CSV_ROW_OBJECT(obj);
|
||||
return_value_if_fail(o != NULL && name != NULL, RET_BAD_PARAMS);
|
||||
col = get_col_from_name(name);
|
||||
col = csv_file_object_parse_col(CSV_FILE_OBJECT(o->csv), name);
|
||||
|
||||
return csv_row_set(&(o->row), col, value_str_ex(v, buff, sizeof(buff) - 1));
|
||||
}
|
||||
@ -50,7 +42,7 @@ static ret_t csv_row_object_get_prop(tk_object_t* obj, const char* name, value_t
|
||||
uint32_t col = 0;
|
||||
csv_row_object_t* o = CSV_ROW_OBJECT(obj);
|
||||
return_value_if_fail(o != NULL && name != NULL, RET_BAD_PARAMS);
|
||||
col = get_col_from_name(name);
|
||||
col = csv_file_object_parse_col(CSV_FILE_OBJECT(o->csv), name);
|
||||
|
||||
value_set_str(v, csv_row_get(&(o->row), col));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user