From 908fa45d1b9b87f651d5ec293eed1f686b5f18ec Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 13 Feb 2025 21:53:33 +0800 Subject: [PATCH] update dsv_io --- lib/io/dsv_io.cpp | 16 ++++------------ lib/io/dsv_io.h | 2 +- tool/dsviewer/dsviewer.cpp | 21 +++++++++++++++++++++ tool/dsviewer/dsviewer.h | 4 +++- tool/dsviewer/dsviewer.md | 3 +++ 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index a9687ee..a04ec52 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -538,7 +538,7 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) { int r = atoi(std::string(ret[1]).c_str()); if (r >= 1 && r <= row_num_) return r; - else return -2; + else return -1; } return -1; @@ -554,7 +554,7 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) { int c = atoi(std::string(ret[1]).c_str()); if (c >= 1 && c <= col_num_) return c; - else return -2; + else return -1; } return -1; @@ -745,15 +745,7 @@ void gctl::dsv_io::filter(linebool_func_t func, table_headtype_e thead) void gctl::dsv_io::cal_column(std::string expr_str, const std::vector &col_list, int p) { array idx(col_list.size()); - idx[0] = name_index(col_list[0]); - if (idx[0] < 0) - { - if (idx[0] == -1) add_column(col_list[0]); - else add_column(); - column_type(Float, col_list[0]); - } - - for (size_t i = 1; i < col_list.size(); i++) + for (size_t i = 0; i < col_list.size(); i++) { idx[i] = name_index(col_list[i]); @@ -764,7 +756,7 @@ void gctl::dsv_io::cal_column(std::string expr_str, const std::vector symbol_table; array var(col_list.size()); diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index cd3fc7b..669f011 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -442,7 +442,7 @@ namespace gctl * * @param name 名称 可以是具体的名称(如有),或者是R和C * @param iter_row 搜索行名称(默认为搜索列名称) - * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1表示匹配失败-2表示匹配成功但超过有效范围 + * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ int name_index(std::string name, bool iter_row = false); diff --git a/tool/dsviewer/dsviewer.cpp b/tool/dsviewer/dsviewer.cpp index 1bd0917..449e685 100644 --- a/tool/dsviewer/dsviewer.cpp +++ b/tool/dsviewer/dsviewer.cpp @@ -500,6 +500,27 @@ void filt_data(const std::vector &cmd_units) return; } +void insert_data(const std::vector &cmd_units) +{ + // insert row|col [] [] + if (cmd_units.size() < 2) throw std::runtime_error("insert: insufficient parameters."); + + if (cmd_units[1] == "row") + { + if (cmd_units.size() == 2) tc.add_row(); + else if (cmd_units.size() == 3) tc.add_row(cmd_units[2]); + else if (cmd_units.size() == 4) tc.add_row(cmd_units[2], cmd_units[3]); + } + else if (cmd_units[1] == "col") + { + if (cmd_units.size() == 2) tc.add_column(); + else if (cmd_units.size() == 3) tc.add_column(cmd_units[2]); + else if (cmd_units.size() == 4) tc.add_column(cmd_units[2], cmd_units[3]); + } + else throw std::runtime_error("insert: invalid parameters."); + return; +} + int main(int argc, char *argv[]) { if (argc >= 2) diff --git a/tool/dsviewer/dsviewer.h b/tool/dsviewer/dsviewer.h index 5354cfa..41a564c 100644 --- a/tool/dsviewer/dsviewer.h +++ b/tool/dsviewer/dsviewer.h @@ -58,8 +58,9 @@ void set_titles(const std::vector &cmd_units); void math_func(const std::vector &cmd_units); void rand_data(const std::vector &cmd_units); void filt_data(const std::vector &cmd_units); +void insert_data(const std::vector &cmd_units); -#define CMD_NUM 15 +#define CMD_NUM 16 const cmd_pair commands[CMD_NUM] = { {"quit", quit, "Quit the program."}, {"info", info, "Show the table information."}, @@ -70,6 +71,7 @@ const cmd_pair commands[CMD_NUM] = { {"stats", statistic, "Calculate statistics of the selected columns."}, {"enable", set_enable, "Enable column/row outputs by name or index."}, {"disable", set_disable, "Disable column/row outputs by name or index."}, + {"insert", insert_data, "Insert a new column/row data."}, {"type", set_type, "Set column/row data types."}, {"title", set_titles, "Set row and column titles."}, {"math", math_func, "Preform mathematic operations of column data."}, diff --git a/tool/dsviewer/dsviewer.md b/tool/dsviewer/dsviewer.md index 4f47c5c..137973d 100644 --- a/tool/dsviewer/dsviewer.md +++ b/tool/dsviewer/dsviewer.md @@ -25,6 +25,9 @@ Enable table outputs. If the input file has no row or column names, use inbuild #### disable column|row \ \ ... Disable table outputs. If the input file has no row or column names, use inbuild names `R` and `C` to select the rows and columns. +#### insert row|col [\] [\] +Insert a new row or column. The new row or column will be attached to the end of the table by default. A `new-name` can be provided if the user wants to rename the new row or column. Provide the `insert-name` to insert the new row or column at the specified row or column. The remaining columns or rows will be moved one step right or down accordingly. If the input file has no row or column names, use inbuild names `R` and `C` to select the rows and columns. + #### type row|col int|float|string \ Set the data type of a row or column. If the input file has no row or column names, use inbuild names `R` and `C` to select the rows and columns.