update dsv_io

This commit is contained in:
张壹 2025-02-13 21:53:33 +08:00
parent 140848e882
commit 908fa45d1b
5 changed files with 32 additions and 14 deletions

View File

@ -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<std::string> &col_list, int p)
{
array<int> 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<std::strin
throw std::runtime_error("[gctl::dsv_io] Invalid column type for numerical calculating.");
}
}
exprtk::symbol_table<double> symbol_table;
array<double> var(col_list.size());

View File

@ -442,7 +442,7 @@ namespace gctl
*
* @param name R<id>C<id>
* @param iter_row
* @return 1 -1-2
* @return 1 -1
*/
int name_index(std::string name, bool iter_row = false);

View File

@ -500,6 +500,27 @@ void filt_data(const std::vector<std::string> &cmd_units)
return;
}
void insert_data(const std::vector<std::string> &cmd_units)
{
// insert row|col [<new-name>] [<insert-name>]
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)

View File

@ -58,8 +58,9 @@ void set_titles(const std::vector<std::string> &cmd_units);
void math_func(const std::vector<std::string> &cmd_units);
void rand_data(const std::vector<std::string> &cmd_units);
void filt_data(const std::vector<std::string> &cmd_units);
void insert_data(const std::vector<std::string> &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."},

View File

@ -25,6 +25,9 @@ Enable table outputs. If the input file has no row or column names, use inbuild
#### disable column|row \<name1\> \<name2\> ...
Disable table outputs. If the input file has no row or column names, use inbuild names `R<id>` and `C<id>` to select the rows and columns.
#### insert row|col [\<new-name\>] [\<insert-name\>]
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<id>` and `C<id>` to select the rows and columns.
#### type row|col int|float|string \<name\>
Set the data type of a row or column. If the input file has no row or column names, use inbuild names `R<id>` and `C<id>` to select the rows and columns.